「C語言」「例題」結構體與共用體,c語言結構體和共用體
本篇收集《C語言程序設計教程》第九章“結構體與共用體”的所有例題。

![]()
1 #include <stdio.h>
2
3 /*輸入一個學生的基本信息,
4 包括學號、姓名、性別、年齡、出生日期、三門成績,
5 輸出該學生的基本信息和平均成績*/
6
7 struct date
8 {
9 int year;/* 年份 */
10 int month;/* 月份 */
11 int day; /* 日期 */
12 };
13
14 struct student
15 {
16 char num[9];/* 學號 */
17 char name[21];/* 姓名 */
18 char sex;/* 性別 */
19 int age;/* 年齡 */
20 struct date birthday;/* 引用了struct dat e類型 */
21 int score[3];/* 三門成績 */
22 float aver;/* 三門成績的平均值 */
23 };
24
25 int main()
26 {
27 struct student stu;
28 int i,sum=0;
29 printf("please input the information:\n");
30 printf("please input num:");
31 scanf("%s",&stu.num);
32 printf("please input name:");
33 scanf("%s",&stu.name);
34 printf("please input sex:");
35 flushall();/* 清楚輸入緩存區。以便正確輸入性別 */
36 scanf("%c",&stu.sex);
37 printf("please input age:");
38 scanf("%d",&stu.age);
39 printf("please input birthday:");
40 scanf("%d%d%d",&stu.birthday.year,&stu.birthday.month,&stu.birthday.day);
41 printf("please input scores:");
42 for(i=0;i<3;i++)/* 循環輸入該學生的三門成績 */
43 scanf("%d",&stu.score[i]);
44 for(i=0;i<3;i++)/* 計算該學生的三門成績的總和 */
45 sum+=stu.score[i];
46 stu.aver=sum/3.0;/* 計算該學生的三門成績的平均值 */
47 printf("\nthe information is:");
48 printf("\nthe num is %s",stu.num);
49 printf("\nthe name is %s",stu.name);
50 printf("\nthe sex is %c",stu.sex);
51 printf("\nthe age is %d",stu.age);
52 printf("\nthe birthday is %02d-%02d-%02d",stu.birthday.year,stu.birthday.month,stu.birthday.day);
53 printf("\nthe score is");
54 for(i=0;i<3;i++)
55 printf("%d ",stu.score[i]);
56 printf("\nthe aver is %.2f\n",stu.aver);
57 return 0;
58 }
9.1 一個學生基本信息的輸入和輸出

![]()
1 #include <stdio.h>
2 #include <string.h>
3
4 /*選票的統計:設有3個班長候選人"fu"、"lu"、"shou",全班共80個人,
5 個人只能選一位班長候選人的名字,再按票數從高到低的順序輸出各候選人的得票數 */
6
7 struct person
8 {
9 char name[20];
10 int count;
11 };
12
13 int main()
14 {
15 struct person leader[3]={"fu",0,"lu",0,"shou",0},t;
16 int i,j,k;
17 int n=3,m;/* 班長候選人數 */
18 char name[20];
19 printf("please input count of votes:");
20 scanf("%d",&m);
21 printf("please input the elestion`s name:\n");
22 for(i=1;i<=m;i++)
23 {
24 printf("No.%d:",i);
25 scanf("%s",&name);
26 for(j=0;j<3;j++)
27 if(strcmp(name,leader[j].name)==0)
28 leader[j].count++;
29 }
30 for(i=0;i<n-1;i++)/* 以得票數為關鍵,進行升序排序 */
31 {
32 k=i;
33 for(j=i+1;j<n;j++)
34 if(leader[k].count<leader[j].count)
35 k=j;
36 if(k!=i)
37 {
38 t=leader[i];
39 leader[i]=leader[k];
40 leader[k]=t;
41 }
42 }
43 printf("the vote leader is:\n");
44 for(i=0;i<3;i++)
45 printf("%s\t%d\n",leader[i].name,leader[i].count);
46 return 0;
47 }
9.2 選票的統計

