- 論壇徽章:
- 0
|
編譯時卡在gets處,提示參數(shù)過多,如何修改通過編譯,請各位高人幫幫看看。。謝謝了。- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #define STMAX 100 /*最大學生數(shù)*/
- struct student
- {
- char id[10];
- char name[20];
- char sex[10];
- float sorc[3];
- float aver;
- float sum;
- };
- FILE * fp=NULL;
- char filename[]="c:\\學生信息表.dat"; /*錄入的學生信息所存儲的文件*/
- /***************************函數(shù)聲明**********************/
- void showmenu();
- void inputinfo(struct student*);
- void aboutinfo(struct student*);
- void allinfo(struct student*);
- void delinfo(struct student*);
- int existRec(struct student*);
- /**********主函數(shù)***********/
- int main(int argc, char *argv[])
- {
- struct student studentInfo[STMAX];
- struct student * p_student;
- int choice;
- p_student=studentInfo;
- /*********************顯示菜單************************/
- showmenu(); /*顯示系統(tǒng)菜單*/
- printf("\t\t 請選擇操作:");
- /*********************輸入選項********************/
- scanf("%d",&choice);
- /***************如果輸入錯誤是否退出*************/
- while(choice!=5);
- {
- /********調用相應的功能*******/
- switch(choice)
- {
- case 1:inputinfo(p_student);break;
- case 2:aboutinfo(p_student);break;
- case 3:allinfo(p_student);break;
- case 4:delinfo(p_student);break;
- case 5:break;
- default:printf("\n\t\t\ 請重新選擇(1~5):");
- scanf("%d",&choice);
- // continue;
- }
- showmenu();
- printf("\t\t 請選擇操作:");
- scanf("%d",&choice);
- }
- printf("\n\n\t\t 退出系統(tǒng)");
-
- }
- /**************顯示系統(tǒng)菜單******************/
- void showmenu()
- {
- system("cls"); //清屏
- printf("\n\n\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\t| 學生信息管理系統(tǒng) |\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\t| 1. 錄入信息 |\n");
- printf("\t\t| |\n");
- printf("\t\t| 2. 查詢信息 |\n");
- printf("\t\t| |\n");
- printf("\t\t| 3. 瀏覽信息 |\n");
- printf("\t\t| |\n");
- printf("\t\t| 4. 刪除信息 |\n");
- printf("\t\t| |\n");
- printf("\t\t| 5. 退出系統(tǒng) |\n");
- printf("\t\t------------------------------------------------\n");
- //return();
- }
- /***********計算文件中現(xiàn)有的記錄數(shù)****************/
- int existRec(struct student * p_student)
- {
- int count;
- system("cls");
- /*******打開文件***************/
- /*******是否出錯***************/
- if((fp=fopen(filename,"rb"))==NULL)
- {
- /***************報告錯誤信息****/
- printf("不能打開文件,請確認磁盤未滿或c:\\學生信息表.dat文件沒有損壞");
- exit(0);
- }
- rewind(fp);
- /***********讀入一條記錄*************/
- /***********是否成功*****************/
- for(count=0;fread(&p_student[count],sizeof(struct student),1,fp)==1;count++);
- /***********關閉文件*******************/
- fclose(fp);
- return count;
- }
- /**********************信息錄入*****************/
- void inputinfo(struct student *p_student)
- {
- int count,j;
- char ch;
- float sum;
- /*******************打開文件****************/
- count=existRec(p_student);
- /***************是否出錯****************/
- if((fp=fopen(filename,"a+b"))==NULL)
- /********************報告錯誤***************/
- {
- printf("不能打開文件,請確認文件存在!");
- exit(0);
- }
- /**********************錄入學生信息*******************/
- printf("\n\t 信息登記表\n");
- printf("\n請輸入第%d個學生的信息(請使用英文或拼音)\n",count+1);
- printf("-----------------------------------------");
- printf("\n 學號:");
- fflush(stdin); /*清楚緩存,保證 scanf 輸入正確*/
- /****************學號為0****************************/
- while(count<STMAX&&gets(p_student[count].id)!=NULL&&p_student[count].id[0]!='\0')
- {
- printf("\n學號");
- gets("%s",p_student[count].id);
- printf("\n姓名");
- gets("%s",p_student[count].name); /*
- *用gets時提示參數(shù)太多,只能接受一個輸入參數(shù)
- *如何解決?問題點。
- */
- printf("\n性別");
- gets("%s",p_student[count].sex);
- printf("\n英語成績:");
- gets("%f",&p_student[count].sorc[0]); //gets函數(shù)在接受非字符串時,必須用&符號
- printf("\n高數(shù)成績:");
- gets("%f",&p_student[count].sorc[1]);
- printf("\n計算機成績:");
- gets("%f",&p_student[count].sorc[2]);
- printf("----------------------------------\n");
- /***************************計算成績*********************/
- sum=0;
- for(j=0;j<3;j++)
- sum=sum+p_student[count].sorc[j]; //計算總分 最好寫成 sum+=p_student[count].sorc[j]
- p_student[count].sum=sum;
- p_student[count].aver=sum/3.0; //計算平均分
- /************************下面開始存入數(shù)據文件*************************/
- fwrite(&p_student[count++],sizeof(struct student),1,fp);
- printf("\n是否寫入第%d個學生的信息(y/n)",count+1);
- ch=getch();
- if(ch=='y'||ch=='Y')
- {
- printf("%c\n--------------------------------",ch);
- printf("\n 學號:");
- fflush(stdin);
- continue;
- }
- else
- {
- fclose(fp);
- printf("%c\n\n 信息錄入結束,按任意鍵返回!",ch);
- getch();
- //system("cls");
- return;
- }
- }
- fclose(fp);
- printf("\n輸入有誤,終止信息錄入,按任意鍵返回!");
- getch();
- return;
- }
- /****************************查詢個人信息******************************/
- void aboutinfo(struct student *p_student)
- {
- int i;
- int count;
- char ch;
- char * temp=NULL;
-
- count=existRec(p_student);
- if(!count)
- {
- printf("\n文件中記錄為空!");
- getch();
- return;
- }
- temp=(char*)malloc(10*sizeof(char));
- printf("\n請輸入要查詢的學生學號:");
- fflush(stdin);
- while(gets(temp)!=NULL&&temp[0]!='\0')
- {
- for(i=0;i<count;i++)
- {
- if(!strcmp(temp,p_student[i].id))
- {
- printf("------------------------------\n");
- printf("姓名:%s\n",p_student[i].name);
- printf("\n性別:%s\n",p_student[i].sex);
- printf("\n英語:%.2f\n",p_student[i].sorc[0]);
- printf("\n高數(shù):%.2f\n",p_student[i].sorc[1]);
- printf("\n計算機:%.2f\n",p_student[i].sorc[2]);
- printf("\n總分:%.2f\n",p_student[i].sum);
- printf("\n平均分:%2f\n",p_student[i].aver);
- printf("------------------------------\n");
- break;
- }
- }
- if(i==count)
- printf("\n沒有該學生!\n",ch);
- printf("\n 查詢下一個學生的信息?(y/n)");
- ch=getch();
- if(ch=='y'||ch=='Y')
- {
- printf("%c\n\n 請輸入要查詢的學生學號:",ch);
- fflush(stdin);
- continue;
- }
- else
- {
- free(temp);
- printf("%c\n\n 查詢結束,返回!",ch);
- getch();
- return;
- }
- }
- free(temp);
- printf("\n 輸入有誤,按任意鍵返回菜單!");
- getch();
- return;
- }
- /*************查詢所有信息*****************/
- void allinfo(struct student * p_student)
- {
- int i;
- int count;
- count=existRec(p_student);
- if(!count)
- {
- printf("\n\n 文件記錄為空!");
- getch();
- return;
- }
- printf("\n\t\t\t 學生信息查詢表\n\n");
- printf("\t 以下是本系統(tǒng)所查詢到的所有學生信息,如果表中數(shù)據為空\n");
- printf("\t 請確認數(shù)據是否成功錄入!\n");
- printf("\n----------------------------------------------------");
- for(i=0;i<count;i++)
- {
- printf("\n 您正查看第[%d]個學生的信息\n",i+1);
- printf("\n學 號:%s\n",p_student[i].id);
- printf("\n姓 名:%s\n",p_student[i].name);
- printf("\n性 別:%s\n",p_student[i].sex);
- printf("\n英 語:%.2f\n",p_student[i].sorc[0]);
- printf("\n高 數(shù):%.2f\n",p_student[i].sorc[1]);
- printf("\n計算機:%.2f\n",p_student[i].sorc[2]);
- printf("\n總 分:%.2f\n",p_student[i].sum);
- printf("\n平均分:%2f\n",p_student[i].aver);
- printf("------------------------------\n");
- printf("按任意鍵繼續(xù)瀏覽……!");
- getch();
- }
- printf("\n\n 查詢結束,按任意鍵返回!");
- getch();
- return;
- }
- /****************刪除信息*********************/
- void delinfo(struct student * p_student)
- {
- int i,j;
- int count;
- char ch;
- char* temp=NULL;
- count=existRec(p_student);
- if(!count)
- {
- printf("\n 不能讀取數(shù)據,請確認已經正確錄入或數(shù)據是否存在");
- getch();
- return;
- }
- temp=(char*)malloc(10*sizeof(char));
- printf("\n 請輸入要刪除的學生學號:");
- fflush(stdin);
-
- while(gets(temp)!=NULL&&temp[0]!='\0')
- {
- for(i=0;i<count;i++)
- {
- if(!strcmp(temp,p_student[i].id))
- {
- printf("\n\n 確定要刪除%s的信息嗎? (y/n)",p_student[i].id);
- ch=getch();
- if(ch=='n'||ch=='N')
- {
- free(temp);
- printf("%c\n\n 該操作被取消,請按任意鍵返回!",ch);
- getch();
- return;
- }
- for(j=i;j<count;j++)
- p_student[j]=p_student[j+1]; //從數(shù)組中刪除指定學生
- count--;
- if((fp=fopen(filename,"w+b"))==NULL)
- {
- free(temp);
- printf("\n不能打開文件,請確認磁盤已滿或文件是否存在");
- getch();
- exit(1);
- }
- for(j=0;j<count;j++)
- {
- if(fwrite(&p_student[j],sizeof(struct student),1,fp)!=1)
- break;
- }
- fclose(fp);
- }
- }
- if(i==count)
- printf("\n\n沒有該學生!\n",ch);
- else
- printf("%c\n\n\n 已成功刪除該學生的信息……\n",ch);
- printf("\n刪除下一個學生信息 (y/n)");
- ch=getch();
- if(ch=='y'||ch=='Y')
- {
- printf("%c\n\n 請輸入你要刪除的學生學號:",ch);
- fflush(stdin);
- continue;
- }
- else
- {
- free(temp);
- printf("%c\n\n 刪除結束,按任意鍵返回!",ch);
- getch();
- return;
- }
- }
- free(temp);
- printf("\n 輸入有誤, 請按任意鍵返回菜單,重新選擇!");
- getch();
- return;
- }
復制代碼 |
|