【项目1-复数结构体】
编写一个程序,首先定义一个复数数据类型,即结构类型。然后按照复数的运算规则进行计算,并按照复数表示的格式进行输出,请将程序补充完整。
解法:#include <stdio.h>
#include <stdlib.h>
int main()
{
typedef struct
{
double real;
double imag;
}complex_n;
complex_n com1,com2,com3;
printf("please enter the real part and imagine part of the two complex number:");
scanf("%lf%lf%lf%lf",&com1.real,&com1.imag,&com2.real,&com2.imag);
com3.real=com1.real*com2.real-com1.imag*com2.imag;
com3.imag=com1.real*com2.imag+com1.imag*com2.real;
printf("the result is %.2lf+%.2lfi",com3.real,com3.imag);
return 0;
}
【项目2-学生成绩处理】
(1)定义学生结构体,在结构体数组中,输入了学生学号和两门课的成绩,计算均分后,输出成绩单,请在下面的程序基础上,将程序补充完整。
#include <stdio.h>
#include <stdlib.h>
#define N 100
struct student
{
int num;
int score_c;
int score_math;
double aver;
};
int main()
{
int n;
int i;
int pass=0;
struct student stu[N];
printf("please enter the amount of the student:");
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;++i)
{
printf("please enter the number score of c and math of the %d-th student:\n",i);
scanf("%d%d%d",&((stu+i)->num),&((stu+i)->score_c),&((stu+i)->score_math));
(stu+i)->aver=((stu+i)->score_c+(stu+i)->score_math)/2;
if((stu+i)->score_c<60&&(stu+i)->score_math<60)
++pass;
}
printf("score report\n");
fflush(stdin);
printf("number\tc\tmath\taverage\t\n");
for(i=0;i<n;++i)
{
printf("%d\t%d\t%d\t%.2lf\t",(stu+i)->num,(stu+i)->score_c,(stu+i)->score_math,(stu+i)->aver);
printf("\n");
}
printf("the amount of the students did not pass:%d",pass);
return 0;
}