c语言 structure,C语言Structure的功能.doc

C语言Structure的功能

第十章 C語言Structure的功能

假設我們有一組學生的資料,包含學生的學號、姓名和體重,我們要如何表示這種資料呢?對很多電腦語言而言,我們必須要有三個陣列。這三個陣列分別表示學生的學號,姓名和體重。舉例而言,假如我們有五位學生,他們的資料如表10-1:

學號姓名體重109John79201Mary60.3159Peter81.4163Kelly76.6200Gloria62.3表10-1

我們就需要三個陣列,如表10-2所示:

學號陣列姓名陣列體重陣列109John79201Mary60.3159Peter81.4163Kelly76.6200Gloria62.3表10-2

麻煩的是:這三個陣列是互有關聯的。如果我們要將學生的資料按照學號的大小排列,學號陣列當然會改變,但是我們必須跟著同時改變姓名、陣列和體重陣列。改過以後的三個陣列如表10-3。

學號陣列姓名陣列體重陣列109John79159Peter81.4163Kelly76.6200Gloria62.3201Mary60.3表10-3

所以,我們只好承認這是一件很複雜的事情。可是,在C語言中,我們有一個簡單的辦法,我們可以利用一種叫做structure的功能,一下子就解決了這個問題。

Structure使我們可以宣告學生的資料有三個欄位:學號、姓名、體重。學號和體重都用整數來代表,姓名用文字來表示,所以我們可以作以下的宣告。

struct student {

int idnum;

char name[20];

weight;

}

從以上的宣告看來,學生的姓名最長不能超過20個英文字。

一旦對下定義,我們就宣告一個陣列有的結構,這個陣列當然也要有一個名字,我們不妨將它叫做,內容有如表10-1所示。假設我們要找第個的資料,我們只要找即可。如果我們要找第個學生的學號,我們就要指定,他的姓名是,而他的體重則是。有了以後,我們可以根據其中任何一個欄位排列。假設我們用學號排列,就可以得到以下的,如表10-4

學號姓名體重109John79159Peter81.4163Kelly76.6200Gloria62.3201Mary60.3表10-4

如果用體重來排列,我們會得到如表10-5所示的資料。

學號姓名體重201Mary60200Gloria 62.3163Kelly76.6109John79.1159Peter81.4表10-5

至於如何會有資料的?當然是靠讀入的,我們通常應該宣告一個文字檔,用讀檔案的方法可以將資料讀到去。

例題10-1 讀入學生資料檔,根據學號將學生資料排列,並將結果

要使用structure來表示學生資料,我們必需在主程式的外面先註明,所以我們會在主程式的前面,有以下的指令:

struct studentdata {

int idnum;

char name[20];

weight;

}

主程式的流程圖如圖10-1。

圖10-1

至於排序的副程式,它的流程圖如圖10-2所示。

圖10-2

當然,我們還要有一個結果的副程式,這個副程式非常簡單,我們就不再討論它的流程圖了。

以下是這個程式。

程式 10-1

#include

#define MAX_ARRAY_SIZE 256

#define STUDENT_DATA_FILE_NAME "student.txt"

#define OUTPUT_FILE_NAME "student_output.txt"

struct student

{

int idnum;

char name[20];

float weight;

};

int read_all_student_data(struct student A[MAX_ARRAY_SIZE], FILE *fp);

void output_all_student_data(FILE* output_data_fp, struct student A[MAX_ARRAY_SIZE], int N);

void sort_student_data(struct student A[MAX_ARRAY_SIZE], int N);

int FMIN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This second edition of Data Structures Using C has been developed to provide a comprehensive and consistent coverage of both the abstract concepts of data structures as well as the implementation of these concepts using C language. It begins with a thorough overview of the concepts of C programming followed by introduction of different data structures and methods to analyse the complexity of different algorithms. It then connects these concepts and applies them to the study of various data structures such as arrays, strings, linked lists, stacks, queues, trees, heaps, and graphs. The book utilizes a systematic approach wherein the design of each of the data structures is followed by algorithms of different operations that can be performed on them, and the analysis of these algorithms in terms of their running times. Each chapter includes a variety of end-chapter exercises in the form of MCQs with answers, review questions, and programming exercises to help readers test their knowledge. Table of Contents Chapter 1. Introduction to C Chapter 2. Introduction to Data Structures and Algorithms Chapter 3. Arrays Chapter 4. Strings Chapter 5. Structures and Unions Chapter 6. Linked Lists Chapter 7. Stacks Chapter 8. Queues Chapter 9. Trees Chapter 10. Efficient Binary Trees Chapter 11. Multi-way Search Trees Chapter 12. Heaps Chapter 13. Graphs Chapter 14. Searching and Sorting Chapter 15. Hashing and Collision Chapter 16. Files and Their Organization Appendix A: Memory Allocation in C Programs Appendix B: Garbage Collection Appendix C: Backtracking Appendix D: Josephus Problem Appendix E: File Handling in C Appendix F: Address Calculation Sort Appendix G: Answers

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值