线性表的基本操作及其作用
来源:互联网 发布:tomcat linux 启动命令 编辑:程序博客网 时间:2024/05/22 05:32
线性表的基本操作及其作用
顺序表
要求:
typedef struct{ char stuID[ID_SIZE]; //学生学号 char stuName[NAME_SIZE]; //学生姓名 double stuScore; //学生成绩 } StuData;
完整代码:
/* * 顺序表 * 一个简陋的学生信息管理程序 * Data: 10/13/2017 20:42 */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <stddef.h>#include <errno.h>#include <inttypes.h>#define STU_NUM_MAX 100#define ID_SIZE 8#define NAME_SIZE 20typedef struct{ char stuID[ID_SIZE]; //学生学号 char stuName[NAME_SIZE]; //学生姓名 double stuScore; //学生成绩 } StuData;typedef StuData* stuPtr;typedef struct{ StuData *elem; //指向动态分配的内存的首地址 int length; //保存已存储的数据据元素的数目 } SqList;void welcome(int *p_choose);/* * 功能:输出欢迎界面,并提示用户执行相应的操作 * 参数:指向choose的指针,通过指针改变变量choose的值,根据其值执行相应的操作 * 返回值:无 */void InitList(SqList *p_seq);/* * 功能:一次性分配所有的储存空间,初始化 * 参数:SqList的指针 * 返回值:无 */void add(SqList *p_seq);/* * 功能: * * */stuPtr info_input(stuPtr info);/* * 功能:对数组赋值,其长度不超过len * 参数:stuID: 指向数组的指针 size_t * 返回值:传入的指针 */void NodeDelete(SqList *p_seq, int locate);/* * 功能:删除指定序号的数据元素 * 参数:p_seq: SqList的指针 locate: 序号(第几个) * 返回值:无 */StuData *search(stuPtr p, size_t len, char *target);/* * 功能:根据指定的字符串遍历查找是否存在相应的ID or Name * 参数:p: 指向第一个顺序元素 len: 已存储的数据元素的长度 target: 需要查找的字符 * 返回值:指向查找到的节点,不存在则返回NULL */void print(StuData *elem, size_t len);/* * 功能:打印一定长度的数据元素 * 参数:elem: 指向某个数据元素 len: 需要打印多少个数据元素(长度) * 返回值:无 */void save(FILE *stream, stuPtr p, size_t len);/* * 功能:将输入的信息保存到文件中 * 参数:stream: 指定的文件输入流 p: 指向第一个数据元素 len: 数据元素的长度 * 返回值:无 */int main(void){ int choose; char ans = 'y'; SqList L; InitList(&L); system("color 2F"); while (1) { fflush(stdin); ans = 'y'; welcome(&choose); switch (choose) { case 1: { while (ans == 'y') { if (L.length >= STU_NUM_MAX) { printf("\a\n\tWarning: Memory is full!\n"); break; } else { //info_input(&info); add(&L); printf("\n\nAdd succeefully! stu's num %u\n", L.length); printf("Continue?[y]\n"); fflush(stdin); ans = getchar( ); if (ans == '\n') { ans = 'y'; } system("cls"); } } break; } case 2: { int locate; while (ans == 'y') { printf("Please enter the node number you want to delete: "); scanf("%d", &locate); NodeDelete(&L, locate); printf("\a\n\n\t\tDelete Successfully\n"); printf("Continue?[y]"); fflush(stdin); ans = getchar( ); if (ans == '\n') { ans = 'y'; } system("cls"); } break; } case 3: { StuData *locate; char target[NAME_SIZE]; while (ans == 'y') { printf("Please enter the ID/Name of the student you want to find: "); scanf("%s", target); locate = search(L.elem, L.length, target); if (locate == NULL) { printf("\a\n\t\tSorry! There is no such person!\n"); } else { printf("\aFind successfully!\n"); print(locate, 1); } printf("Continu?[y] "); fflush(stdin); ans = getchar( ); if (ans == '\n') { ans = 'y'; } system("cls"); } break; } case 4: { printf("All of the stu's info are:\n\n"); print(L.elem, L.length); getchar( ); getchar( ); system("cls"); break; } case 5: { FILE *stream; if ((stream = fopen("info.dat", "w+")) == NULL) { perror("\a\n\n\t\tSorry: Open fail!\n"); break; } else { save(stream, L.elem, L.length); getchar( ); sleep(3); fclose(stream); system("cls"); break; } } case 6: { free(L.elem); L.elem = NULL; printf("\a\n\n\t\tBye Bye!\n\n"); sleep(2); system("cls"); system("color 0F"); exit(0); } default : { printf("\a\n\tSorry! I have not develop the function what you want!\n"); sleep(2); system("cls"); break; } } } return 0;}void welcome(int *p_choose){ printf("\n\n\n WELCOME\n"); printf("------------------------------------------------------\n"); printf("-- 1.增加指定学生信息\n"); printf("-- 2.删除指定位置信息\n"); printf("-- 3.按学号或姓名查找\n"); printf("-- 4.显示所有学生信息\n"); printf("-- 5.保存\n"); printf("-- 6.退出\n"); printf("------------------------------------------------------\n"); printf("请输入那想要执行的操作的序号: "); scanf("%d", p_choose); system("cls");}void InitList(SqList *p_seq){ p_seq->elem = (StuData *)malloc(STU_NUM_MAX*sizeof(StuData)); if (p_seq->elem == NULL) { perror("\n\n\t\tError: memory may full"); //perror?????????????? _exit(1); } else { p_seq->length = 0; }}void add(SqList *p_seq){ printf("Please enter information:\n"); while (1) { printf("ID: "); scanf("%s", p_seq->elem[p_seq->length].stuID); if (strlen(p_seq->elem[p_seq->length].stuID) >= ID_SIZE) { printf("It's too long, enter again\n"); sleep(1); system("cls"); } else { break; } } while (1) { printf("Name: "); scanf("%s", p_seq->elem[p_seq->length].stuName); if (strlen(p_seq->elem[p_seq->length].stuName) >= NAME_SIZE) { printf("It's too long, enter again\n"); sleep(1); system("cls"); } else { break; } } while (1) { printf("Score: "); scanf("%lf", &p_seq->elem[p_seq->length].stuScore); if (p_seq->elem[p_seq->length].stuScore <0 || p_seq->elem[p_seq->length].stuScore > 100) { printf("The score is percentage system\n"); sleep(1); system("cls"); } else { break; } } p_seq->length++;}void NodeDelete(SqList *p_seq, int locate){ for (int i=locate; i<=p_seq->length; i++) { memccpy((p_seq->elem[i-1]).stuID, (p_seq->elem[i]).stuID, '\0', ID_SIZE); memccpy((p_seq->elem[i-1]).stuName, (p_seq->elem[i]).stuName, '\0', NAME_SIZE); (p_seq->elem[i-1]).stuScore = (p_seq->elem[i]).stuScore; } p_seq->length--;}stuPtr search(stuPtr p, size_t len, char *target){ for (unsigned i=0; i<len; i++) { if (strcmp(p[i].stuID, target) == 0) { return (p+i); } else if (strcmp(p[i].stuName, target)== 0) { return (p+i); } } return NULL;}void print(StuData *elem, size_t len){ printf(" ID Name Score\n"); printf(" -----------------------------------------------\n"); if (len <= 0) { printf("\a\n\t\tWarning: NO data exist!\n"); } else { unsigned count = 0; for (unsigned i=0; i<len; i++) { printf("%3d-", ++count); printf(" %s\t\t", elem[i].stuID); printf(" %s\t\t", elem[i].stuName); printf(" %.2lf\n", elem[i].stuScore); } printf(" num: %lu\n", len); } printf(" -----------------------------------------------\n");}void save(FILE *stream, stuPtr p, size_t len){ if (len <= 0) { printf("\a\n\n\t\tSorry: No data exist!\n"); sleep(2); return; } else { unsigned count = 0; fprintf(stream, " ID Name Score\n"); fprintf(stream, " -------------------------------------------------\n"); for (unsigned i=0; i<len; i++) { fprintf(stream, "%3d-", ++count); fprintf(stream, " %s\t\t", p[i].stuID); fprintf(stream, " %s\t\t", p[i].stuName); fprintf(stream, " %.2lf\n", p[i].stuScore); } fprintf(stream,"\n -------------------------------------------------\n"); printf("\a\n\n\tSave successfully!\n\n"); }}
阅读全文
0 0
- 线性表的基本操作及其作用
- 线性表的基本操作及其作用
- 线性表的基本操作及其应用
- 线性表的实现及其基本操作
- 线性表的引入及其基本操作
- 线性表的基本操作及其应用
- 线性表及其基本操作
- 线性表的顺序表示及其基本操作
- 实验一线性表的基本操作实现及其应用
- 实验一 线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一 线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 虚拟机Centos系统下hadoop集群中静态IP配置
- Nest 推出新款家用监控摄像头 Cam IQ,售价 299 美元
- 经济学人智库:新范式正在兴起,大部分传统行业愿意拥抱新技术
- 用 AI 帮助企业实现销售流程自动化,People.ai 获 700 万美元 A 轮融资
- ubuntu 16.04 安装配置nvidia1000系列显卡硬件 埋坑经验
- 线性表的基本操作及其作用
- 实验5:树和二叉树的实验1
- elasticsearch源码分析之集群服务(八)
- WWDC 2017前瞻:除了新版OS和MacBook,我们还能期待哪些惊喜?
- 政府单位专享,国内“特供版”Windows 10系统真的来了!
- 结盟Intel施压Nvidia,Google 云计算服务迎来更新
- Spring Struts2 Hibernate xml开发整合
- eclipse 创建 Maven 项目
- linux下如何关闭防火墙