c语言函数与结构体实验 c 结构体函数( 三 )


以上是将b中c语言函数与结构体实验的数据赋值给a,相当于a=b;这样的一个操作
returna;
}
main()
{STUc= { "Qian",'f' ,  95,92 } ,  d;
d = f(c);//这里传的c是个复制品(传递的不是结构体指针),所以c不会变,d接收f()函数的返回值,因为返回值的内容是f()中的b的值,所以,d与c不同
printf ("%s , %c,%d,%d," ,  d.name,d.sex,d.score[0],d.score[1]);
printf ("%s,%c,%d,%d\n",c.name, c.sex,c.score[0],c.score[1]);
}
输出结果为:A)Zhao,m,85,90,Qian,f,95,92
C语言实验 结构体与文件程序设计//main.c文件
#includestdio.h
#includestdlib.h
#define BufLen 20
#define KeChengCount 3
typedef struct tagStuInfor
{
char xh[BufLen]; //学号
char xm[BufLen]; //姓名
char kchm[KeChengCount][BufLen]; //课程名
float pshchj[KeChengCount]; //平时成绩
float kshchj[KeChengCount]; //考试成绩
float zpchj[KeChengCount]; //总评成绩
float zf;
}StudentInformation;
typedef struct tagBookInfor
{
char shm[BufLen]; //书名
float dj; //单价
}BookInformation;
void shurustu();
void zongfen();
void shuchumaxmin();
void sortbook();
void freeall();
//学生数,书数
int xshsh,shsh;
StudentInformation *ptstu=NULL;
BookInformation *ptbook=NULL;
int main()
{
char in[]="data.txt",out[]="T_data.txt";
int i;
float chjs[6],zf;
FILE *fin,*fout;
printf("250这个数字太不好c语言函数与结构体实验了,能换成350吗c语言函数与结构体实验?\n\n");
shurustu();
zongfen();
shuchumaxmin();
printf("请输入书数:");
scanf("%d",shsh);
ptbook=(BookInformation*)calloc(shsh,sizeof(BookInformation));
for(i=0;ishsh;i++)
{
printf("请输入第%d本书c语言函数与结构体实验的书名:",i+1);
scanf("%s",ptbook[i].shm);
printf("请输入第%d本书c语言函数与结构体实验的单价:",i+1);
scanf("%f",ptbook[i].dj);
}
printf("\n");
sortbook();
printf("按书的单价升序排序后的结果:\n书名\t单价\n");
for(i=0;ishsh;i++)
{
printf("%s\t%.2f\n",ptbook[i].shm,ptbook[i].dj);
}
fin=fopen(in,"r");
fout=fopen(out,"w");
while(!feof(fin))
{
for(i=0;i6;i++)
{
fscanf(fin,"%f",chjs[i]);
}
zf=0.2*(chjs[0]+chjs[1]+chjs[2])+0.8*(chjs[3]+chjs[4]+chjs[5]);
printf("%.2f\n",zf);
fprintf(fout,"%.2f\r\n",zf);
}
printf("\n");
fclose(fin);
fclose(fout);
freeall();
system("PAUSE");
return EXIT_SUCCESS;
}
void shurustu()
{
int i;
printf("请输入学生的个数:");
scanf("%d",xshsh);
ptstu=(StudentInformation*)calloc(xshsh,sizeof(StudentInformation));
for(i=0;ixshsh;i++)
{
strcpy(ptstu[i].kchm[0],"C语言");
strcpy(ptstu[i].kchm[1],"高等数学");
strcpy(ptstu[i].kchm[2],"英语");
printf("请输入第%d个学生的学号:",i+1);
scanf("%s",ptstu[i].xh);
printf("请输入第%d个学生的姓名:",i+1);
scanf("%s",ptstu[i].xm);
printf("请输入第%d个学生的C语言课程的平时成绩和考试成绩:",i+1);
scanf("%f %f",ptstu[i].pshchj[0],ptstu[i].kshchj[0]);
printf("请输入第%d个学生的高等数学课程的平时成绩和考试成绩:",i+1);
scanf("%f %f",ptstu[i].pshchj[1],ptstu[i].kshchj[1]);
printf("请输入第%d个学生的英语课程的平时成绩和考试成绩:",i+1);
scanf("%f %f",ptstu[i].pshchj[2],ptstu[i].kshchj[2]);
}
printf("\n");

推荐阅读