![]()
1 #include <stdio.h>
2 #include <conio.h>
3 #include <windows.h>
4
5 //編寫一個競賽用的時鐘程序,按S鍵開始計時,按E鍵停止計時
6
7 struct clock
8 {
9 int hours;
10 int minutes;
11 int seconds;
12 };
13
14 void display(struct clock t)/* 顯示時鐘時間,結構體變量整體作為函數參數 */
15 {
16 printf("\r%02d:",t.hours);
17 printf("%02d:",t.minutes);
18 printf("%02d",t.seconds);
19 }
20
21 struct clock update(struct clock t)/* 時鐘時間每隔1秒進行更新 */
22 {/* 結構體類型作為函數的返回值 */
23 t.seconds++;
24 if(t.seconds==60)
25 {
26 t.seconds=0;
27 t.minutes++;
28 }
29 if(t.minutes==60)
30 {
31 t.minutes=0;
32 t.hours++;
33 }
34 if(t.seconds==24)
35 t.hours=0;
36 sleep(1000);/* 系統暫定一秒 */
37 return t;
38 }
39
40 int main()
41 {
42 struct clock cl={0,0,0};/* 初始化從0開始 */
43 char ch;
44 printf("please press \"s\" to start my clock\n");
45 printf("please press \"e\" to end my clock\n");
46 display(cl);
47 ch=getch();
48 while(1)
49 {
50 if(ch=='s' || ch=='S')
51 {
52 cl=update(cl);
53 display(cl);
54 if(kbhit())/* 檢測當前是否有鍵盤輸入 */
55 {
56 ch=getch();
57 if(ch=='e' || ch=='E')
58 break;
59 }
60 }
61 else if(ch=='e' || ch=='E')
62 break;
63 else
64 ch=getch();
65 }
66 printf("\n");
67 return 0;
68 }
9.3 編寫一個競賽用的時鐘程序,按S鍵開始計時,按E鍵停止計時

![]()
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define N 20 /*學生總人數不超過20人*/
4
5 //編寫程序完成對學生成績的管理,要求實現的功能包括:
6 //(1)學生信息錄入:從鍵盤按學號順序輸入n名學生信息(學號、姓名、3門課程成績)。
7 //(2)計算平均值:計算每個學生3門課程成績的平均值。
8 //(3)學生信息排序:按照平均值從低到高的順序對錄入的學生信息進行排序。
9 //(4)學生信息輸出:將排好序的學生信息輸出。
10
11 struct student/* 學生結構體類型定義 */
12 {
13 int id;
14 char name[20];
15 int score[3];
16 float aver;
17 };
18
19 void Input(struct student st[],int n)/* n個學生信息的錄入 */
20 {
21 int i,j;
22 printf("Please input Information:\n");
23 for(i=0;i<n;i++)
24 {
25 scanf("%d",&st[i].id);
26 scanf("%s",st[i].name);
27 for(j=0;j<3;j++)
28 scanf("%d",&st[i].score[j]);
29 }
30 }
31
32 void CalAver(struct student st[],int n)/* 計算n個學生3門課的平均值 */
33 {
34 int i,j;
35 for(i=0;i<n;i++)
36 {
37 int s=0;
38 for(j=0;j<3;j ++)
39 s+=st[i].score[j];
40 st[i].aver=s/3.0;
41 }
42 }
43
44 void Sort(struct student st[],int n)/* 依據平均值,對n個學生的信息進行排序 */
45 {
46 int i,j,k;
47 struct student t;
48 for(i=0;i<n-1;i++)
49 {
50 k=i;
51 for(j=i+1;j<n;j++)
52 if(st[k].aver>st[j].aver)
53 k=j;
54 if(k!=i)
55 {
56 t=st[k];
57 st[k]=st[i];
58 st[i]=t;
59 }
60 }
61 }
62
63 void Output(struct student st[],int n)/* n個學生的信息輸出 */
64 {
65 int i,j;
66 for(i=0;i<n;i++)
67 {
68 printf("%d\t%s",st[i].id,st[i].name);
69 for(j=0;j<3;j++)
70 printf("\t%d",st[i].score[j]);
71 printf("\t%f\n",st[i].aver);
72 }
73 }
74
75 int main()
76 {
77 int n;
78 struct student st[N];
79 printf("please input numbers: ");
80 scanf("%d",&n);
81 Input(st,n);/* 調用輸入函數 */
82 CalAver(st,n);/* 調用計算平均值函數 */
83 Sort(st,n);/* 調用排序函數 */
84 Output(st,n);/* 調用輸出函數 */
85 return 0;
86 }
9.4 學生成績的管理(錄入、平均值、排序、輸出)

![]()
1 #include <stdio.h>
2
3 //通過共用體變量,將一個整數的2字節分別按十六進制和字符方式輸出
4
5 union int_char
6 {
7 char ch[2];
8 int i;
9 };
10
11 void OutPut(union int_char x)
12 {
13 printf("i=%d\ti=%X\n",x.i,x.i);
14 printf("ch0=%X,ch1=%X\n",x.ch[0],x.ch[1]);
15 printf("ch0=%c,ch1=%c\n",x.ch[0],x.ch[1]);
16 }
17
18 int main()
19 {
20 union int_char x;
21 x.i=19788;
22 OutPut(x);
23 return 0;
24 }
9.5 通過共用體變量,將一個整數的2字節分別按十六進制和字符方式輸出

