程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> c語言結構體和聯合體例題

c語言結構體和聯合體例題

編輯:關於C
 第一題: 要求你設計一個能夠保存圖書信息的結構。圖書屬性包括:書名(title)、作者(author)和單價信息( price),並按照下面要求完成對於各種圖書的相關操作。 /*  struct books {  char title[100];  char author[20];  double price;  } doyle = { "My life as a budgie", "Mack Tom", 14.6 };  int main(void) {  struct books dicken = { "Thinking in C++", "Stephen Prata", 78 };  struct books panshin = { .title = "C++ Primer", .author = "Stanley Lippman",  .price = 92.5 };  printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",  doyle.title, doyle.author, doyle.price);  printf("\n");  printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",  dicken.title, dicken.author, dicken.price);  printf("\n");  printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",  panshin.title, panshin.author, panshin.price);  printf("\n");  printf("“Thinking in C++”這本書的價格調整後為:\n");  printf("\n");  printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",  dicken.title, dicken.author, dicken.price = 85);  return EXIT_SUCCESS;  }  */ 第二題: 為上面的關於圖書的程序,添加三個函數: /*(1)編寫顯示圖書信息函數show()。參數為結構的指針。顯示圖書信息的結構如下: The title is :My life as a budgie The author is :Mack Tom The price is :14.6 #include <stdio.h> #include <stdlib.h> struct Library {  const char title[20];  const char author[10];  double price; } panshin; void show(struct Library *doy) {  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",doy->title, doy->author, doy->price); } int main(void) {  struct Library doyle = { "My life as a budgie", "Mack Tom", 14.6 };  show(&doyle);  return EXIT_SUCCESS; }*/ /*(2)編寫初始化結構變量函數init(),參數為結構的指針。函數功能是將結構變量中的成員進行初始化。 #include <stdio.h> #include <stdlib.h> struct Library {  const char title[20];  const char author[10];  double price; } panshin; void init(struct Library *doyle, struct Library *dicken, struct Library *panshin) {  doyle ->title;  dicken ->author;  panshin ->price; } int main(void) {  struct Library doyle = { "My life as a budgie", "Mack Tom", 14.6 };  struct Library dicken ={ "Thinking in C++", "Stephen Prata", 78 };  struct Library panshin = { "C++ Prinner", "Stanley Lippman", 92.5 };  init(&doyle,&dicken,&panshin);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     doyle->title, doyle->author, doyle->price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     dicken->title, dicken->author, dicken->price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     panshin->title, panshin->author, panshin->price);  return EXIT_SUCCESS; }  */ /*(3)編寫從鍵盤上接受圖書信息的函數input(),參數為結構的指針。函數的功能是從鍵盤上接收相關圖 書信息,並將信息保存到指針所執行的圖書結構變量裡。 #include <stdio.h> #include <stdlib.h> struct Library {  const char title[20];  const char author[10];  double price; }; void input(struct Library *doyle,struct Library dicken,){  scanf("%s%s%lf", doyle->title, doyle->author,&doyle->price);  scanf("%s%s%lf",dicken->title, dicken->author, &dicken->price);  scanf("%s%s%lf", panshin->title, panshin->author, &panshin->price);  } int main(void) {  struct Library doyle;  struct Library dicken;  struct Library panshin ;  input(&doyle,&dicken,&panshin);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     doyle->title, doyle->author, doyle->price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     dicken->title, dicken->author, dicken->price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     panshin->title, panshin->author, panshin->price);  return EXIT_SUCCESS; }*/ /*(4)主程序按照下面流程完成功能實現: a)定義三個圖書對象doyle、dicken、panshin。 b)對結構對象進行初始化。 c)從鍵盤上接收圖書信息,分別保存到三個圖書對象中。 d)輸出三個圖書對象的圖書信息。   #include <stdio.h> #include <stdlib.h> struct Library {  const char title[20];  const char author[10];  double price; }doyle,dicken,panshin; int main(void) {  struct Library doyle;  struct Library dicken;  struct Library panshin ;  scanf("%s%s%lf", &doyle.title, &doyle.author, &doyle.price);  scanf("%s%s%lf",&dicken.title, &dicken.author, &dicken.price);  scanf("%s%s%lf", &panshin.title, &panshin.author, &panshin.price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     doyle.title, doyle.author, doyle.price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     dicken.title, dicken.author, dicken.price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     panshin.title, panshin.author, panshin.price);  return EXIT_SUCCESS; }  */ 第三題: 創建一個圖書館library(結構數組),裡面一共包含了上面這三本書。 創建一個結構數組library,使用上面所設計的函數init()對每本書進行初始化。 使用上面所設計的函數input()從鍵盤上接收圖書信息。 使用上面的函數show,將輸入的圖書信息顯示出來。 #include <stdio.h> #include <stdlib.h> struct Library {  const char title[20];   const char author[10];   double price; } book[3]; void input(struct Library *(book+1),struct Library *(book+2),struct Library *(book+3)){  scanf("%s%s%lf", (book+1)->title, (book+1)->author,&(book+1)->price);  scanf("%s%s%lf",(book+2)->title, (book+2)->author, &(book+2)->price);  scanf("%s%s%lf",(book+3)->title, (book+3)->author, &(book+3)->price);  } void init() {  struct Library book[3]; } void show(struct Library *(book+1), struct Library *(book+2), struct Library *(book+3)) {  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     (book+1)->title, (book+1)->author, (book+1)->price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     (book+2)->title,(book+2)->author, (book+2)->price);  printf("The title is: %s\n The author is : %s\n The price is : %.1lf",     (book+3)->title, (book+3)->author, (book+3)->price); } int main(){  input(&(book+1),&(book+2),&(book+3));  init();  show(&(book+1),&(book+2),&(book+3));  return EXIT_SUCCESS; } 第四題: 設計一個表示汽車信息的結構。 int main(){ struct car {  char name[20];  char sex[5];  char buyDate[20]; } owner = { "Jone", "M", "2008-01-01" }; struct company {  char name[20];  char tel[10]; } leaseCompany = { "hualong", "010-88064420" }; union data {  struct car owner;  struct company leaseCompany; }; struct carData {  char make[20];  int status;  union data {   struct car owner;   struct company leaseCompany;  } ownerInfo; } ; struct carData flits = { .status = 0, .make = "volvo", .ownerInfo.ownerCar.sex =   'M', .ownerInfo.ownerCar.buyDate = '2008-11-21',   .ownerInfo.ownerCar.name = 'Rebort Carter' }; return 0; } 第五題: Wiliam Wingate從事比薩分析服務。對於每個比薩餅,他都需要記錄下列信息: 1.比薩餅公司的名稱。可以有多個單詞組成。 2.比薩餅的直徑。 3.比薩餅的重量。 請設計一個能夠存儲這些信息的結構,並編寫一個使用這種結構變量的程序。 程序將請求用戶輸入上述信息,然後顯示這些信息。 /*int main(){  struct pisa{   char name[20];   int zhijing;   int zhongliang;  }a={"Wiliam Wingate",6,2};  printf("比薩餅公司的名稱:%s\n比薩餅的直徑:%d\n比薩餅的重量:%d \n",a.name,a.zhijing,a.zhongliang);  return 0; }*/ 第六題: 要求設計一個能夠保存學生信息的結構。學生信息包括:姓名(Name)、年級(Grade)和成績(score) ,並按照下面要求完成對於學生信息的操作。 /*struct stu {  char Name[100];  char Grade[20];  int score;  } stu1 = { "姜楠", "二年級", 78};  int main(void) {  struct stu stu2 = { "何北", "二年級", 85 };  struct stu stu3 = { .Name = "董璐", .Grade = "二年級",  .score = 99 };  printf("The Name is :%s\nThe Grade is :%s\nThe Score is  :%d\n",  stu1.Name, stu1.Grade, stu1.score);  printf("\n");  printf("The Name is :%s\nThe Grade is :%s\nThe Score is  :%d\n",   stu2.Name, stu2.Grade, stu2.score);  printf("\n");  printf("The Name is :%s\nThe Grade is :%s\nThe Score is  :%d\n",   stu3.Name, stu3.Grade, stu3.score);  printf("\n");  return EXIT_SUCCESS;  }*/ 第七題: 為上面關於學生信息的程序添加三個函數: 1.編寫顯示學生信息的函數showInfo(),參數為結構的指針。顯示學生信息的結構如下: The Name is:Donglu The Grade is:Two The Score is:99   #include <stdio.h> #include <stdlib.h> struct Student {  char Name[20];  int Grade[4];  int score; }; void showInfo(struct Student *stu3) {  printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu3->Name,    stu3->Grade,stu3->score); } int main() {  struct Student stu3 = { 'Donglu', 'Two', 99 };  showInfo(&stu3);  return EXIT_SUCCESS; }  *  */ /*  * 2.編寫初始化結構變量的函數init(),參數為結構的指針。函數功能是將結構變量中的成員進行初始 化。 #include <stdio.h> #include <stdlib.h> struct Student {  char Name[20];  int Grade[4];  int score; }; void init(struct Student *stu1,struct Student *stu2,struct Student *stu3){  stu1->Name;  stu1->Grade;  stu1->score;  stu2->Name;  stu2->Grade;  stu2->score;  stu3->Name;  stu3->Grade;  stu3->score; }  */ /*3.編寫從鍵盤上接收學生信息的函數input(),參數也是結構的指針。函數的功能是從鍵盤上接收相關 學生信息,並把信息保存到指針所指向的結構變量裡。   #include <stdio.h> #include <stdlib.h> struct Student {  char Name[20];  int Grade[4];  int score; }; void input(struct Student *stu1,struct Student *stu2,struct Student *stu3){  scanf("%s%d%d",&stu1->Name,&stu1->Grade,&stu1->score);  scanf("%s%d%d",&stu2->Name,&stu2->Grade,&stu2->score);  scanf("%s%d%d",&stu3->Name,&stu3->Grade,&stu3->score); } */ /*4.主函數按照下面流程完成功能實現: a)定義三個學生對象stu1,stu2,stu3. b)對結構對象進行初始化. c)從鍵盤上接收學生信息,分別保存到三個學生對象中。 d)輸出三個學生對象的信息。 #include <stdio.h> #include <stdlib.h> struct Student {  char Name[20];  int Grade[4];  int score; };   void input(struct Student *stu1,struct Student *stu2,struct Student *stu3){  scanf("%s%d%d",&stu1->Name,&stu1->Grade,&stu1->score);  scanf("%s%d%d",&stu2->Name,&stu2->Grade,&stu2->score);  scanf("%s%d%d",&stu3->Name,&stu3->Grade,&stu3->score); } int main(){  struct Student stu1;  struct Student stu2;  struct Student stu3;  input(&stu1,&stu2,&stu3);  printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu1->Name,stu1- >Grade,stu1->score);  printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu2->Name,stu2- >Grade,stu2->score);  printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu3->Name,stu3- >Grade,stu3->score); } */ 第八題: 用一個數組存放圖書信息,每本圖書包含書名(booktitle)、作者(author)、出版年月(date)、出版 社(publishunit)、借出數目(lendnum)、庫存數目(stocknum)等信息。編寫程序輸入若干本圖書的 信息,按出版年月排序後輸出。 #include <stdio.h> #include <stdlib.h> struct Data {  int year;  int month;  int day; }; struct library {  char booktitle[50];  char author[10];  struct Data data;  char publishunit[100];  int lendnum;  int stocknum; }; int main() {  int i, j, n, temp = 0;  struct library book[n];  printf("請輸入要處理的圖書數量:\n");  fflush(stdout);  scanf("%d", &n);  for (i = 0; i < n; i++) {   printf("請輸入第%d本書的信息:\n", i + 1);   printf("書名:");   fflush(stdout);   scanf("%s", &book[i].booktitle);   printf("作者:");   scanf("%s", &book[i].author);   printf("出版年月:");   scanf("%s", &book[i].data);   printf("出版社:");   scanf("%s", &book[i].publishunit);   printf("借出數:");   scanf("%s", &book[i].lendnum);   printf("庫存數:");   scanf("%s", &book[i].stocknum);  }  for (i = 0; i < n - 1; i++) {   for (j = i + 1; j < n; j++) {    if (book[i].publishunit < book[j].publishunit) {     temp = book[i];     book[i] = book[j];     book[j] = temp;    }   }   printf("\n排序後的圖書信息:");   for (i = 0; i < n; i++) {    printf(      "\n書名:  %s\n, 作者:  %s\n, 出版年月:  %s\n, 出版 社:  %s\n, 借出數:  %s\n, 庫存數:%s\n",      book[i].booktitle, book[i].author, book[i].data,      book[i].publishunit, book[i].lendnum, book [i].stocknum);   }  }  return EXIT_SUCCESS; } 第九題: 編寫程序,用union實現兩個數的加、減、乘、除運算,每種運算用函數完成,並請考慮多個數的運算如何 實現。 union yunsuan{  int a;  int b; }f; int add(int a,int b) {  int sum =0;  f.a = a;  sum+= f.a;  f.b = b;  sum+=f.b;  printf("%d\n",sum);  return sum; } int jian(int a,int b){  int sum =0;  f.a = a;  sum-=f.a;  f.b =b;  sum-=f.b  printf("%d\n",sum);  return sum; }   int cheng(int a,int b){  int sum =0;  f.a = a;  sum*=f.a;  f.b =b;  sum*=f.b  printf("%d\n",sum);  return sum; } int chu(int a,int b){  int sum =0;  f.a = a;  sum/=f.a;  f.b =b;  sum*/=f.b  printf("%d\n",sum);  return sum; }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved