#include<iostream>#include<fstream>#include<string>#include<stdio.h>#include<stdlib.h>#include<conio.h>//#include<iomanip>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2#define LEN(a) sizeof(a)/sizeof(a[0]) typedef int Status; //Status 是函數(shù)返回值類型,其值是函數(shù)結(jié)果狀態(tài)代碼。typedef? int? ElemType; //ElemType 為可定義的數(shù)據(jù)類型,此設(shè)為int類型#define MAXSIZE 100//順序表可能達到的最大長度//學生信息的定義:typedef struct {??? char no[8];?? //8位學號??? char name[80]; //姓名?int age;//年齡?int sex; //性別??? ElemType? price[4];???? //成績}Student;//順序表的定義(第一個程序用)typedef? struct {? Student? *elem;???? //指向數(shù)據(jù)元素的基地址? int? length;?????? //線性表的當前長度?????????????????????????????????????????????????????????? ?}SqList;FILE *out,*in;int? u=0,a=0,b=0,c=0;int i;//打開文件int reload(SqList &L){?if ((out = fopen("E:\\ALL\\cs work\\數(shù)據(jù)結(jié)構(gòu)\\作業(yè)\\student.txt", "r+"))== NULL) ?? printf("文件打開失敗!\n");??rewind(out);??????? for(int j=0;j<L.length;j++) {??fprintf(out,"%s",L.elem[L.length].no); ???? fprintf(out,"%s",L.elem[L.length].name); ??fprintf(out,"%d",L.elem[L.length].age); ???? fprintf(out,"%d",L.elem[L.length].sex);???? ?fprintf(out,"%d",L.elem[L.length].price[0]);???? fprintf(out,"%d",L.elem[L.length].price[1]); ???? fprintf(out,"%d",L.elem[L.length].price[2]); ??? ?fprintf(out,"%d",L.elem[L.length].price[3]); ??}//for???? fclose(out);???return OK;?}//將文件內(nèi)容讀入順序表int ToList(SqList &L){?? char tn[20]="student.txt";?? int i;?if((in=fopen(tn,"r"))==NULL)?{??printf("不能打開文件:%s\n",tn);??exit(ERROR);?}?while(!feof(in))?{??fscanf(in,"%s",L.elem[L.length].no);??fscanf(in,"%s",L.elem[L.length].name);??fscanf(in,"%d",L.elem[L.length].sex);??fscanf(in,"%d",L.elem[L.length].age);??fscanf(in,"%d",L.elem[L.length].price[0]);??fscanf(in,"%d",L.elem[L.length].price[1]);??fscanf(in,"%d",L.elem[L.length].price[2]);??fscanf(in,"%d",L.elem[L.length].price[3]);??L.elem[i]=L.elem[L.length];??L.length++;??i++;?}?fclose(in);?return OK;}//逐個顯示表中學生信息void show(SqList &L){?cout<<"學生信息如下:"<<endl;?for(int i=1;i<=L.length;i++){??cout<<"學號"<<L.elem[i].no<<" "<<"姓名:"<<L.elem[i].name<<" "?????<<"性別:"<<L.elem[i].sex<<" "<<"年齡:"<<L.elem[i].age<<" "?????<<"成績:"<<L.elem[i].price[0]<<L.elem[i].price[1]??????? ?<<L.elem[i].price[2]<<L.elem[i].price[3]<<endl;?}}//將學生信息插入表中指定位置? void ListInsert(SqList &L){??int i;??cout<<"請輸入要插入的位置:"<<endl;??cin>>i;??if(L.length==MAXSIZE)???cout<<"沒有內(nèi)存空間!"<<endl;??else if(i<1||i>L.length+1)???cout<<"輸入位置不正確,請檢查!"<<endl;??else{???for(int j=L.length;j>=i;j--)????L.elem[j+1]=L.elem[j];???cout<<"請分別輸入要插入學生的學號,姓名,性別,年齡,四科成績:"<<endl;???cout<<"學生學號:";????? ?cin>>L.elem[i].no;??????? ?cout<<"學生姓名:";???? ?cin>>L.elem[i].name;???? ?cout<<"學生性別:";???? ?cin>>L.elem[i].sex;????? ?cout<<"學生年齡:";????? cin>>L.elem[i].age;???? ?cout<<"學生成績:";????? cin>>L.elem[i].price[0];????? cin>>L.elem[i].price[1];????? cin>>L.elem[i].price[2];????? cin>>L.elem[i].price[3];????? cout<<endl;???cout<<"插入成功!"<<endl;???cout<<"插入的學生信息為:"<<endl;???cout<<"學號:"<<L.elem[i].no<<endl;???cout<<"姓名:"<<L.elem[i].name<<endl;???cout<<"性別:"<<L.elem[i].sex<<endl;???cout<<"年齡:"<<L.elem[i].age<<endl;???cout<<"成績:"<<L.elem[i].price[0]<<L.elem[i].price[1]????<<L.elem[i].price[2]<<L.elem[i].price[3]<<endl;??}?}??//刪除指定學生的信息Status? ListDelete(SqList &L,int i)?{?FILE *fp;?if(i>=0&&i<=L.length)?{??for(int j=i;j<L.length;j++)??{???//將指定位置之后的元素依次往前移???L.elem[j-1]? =L.elem[j];??}??--L.length;?}?else{??printf("輸入的位置有誤!");?}?fp=fopen("Database.txt","w");?if(fp==NULL)?{??printf("文件無法打開!\n");?exit(ERROR);?}?return OK;?}//統(tǒng)計表中學生個數(shù)int GetLength(SqList L){?return L.length;}//直接排序void InsertSort(SqList &L){?for(int i=2;i<=L.length;i++){??if(strcmp(L.elem[i].name,L.elem[i-1].name)<0)??{???L.elem[0]=L.elem[i];???L.elem[i]=L.elem[i-1];???for(int j=i-2;strcmp(L.elem[0].name,L.elem[j].name)<0;j--)???{????L.elem[j+1]=L.elem[j];????L.elem[j]=L.elem[0];???}??}?}?a=1;}//快速排序int Partition(SqList &L,int low,int high)? { ??? char pivotkey[10];? //設(shè)置關(guān)鍵字pivotkey??? L.elem[0]=L.elem[low];// 把表的第一個記錄作為樞軸??? strcpy(pivotkey,L.elem[low].no);//將學號作為關(guān)鍵字放入pivotkey中保存??? while(low< high)? ??? {? ??????? while((low<high)&&(L.elem[high].no>=pivotkey))? ??????????? --high;? ??????? L.elem[low]=L.elem[high];? ??????? while((low<high)&&(L.elem[low].no<=pivotkey))? ??????????? ++low;? ??????? L.elem[high]=L.elem[low];??? }? ??? L.elem[low]=L.elem[0];??? return low;}? void QSort(SqList &L,int low,int high)? {? ????? ??? if(low<high)? ??? {?? ??????? int pivotloc=Partition(L,low,high); ??????? QSort(L,low,pivotloc-1); //對低子表遞歸排序,pivotloc是樞軸位置??????? QSort(L,pivotloc+1,high); //對高子表遞歸排序? ??? }? }? ? void QuickSort(SqList &L)? { ??? QSort(L,1,L.length);? }? ?? //根據(jù)姓名進行折半查找,使用遞歸,返回學生學號和成績。int Search_Dname(SqList &L,char a[],int low,int high){?low=1;??? high=L.length;?int mid=(low+high)/2;?if(strcmp(a,L.elem[mid].name)==0) return mid;??? if(strcmp(a,L.elem[mid].name)<0)? return Search_Dname(L,a,low,mid-1);?if(strcmp(a,L.elem[mid].name)>0) return Search_Dname(L,a,mid+1,high);??return 0;}//根據(jù)姓名進行折半查找,不使用遞歸,返回學生的學號和成績。int Search_No(SqList &L,char b[]){?int mid;?int low=1;?int high=L.length;??? while(low<=high){??mid=(high+low)/2;???? if(strcmp(b,L.elem[mid].name)==0) return mid;??else if(strcmp(b,L.elem[mid].name)<0) high=mid-1;??else low=mid+1;?}?return 0;}void stop(){?char i;?cout<<"您真的要退出程序嗎 ?"<<endl;?cout<<"1"<<"確定";?cout<<"2"<<"取消";?cin>>i;?switch(i){?case '1':cout<<"謝謝使用!"<<endl;exit(0);?case '2':break;?default:??cout<<"輸入錯誤!"<<endl;??cin>>i;break;?}}//主函數(shù)void main(){?//freopen("student.txt","r","stdin");?//freopen("student2.txt","w",stdout);?//int no;?int n;?SqList L;?while(1){??cout<<"*************歡迎使用學生信息查詢系統(tǒng)******************"<<endl;??cout<<"1.讀入學生信息(學號,姓名,性別,年齡,成績)。"<<endl;??cout<<"2.逐個顯示學生相關(guān)信息。"<<endl;??cout<<"3.插入學生信息到指定位置。"<<endl;??cout<<"4.刪除指定位置學生。"<<endl;??cout<<"5.統(tǒng)計表中學生人數(shù)。"<<endl;??cout<<"6.直接插入排序按學生姓名排序。"<<endl;??cout<<"7.快速排序按學生學號排序。"<<endl;??cout<<"8.根據(jù)姓名進行折半查找。(遞歸)"<<endl;??cout<<"9.跟學號進行折半查找(非遞歸)。"<<endl;??cout<<"0.退出系統(tǒng)。"<<endl;??cout<<endl;//設(shè)置菜單欄????cout<<"請選擇您需要的服務(wù):";??cin>>n;??switch(n)??{??case 1:{???reload(L);???ToList(L);????? }??case 2:if(u){???show(L);???getch();????? }??case 3:if(u){???ListInsert(L);???getch();????? }else?????? cout<<"請先讀入文件!"<<endl;break;??case 4:if(u){???int i;???ListDelete(L,i);???getch();????? }else?????? cout<<"請先讀入文件!"<<endl;break;??case 5:{if(u){???int i=GetLength(L);???cout<<"學生總?cè)藬?shù)為:"<<i<<"個"<<endl;break;}??case 6:if(u){???InsertSort(L);???b=0;???cout<<"直接插入排序成功,按姓名從小到大排序為:"<<endl;???for(int k=1;k<=L.length;k++){????cout<<"學號"<<L.elem[i].no<<" "<<"姓名:"<<L.elem[i].name<<" "?????<<"性別:"<<L.elem[i].sex<<" "<<"年齡:"<<L.elem[i].age<<" "?????<<"成績:"<<L.elem[i].price[0]<<L.elem[i].price[1]??????? ?<<L.elem[i].price[2]<<L.elem[i].price[3]<<endl;????getch();???}????? }???else????cout<<"請先讀入文件!"<<endl;break;????? }??case 7:if(u){???QSort(L,1,L.length);???a=1;???cout<<"快速排序成功,按學號從小到大輸出如下:"<<endl;???for(int k=1;k<=L.length;k++){????cout<<"學號"<<L.elem[i].no<<" "<<"姓名:"<<L.elem[i].name<<" "?????<<"性別:"<<L.elem[i].sex<<" "<<"年齡:"<<L.elem[i].age<<" "?????<<"成績:"<<L.elem[i].price[0]<<L.elem[i].price[1]??????? ?<<L.elem[i].price[2]<<L.elem[i].price[3]<<endl;???}???getch();????? }else?????? cout<<"請先讀入文件!"<<endl;break;??case 8:if(u==0)?????? cout<<"請先讀入文件!"<<endl;?????? else if(u==1&&a==0)?????cout<<"請先使用命令6進行排序。"<<endl;????else{?????char a[20];?????cout<<"請輸入你要查找的學生名字:";?????cin>>a;?????int i;?????i=Search_Dname(L,a,0,L.length);?????if(i>=0)??????cout<<"您查找的學生個人信息為:學號"<<L.elem[i].no<<" "<<"姓名:"<<L.elem[i].name<<" "?????<<"性別:"<<L.elem[i].sex<<" "<<"年齡:"<<L.elem[i].age<<" "?????<<"成績:"<<L.elem[i].price[0]<<L.elem[i].price[1]??????? ?<<L.elem[i].price[2]<<L.elem[i].price[3]<<endl;?????else??????cout<<"學生不存在!"<<endl;?????getch();????}break;??case 9:if(u==0)?????? cout<<"請先讀入文件!"<<endl;?????? else if(u==1&&a==0)?????cout<<"請先使用命令7進行排序。"<<endl;????else{?????char b[10];?????cout<<"請輸入您要查找的學生學號:";?????cin>>b;?????int j=Search_No(L,b);?????if(j>=0)??????cout<<"您查找的學生個人信息為:學號"<<L.elem[i].no<<" "<<"姓名:"<<L.elem[i].name<<" "?????<<"性別:"<<L.elem[i].sex<<" "<<"年齡:"<<L.elem[i].age<<" "?????<<"成績:"<<L.elem[i].price[0]<<L.elem[i].price[1]??????? ?<<L.elem[i].price[2]<<L.elem[i].price[3]<<endl;?????else??????cout<<"學生不存在!"<<endl;?????getch();????}break;??case 0:cout<<endl;???????? stop();break;??default:???cout<<"輸出錯誤!"<<endl;break;????? }???cout<<endl;????? }??}
求大神看看異常怎么解決。
Eminem_Messi
2016-11-13 16:01:09