结构体练习题
结构体练习题

结构体练习题:第1 题:计算日期的差值(1)编写一函数,计算两个日期之间的时间差,并将其值返回。
日期以年、月、日表示。
“时间差”以天数表示。
注意考虑日期之间的闰年。
函数的输入参数为日期1和日期2,函数的返回值为时间差,单位为天数。
(2)编写一程序,在主函数中输入两个日期,调用上述函数计算两个日期之间的时间差,并将结果输出。
为了计算简便,假设用户输入的日期1总是早于日期2。
参考代码:#include<stdio.h>struct date{int year;int month;int day;};int isLeap(int year);int dif(struct date a, struct date b);void main(){struct date a, b;printf("请输入日期1(空格分隔,年月日):\n");scanf("%d%d%d", &a.year, &a.month, &a.day);printf("请输入日期2(空格分隔,年月日,晚于日期1):\n");scanf("%d%d%d", &b.year, &b.month, &b.day);printf("相差天数为:");printf(" %d 天\n", dif(a, b));}int isLeap(int year) //判断一个年份是否是闰年的函数{if(year%400==0 || (year%4==0 && year%100!=0))return 1;elsereturn 0;}int dif(struct date a, struct date b){int i;long day=0, day1=0, day2=0;intd[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,3 1,31,30,31,30,31}};// day变量为年份a到年份b前一年的年份总天数for(i=a.year; i<b.year; i++)if(isLeap(i))day += 366;elseday += 365;// day1变量为年份a年初到当天的年内总天数for(i=1; i<a.month; ++i)day1 += d[isLeap(a.year)][i];day1 += a.day;// day1变量为年份b年初到当天的年内总天数for(i=1; i<b.month; ++i)day2 += d[isLeap(b.year)][i];day2 += b.day;return day + day2 - day1;}参考截图:第2 题:结构体数组应用请定义一个描述学生基本信息的结构,包括姓名,学号,籍贯,身份证号,年龄,家庭住址,性别,联系方式等。
结构体习题

结构体习题49、以下程序运行结果是______。
#include <string.h>typedefstruct student{char name[10];longsno;float score;}STU;main(){ STUa={"Zhangsan",2001,95},b={"Shangxian",2002,90},c={"Anhua",2003,95},d,*p=&d;d=a;if(strcmp(,)>0) d=b;if(strcmp(,)>0) d=c;printf("%ld %s\n",d.sno,p->name);}标准答案为:2002 Shangxian1. 填空题人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。
函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若指定编号不存在,返回数据中的编号为空串。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!#include <stdio.h>#include <string.h>#define N 8typedefstruct{ charnum[10];intyear,month,day ;}STU;/**********found**********/STU fun(STU *std, char *num){ int i; STU a={"",9999,99,99};for (i=0; i<N; i++)/**********found**********/if(strcmp(std[i].num,num)==0 )/**********found**********/return (std[i]);return a;}main(){ STU std[N]={ {"111111",1984,2,15},{"222222",1983,9,21},{"333333",1984,9,1},{"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15},{"777777",1983,6,22},{"888888",1984,8,19}};“”STU p;char n[10]="666666";p=fun(std,n);if(p.num[0]==0)printf("\nNot found !\n");else{ printf("\nSucceed !\n ");printf("%s %d-%d-%d\n",p.num,p.year,p.month,p.day);}}第1 处:STU fun(STU *std, char *num)第2 处:if(strcmp(std[i].num,num)==0 )第3 处:return (std[i]);2. 填空题人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中。
第8章结构体(答案)

