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
[csharp] view plain copy
print?
#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(),引數為結構的指標。函式功能是將結構變數中的成員進行初始化。
[csharp] view plain copy
print?
#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,將輸入的圖書資訊顯示出來。
print?
#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;
}
第四題:
設計一個表示汽車資訊的結構。
print?
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.比薩餅的重量。
請設計一個能夠儲存這些資訊的結構,並編寫一個使用這種結構變數的程式。
程式將請求使用者輸入上述資訊,然後顯示這些資訊。
[html] view plain copy
print?
/*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),並按照下面要求完成對於學生資訊的操作。
print?
/*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
[html] view plain copy
print?
#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(),引數為結構的指標。函式功能是將結構變數中的成員進行初始化。
[csharp] view plain copy
print?
#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(),引數也是結構的指標。函式的功能是從鍵盤上接收相關學生資訊,並把資訊儲存到指標所指向的結構變數裡。
[csharp] view plain copy
print?
#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;
};
truct library {
char booktitle[50];
char author[10];
struct Data data;
char publishunit[100];
int lendnum;
int stocknum;
};
nt 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;
}
相關推薦
C語言——結構體與聯合體
第一題: 要求你設計一個能夠儲存圖書資訊的結構。圖書屬性包括:書名(title)、作者(author)和單價資訊(price),並按照下面要求完成對於各種圖書的相關操作。 /* struct books { char t
C語言結構體與聯合體
聯合體 C語言的聯合體union又叫做共用體,並不常用,類似於大家所熟知的結構體struct。如其名,我們差不多能獲知這個資料結構有一部分東西是共用的,結構裡面除了變數空間,也沒什麼可以共用的了。即,共用體內部所有宣告的變數,均為共用一個記憶體首址
C語言 結構體與結構體指標用法總結
在C語言開發中,結構體用到的機會很多。所謂結構體,就是定義一種裡面包含多種元素的變數。 我們來看一個簡單的例子。比如你想定義一個書名列表然後為每本書建立書名和作者作為書的資訊。結構體變數定義如下: struct book { char name[30]; ch
C語言——結構體與指標引用&結構體陣列與指標引用
接下來,程式的控制權交給Mutiline函式,進入函式Mutiline。在程式進入函式Mutiline之後,pPoints 指向oPoints的首地址,跟蹤程式表明此時變數pPoints的當前值為0X0F82。另外,由於陣列第一個元素oPoints[0]的地址與陣列的首地址相同。亦可以理解為此時pPoints
C語言----結構體---結構體與函數
urn 全局變量 月的天數 [] strong ret 例子 c語言 += 結構作為參數的函數 整個結構可以作為參數傳入函數 這時是在函數中新建了一個結構變量,並復制調用這個結構的值(重點,只是把值傳入函數,而函數外面真正的變量並沒有改變,與數組不同) 函數也可以返
第22節 C語言結構體之結構體巢狀、結構體指標與結構體陣列的程式碼實現
結構體 #include <stdio.h> //第一步 struct Student { //學號 int no; //姓名 char name[20]; //性別 char
C語言結構體中的函式指標與函式
1、函式指標 一般的函式指標可以這麼定義: int(*func)(int,int); 表示一個指向含有兩個int引數並且返回值是int形式的任何一個函式指標. 假如存在這樣的一個函式: int add2(int x,int y) { return x+y;
C++ 與 C 語言 結構體初始化
C++中的struct可以看作class,結構體也可以擁有建構函式,所以我們可以通過結構體的建構函式來初始化結構體物件。(在C++中,結構體與類在使用上已沒有本質上的區別了,所以
C語言結構體對齊與不對齊設定總結
相信不同的編譯平臺間的預設設定差異給大家帶來了很多困擾。在此,僅就結構體對齊解析下之間的差異設定方法。 1.gcc中結構體預設是4個位元組對齊,即為32的倍數。 1.1修改位元組對齊: struct data{ int a; char b; char c; }__
程式設計基石與實踐系列之失落的C語言結構體封裝藝術
誰該閱讀這篇文章本文是關於削減C語言程式記憶體佔用空間的一項技術——為了減小記憶體大小而手工重新封裝C結構體宣告。你需要C語言的基本知識來讀懂本文。如果你要為記憶體有限制的嵌入式系統、或者作業系統核心寫程式碼,那麼你需要懂這項技術。如果你在處理極大的應用程式資料集,以至於你的
Linux C語言結構體2-遞迴與地推
遞迴原理 函式呼叫 int functionB(int a,int b){ return (a+b); } int functionA(){ ... functionB(10,10); ... return 0; } 遞迴函式 int func(int
在C語言結構體中添加成員函數
我們 pau 打印 log print class 控制 stdio.h 語言 我們在使用C語言的結構體時,經常都是只定義幾個成員變量,而學過面向對象的人應該知道,我們定義類時,不只是定義了成員變量,還定義了成員方法,而類的結構和結構體非常的相似,所以,為什麽不想想如何
c語言結構體大小 sizeof(struct A)
成員 print ble ret 意義 cnblogs () pan color 1,數據類型自身對齊 數據類型的起始地址為其大小的整數倍 2,結構體的自身對齊 結構體的自身對齊值為其中最大的成員大小 3,指定對齊 可以使用關鍵詞#pragma pack(1) 來指定結構體
C語言結構體數組內帶字符數組初始化和賦值
指定 char 字符數 全局 種類 def 變量 指針 變量定義 1.首先定義結構體數組: typedef struct BleAndTspRmtCmd{ char terminal[3]; char note[3]; char rmtCmd[10]; char cmdP
C語言結構體作業
數列 class 系統 過程 函數功能 文字 inf not 類型 一、PTA實驗作業 題目1:6-3 結構體數組中查找指定編號人員 1. 本題PTA提交列表 2. 設計思路 定義一個結構體指針*p for i=0 to i=7 如果std+i的編號與輸入的編號
C語言結構體所占內存大小
oat 接下來 面試題 語言 將不 位置 idt str 返回 用一道面試題題引入struct str1 { char a; int b; float c; double d; };char類型占用
C語言結構體在內存中的存儲情況探究------內存對齊
.com ret size \n str 技術 urn 存儲 typedef 條件(先看一下各個基本類型都占幾個字節): void size_(){ printf("char類型:%d\n", sizeof(char)); printf("in
c語言結構體定義的幾種形式
sdn 方法 tps statistic htm def courier ng- target 轉自https://blog.csdn.net/ziguo2010/article/details/79897327 1、最常用定義方式:定義結構體data,
C語言結構體字節對齊簡單計算方法
最大的 一個數 數據類型 double short 位操作 結構體字節對齊 根據 struct 1.在C語言裏面每一種數據類型都有字節對齊比如在32位操作系統下:整型的自身對齊數就是 4 字節,字符型就是 1 字節,double就是 8 字節。 但是結構體的計算方式就和普通
C語言結構體訓練
結構體大小和記憶體結構 1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<string.h> 4 #include<stdlib.h> 5 6 //結構體需要根據資料型別進