3.有3个学生,每个学生有2门功课的成绩,从键盘上输入学生数据(学号、姓名、两门课成绩)

3.有3个学生,每个学生有2门功课的成绩,从键盘上输入学生数据(学号、姓名、两门课成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件"stu.txt"中。其中学号、各科成绩及平均成绩均为整数。
#include <stdio.h>
#include <string.h>
typedef struct student
{int no;
char name[10];
int score1, score2,average;
}student;
void main( )
{ FILE *fp;
char fname[100];
student stu[n];
int i;
strcpy(fname,”stu.txt”);
fp=fopen(fname,”wb”);
if(!fp)
{ printf(“打开文件失败“);
Return; }
for(i=0;i<3;i++)
{ printf(“输入第%d个学生的数据\n”,i+1);
Printf(“学号”);
scanf(“%d”,&stu[i].no);
Printf(“姓名”);
scanf(“%d”,&stu[i].name);
Printf(“成绩1”);
scanf(“%d”,&stu[i].score1);
Printf(“成绩2”);
scanf(“%d”,&stu[i].score2);
stu[i].average=(stu[i].score1+stu[i].score2)/2;
fprintf(fp,”%3d%-10s%-3d%-3d%-3d%-3d”,stu[i].no,
stu[i].name,stu[i].score,stu[i].score2,stu[i].average);
}
fclose(fp);
}
请帮我分析以上程序中各语句的意思,非常谢谢!

#include<stdio.h>
typedef struct student
{
long num;
char name[10];
int scroe[2];
int aver;
}student;
void main()
{
FILE*out;
student st[3];
int i;
for(i=0;i<3;i++)
scanf("%ld%s %d%d",&st[i].num,st[i].name,&st[i].scroe[0],&st[i].scroe[1]);
for(i=0;i<3;i++)
st[i].aver=(st[i].scroe[0]+st[i].scroe[1])/2;
out=fopen("stu.txt","w");
fprintf(out,"学号 姓名 语文 数学 平均分\n");
for(i=0;i<3;i++)
fprintf(out,"%ld %s %d %d\n",st[i].num,st[i].name,st[i].scroe[0],st[i].scroe[1],st[i].aver);
fclose(out);
}追问

out=fopen("stu.txt","w");
请问这句话什么意思?
怎样写如stu.txt文件中?

追答

打开一个文件名位stu.txt的文件,如果文件不存在就新建一个,以为是以"w"方式打开的!

追问

请帮我解决另外一题:分析程序中每句什么意思?
输入一个字符串,写一个程序统计并显示各小写字母的个数。
#include
void main( )
{chai str[60];
int count[26]={0};
char *P_str;
int i;
P_str=str;
Printf(“请输入字符串:\n”);
gets(str);
for( ;*P_str!=’\0’;P_str++)
{count[*P_str-‘a’]++;}
for (i=0;i<=5;i++)
{printf(“%c的个数为:%d个\n”,i+’a’,count[i]);}
}

追答


void main( )
{char str[60];
int count[26]={0};
char *P_str;
int i;
P_str=str;
printf("请输入字符串:\n");
gets(str);
for( ;*P_str!='\0';P_str++)
{count[*P_str-'a']++;}
for (i=0;i<26;i++)
if(count[i]!=0)
{printf("%c的个数为:%d个\n",i+'a',count[i]);}
}

追问

谢谢,你的程序每一句什么意思,帮我写一下好吗?

追答

就是改这两句呀!
for (i=0;i<26;i++)
if(count[i]!=0)

从26个字母中选出字符串里出现的字母!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-18
你问的什么?本回答被提问者采纳
相似回答