第8章结构体(答案)第8章结构⼀、选择题1、设有以下说明语句struct ex{ int x;float y;char z;}example;则下⾯的叙述中不正确的是(B)A)struct是结构体类型的关键字B)example是结构体类型名C)x,y,z都是结构体成员名D)struct ex是结构体类型2、设有以下说明语句typedef struct{int a;char c[10];} ST;则下⾯的叙述中正确的是(B)A)ST是结构体变量名B)ST是结构体类型名C)typedef struct是结构体类型名D)struct是结构体类型名3、有以下定义,能输出字母M的语句是(D)struct person{char name[9];int age;};struct person class[10]={ “ohu”,17, “Paul”,19,“Mary”,18,“Adam”,16 } A)printf(“%c\n”,class[3].name[0]);B)printf(“%c\n”,class[3].name[1]);C)printf(“%c\n”,class[2].name[1]); D)printf(“%c\n”,class[2].name[0]);4、以下对结构体类型变量的定义中,不正确的是( B )A)typedef struct B)#define AA struct aa{int n; AA{int n;C)struct aa D)struct{int n;float m; {int n;}; float m;struct aa td1; }td1;5、下列程序输出的结果是(B)A)5 B)6 C)7 D)8struct abc{int a,b,c;};main(){struct abc s[2]={{1,2,3},{4,5,6}};int t;t=s[0].a+s[1].b;printf("%d\n",t);}6、在TC2.0下,int 类型为2个字节。
C语言结构体习题及答案知识讲解

{
printf("%c,%d\n",kw[3].Key[0], kw[3].ID);
}
A) i,3B) n,3C)f,4D)l,4
7.定义以下结构体类型
structstudent
{
char name[10];
int score[50];
float average;
}stud1;
则stud1占用内存的字节数是【 】。
} x[4]={{2008,10,1, "guangzhou"}, {2009,12,25, "Tianjin"}};
语句
printf("%s,%d,%d,%d",x[0].name,x[1].birthday.year);的输出结果为【】。
A)guangzhou,2009B)guangzhou,2008
C)printf("%s\n",class[3].name);
D)printf("%s\n",class[0].name);
10.定义以下结构体数组
struct date
{ int year;
int month;
int day; };
struct s
{ struct date birthday;
char name[20];
struct c
{ int x;
int y;
}s[2]={1,3,2,7};
语句printf("%d",s[0].x*s[1].x)的输出结果为【】
A) 14B)6C)2D) 21
5.运行下列程序段,输出结果是【】
struct country
3-结构体练习题

{"009","Zhangjie",'m',19,66},{"010","Wangmei",'f',21,39}};
C++程序设计课件 设计制作:徐龙琴 12
void sort(int n) //冒泡排序函数 {int i,j; struct student temp; for(i=0;i<n-1;i++) for(j=0;j<n-i;j++) if(st[j].mathscore<st[j+1].mathscore) {temp=st[j]; st[j]=st[j+1]; st[j+1]=temp; } } void print(int n) //输出函数 {int j;
b.p++
c.(*p).num d.p=&stu.age
结构基础练习(带答案)

for(;p<q;p++)
{ printf(“age:sex:name”);
scanf(“%u%s”,&p->age,p->sex);
__ scanf(“%s”,p->name_____________; }
}
data_out(struct man *p,int n)
C.scanf(“%d”,&p->age);D.scanf(“%d”,&stu[0].num);
8.已知struct{
int x,y;
}s[2]={{1,2},{3,4}},*p=s;
则表达式++p->x值___2_____表达式(++p)->x值__3_____
9.以下程序运行的正确结果是___10,x___
C.(a.x<b.x) ? a.x: b.x D. c.x=a.x, c.y=a.y
3.若有以下结构体定义,正确引用或定义是______D____
struct example{
int x,y;}v1;
A.example.x=10; B.example v2.x=10;
C.struct v2;v2.x=10 D.struct example v2={10}
if (head==NULL)
{______head=p0____;
p0->next=NULL;
}
else
while ((p0->data < p1->data) && (p1->next!=NULL))
习题十结构体共用体习题

习题十结构体班级: 学号: 姓名:1. 填空题(1) 定义结构体的关键字是,定义共用体的关键字是。
(2) 结构体和共用体的相同点是,不同点是。
(3) 若有以下定义和语句,则sizeof(a)的值是__ ___,而sizeof(b)的值是__ _。
struct tu{ int m; char n; int y;}a;struct{ float p, char q; struct tu r} b;(4) 设有下面结构类型说明和变量定义,则变量a在内存所占字节数是。
如果将该结构改成共用体,结果为。
struct stud{ char num[6]; int s[4]; double ave; } a;(5) 下面程序用来输出结构体变量ex所占存储单元的字节数,请填空。
struct st{ char name[20]; double score; };main(){ struct st ex ; printf("ex size: %d\n",sizeof( )); }(6) 下面定义的结构体类型拟包含两个成员,其中成员变量info用来存入整形数据;成员变量link是指向自身结构体的指针,请将定义补充完整。
struct node{ int info; link; }(7) 以下程序执行后输出结果是。
main(){ union { unsigned int n; unsigned char c; } u1;u1.c='A'; printf("%c\n",u1.n); }(8) 变量root如图所示的存储结构,其中sp是指向字符串的指针域,next是指向该结构的指针域,data用以存放整型数。
请填空,完成此结构的类型说明和变量root的定义。
rootstruct{ char *sp ; __ __; _ _; } root;2. 阅读下面的程序,写出程序结果(1)struct info{ char a,b,c;};main(){ struct info s[2]={{‘a’,‘b’,‘c’},{‘d’,‘e’,‘f’}};int t;t=(s[0].b-s[1].a)+(s[1].c-s[0].b);printf("%d\n",t); }(2)void main(){ union { char i[2]; int k; } stu;stu.i[0]='2'; stu.k=0;printf("%s,%d\n",stu.i,stu.k);}(3)union myun{ struct{ int x, y, z; } u; int k; } a;main(){ a.u.x=4; a.u.y=5; a.u.z=6; a.k=0; printf(“%d\n",a.u.x); }3. 程序设计题(1)设计一个程序,用结构类型实现两个复数相加。
结构体练习题

5. 有以下程序输出结果是(B )。
#include<stdio.h>
struct stu
{ int num;
char name[10];
int age; };
void fun(struct stu *p)
{ print("%s\n",(*p).name); }
void main()
{ struct stu students[3]={{9801,"zhang",20},
则下面的叙述中不正确的是_____B___。
A)struct 是结构体类型的关键字 B)example是结构体类型名
C)x、y、z都是结构体成员名
D)struct ex是结构体类
11.设有如下定义:
struct ss
{ char name[10];
int age;
char sex;
}std[3],*p=std;
}
程序运行后的输出结果是_______11_。
14.C语言共用体类型变量在程序执行期间:B
A.所有成员一直驻留在内存中 B.只有一个成员驻留在内存中
C.部分成员驻留在内存中
D. 没有成员驻留在内存中
15.设有以下说明,则下面的叙述不正确的是:C
union data
1、 以下程序的输出结果是A A) 0 B) 1 C) 3 D) 6
main() { struct cmp
{ int x; int y; } cnum[2]={1,3,2,7}; printf(“%d\n”,cnum[0].x/cnum[1].y*cnum[1].x); } 2、根据以下定义,能输出字母M的语句是D
a[2].n=9; a[2].next='\0';
结构体练习题

算并输出每个人的学号和平均成绩。
2.已知链表结点结构如下,假设动态链表已经建立,请编写删除给定学号的结点的函数。
(只编写删除子函数即可)3.编写函数实现动态链表的建立。
链表结点结构如下,要求在主函数中将你所建立的链表输出到屏幕上。
4.有10个学生,每个学生的信息包括学号、姓名、3门课的成绩,从键盘输入10个学生数据存入结构体数组中,要求输出个人总分最高的学生的信息(包括学号、姓名、3门课成绩、总分)。
5.链表的结点数据类型如下:struct node{int data;struct node *next;};链表的建立和输出函数如下,编写将第i个结点删除的函数,并完善主函数,调试运行整个程序。
struct node *creat(){ int x;struct node *h,*s,*r;h=(struct node *)malloc(sizeof(struct node));r=h;scanf("%d",&x);while(x!=-1){ s=(struct node*)malloc(sizeof(struct node));s->data=x;r->next=s;r=s;scanf("%d",&x);}r->next=NULL;return h;}void print(struct node *h) //打印函数{ struct node *p;p=h->next;if(p==NULL)printf("list is empty!");else{ while(p!=NULL){ printf("%4d",p->data);p=p->next;}}}并输出其平均成绩。
7.试利用指向结构体的指针编制一程序,实现输入三个学生的学号、数学期中和期末成绩,然后计算其平均成绩并输出成绩表。
c语言结构体试题及答案

c语言结构体试题及答案1. 定义一个结构体,包含学生的姓名、学号和成绩。
```cstruct Student {char name[50];int id;float score;};```2. 编写一个函数,计算结构体数组中所有学生的平均成绩。
```cfloat calculateAverageScore(struct Student students[], int size) {float sum = 0.0;for (int i = 0; i < size; i++) {sum += students[i].score;}return sum / size;}```3. 给定一个结构体数组,编写一个函数,返回成绩最高的学生。
```cstruct Student* findHighestScoreStudent(struct Student students[], int size) {struct Student* highest = students;for (int i = 1; i < size; i++) {if (students[i].score > highest->score) {highest = &students[i];}}return highest;}```4. 编写一个函数,将结构体数组中的学生信息打印出来。
```cvoid printStudents(struct Student students[], int size) { for (int i = 0; i < size; i++) {printf("Name: %s, ID: %d, Score: %.2f\n", students[i].name, students[i].id, students[i].score);}}```5. 如何定义一个结构体,其中包含另一个结构体类型的成员?```cstruct Inner {int a;float b;};struct Outer {struct Inner inner;char c[100];};```6. 编写一个函数,交换两个结构体变量的值。
结构体练习题

结构体练习题1.设有如下说明语句:struct ex{ int x;float y; char z;} example;则下面的叙述中不正确的是________。
A)struct 是结构体类型的关键字B)example是结构体类型名C)x、y、z都是结构体成员名D)struct ex是结构体类型正确答案:B(知识点:结构体类型及结构体变量的定义)试题分析:example是结构体类型的变量名。
2.设有如下定义:struct ss{ char name[10];int age;char sex;}std[3],*p=std;下面各输入语句中错误的是________。
A)scanf("%d",&(*p).age); B)scanf("%s",&);C)scanf("%c",&std[0].sex); D)scanf("%c",&(p->sex));正确答案:B(知识点:结构体数组、指向结构体数组的指针、结构体变量的引用)3.设有如下定义:struct sk{ int a;float b;}data;int *p;若要使p指向data中的a域,正确的赋值语句是________。
A)p=&a B)p=data.a; C)p=&data.a D)*p=data.a正确答案:C(知识点:结构体成员地址的引用、指向结构体成员的指针)4.以下选项中不能正确把c1定义成结构体变量的是________。
A)typedef struct B)struct color c1{int red; {int red;int green; int green;int blue;}COLOR int blue;};COLOR c1;C)struct color D)struct{int red; {int n;int green; int green;int blue;}c1; int blue;}c1;正确答案:B(知识点:结构体变量的定义、自定义变量)5.有以下程序:struct s{int x,y;}data[2]={10,100,20,200};main(){struct s *p=data;printf("%d\n ",++(p->x));}程序运行后的输出结果是________。
结构体-练习题

结构体-练习题1:输⼊若⼲⼈员姓名电话,义字符“#”表⽰结束,输⼊姓名,查找该⼈电话scanf :当遇到回车,空格和tab键会⾃动在字符串后⾯添加'\0',但是回车,空格和tab键仍会留在输⼊的缓冲区中。
scanf()可以读取所有类型的变量 gets:可接受回车键之前输⼊的所有字符,并⽤'\n'替代'\0'.回车键不会留在输⼊缓冲区中。
gets()⽤到读取字符串,⽤回车结束输⼊使⽤ gets() 时,系统会将最后“敲”的换⾏符从缓冲区中取出来,然后丢弃,所以缓冲区中不会遗留换⾏符。
这就意味着,如果前⾯使⽤过 gets(),⽽后⾯⼜要从键盘给字符变量赋值的话就不需要吸收回车清空缓冲区了,因为缓冲区的回车已经被 gets() 取出来扔掉了。
如果前⾯使⽤的不是 gets() ⽽是scanf,那么通过键盘给 ch 赋值前就必须先使⽤getchar() 清空缓冲区。
struct person{char name[10];char tel[20];};void search1(struct person a[],char *x,int j){int i=0;while(strcmp(a[i].name,x)!=0&&i<j)i++;if(i<j)printf("%s",a[i].tel);else printf("null");}int main(){struct person s[100];int i=0;char na[10],te[20];while(1){printf("输⼊姓名:");gets(na);if(strcmp(na,"#")==0){break;}printf("输⼊电话:");gets(te);strcpy(s[i].name,na);strcpy(s[i].tel,te);i++ ;}printf("输⼊查找姓名");gets(na);search1(s,na,i);return0;}2:1:根据输⼊⽇期(年⽉⽇)求出改天是该年的第⼏天 2:根据输⼊的年份和天数,求出对应⽇期1,3,5,7,8,10,12:31天闰年:2:29平年:2:28其余4,6,9,11:30天根据年份算出是闰年还是平年四年⼀闰,百年不闰,四百年再闰struct DATE{int year;int month;int day;}date;int main(){int run=0,curdays;int monthday[13]={0,31,0,31,30,31,30,31,31,30,31,30,31};printf("输⼊年⽉⽇天数:");scanf("%d %d %d %d",&date.year,&date.month,&date.day,&curdays);if(date.year%4==0&&date.year%100!=0||date.year%400==0){run=1;}if(run==1){//润年monthday[2]=29;}else{monthday[2]=28;}//求天数int temp=0,j,num;switch(date.month){case1:for(j=0;j<1;j++)temp+=monthday[j];break;case2:for(j=0;j<2;j++)temp+=monthday[j];break;case3:for(j=0;j<3;j++)temp+=monthday[j];break;case4:for(j=0;j<4;j++)temp+=monthday[j];break;case5:for(j=0;j<5;j++){temp+=monthday[j];printf("%d+",monthday[j]);}break;case6:for(j=0;j<6;j++){temp+=monthday[j];printf("%d+",monthday[j]);}break;case7:for(j=0;j<7;j++){temp+=monthday[j];printf("%d+",monthday[j]);}break;case8:for(j=0;j<8;j++){temp+=monthday[j];printf("%d+",monthday[j]);}break;case9:for(j=0;j<9;j++){temp+=monthday[j];printf("%d+",monthday[j]);}break;case10:for(j=0;j<10;j++){temp+=monthday[j];printf("%d+",monthday[j]);}break;case11:for(j=0;j<11;j++)temp+=monthday[j];break;case12:for(j=0;j<12;j++)temp+=monthday[j];break;}num=temp+date.day;printf("%d",date.day);printf("\n第%d天\n",num);//求年⽉⽇int i,curM=0,curD=0;for(i=0;i<13;i++){curdays-=monthday[i];printf("curdays=%d\n",curdays);curM++;if(curdays<=31&&curdays>0){curD=curdays;break;}}printf("%d年%d⽉%d⽇",date.year,curM,curD);}//可以做成菜单再计算(后续⽅法不写了)int a[2][13]={{0,31,29,31,30,31,30,31,31,30,31,30,31},{0,31,28,31,30,31,30,31,31,30,31,30,31}}; int main(){while(1){int m;printf("选择1:输⼊年⽉⽇,输出第⼏天;2:输⼊年,第⼏天,输出年⽉⽇\n");scanf("%d\n",&m);if(m==1){int n,y,r;printf("请输⼊年⽉⽇\n");scanf("%d %d %d",&n,&y,&r);searchdd(n,y,r);}else if(m==2){int n,tprintf("请输⼊年天数\n");scanf("%d %d",&n,&t);searchyr(n,t);}else break;}}。
C语言程序设计基础-结构体习题

{ int num; char name[20]; float score; };
int main()
{ struct Student stu[5]={{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",
86
},{10108,“Ling”, 73.5},{10110,“Fun”, 100 } };
hing at a time and All things in their being are good for somethin
#include <stdio.h>
#define N 3
struct Student
{ int num;
char name[20];
float score[3];
float aver;
printf(" No. Name sex age\n"); for(p=stu;p<stu+3;p++)
printf(“%5d %-20s %2c %4d\n”,p->num, p->name, p->sex, p->age); return 0; }
7、有 n 个结构体变量,内含学生学号、姓名和 3 门课程的成绩。要求输出平均成绩最高的 学生的信息(包括学号、姓名、3 门课程成绩和平均成绩)。
person[i].category.clas); else
printf("%-6d%-10s%-4c%-4c% -10s\n",person[i].num,person[i].
name,person[i].sex, person[i].job, person[i].category.position); } return 0; }
结构体习题1

