结构体
简单例子
要求:
结构体保存学生信息操作
代码
#include <stdio.h>//定义结构体
struct student{int ID;char name[20];char sex;char birthday[8];int grade;
};int main(){int number;printf("请输入学生个数:");scanf("%d", &number);//声明结构体变量struct student stu[number];//输入数据for (int i=0; i<number; ++i){printf("请输入(每个数据用空格隔开):");scanf("%d %s %s %s %d", &stu[i].ID, &stu[i].name, &stu[i].sex, &stu[i].birthday, &stu[i].grade);}printf("--------------------------------\n");for (int i=0; i<number; ++i){printf("学号:%d\t姓名:%s\t性别:%c\t 出生日期:%s\t成绩:%d\n",stu[i].ID, stu[i].name, stu[i].sex, stu[i].birthday, stu[i].grade);}return 0;
}
运行效果
课堂作业
要求:
对结构体操作:输入数据,查找数据,插入数据,删除数据,合并数据比进行排列输出
代码
#include <stdio.h>
#define MAX_SIZE 100typedef struct {int data[MAX_SIZE];int length;
} SeqList;//初始化操作
void init(SeqList *list) {list->length = 0;
}//插入操作
void insert(SeqList *list, int pos, int value) {if (pos < 1 || pos > list->length + 1) {printf("插入位置错误\n");return;}if (list->length >= MAX_SIZE) {printf("顺序表已满,无法插入\n");return;}for (int i = list->length; i >= pos; i--) {list->data[i] = list->data[i - 1];}list->data[pos - 1] = value;list->length++;
}//查找操作
int find(SeqList *list, int value) {for (int i = 0; i < list->length; i++) {if (list->data[i] == value) {return i;}}return -1;
}//展示操作
void display(SeqList *list) {for (int i = 0; i < list->length; i++) {printf("%d ", list->data[i]);}printf("\n");
}//删除操作
void del(SeqList *list, int del_value){if (list->length==0){printf("线性表为空!\n");return;}int temp=0;for(int i=0; i<list->length; i++){if(list->data[i] != del_value){list->data[temp] = list->data[i];temp++;}}list->length = temp;}//合并操作,降序排列
void merge_sort(SeqList *L_A, SeqList *L_B, SeqList *L_C){int k=0;for (int i = 0; i < L_A->length; i++) {L_C->data[k] = L_A->data[i];k++;L_C->length++;}for (int j = 0; j < L_B->length; j++) {L_C->data[k] = L_B->data[j];k++;L_C->length++;}//冒泡排序for(int x=0; x <= L_C->length -1; x++){for(int y=0; y <= L_C->length-x; y++){if(L_C->data[y] < L_C->data[y+1]){int temp = L_C->data[y];L_C->data[y] = L_C->data[y+1];L_C->data[y+1] = temp;}}}
}//升序排列
void merge_ascending(SeqList *L_C, SeqList *L_D){int i=0;for(int k=L_C->length-1; k>=0; k--){L_D->data[k] = L_C->data[i];L_D->length++;i++;}
}int main() {//声明结构体变量SeqList L_A;SeqList L_B;SeqList L_C;SeqList L_D;//初始化init(&L_A);init(&L_B);init(&L_C);init(&L_D);// 用户输入数据printf("请输入数据(每输入一个数据后回车,输入777可结束):\n");for (int i = 0; i < MAX_SIZE; i++) {int data_value;scanf("%d", &data_value);if(data_value == 777){break;}L_A.data[i] = data_value;L_A.length++;}// 默认查找数据 4,并输出数据4的位置int index = find(&L_A, 4);if (index != -1) {printf("找到数据 4,位置为:%d\n", index + 1);} else {printf("未找到数据 4\n");}// 在第四个位置上插入数据 100int in_value;printf("请输入要插入的数字:\n");scanf("%d", &in_value);insert(&L_A, index+1, in_value);// 展示数据表结果display(&L_A);// 删除操作printf("请输入删除的数字:\n");int del_value;scanf("%d",&del_value);del(&L_A, del_value);// 展示数据表结果display(&L_A);// 输入第二个数据表并进行合并操作printf("请输入第二个数据表的数据,(每输入一个数据后回车,输入777可结束)\n");for (int i = 0; i < MAX_SIZE; i++) {int data_value;scanf("%d", &data_value);if(data_value == 777){break;}L_B.data[i] = data_value;L_B.length++;}printf("第二个数据表的数据为:\n");display(&L_B);printf("\n");//降序排列printf("降序排列为:\n");merge_sort(&L_A, &L_B, &L_C);//展示数据表结果display(&L_C);printf("\n");//升序排列printf("升序排列为:\n");merge_ascending(&L_C, &L_D);// 展示数据表结果display(&L_D);return 0;
}