![]()
1 #include <stdio.h>
2 #define N 10
3
4 /*
5 一個班體育課成績,男生測驗1500米成績x分x秒,女生測驗柔韌性(分A、B、C、D和E5等)
6 和俯臥撐次數,將測驗數據放在一張表中,表中包括學號、姓名、 性別和體育成績。
7 最後一項“體育成績 ”的內容根據性別填寫不同的內容。
8 編程輸入成績數據,再以表格形式輸出。
9 */
10
11 struct boyscore
12 {
13 int minute;
14 int second;
15 };
16
17 struct girlscore
18 {
19 char flexibility;
20 int number;
21 };
22
23 struct student
24 {
25 char num[9];
26 char name[21];
27 char sex;
28 union
29 {
30 struct boyscore bs;
31 struct girlscore gs;
32 }score;
33 };
34
35 int main()
36 {
37 struct student st[N];
38 int n,i;
39 printf("please input number of students:");
40 scanf("%d",&n);
41 printf("please input num name sex score:\n");
42 for(i=0;i<n;i++)
43 {
44 printf("No%d.stu is: ",i+1);
45 scanf("%s%s %c",st[i].num,st[i].name,&st[i].sex);
46 if(st[i].sex=='b' || st[i].sex=='B')
47 scanf("%d%d",&st[i].score.bs.minute,&st[i].score.bs.second);
48 else if(st[i].sex=='g' || st[i].sex=='G')
49 scanf(" %c%d",&st[i].score.gs.flexibility,&st[i].score.gs.number);
50 else
51 printf("input error!");
52 }
53 printf("\nthe information is:\n");
54 for(i=0;i<n;i++)
55 {
56 printf("%-8s %s\t",st[i].num,st[i].name,st[i].sex);
57 if(st[i].sex=='b' || st[i].sex=='B')
58 printf("%d:%d\n",st[i].score.bs.minute,st[i].score.bs.second);
59 else if(st[i].sex=='g' || st[i].sex=='G')
60 printf("%c and %d\n",st[i].score.gs.flexibility,st[i].score.gs.number);
61 }
62 return 0;
63 }
9.6 男女生的體育成績

![]()
1 #include <stdio.h>
2
3 //已知口袋中有紅、黃、白、藍、黑共5種不同顏色的小球,若依次從袋中取3個
4 //,問得到3種不同色球的可能取法。以每行顯示5種的方式,輸出所有的排列情況。
5
6 enum color {red,yellow,white,blue,black};/* 定義枚舉類型 */
7
8 int main()
9 {
10 enum color b[3];
11 int i,count=0;
12 for(b[0]=red;b[0]<=black;b[0]++)
13 for(b[1]=red;b[1]<=black;b[1]++)
14 for(b[2]=red;b[2]<=black;b[2]++)
15 if(b[0]!=b[1] && b[0]!=b[2] && b[1]!=b[2])/* 三種球顏色不同 */
16 {
17 count++;/* 使累加器count+1 */
18 printf("No.%-2d ",count);
19 for(i=0;i<3;i++)
20 {
21 switch(b[i])/* 根據不同情況,輸出球的顏色 */
22 {
23 case red:
24 printf("紅");
25 break;
26 case yellow:
27 printf("黃");
28 break;
29 case white:
30 printf("白");
31 break;
32 case blue:
33 printf("藍");
34 break;
35 case black:
36 printf("黑");
37 break;
38 }
39 }
40 if(count%5==0)/* 每行輸出5種情況 */
41 printf("\n");
42 else
43 printf("\t");
44 }
45 return 0;
46 }
9.7 摸不同顏色球的方案

![]()
1 #include <stdio.h>
2
3 //編寫程序輸出初始化好的結構體數組中的元素信息
4
5 typedef struct student/* 結構體類型定義 */
6 {
7 int id;
8 char name[20];
9 int score;
10 }STUDENT,*STU;
11
12 int main()
13 {
14 STUDENT st[3]={{10101,"Li Lin",98},{10102,"Zhang Fun",87},{10103,"Wang Min",79}};/* 結構體數組初始化 */
15 STUDENT *p;/* STU p;也可以定義結構體類型指針變量p */
16 for(p=st;p<st+3;p++)
17 printf("%-6d%s\t%d\n",p->id,p->name,p->score);
18 return 0;
19 }
9.8 編寫程序輸出初始化好的結構體數組中的元素信息

![]()
1 #include <stdio.h>
2
3 //編寫程序輸入一個學生的基本信息,包括學號、姓名、三門課程的成績、
4 //並計算該學生三門課程成績的平均值,最後輸出該學生信息
5
6 typedef struct person
7 {
8 int num;
9 char name[21];
10 int s[3];
11 float aver;
12 }PERSON;
13
14 void Input(PERSON *p)/* 用指向結構體的指針變量作形參 */
15 {
16 scanf("%d%s%d%d%d",&p->num,p->name,&p->s[0],&p->s[1],&p->s[2]);
17 p->aver=(p->s[0]+p->s[1]+p->s[2])/3.0;
18 }
19
20 int main()
21 {
22 PERSON st;
23 printf("please input information\n");
24 Input(&st);/* 實現引用傳遞 */
25 printf("\nthe information is:\n");
26 printf("%d\t%s\t %d %d %d %5.2f\n",st.num,st.name,st.s[0],st.s[1],st.s[2],st.aver);
27 return 0;
28 }
9.9 typedef及結構體指針實現學生的基本信息