1、在说明一个结构体变量时,系统分配给它的存储空间是。
A、该结构体中第一个成员所需的存储空间B、该结构体中最后一个成员所需的存储空间C、该结构体中占用最大存储空间的成员所需的存储空间D、该结构体中所有成员所需存储空间的总和2、结构体变量在程序执行期间。
A、所有成员一直驻留在内存中B、只有一个成员驻留在内存中C、部分成员驻留在内存中D、没有成员驻留在内存中3、设有以下说明语句:typedef struct{ int n;char ch[8];}PER;则下面叙述中正确的是。
A、PER是结构体变量名B、PER是结构体类型名C、typedef struct 是结构体类型D、struct 是结构体类型名4、有如下定义:struct date{ int year;int month;int day;};struct worklist{ char name[20];char sex;struct date birthday;}person;对结构体变量person的出生年份进行赋值时,下面正确的赋值语句是。
A、year=1958B、birthday. year=1958C、person. birthday. year=1958D、person. year=19585、若有下面的说明和定义:struct test{int m1;char m2;float m3;union uu{char u1[5];int u2[2];}ua;}myaa;则sizeof(stuct test)的值是。
A、12B、16C、14D、96、以下对结构体类型变量td1的定义中,不正确的是。
A 、typedef struct aa{ int n;float m;} AA;AA td1; B 、struct aa { int n;float m;} td1;C 、struct { int n;float m;} aa;struct aa td1;D 、struct { int n;float m;} td1;7、设有如下定义:struct sk{int a;float b;} data;int *p;若要使p指向data中的a域,正确的赋值语句是。
C语言结构体习题及答案

C语言结构体习题及答案第9章结构体1.定义以下结构体类型structs{inta;charb;floatf;};则语句printf(\的输出结果为【】。
a)3b)7c)6d)42.当定义一个结构体变量时,系统为它分配的内存空间就是【】a)结构中一个成员所需的内存容量b)结构中第一个成员所需的内存容量c)结构体中占内存容量最大者所需的容量d)结构中各成员所需内存容量之和3.定义以下结构体类型structs{intx;floatf;}a[3];语句printf(\的输入结果为【】a)4b)12c)187.定义以下结构体类型structstudent{charname[10];intscore[50];floataverage;}stud1;则stud1占用内存的字节数是【】。
a)64b)114c)228d)79、建有一结构体类型变量定义如下:structdate{intyear;intmonth;intday;};structworklist{charname[20];charsex;structdatebirthday;}person;若对结构体变量person的出生年份进行赋值时,下面正确的赋值语句是。
d)6a.year=1976b.birthday.year=1976c.person.birthday.year=1976d.person.year=19761、若程序中存有以下的表明和定义:structabc{intx;chary;}花括号后少了分号。
structabcs1,s2;则可以出现的情况就是______。
a)编程时错b)程序将顺序编程、相连接、继续执行c)能够顺序通过编程、相连接、但无法继续执行d)能够顺序通过编程、但相连接失效。
结构体和共用体习题

