#include "stdio.h" void TestFunc(); struct COMP {int real; int image; }; struct COMP fun(struct COMP a,struct COMP b) { /*********Begin**********/ /********** End **********/ } main() { struct COMP a1,b1,c1; scanf("%d%d",&a1.real,&a1.image); /*第一个复数a1的实部、虚部*/ scanf("%d%d",&b1.real,&b1.image); c1=fun(a1,b1); if(c1.image>=0) printf("%d+%di",c1.real,c1.image); else printf("%d%di",c1.real,c1.image); TestFunc(); } void TestFunc() /* 此函数无需改动 */ { FILE *IN,*OUT; int j; struct COMP c1[3],c2[3],o[3]; IN=fopen("in.dat","r"); if(IN==NULL) {printf("Read File Error"); } OUT=fopen("out.dat","w"); if(OUT==NULL) {printf("Write File Error"); } /* 读入数据用于测试 */ for(j=0;j<3;j++){ fscanf(IN,"%d%d",&c1[j].real, &c1[j].image); fscanf(IN,"%d%d",&c2[j].real, &c2[j].image); o[j]=fun(c1[j],c2[j]); } for(j=0;j<3;j++) { if(o[j].image>=0) fprintf(OUT,"%d+%di\n",o[j].real,o[j].image); else fprintf(OUT,"%d%di\n",o[j].real,o[j].image); } fclose(IN); fclose(OUT); }补全该程序
时间: 2024-01-17 12:40:45 浏览: 23
#include "stdio.h"
struct COMP {
int real;
int image;
};
struct COMP fun(struct COMP a, struct COMP b) {
struct COMP c;
c.real = a.real + b.real;
c.image = a.image + b.image;
return c;
}
void TestFunc() {
FILE *IN, *OUT;
int j;
struct COMP c1[3], c2[3], o[3];
IN = fopen("in.dat", "r");
if (IN == NULL) {
printf("Read File Error");
}
OUT = fopen("out.dat", "w");
if (OUT == NULL) {
printf("Write File Error");
}
for (j = 0; j < 3; j++) {
fscanf(IN, "%d%d", &c1[j].real, &c1[j].image);
fscanf(IN, "%d%d", &c2[j].real, &c2[j].image);
o[j] = fun(c1[j], c2[j]);
}
for (j = 0; j < 3; j++) {
if (o[j].image >= 0) {
fprintf(OUT, "%d+%di\n", o[j].real, o[j].image);
} else {
fprintf(OUT, "%d%di\n", o[j].real, o[j].image);
}
}
fclose(IN);
fclose(OUT);
}
int main() {
struct COMP a1, b1, c1;
scanf("%d%d", &a1.real, &a1.image);
scanf("%d%d", &b1.real, &b1.image);
c1 = fun(a1, b1);
if (c1.image >= 0) {
printf("%d+%di", c1.real, c1.image);
} else {
printf("%d%di", c1.real, c1.image);
}
TestFunc();
return 0;
}