数据结构实训代码部分

来源:互联网 发布:packet tracer mac 编辑:程序博客网 时间:2024/05/17 22:24
#include<iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <windows.h># define LEN sizeof(struct Student)/*宏定义   LEN替代Student*/using namespace std;struct Student    /*创建结构体*/ {   char num[11];  /*学号*/   char name[11]; /*姓名*/   char sex[11];  /*性别*/   int  age;      /*出生日期*/   char phone[11];/*电话*/   char Email[11];/*电子邮箱*/   struct Student *next;/*指向下一个学生的结构体*/};   char filename[40];//全局变量,可以被本程序所有对象或函数引用struct Student *Creat(int n)   /*创建信息链表*/{void menu_print_in(void);struct Student *head;struct Student *p1, *p2;system("cls");for(int i=1;i<n+1;i++)    {p1 = (struct Student*)malloc(LEN);menu_print_in();cin>>p1->num>>p1->name>>p1->sex>>p1->age>>p1->phone>>p1->Email;p1->next = NULL;if(i==1)        {head = p2 = p1;}else{p2->next = p1;p2 = p1;}}return(head);}                /*数据存储*/void WriteData_wb(struct Student *head)/*wb 只写方式打开或新建一个二进制文件,只允许写数据*/{FILE *fp;struct Student *p;if((fp = fopen(filename, "wb"))==NULL)printf("不能打开此文件!");p = head;while(p!=NULL)    {if(fwrite(p,LEN,1,fp)!=1)        {printf("写入数据出错\n");fclose(fp);return;}p=p->next;}fclose(fp);}void WriteData_ab(struct Student *head)/*读写打开一个二进制文件,允许读或在文件末追加数据*/{FILE *fp;struct Student *p;if((fp = fopen(filename, "ab"))==NULL)cout<<"读取文件失败"<<endl;p = head;while(p!=NULL)    {if(fwrite(p,LEN,1,fp)!=1){cout<<"写入数据错误";fclose(fp);    return;    }p=p->next;}fclose(fp);}/*读取数据*//*读取数据文件保存到链表中 ,返回指向此链表头指针*/struct Student *ReadData(void){struct Student *head = NULL;struct Student *p1, *p2;     //s = p1;p = p2;FILE *fp;if((fp=fopen(filename,"rb+"))==NULL){cout<<"打开文件错误"<<endl;exit(0);}while(!feof(fp))    {if((p1=(struct Student*)malloc(LEN))==NULL){cout<<"内存申请错误"<<endl;fclose(fp);exit(0);}if(fread(p1,LEN,1,fp)!=1){free(p1);break;}if(head==NULL)head=p2=p1;else{p2->next=p1;p2=p1;}}fclose(fp);return (head);}    /*查询功能*/void Print_inquire_all(void){void menu_print_out(void);struct Student *pt;pt = ReadData();menu_print_out();do    {cin>>pt->num>>pt->name>>pt->sex>>pt->age>>pt->phone>>pt->Email;pt = pt->next;}while(pt!=NULL);printf("\n\n");}int Print_inquire_num()   /*学号查询*/{void menu_print_out(void);struct Student *pt;char str_num[10];cout<<"◎请输入您要查询的学号:";cin>>str_num;pt = ReadData();menu_print_out();do    {if(strcmp(pt->num,str_num)==0)/*比较两个字符串的大小,两个字符串相同时返回0*/{   cout<<pt->num<<pt->name<<pt->sex<<pt->age<<pt->phone<<pt->Email;printf("\n\n");return 0;}pt = pt->next;}while(pt!=NULL);printf("数据库中没有存储您要查询的数据!\n");printf("\n\n");return 0;}int Print_inquire_name()   /*姓名查询*/{void menu_print_out(void);struct Student *pt;char str_name[20];printf("◎请输入您要查询的姓名:");scanf("%s", str_name);pt = ReadData();menu_print_out();do    {if(strcmp(pt->name,str_name)==0){printf("%-10s%6s%8s%4d%13s%11s  %4.1f %4.1f %4.1f %4.1f\n",pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->Email);printf("\n\n");return 0;}pt = pt->next;}while(pt!=NULL);printf("数据库中没有存储您要查询的数据!\n");printf("\n\n");return 0;}int Delete()   /*删除信息*/{struct Student *pt1, *pt2, *head;char str_num[20];printf("\n◎请输入您要删除的学号信息:");scanf("%s", str_num);pt1 = ReadData();pt2 = pt1->next;head = pt1;while(pt2!=NULL)    {if(strcmp(pt1->num,str_num)==0){WriteData_wb(pt2);}else if(strcmp(pt2->num,str_num)==0){pt1->next = pt2->next;WriteData_wb(head);}pt2 = pt2->next;pt1 = pt1->next;}if(pt2!=NULL)printf("数据库中没有存储您要删除的数据!\n");printf("\n\n");return 0;}int Amend()      /*修改信息*/{void menu_print_in(void);struct Student *pt1, *pt2, *head;char str_num[20];printf("请输入您要修改的学号信息:");scanf("%s", str_num);pt1 = ReadData();pt2 = pt1->next;head = pt1;while(pt2!=NULL)    {if(strcmp(pt1->num,str_num)==0){menu_print_in();scanf("%s%s%s%d%s%s%f%f",pt1->num,pt1->name,pt1->sex,&pt1->age,pt1->phone,pt1->Email);WriteData_wb(head);}else if(strcmp(pt2->num,str_num)==0){menu_print_in();scanf("%s%s%s%d%s%s%f%f",pt2->num,pt2->name,pt2->sex,&pt2->age,pt2->phone,pt2->Email);WriteData_wb(head);}pt2 = pt2->next;pt1 = pt1->next;}if(pt2!=NULL)printf("数据库中没有存储您要删除的数据!\n");return 0;}int Creat_num(void)   /*输入写入数据个数*/{printf("\n◎请输入您此次要添加的数据个数:");int n;if(scanf("%d", &n)!=1)    {printf("\a error!");}return n;}int File_name()     /*选择要打开的文件*/{printf("\n◎请输入您想要打开的文件:");if(scanf("%s", filename)!=1)printf("\a error!");return 0;}void menu(void)   /*主菜单*/{void menu_add(void);void menu_inquire(void);void menu_amend(void);    cout<<"            \\\ ///                         "<<endl;    cout<<"           \\  -.- //                      "<<endl;    cout<<"            ( .@.@ )                       "<<endl;    cout<<"     +-------oOOo-----(_)-----oOOo---------+       "<<endl;     cout<<"     |                   |       "<<endl;    cout<<"     |         学生信息管理系统                   "<<endl;    cout<<"     |                   |       "<<endl;    cout<<"     +---------------------Oooo------------+       "<<endl;           cout<<"==================================================="<<endl;    cout<<"=                 (1)添加信息                     ="<<endl;                     cout<<"=                 (2)查询数据                     ="<<endl;    cout<<"=                 (3)修改数据                     ="<<endl;    cout<<"=                 (4)退出系统                     ="<<endl;    cout<<"==================================================="<<endl;    printf("◎请输入功能前的序号进入相应的工具:【   】\b\b");int a = 0;a = getchar();while(a!='1'&&a!='2'&&a!='3'&&a!='4')    {cout<<"error! please input the right number!"<<endl;        putchar('\a');getchar();        printf("◎请重新输入功能前的序号进入相应的工具:【   】\b\b");a = getchar();}switch(a){case '1': File_name();menu_add();break;case '2': File_name();menu_inquire();break;case '3': File_name();menu_amend();break;case '4': exit(0);break;}getchar();}      /*添加数据*/void menu_add(void){system("cls");getchar();    cout<<"******************数据添加******************"<<endl;    cout<<"***          (1)新建文件                 ***"<<endl;    cout<<"***          (2)增加数据                 ***"<<endl;    cout<<"***         (3)返回菜单                 ***"<<endl;    cout<<"********************************************"<<endl; printf("◎请输入功能前的序号进入相应的工具:【   】\b\b");int a = 0;a = getchar();while(a!='1'&&a!='2'&&a!='3')    {printf("error! please input the right number!\n");putchar('\a');getchar();printf("◎请重新输入功能前的序号进入相应的工具:【   】\b\b");a = getchar();}switch(a){case '1': WriteData_wb(Creat(Creat_num())); printf("\n◎新建文件成功且数据已成功保存◎\n"); system("pause"); system("cls"); menu_add();break;case '2': WriteData_ab(Creat(Creat_num())); printf("\n◎数据已成功添加◎\n"); system("pause"); system("cls"); menu_add();break;case '3': system("cls"); getchar(); menu();break;}}            /*数据查询*/void menu_inquire(void){system("cls");getchar();while(1)    {system("cls");    cout<<"************数据查询**************"<<endl;    cout<<"**      (1)全量查询               **"<<endl;    cout<<"**      (2)学号查询               **"<<endl;    cout<<"**      (3)姓名查询               **"<<endl;    cout<<"**      (4)返回菜单               **"<<endl;    cout<<"************************************"<<endl;printf("◎请输入功能前的序号进入相应的工具:【   】\b\b");int a = 0;a = getchar();while(a!='1'&&a!='2'&&a!='3'&&a!='3'&&a!='4')        {printf("error! please input the right number!\n");putchar('\a');getchar();printf("◎请重新输入功能前的序号进入相应的工具:【   】\b\b");a = getchar();}switch(a){case '1': Print_inquire_all();system("pause");getchar();break;case '2': Print_inquire_num();system("pause");getchar();break;case '3': Print_inquire_name();system("pause");getchar();break;        case '4': system("cls");getchar();menu();break;}}}          /*修改数据*/void menu_amend(void){system("cls");getchar();while(1)    {system("cls");    cout<<"*****************修改数据*****************"<<endl;    cout<<"**      (1)删除记录                     **"<<endl;    cout<<"**      (2)修改记录                     **"<<endl;    cout<<"**      (3)返回菜单                     **"<<endl;    cout<<"******************************************"<<endl;    printf("\n");printf("◎请输入功能前的序号进入相应的工具:【   】\b\b");int a = 0;a = getchar();while(a!='1'&&a!='2'&&a!='3'&&a!='4'){printf("error! please input the right number!\n");putchar('\a');getchar();printf("◎请重新输入功能前的序号进入相应的工具:【   】\b\b");a = getchar();    }switch(a){case '1': Delete(); printf("\n\n◎已成功删除指定数据◎\n"); system("pause"); getchar();break;case '2': Amend(); printf("\n\n◎已成功修改指定数据◎\n"); system("pause"); getchar();break;    case '3': system("cls"); getchar(); menu();break;}}}void menu_print_in(void){printf("========================================================================\n");printf("学号      姓名     性别    出生日期    电话       Email                \n");printf("========================================================================\n");}void menu_print_out(void){printf("==========================================================================\n");printf("学号      姓名     性别    出生日期   电话              Email            \n");printf("==========================================================================\n");}/*主函数*/int main(void){SetConsoleTitle("C++学生信息管理系统");menu();return 0;}

0 0
原创粉丝点击