getchar()不停止的原因-------學生成績管理系統源碼

你的輸入流中還有殘留的字符,getchar()()會接受那個字符。你可以在調用getchar()之前用fflush(stdin)刷新一下輸入緩衝區。  
應該有讀入語句吧,沒讀乾淨,輸入流裏還有剩餘的字符,就直接被getchar吞掉了,所以沒有等待輸入。
#include "stdio.h"
#include"string.h"
#include"stdlib.h"
void creat(), add(), modify(), del(), show(), search(), sort(), showmenu();
#define N 100
struct student
{
	char num[6];
	char name[8];
	int score[3];
	double av;
}stu[N];
int main()
{
	int choice;
	showmenu();
	scanf("%d", &choice);
	while (choice != 0)
	{
		switch (choice)
		{
		case 1:creat(); break;
		case 2:add(); break;
		case 3:modify(); break;
		case 4:del(); break;
		case 5:show(); break;
		case 6:search(); break;
		case 7:sort(); break;
		}
		showmenu();
		scanf("%d", &choice);
	}
	return 0;
}
void showmenu()
{
	system("cls"); /*清屏*/
	printf("\n        學生成績管理系統         \n");
	printf("\n=================================\n");
	printf("        1. 錄入學生數據        \n");
	printf("        2. 追加學生數據        \n");
	printf("        3. 修改學生數據        \n");
	printf("        4. 刪除學生數據        \n");
	printf("        5. 顯示學生數據        \n");
	printf("        6. 查找學生數據        \n");
	printf("        7. 排序學生數據        \n");
	printf("        0. 退出系統            \n");
	printf("===============================\n");
	printf("\n請輸入您的選擇(0-7):");
}
void save(int n)     /*將學生記錄保存到二進制格式文件stud中*/
{
	FILE *fp;
	int i;
	fp = fopen("stud", "wb");
	for (i = 0; i<n; i++)
		fwrite(&stu[i], sizeof(struct student), 1, fp);
	fclose(fp);
}
/*******************************統計函數*****************************/
int count()   /*統計數據文件"stud"中記錄個數*/
{
	FILE *fp;
	int i;
	fp = fopen("stud", "rb");
	for (i = 0; !feof(fp); i++)
		fread(&stu[i], sizeof(struct student), 1, fp);
	fclose(fp);
	return(i - 1);
}
void  creat()  /*錄入學生原始數據並寫入磁盤文件*/
{
	int  i, j, n;
	system("cls");   /*清屏*/
	printf("請輸入首次輸入記錄的數量:");
	scanf("%d", &n);
	for (i = 0; i<n; i++)
	{
		printf("\n請輸入第%d個學生數據:\n", i + 1);
		printf("學號: ");
		scanf("%s", stu[i].num);
		printf("姓名: ");
		scanf("%s", stu[i].name);
		for (j = 0; j<3; j++)
		{
			printf("成績%d: ", j + 1);
			scanf("%d", &stu[i].score[j]);
		}
		stu[i].av = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
	}
	save(n);      /*調用保存函數將錄入的記錄存入數據文件中*/
}
void  add()  /*追加數據並寫入磁盤文件*/
{
	int  i, j, n, k, sum = 0;
	system("cls");
	n = count();  /*調用統計函數統計數據文件中的記錄數*/
	printf("請輸入追加記錄的個數:");
	scanf("%d", &k);
	for (i = 1; i <= k; i++)
	{
		printf("\n請輸入需追加的第%d個學生數據:\n", i);
		printf("學號: ");
		scanf("%s", stu[n - 1 + i].num);
		printf("姓名: ");
		scanf("%s", stu[n - 1 + i].name);
		sum = 0;
		for (j = 0; j<3; j++)
		{
			printf("成績%d: ", j + 1);
			scanf("%d", &stu[n - 1 + i].score[j]);
			sum += stu[n - 1 + i].score[j];
		}
		stu[n - 1 + i].av = sum / 3.0;
	}
	save(n + k);   /*調用保存函數把新追加的數據存入數據文件中*/
}
void  modify()  /*根據學號修改學生數據*/
{
	int  i, j, n, k = -1;
	struct student s;
	system("cls");
	n = count();
	printf("請輸入要修改數據的學號:");
	scanf("%s", s.num);
	for (i = 0; i<n; i++)
		if (strcmp(s.num, stu[i].num) == 0)
		{
			k = i; break;
		}
	if (k == -1) printf("該學號不存在\n");
	else
	{
		printf("學號\t姓名\t成績1\t成績2\t成績3\t平均成績\n");
		printf("%s\t%s\t", stu[k].num, stu[k].name);
		for (j = 0; j<3; j++)
			printf("%d\t", stu[k].score[j]);
		printf("%.2lf\n", stu[k].av);
		printf("\n");
		printf("\n請重新輸入該學生數據:\n");
		printf("學號: ");
		scanf("%s", stu[k].num);
		printf("姓名: ");
		scanf("%s", stu[k].name);
		for (j = 0; j<3; j++)
		{
			printf("成績%d: ", j + 1);
			scanf("%d", &stu[k].score[j]);
		}
		stu[k].av = (stu[k].score[0] + stu[k].score[1] + stu[k].score[2]) / 3.0;
	}
	save(n);  /*調用保存函數保存修改後的數據*/
}
void  del()  /*根據學號刪除學生的記錄*/
{
	int  i, j, k, n;
	struct student s;
	system("cls");
	n = count();
	printf("\n\n-----------------學生成績表------------------\n\n");
	printf("學號\t姓名\t成績1\t成績2\t成績3\t平均成績\n");
	for (i = 0; i<n; i++)
	{
		printf("%s\t%s\t", stu[i].num, stu[i].name);
		for (j = 0; j<3; j++)
			printf("%d\t", stu[i].score[j]);
		printf("%.2lf", stu[i].av);
		printf("\n");
	}
	printf("請輸入要刪除的學生的學號:");
	scanf("%s", s.num);
	for (i = 0; i<n; i++)
		if (strcmp(s.num, stu[i].num) == 0)
		{
			k = i; break;
		}
	if (k == -1) printf("數據不存在\n");
	else
	{
		printf("學號\t姓名\t成績1\t成績2\t成績3\t平均成績\n");
		printf("%s\t%s\t", stu[k].num, stu[k].name);
		for (j = 0; j<3; j++)
			printf("%d\t", stu[k].score[j]);
		printf("%.2lf\n", stu[k].av);
		printf("\n");
		for (i = k; i<n - 1; i++)
			stu[i] = stu[i + 1];
		n = n - 1;
		save(n);  /*調用保存函數保存刪除後的數據文件*/
	}
}
void  show() /*顯示所有學生數據*/
{
	int  i, j, n;
	system("cls");
	n = count();
	printf("\n\n-----------------學生成績表------------------\n\n");
	printf("學號\t姓名\t成績1\t成績2\t成績3\t平均成績\n");
	for (i = 0; i<n; i++)
	{
		printf("%s\t%s\t", stu[i].num, stu[i].name);
		for (j = 0; j<3; j++)
			printf("%d\t", stu[i].score[j]);
		printf("%.2lf", stu[i].av);
		printf("\n");
	}
	fflush(stdin);
	getchar();   /*按任意鍵繼續*/
}
void  search()  /*按學號查找學生數據並顯示*/
{
	int  i, j, n, k = -1;
	struct student s;
	system("cls");
	n = count();
	printf("請輸入要查找的學生的學號:");
	scanf("%s", &s.num);
	for (i = 0; i<n; i++)
		if (strcmp(s.num, stu[i].num) == 0)
		{
			k = i; break;
		}
	if (k == -1) printf("數據不存在\n");
	else
	{
		printf("學號\t姓名\t成績1\t成績2\t成績3\t平均成績\n");
		printf("%s\t%s\t", stu[k].num, stu[k].name);
		for (j = 0; j<3; j++)
			printf("%d\t", stu[k].score[j]);
		printf("%.2lf\n", stu[k].av);
		printf("\n");
	}
	getchar();
}
void  sort()  /*按個人平均成績降序排序*/
{
	int  i, j, n;
	struct student  temp;
	system("cls");
	n = count(); /*調用統計函數統計數據文件中的記錄數*/
	for (i = 0; i<n - 1; i++)   /*排序*/
		for (j = i + 1; j<n; j++)
			if (stu[i].av<stu[j].av)
			{
				temp = stu[i];
				stu[i] = stu[j];
				stu[j] = temp; printf("學號\t姓名\t成績1\t成績2\t成績3\t平均成績\n");
				for (i = 0; i<n; i++)
				{
					printf("%s\t%s\t", stu[i].num, stu[i].name);
					for (j = 0; j<3; j++)
						printf("%d\t", stu[i].score[j]);
					printf("%.2lf\n", stu[i].av);
				}
				printf("\n\n");
				getchar();
			}

}
//printf("-------------學生成績表(排序後)--------------\n");

支付寶賬號:[email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章