C語言之結構和其他資料形式
阿新 • • 發佈:2018-11-21
1.結構變數
C語言中,提供了**結構變數(structure variable)**用於提高我們表示資料的能力,如果我們要列印一本書的圖書目錄,其中包含書名作者等等資訊,這樣我們需要這種資料形式既能包含字串,又能包含數字,而且各資訊獨立,此時我們就可以使用結構變數來儲存資料。
1.1 結構變數宣告
**結構宣告(structure declaration)**描述了一個結構的組織布局,如下所示:
struct book { //結構模板:標記是book
char title[MAXTITL];
char author[MAXAUTL];
float value;
}; //結構模板結束
該宣告描述了一個由兩個字元陣列和一個float型別變數組成的結構。
struct book library; //把library宣告為一個使用book結構佈局的結構變數
1.2 初始化結構
初始化一個結構變數與初始化陣列的語法類似:
struct book library = {
"The Pious Pirate and the Devious Damsel",
"Renee Vivotee",
1.95
};
1.3 訪問結構成員
使用結構成員運算子-點(.)訪問結構中的成員。如
library.value //訪問library的value部分
本質上,.title、.author、.value的作用相當於book的下表。
1.4 例項
採用結構變數儲存一本書的目錄,並顯示:
//一本圖書的目錄
#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL 41 //書名的最大長度+1
#define MAXAUTL 31 //作者姓名的最大長度+1
struct book { //結構模板:標記是book
char title[MAXTITL];
char author[MAXAUTL] ;
float value;
}; //結構模板結束
int main(void)
{
struct book library; //把library宣告為一個book型別的變數
printf("please enter the book title.\n");
s_gets(library.title, MAXTITL);
printf("enter the author.\n");
s_gets(library.author, MAXAUTL);
printf("now enter the value.\n");
scanf("%f", &library.value);
printf("%s by %s : $%.2f\n", library.title, library.author, library.value);
printf("Done.\n");
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); //查詢換行符
if (find) //如果地址不是NULL
*find = '\0'; //在此處放置一個空字元
else
while (getchar() != '\n')
continue; //處理輸入行中剩餘的字串
}
return ret_val;
}
執行結果:
2. 結構陣列
當上個例子要處理多本書時,顯然此時每本書都可用一個book型別的結構變數來表示,當使用多個結構陣列來處理多本書是,就可以使用如下程式所示的陣列。
//包含多本書的圖書目錄
#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL 40 //書名的最大長度+1
#define MAXAUTL 40 //作者姓名的最大長度+1
#define MAXBKS 100 //書籍的最大數量
struct book { //結構魔板:標記是book
char title[MAXTITL];
char author[MAXAUTL];
float value;
}; //結構模板結束
int main(void)
{
struct book library[MAXBKS]; //book型別的資料結構 宣告結構陣列
int count = 0;
int index;
printf("Please enter the book title.\n");
while(count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')
{
printf("enter the author.\n");
s_gets(library[count].author, MAXAUTL); //獲取輸入的作者名,並存入結構陣列
printf("now enter the value.\n");
scanf("%f", &library[count++].value);
while(getchar() != '\n')
continue; //清理輸入行
if (count < MAXBKS)
printf("enter the next title.\n");
}
if (count > 0)
{
printf("here is the list of your books:\n");
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
}
else
printf("No books?Too bad.\n");
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); //查詢換行符
if (find) //如果地址不是NULL
*find = '\0'; //在此處放置一個空字元
else
while (getchar() != '\n')
continue; //處理輸入行中剩餘的字串
}
return ret_val;
}
執行結果:
2.1 宣告結構陣列
宣告結構陣列和宣告其他型別的陣列類似,如下
struct book library[MAXBKS]; //library為一個內含MAXBKS個元素的陣列
陣列中的每個元素都是一個book型別的陣列。
2.2 標識結構陣列的成員
2.3 巢狀結構
在一個結構中包含另一個結構即巢狀結構。如,我們想建立一個有關自己朋友資訊的結構,顯然,結構中需要一個成員表示朋友的名字,此時,名字可以用陣列表示,包含名和姓兩個成員。
例子程式:
//巢狀結構示例
#include <stdio.h>
#define LEN 20
const char * msgs[5] =
{
"thank your for the wonderful evening,",
"You certainly prove that a",
"is a special kind of guy. We must get together",
"over a delicious",
" and have a few laughs"
};
struct names{ //第一個結構
char first[LEN];
char last[LEN];
};
struct guy{ //第2個結構
struct names handle; //巢狀結構
char favfood[LEN];
char job[LEN];
float income;
};
int main(void)
{
struct guy fellow = { //初始化一個結構變數
{ "Ewen", "Villard"},
"grilled salmon",
"personality coach",
68112.00
};
printf("Dear %s, \n\n", fellow.handle.first);
printf("%s%s.\n", msgs[0], fellow.handle.first);
printf("%s%s\n", msgs[1], fellow.job);
printf("%s\n", msgs[2]);
printf("%s%s%s", msgs[3], fellow.favfood, msgs[4]);
if (fellow.income > 150000.0)
puts("!!");
else if (fellow.income > 75000.0)
puts("!");
else
puts(".");
printf("\n%40s%s\n", " ", "See you soon,");
printf("%40s%s\n", " ", "Shalala");
return 0;
}
執行結果: