Tệp input.txt tạo và lưu trên nền ổ C
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
typedef struct sp
{
floatthuc,ao;
sp *next;
};
sp *doctep(sp *x) // doc tep c://input.txt
{
FILE *u;
x=NULL;
sp *p;
u=fopen("C:\\input.txt","rt");
while(!feof(u))
{
p=new sp;
fscanf(u,"%f",&p->thuc);
fscanf(u,"%f",&p->ao);
p->next=x;
x=p;
}
fclose(u);
returnx;
}
void xem(sp x) // xem tong so phuc
{
printf("\n %5.2f + %5.2f i ",x.thuc,x.ao);
}
void xemtoanbo(sp *x)// xem toàn bô sô phuc vua doc tu tep input.txt
{
sp *tam=x;
while(tam!=NULL)
{
//printf("\n %5.2f + %5.2f i ",tam->thuc,tam->ao);
xem(*tam);
tam=tam->next;
}
}
sp tong2sp(sp a,sp b) // tong 2 so phuc
{
sp z;
z.thuc=a.thuc+b.thuc;
z.ao=a.ao+b.ao;
returnz;
}
sp tong(sp *x) // tong danh sach so phuc
{
sp tong,*tam=x;
tong.thuc=0; //khoi tao sp tong=0
tong.ao=0; //
while(tam!=NULL)
{
tong=tong2sp(tong,*tam);
tam=tam->next;
}
returntong;
}
void main()
{
sp *x;
x=newsp;
x=doctep(x);
xemtoanbo(x);
printf("\nTong mang so phuc : ");
xem(tong(x));
getch();
}
No comments:
Post a Comment