20分求c语言编程题答案
我这里有一些程序(dev-c++编译器),虽然并不是你所想要的完美答案,但是其中的一些代码,希望对你有所帮助:
No.1有5个学生,每个学生有3门课的成绩,从键盘输入数据(包括学生号,姓名,3门课的成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
#include
#include
structstudent
charnum[10];
charname[8];
intscore[3];
floatave;
}stu[5];
intmain()
inti,j,sum;
FILE*fp;
for(i=0;i<5;i++)
printf("\ninputscoreofstudent%d:\n",i+1);
printf("No.:");
scanf("%s",stu[i].num);
printf("name:");
scanf("%s",stu[i].name);
sum=0;
for(j=0;j<3;j++)
printf("score%d:",j+1);
scanf("%d",&stu[i].score[j]);
sum+=stu[i].score[j];
stu[i].ave=sum/3.0;
fp=fopen("stud","w");
for(i=0;i<5;i++)
if(fwrite(&stu[i],SIzeof(structstudent),1,fp)!=1)
printf("filewriteerror\n");
fclose(fp);
fp=fopen("stud","r");
for(i=0;i<5;i++)
fread(&stu[i],sizeof(structstudent),1,fp);
printf("\n%s,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,
stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
system("pause");
return0;
No.2将运行No.1程序所得到的stud文件中的学生数据按平均分进行排序处理,将已排序的学生数据存入一个新文件stu_sort中。
#include
#include
#defineN10
structstudent
charnum[10];
charname[8];
intscore[3];
floatave;
}st[N],temp;
intmain()
FILE*fp;
inti,j,n;
if((fp=fopen("stud","r"))==NULL)
printf("cannotopen.");
exit(0);
printf("Filestud:");
for(i=0;fread(&st[i],sizeof(structstudent),1,fp)!=0;i++)
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
printf("\n");
fclose(fp);
for(i=0;i
for(j=i+1;j
if(st[i].ave
temp=st[i];
st[i]=st[j];
st[j]=temp;
printf("\nNow:");
fp=fopen("stu_sort","w");
for(i=0;i
fwrite(&st[i],sizeof(structstudent),1,fp);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
printf("\n");
fclose(fp);
system("pause");
return0;
No.3将题No.2已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新文件。
#include
#include
structstudent
charnum[10];
charname[8];
intscore[3];
floatave;
}st[10],s;
intmain()
FILE*fp,*fp1;
inti,j,t,n;
printf("\nNo.:");
scanf("%s",s.num);
printf("name:");
scanf("%s",s.name);
printf("score1,score2,score3:");
scanf("%d,%d,%d",&s.score[0],&s.score[1],&s.score[2]);
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;
if((fp=fopen("stu_sort","r"))==NULL)
printf("cannotopenfile.");
exit(0);
printf("Originaldata:\n");
for(i=0;fread(&st[i],sizeof(structstudent),1,fp)!=0;i++)
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
for(t=0;st[t].ave>s.ave&&t
printf("\nNow:\n");
fp1=fopen("sort1.dat","w");
for(i=0;i
fwrite(&st[i],sizeof(structstudent),1,fp1);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[t].ave);
fwrite(&s,sizeof(structstudent),1,fp1);
printf("\n%8s%8s%8d%8d%8d%10.2f",s.num,s.name,s.score[0],
s.score[1],s.score[2],s.ave);
for(i=t;i
fwrite(&st[i],sizeof(structstudent),1,fp1);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
printf("\n");
fclose(fp);
fclose(fp1);
system("pause");
return0;
No.4将No.3的结果仍存入原有的文件而不另建立新文件。
#include
#include
structstudent
charnum[10];
charname[8];
intscore[3];
floatave;
}st[10],s;
intmain()
FILE*fp;
inti,j,t,n;
printf("\nNo.:");
scanf("%s",s.num);
printf("name:");
scanf("%s",s.name);
printf("score1,score2,score3:");
scanf("%d,%d,%d",&s.score[0],&s.score[1],&s.score[2]);
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;
if((fp=fopen("stu_sort","r"))==NULL)
printf("cannotopenfile.");
exit(0);
printf("Originaldata:\n");
for(i=0;fread(&st[i],sizeof(structstudent),1,fp)!=0;i++)
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
for(t=0;st[t].ave>s.ave&&t
printf("\nNow:\n");
fp=fopen("stu_sort","w");
for(i=0;i
fwrite(&st[i],sizeof(structstudent),1,fp);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[t].ave);
fwrite(&s,sizeof(structstudent),1,fp);
printf("\n%8s%8s%8d%8d%8d%10.2f",s.num,s.name,s.score[0],
s.score[1],s.score[2],s.ave);
for(i=t;i
fwrite(&st[i],sizeof(structstudent),1,fp);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
printf("\n");
fclose(fp);
system("pause");
return0;
对链表的综合操作:
#include
#include
#include
#defineLENsizeof(structstudent)
structstudent
longnum;
floatscore;
structstudent*next;
intn;
structstudent*creat(void)
structstudent*head;
structstudent*p1,*p2;
p1=p2=(structstudent*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
n=n+1;
if(n==1)head=p1;
elsep2->next=p1;
p2=p1;
p1=(structstudent*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
p2->next=NULL;
return(head);
voidprint(structstudent*head)
structstudent*p;
printf("\nNow,These%drecordsare:\n",n);
p=head;
if(head!=NULL)
printf("%ld%5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
structstudent*del(structstudent*head,longnum)
structstudent*p1,*p2;
if(head==NULL)
printf("\nlistnull!\n");
return(head);
p1=head;
while(num!=p1->num&&p1->next!=NULL)
p2=p1;p1=p1->next;
if(num==p1->num)
if(p1==head)head=p1->next;
elsep2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
elseprintf("%ldnotbeenfound!\n",num);
return(head);
structstudent*insert(structstudent*head,structstudent*stud)
structstudent*p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
head=p0;
p0->next=NULL;
while((p0->num>p1->num)&&(p1->next!=NULL))
p2=p1;
p1=p1->next;
if(p0->num<=p1->num)
if(head==p1)head=p0;
p2->next=p0;
p0->next=p1;
p1->next=p0;
p0->next=NULL;
n=n+1;
return(head);
intmain()
structstudent*head,*stu;
longdel_num;
printf("inputrecords:\n");
head=creat();
print(head);
printf("\ninputthedeletednumber:");
scanf("%ld",&del_num);
while(del_num!=0)
head=del(head,del_num);
print(head);
printf("\ninputthedeletednumber:");
scanf("%ld",&del_num);
printf("\ninputtheinsertedrecord:");
stu=(structstudent*)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
head=insert(head,stu);
print(head);
printf("\ninputtheinsertedrecord:");
stu=(structstudent*)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
system("pause");
return0;
本回答由网友推荐
求一道C语言编程题答案
#includemain(){int n,a,b,c,num=0;float sum=0;printf("输入每件衣服的价格和三位顾客各买多少件衣服,以空格隔开:");scanf("%d%d%d%d",&n,&a,&b,&c);if(a<=20)if(b>20&&b<=50)if(c>50){sum=a*n+b*n*0.8+c*n*0.5;num=a+b+c;printf("售出%d衣服.\n共%.2f钱.\n",num,sum);}else printf("输入错误");}
main( ) {int n1=123; long int n2=123456; printf("n1=%ld\n",); } 请问这道题的答案是怎么算出来的?
你好!printf("n1=%ld\n",);这句话不完整啊。题目是要求补充完程序???我的回答你还满意吗~~