抄写例题作业
#include<stdio.h> #include<stdlib.h> main() { struct student {int num;//学号为整形 char name[20];//姓名为字符串 char sex;//性别为字符串 int age;//年龄为整形 float score; char addr[30]; }; } -------------------------------- Process exited after 0.01214 seconds with return value 1 请按任意键继续. . .
这道题是基础 没啥说的
9.2
#include<stdio.h> #include<stdlib.h> int main() {struct Student {int num; char name[20]; float score; }; struct Student student1={10101,"LLL",89}; struct Student student2={10103,"AAA",98}; if(student1.score>student2.score) //if语句 printf("%d,%s,%f",student1.num,student1.name,student1.score); else if(student1.score<student2.score) printf("%d,%s,%f",student2.num,student2.name,student2.score); else { printf("%d,%s,%f",student1.num,student1.name,student1.score); printf("%d,%s,%f",student2.num,student2.name,student2.score); } return 0; } 10103,AAA,98.000000 -------------------------------- Process exited after 0.01196 seconds with return value 0 请按任意键继续. . .
这道题还不算难,但用了if语句,巩固一下知识.
9.3
#include<string.h> #include<stdio.h> struct Person//声明结构体类型 struct person {char name[20];//候选人姓名 int count;//候选人的票数 } leader[3]={"yu",0,"zhang",0,"Sun",0};//定义结构体数组并初始化 int main() {int i,j; char leader_name[20]; for(i=1;i<=10;i++) {scanf("%s",leader_name);//输入所选的候选人姓名 for(j=0;j<3;j++) if(strcmp(leader_name,leader[j].name)==0)leader[j].count++; //if语句 } printf("\nResult:\n"); for(i=0;i<3;i++) printf("%5s:%d\n",leader[i].name,leader[i].count); return 0; } yu zhang Sun yu yu zhang Sun zhang zhang zhang 3 Result: yu:3 zhang:5 Sun:2 -------------------------------- Process exited after 60.11 seconds with return value 0 请按任意键继续. . .
这题挺有意思,第一次接触,照着书抄完,发现输入的时候很逗,投票......错误出现过,但看底下提示,貌似还可以吧
9.4
#include<stdio.h> #include<stdlib.h> struct Student//声明结构体类型 struct person { int num; char name[20]; float score; }; int main() { struct Student stu[5]={{10101,"mm",78},{10103,"qq",98},{10106,"ee",86},{10108,"cc",73},{10110,"ff",100}};//定义结构体数组并初始化 struct Student temp; const int n=5; int i,j,k; printf("the order is:\n"); for(i=0;i<n-1;i++) {k=i; for(j=i+1;j<n;j++) if(stu[j].score>stu[k].score) //if 语句 k=j; temp=stu[k];stu[k]=stu[i];stu[i]=temp; //stu[k]和stu[i]元素互换 } for(i=0;i<n;i++) printf("%d,%s,%d",stu[i].num,stu[i].name,stu[i].score); printf("\n"); return 0; } the order is: 10110,ff,010103,qq,010106,ee,010101,mm,010108,cc,0 -------------------------------- Process exited after 0.01435 seconds with return value 0 请按任意键继续. . .
这题给我搞得恶心了 ,打字很累........难度还可以吧,用了for循环.
9.5
#include<stdio.h> #include<stdlib.h> struct Student//声明结构体类型 { int num; char name[20]; char sex; int age; }; struct Student stu[3]={{10101,"yu",'M',78},{10103,"pp",'M',98},{10106,"ll",'M',86}};//定义结构体数组并初始化 main() {struct Student *p;//定义指向struct student类型数据的指针变量p printf("NO.Name sex age\n"); for(p=stu;p<stu+3;p++) //for 循环 printf("%d,%s,%c,%d",p->num,p->name,p->sex,p->age); return 0; } NO.Name sex age 10101,yu,M,7810103,pp,M,9810106,ll,M,86 -------------------------------- Process exited after 0.06786 seconds with return value 0 请按任意键继续. . .
这道题学习了结构指针,三种打印方法。发生了一个错误,注释竟然忘了加// .......... 此题改动了.
9.6
#include<stdio.h> #include<string.h> main() {struct Student //结构 {long num; char name[20]; char sex; float score; }; struct Student stu_1;//定义struct student类型的变量stu_1 struct Student *p;//定义指向struct student类型数据的指针变量p p=&stu_1;//p指向stu—1 stu_1.num=10101;//对结构体变量的成员赋值 strcpy(stu_1.name,"li lin");//用字符串复制函数给stu_1.name赋值 stu_1.sex='M'; stu_1.score=89; printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score); printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",(*p).num,(*p).name,(*p).sex,(*p).score); return 0; } NO.:10101 name:li lin sex:M score:0 NO.:10101 name:li lin sex:M score:0 -------------------------------- Process exited after 0.01277 seconds with return value 0 请按任意键继续. . .
9.7
#include <stdio.h> #define N 3 //定义 struct Student //结构体 { int num; char name[20]; float score[3]; float aver; }; int main() { void input(struct Student stu[]);//声明函数 struct Student max(struct Student stu[]); void print(struct Student stu); struct Student stu[N],*p=stu; input(p);//调用函数 print(max(p));//返回指针 return 0; } void input(struct Student stu[]) //定义 { int i; printf("请输入各学生的信息:学号、姓名、三门学科的成绩:\n"); for(i=0;i<N;i++) { scanf("%d %s %f %f %f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0; } } struct Student max(struct Student stu[]) { int i,m=0; for(i=0;i<N;i++) if(stu[i].aver>stu[m].aver)m=i; return stu[m]; } void print(struct Student stud) { printf("\n成绩最高的学生是:\n"); printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2]); } 请输入各学生的信息:学号、姓名、三门学科的成绩: 13 yu 100 90 95 14 sa 90 90 90 15 qq 90 85 95 成绩最高的学生是: 学号:13 姓名:yu 三门课成绩:100.0, 90.0, 95.0平均成绩: 0.00 -------------------------------- Process exited after 90.8 seconds with return value 0 请按任意键继续. . .
总结一下,这些题一道比一道难,尤其是最后一个,受不了啊,头疼。学到了一些知识,用了这么长时间.......虽然是抄写,但还是出现了很多错误,有的是因为没看见,有的是混淆概念。
还是那句话,希望课上老师能领着大家做做题,大家做完题能讲解一下。下面上截图。
总结一下 ,我想说这是个什么玩意,虽然学到点东西,但这也太麻烦了吧。真心累。老师,下次作业不用他可以吗.........
忽然发现第一题写绿书上的了,尴尬,容我补上。
#include<stdio.h> #include<stdlib.h> main() {struct Student {long int num; char name[20]; char sex; char addr[20]; }a={10101,"li lin",'M',"123 beijing Road"}; printf("NO.;%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr); return 0; } NO.;10101 name:li lin sex:M address:123 beijing Road -------------------------------- Process exited after 0.029 seconds with return value 0 请按任意键继续. . .
ok 可以了