11.26C語言實驗課作業

編寫學生成績管理系統V4.0。
某班有最多不超過30人(具體人數由鍵盤輸入)參加期末考試,考試科目最多不超過6門(具體門數由鍵盤輸入)。參考例題8.12,用二維數組作函數參數編程實現如下學生成績管理:

  1.  錄入每個學生的學號、姓名和各科考試成績,學號用long int[]類型定義。考試成績用int[][M]定義。第一維爲學生數,第二維爲課程數。
    
  2.  計算每門課程的總分和平均分
    
  3.  計算每個學生的總分和平均分
    
  4.  按每個學生的總分由高到低排出名次表
    
  5.  按每個學生的總分由低到高排出成績表
    
  6.  按學號由小到大排出成績表
    
  7.  按姓名的字典順序排出成績表
    
  8.  按學號查詢學生排名及各科考試成績
    
  9.  按姓名查詢學生的排名及各科考試成績
    
  10. 按優秀(90~100)、良好、中等、及格、不及格5個類別,對每門課程分別統計每個類別的人數以及所佔的百分比。
  11. 輸出每個學生的學號、姓名、各科考試成績、總分、平均分,以及每門課程的總分和平均分。
    image
//假定名字輸入時不含空格
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 40
#define C 6
#define SWAP(a,b) xx=a,a=b,b=xx;
struct Node{
	long num;
	char name[N];
	int score[C], sumev;
	double aver;
};
/*
void LSwap(long *, long *);
void ISwap(int *, int *);
void DSwap(double *, double *);
*/
void StrSwap(char *, char *);
int Choose(struct Node ,struct Node, int);
void Average(int , int , struct Node *, int *, double *);
//求學生總分、平均值,各科總分、平均值
void Read (int , int , struct Node*);//輸入數據
void Print(int , int , struct Node*);//輸出
void Sort(int , int , struct Node *, int op);//排序,op==1表示按總分降序,op==2表示按總分升序,op==3表示按學號升序,op==4表示按字典序
void Find(int , int , struct Node *, int id, char *, int op);
//查找,op==1表示按學號查,op==2表示按名字查
void Account(int , int , struct Node *);
//統計分析