结构体和共用体习题习题六1. 从下列四个选项中选择一个正确的填入括号中。
(1)在说明一个结构体变量时系统分配给它的存储空间是( D)。
A该结构体中第一个成员所需存储空间 B该结构体中最后一个成员所需存储空间C该结构体中占用最大存储空间的成员所需存储空间 D该结构体中所有成员所需存储空间的总和(2)在说明一个共用体变量时系统分配给它的存储空间是(D )。
A该共用体中第一个成员所需存储空间 B该共用体中最后一个成员所需存储空间C该共用体中占用最大存储空间的成员所需存储空间 D该共用体中所有成员所需存储空间的总和(3)共用体类型在任何给定时刻, ( B)。
A所有成员一直驻留在内存中 B只有一个成员驻留在内存中 C部分成员驻留在内存中D没有成员驻留在内存中(4)以下定义结构体类型的变量st1,其中不正确的是(A )A typedef stuct student{int num; int age; }STD; STD st1; B struct student{int num,age; }st1; C struct{int num; float age; }st1;D struct student{int num; int age; };struct student st1;(5)已知职工记录描述为:struct workers {int no;char name[20]; char sex; struct {int day; int month; int year; }birth; };struct workers w;设变量w中的”生日”应是”1993年10月25日”,下列对”生日”的正确赋值方式是( C)。
A day=25; month=10; year=1993;B w.day=25w.month=10; w.year=1993;C w.birth.day=25; w.birth.month=10; w.birth.year=1993;D birth.day=25; birth.month=10; birth.year=1993;(6)设有如下定义:struct sk {int a; float b; }data,*p;若有p=&data;则对data中的a成员的正确引用是( B)。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
算并输出每个人的学号和平均成绩。
2.已知链表结点结构如下,假设动态链表已经建立,请编写删除给定学号的结点的函数。
(只编写删除子函数即可)3.编写函数实现动态链表的建立。
链表结点结构如下,要求在主函数中将你所建立的链表输出到屏幕上。
4.有10个学生,每个学生的信息包括学号、姓名、3门课的成绩,从键盘输入10个学生数据存入结构体数组中,要求输出个人总分最高的学生的信息(包括学号、姓名、3门课成绩、总分)。
5.链表的结点数据类型如下:struct node{int data;struct node *next;};链表的建立和输出函数如下,编写将第i个结点删除的函数,并完善主函数,调试运行整个程序。
struct node *creat(){ int x;struct node *h,*s,*r;h=(struct node *)malloc(sizeof(struct node));r=h;scanf("%d",&x);while(x!=-1){ s=(struct node*)malloc(sizeof(struct node));s->data=x;r->next=s;r=s;scanf("%d",&x);}r->next=NULL;return h;}void print(struct node *h) //打印函数{ struct node *p;p=h->next;if(p==NULL)printf("list is empty!");else{ while(p!=NULL){ printf("%4d",p->data);p=p->next;}}}并输出其平均成绩。
7.试利用指向结构体的指针编制一程序,实现输入三个学生的学号、数学期中和期末成绩,然后计算其平均成绩并输出成绩表。
8.有一个unsigned long型整数,现要分别将其前2个字节和后2个字节作为两个unsigned int型整数输出(设一个int型数据占2个字节),试编一函数partition实现上述要求。
要求在主函数中输入该long型整数,在函数partition中输出结果。
9.编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num、name、score[3],用主函数输入这些记录,用print函数输出这些记录。
用结构体共用体来实现。
10.有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生数据,要求打印出3门课总平均成绩(指全体学生所有成绩的平均值),以及最高分的学生的数据(包括学号、姓名、3门课成绩、平均分数)。
用结构体来实现。
11.有一个结构体变量stu,内含学生学号、姓名和3门课的成绩。
要求在main函数中赋以值,在另一函数print中将它们打印输出。
用结构体变量作函数参数。
12.对候选人得票的统计程序。
设有3个候选人,有20个人对其进行投票,每次输入一个得票的候选人的名字,要求最后输出各人得票结果。
用结构体的方法实现。
13.定义一个结构体变量(包括年、月、日)。
计算该日在本年中是第几天?注意闰年问题。
14.请删除已知链表中的指定结点。
要求删除的结点数据从键盘输入。
#define NULL 0struct student{long num;float score;struct student *next;};main(){struct student a,b,c,*head,*p;a.num=99101; a.score=89.5;b.num=99103; b.score=90;c.num=99107; c.score=85;head=&a;a.next=&b;b.next=&c;c.next=NULL;p=head;do{printf("%ld %5.1f\n",p->num,p->score);p=p->next;}while(p!=NULL);del();/*请完成此函数来删除某一结点*/}答案:略15.请在已知链表中插入某一指定结点。
要求插入的结点数据从键盘输入,顺序按学号升序插入。
#define NULL 0struct student{long num;int score;struct student *next;};main(){struct student a,b,c,*head,*p;a.num=99101; a.score=89;b.num=99103; b.score=90;c.num=99107; c.score=85;head=&a;a.next=&b;b.next=&c;c.next=NULL;p=head;do{printf("%ld %5d\n",p->num,p->score);p=p->next;}while(p!=NULL);insert();/*请完成此函数来插入某一结点*/}答案:略16.请编写程序动态生成一个链表。
要求结点数据从键盘输入。
并将建立好的链表输出到屏幕上。
结点数据类型如下:struct student{long num;int score;struct student *next;};答案:略17.定义一个结构体变量(包括年、月、日)。
计算该日在本年中是第几天?注意闰年问题。
请写一函数days来完成此功能。
要求由主函数将年、月、日传递给days函数,计算后将日子数传回主函数输出。
答案:略18.有两个链表a和b。
设结点中包含学号、姓名。
从a链表中删去与b链表中有相同学号的那些结点。
链表a的数据:{"101","wang"},{"102","li"},{"105","zhang"},{"106","wei"}链表b的数据:{"103","chang"},{"104","ma"},{"105","zhang"},{"107","guo"},{"108","liu"}19.建立一个链表,每个结点包括:学号、姓名、性别、年龄。
输入一个年龄,如果链表中的结点所包括的年龄等于此年龄,将此结点删去。
请完善程序。
#include <malloc.h>#define NULL 0#define LEN sizeof(struct student)struct student{ char num[8];char name[10];char sex[2];int age;struct student *next;}stu[10];main(){struct student *p,*pt.*head;int i,length,iage,flag=1;int find=1;while(flag==1){ printf("input list's length (<10):\n");scanf("%d",&length);if(length<10)flag=0;}/*请完成建立链表*//*输出*/p=head;printf("\n num name sex age \n");while(p!=NULL){ printf("%8s%8s8s%8d\n",p->num,p->name,p->sex,p->age);p=p->next;}/*请完成删除*//*输出*/p=head;printf("\n num name sex age \n");while(p!=NULL){ printf("%8s%8s8s%8d\n",p->num,p->name,p->sex,p->age);p=p->next;}}20.以下为有3名学生数据的单向动态链表的综合操作。
今给出main函数、建立链表函数creat、链表输出函数print。
建立链表时输入各结点的数据按学号增序排列,使建立的链表有序。
请完善程序,要求编写按学号顺序插入结点的函数insert。
#include <malloc.h>#define NULL 0#define LEN sizeof(struct student)struct student{ long num;int score;struct student *next;};int n;struct student *creat(void) /*要求按学号递增顺序输入*/{ struct student *head;struct student *p1,*p2;n=0;p1=p2=(struct student*) malloc(LEN);scanf("%ld,%d",&p1->num,&p1->score);head=NULL;while(p1->num!=0){ n=n+1;if(n==1) head=p1;else p2->next=p1;p2=p1;p1=(struct student*)malloc(LEN);scanf("%ld,%d",&p1->num,&p1->score);}p2->next=NULL;return(head);}void print(struct student *head){ struct student *p;printf("\nNow,These %d records are:\n",n);p=head;if(head!=NULL)do{ printf("%ld %d\n",p->num,p->score);p=p->next;}while(p!=NULL);}struct student *insert(struct student *head,struct student *stud){}main(){ struct student *head,stu;printf("Input records:\n");head=creat();print(head);printf("\ninput the inserted record");scanf("%ld,%d",&stu.num,&stu.score);head=insert(head,&stu);print(head);getch();}21.有两个链表a和b。