int main(void)
{
	int n = 0, c = 0, i, j, in, id, sum[C] = {0};
	char na[N];
	double ave[N];
	struct Node stu[N] = {0};;
	do{
		puts("");
		puts("1.Input score, ID and name");
		puts("2.Caculate total and average score of course");
		puts("3.Caculate total and average score of students");
		puts("4.Sort in descending order by score");
		puts("5.Sort in ascending order by score");
		puts("6.Sort in ascending order by number");
		puts("7.Sort in alphabetical order by name");
		puts("8.Search by number");
		puts("9.Search by name");
		puts("10.Statistic analysis");
		puts("11.List record");
		puts("0.Exit");
		printf("Please enter your choice:");

		scanf("%d", &in);
		switch(in)
		{
			case 0:break;
			case 1:
				printf("Please enter the number of students and the number of scores: ");
				scanf("%d %d", &n, &c);
				puts("Please enter student's ID, name and score");
				puts("Attention: No space in their names");
				Read(n, c, stu);
				Average(n, c, stu, sum, ave);
				break;
			case 2:
				for(j = 0; j < c; ++j)
					printf("%5d %5.1lf\n", sum[j], ave[j]);
				break;
			case 3:
				for(i = 0; i < n; ++i)
					printf("%5d %5.1lf\n", stu[i].sumev, stu[i].aver);
				break;
			case 4:
				Sort(n,c,stu,1);
				Print(n,c,stu);
				break;
			case 5:
				Sort(n,c,stu,2);
				Print(n,c,stu);
				break;
			case 6:
				Sort(n,c,stu,3);
				Print(n,c,stu);
				break;
			case 7:
				Sort(n,c,stu,4);
				Print(n,c,stu);
				break;
			case 8:
				Sort(n,c,stu,1);
				printf("Please input the ID: ");
				scanf("%d", &id);
				Find(n,c,stu,id,"",1);
				break;
			case 9:
				Sort(n,c,stu,1);
				printf("Please input the name: ");
				scanf("%s", na);
				Find(n,c,stu,0,na,2);
				break;
			case 10:
				Account(n,c,stu);
				break;
            case 11:
                Print(n, c, stu);
				for(i = 0; i < 21; ++i)putchar(' ');
				for(j = 0; j < c; ++j)
					printf("%5d", sum[j]);
				puts("");
				for(i = 0; i < 21; ++i)putchar(' ');
				for(j = 0; j < c; ++j)
					printf("%5.1lf ", ave[j]);
                break;
			default:puts("Inputs Error!");break;
		}
	}while(in);
	return 0;
}
/*
void DSwap(double *a, double *b)
{
	double c = *a;
	*a = *b;
	*b = c;
}
void ISwap(int *a, int *b)
{
	int c = *a;
	*a = *b;
	*b = c;
}
void LSwap(long *a, long *b)
{
	long c = *a;
	*a = *b;
	*b = c;
}
*/
void StrSwap(char *a, char *b)
{
	char c[N];
	strcpy(c,a);
	strcpy(a,b);
	strcpy(b,c);
}
void Read(int n, int c, struct Node stu[])
{
	int i, j;
	for(i = 0; i < n; ++i)
	{
		scanf("%ld", &stu[i].num);
		scanf("%s" , stu[i].name);
		for(j = 0; j < c; ++j)
			scanf("%d", &stu[i].score[j]);
	}
	return ;
}
void Print(int n, int c, struct Node stu[])
{
	int i, j;
	for(i = 0; i < n; ++i)
	{
		printf("%-5ld %-15s", stu[i].num, stu[i].name);
		for(j = 0; j < c; ++j)
			printf("%5d ", stu[i].score[j]);
		printf("%5d %5.1lf\n", stu[i].sumev, stu[i].aver);
	}
	return ;
}
void Average(int n, int c, struct Node *stu, int sum[], double ave[])
{
	int i, j;
	for(i = 0; i < n; ++i)
	{
		for(j = 0; j < c; ++j)
		{
			sum[j]		 += stu[i].score[j];
			stu[i].sumev += stu[i].score[j];
		}
		stu[i].aver = 1.0 * stu[i].sumev / c;
	}
	for(j = 0; j < c; ++j)
		ave[j] = 1.0 * sum[j] / n;
	return ;
}
int Choose(struct Node stu1, struct Node stu2, int op)
{
	switch(op)
	{
		case 1:return stu1.sumev < stu2.sumev;
		case 2:return stu1.sumev > stu2.sumev;
		case 3:return stu1.num	 > stu2.num;
		case 4:return strcmp(stu1.name, stu2.name) > 0;
	}
}
void Sort(int n, int c, struct Node *stu, int op)
{
	int i, j, k;
	double xx;
	for(i = 0; i < n; ++i)
	{
		k = i;
		for(j = i+1; j < n; ++j)
			if(Choose(stu[k], stu[j], op))
				k = j;
		if(k != i)
		{
			StrSwap(stu[i].name, stu[k].name);
			SWAP(stu[i].num, stu[k].num);
			SWAP(stu[i].sumev, stu[k].sumev);
			SWAP(stu[i].aver, stu[k].aver);
			for(j = 0; j < c; ++j)
				SWAP(stu[i].score[j], stu[k].score[j]);
		}
	}
	return ;
}
void Find(int n, int c, struct Node *stu, int id, char na[], int op)
{
	int i, j;
	for(i = 0; i < n; ++i)
	{
		if(op == 1 && stu[i].num == id)
			break;
		else
			if(op == 2 && !strcmp(stu[i].name, na))
				break;
	}
	if(i != n)
	{
		printf("Rank: %d\n", i+1);
		printf("%-5ld%-15s", stu[i].num, stu[i].name);
		for(j = 0; j < c; ++j)
			printf("%5d", stu[i].score[j]);
		puts("");
	}
	else	puts("Not find!");
	return ;
}
void Account(int n, int c, struct Node *stu)
{
	char grade[5]={'E','D','C','B','A'};
	int i, j, peo[C][5] = {0};
	for(j = 0; j < c; ++j)
	{
		for(i = 0; i < n; ++i)
		{
			if(stu[i].score[j] == 100)
				++peo[j][4];
			else
				if(stu[i].score[j] >= 60)
					++peo[j][(int)(stu[i].score[j]/10)-5];
				else	++peo[j][0];
		}
		printf("Subject %d\n: ", j+1);
		for(i = 4; i >= 0; --i)
			printf("\tgrade %c: %d\t%lf%%\n", grade[i], peo[j][i], 100.0*peo[j][i]/n);
	}
	return ;
}

data

5 3
101 nieleheng 95 93 94
102 yanglang  85 87 88
103 buzhidao 58 58 58
104 yanzhichong 75 85 85
105 suxuewen 88 86 87
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章