廣東工業大學 C語言課程設計報告

Mrchesian發表於2017-02-11


一、 設計任務目的與要求

1、  進一步掌握和利用C語言進行程設計的能力;
2、  進一步理解和運用結構化程設計的思想和方法;
3、  初步掌握開發一個小型實用系統的基本方法;
4、  學會除錯一個較長程式的基本方法;
5、  學會利用流程圖或N-S圖表示演算法;
6、  掌握書寫程設計開發文件的能力(書寫課程設計報告);


二、 設計內容  

1、 總體設計

l 說明包含幾大功能模組

l 畫出系統功能模組結構圖和系統流程圖

l 資料結構設計及用法說明

 

2、 詳細設計

l 函式名字:

l 函式功能:

l 資料結構設計描述,引數說明

實現過程:寫演算法或解決思路,貼(NS圖或流程圖)


學生獎學金管理系統流程圖如下

 

使用到的結構體變數

 

struct Student

{

char Sid[10];

char Sclass[20];

char Sname[20];

int Math;

int English;

int Physic;

int Sum;

int rank;

struct Student *next;

};


簡介:以上的結構體為學生獎學金管理系統的核心部分,是貫穿整個系統的靈魂。儲存和讀取都是以結構體的形式進行的,每個結構體包含著豐富的資訊。


專案原始碼:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# define SIZE sizeof(struct Student)

struct Student
{	
	char Sid[10];
	char Sclass[20];
	char Sname[20];
	int Math;
	int English;
	int Physic;
	int Sum;
	int rank;
	struct Student *next;
}; 

void ShowMenu()
{
	printf("\n***********************************************************\n");
	printf("\twelcome to Stugent bonus management system\n\n");
	printf("\t1-------------- Record\n");                    
	printf("\t2-------------- Show all record\n");                  
	printf("\t3-------------- Find\n");
	printf("\t4-------------- Statistics\n");
	printf("\t5-------------- Delete\n");
	printf("\t6-------------- Add\n");
	printf("\t7-------------- Sort\n");
	printf("\t8-------------- Save File\n");
	printf("\t9-------------- Copy File\n");
	printf("\t10------------- Exit\n\n");                   
	printf("***********************************************************\n");
}

//從鍵盤中輸入的資料建立連結串列
struct Student * in_stuNode()
{
	struct Student * head, * p1, * p2;
	int n = 0;
	p1 = p2 = (struct Student *)malloc(SIZE);

	head = NULL;
	
	scanf("%s",  p1->Sid);
	while(p1->Sid[0] != '@')
	{
		scanf("%s%s%d%d%d", p1->Sclass, p1->Sname, &p1->Math, &p1->English, &p1->Physic);
		p1->Sum = 0; p1->rank = 0;
		n++;
		if(n == 1)
		{
			head = p1;
		}else
		{
			p2->next = p1;
		}
		p2 = p1;

		p1 = (struct Student *)malloc(SIZE);
		scanf("%s",  p1->Sid);
	}
	p2->next = NULL;        
	return head;
}

//從連結串列中的資料 寫入檔案
void Record(struct Student *head)
{
	FILE *fp;
	struct Student  *p1;
	p1 = head;

	if((fp = fopen("D:\\Course\\student.txt", "w")) == NULL)
	{
		printf("cannot open this file\n");
		return;
	}

	while(p1 != NULL)
	{
		fprintf(fp, "%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);
/*		if(fwrite(p1, SIZE, 1, fp) != 1)
			{
				printf("file write error\n");
			}
*/
		p1 = p1->next; 

	}

	if(p1 == NULL){
	printf("Record ok!\n");
	}

	free(p1);
	free(head);
	fclose(fp);

}

//從檔案中讀取的資料建立連結串列
struct Student * File_stuNode(){

	FILE *fp;
	int n = 0;
	struct Student *head, *p2, *p1;

	if((fp = fopen("D:\\Course\\student.txt", "r")) == NULL)
	{
		printf("cannot open this file\n");
		return NULL;
	}

	head = NULL;

	p2 = p1 = (struct Student *)malloc(SIZE);
	fscanf(fp, "%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d", p1->Sid, p1->Sclass, p1->Sname, &p1->Math, &p1->English, &p1->Physic, &p1->Sum, &p1->rank);
//	fread(p1, SIZE, 1, fp);
	while(!feof(fp))
	{
		
		n++;
		if(n == 1)
		{
			head = p2;
		}else
		{
			p2->next = p1; 
		}
		p2 = p1;
		p1 = (struct Student *)malloc(SIZE);
	    fscanf(fp, "%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d", p1->Sid, p1->Sclass, p1->Sname, &p1->Math, &p1->English, &p1->Physic, &p1->Sum, &p1->rank);
//		fread(p1, SIZE, 1, fp);
	}

	p2->next = NULL;
	fclose(fp);

	return head;
}

void Show_stuNode()
{
	printf("---------------------------STUDENT------------------------------------\n");
	printf("|no		|class	|name	|sc1	|sc2	|sc3    |sum	|order|\n");
	printf("----------------------------------------------------------------------\n");
	struct Student *p, *head;
	head = File_stuNode();     ////呼叫 File_stuNode()

	p = head;
	while(p != NULL)
	{
		printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p->Sid, p->Sclass, p->Sname, p->Math, p->English, p->Physic, p->Sum, p->rank);
		
		p = p->next; 
	}

}

void ShowMenu3()
{
	printf("\t****************************************\n");

	printf("\t1----------按名次查詢某學生的個人成績\n");

	printf("\t2----------按班級查詢班級前3名學生的個人成績\n");

	printf("\t3----------按成績段查詢某學生的個人成績\n");

	printf("\t4----------exit\n");

	printf("\t****************************************\n");

}

void SortRank()
{
	struct Student *p1, *head, *p2;
	int i;
	head = File_stuNode();

	p1 = head;

	while(p1 != NULL)
	{
		i = 1;
		p2 = head;
		while(p2 != NULL)
		{
			if(p1->Sum < p2->Sum)
			{
				i++;
			}
			p2 = p2->next;
		}
		p1->rank = i;
		p1 = p1->next;

	}

	Record(head);
}

void SearchByRank()
{
	struct Student *p1, *head;
	int number;
	head = File_stuNode();
	p1 = head;

	if(p1->rank == 0)
	{
		printf("你還沒有排名呢?請返回主選單執行操作7\n");
		return;
	}
	printf("請輸入要查詢的名次:");

	scanf("%d", &number);

	while(p1 != NULL)
	{
		if(p1->rank == number)
		{
			printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);
		}
		p1 = p1->next;
	}

	free(p1);
	free(head);

}

void SearchByClass()
{

	int i = 1;
	char Class[10] = {0};
	struct Student *p1, *head, *p2;

	printf("請輸入要查詢的班級:如201501 \n");
	scanf("%s", Class);

	head = File_stuNode();
	p1 = head;

	while(p1 != NULL)
	{
		if(strcmp(Class, p1->Sclass) == 0)
		{
			i = 1;
			p2 = head;
			while(p2 != NULL)
			{
				if(strcmp(Class, p2->Sclass) == 0)
				{
					if(p1->Sum < p2->Sum)
						{
							i++;
						}
				}
				p2 = p2->next;
			}

			if(i <= 3)
			{
				printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);	
			}
			else if(i > 3)
			{
				break;
			}
		}
		p1 = p1->next;
	}

	free(p1);
	free(head);

}

void SearchByScore()
{
	int scope1, scope2;
	int i = 0;
	struct Student *p1, *head;
	char course[10] = {0};

	printf("請輸入要查詢的分數段:如 70 - 80 \n");
	scanf("%d-%d", &scope1, &scope2);
	if(scope1 >= scope2)
	{
		printf("scope2 must be bigger than scope1!\n");
		return;
	}

	printf("請輸入要查詢的科目:(Math、English、Physic) \n");

	scanf("%s", course); 
	if(!(strcmp(course, "Math") == 0 || strcmp(course, "English") == 0 || strcmp(course, "Physic") == 0))
	{
		printf("no found the course\n");
		return;
	}

	head = File_stuNode();

	p1 = head;

	while(p1 != NULL)
	{
		if(strcmp(course, "Math")==0)
		{
			if(p1->Math >= scope1 && p1->Math <= scope2)
			{
				printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);		
				i++;
			}
		}else if(strcmp(course, "English")==0)
		{
			if(p1->English >= scope1 && p1->English <= scope2)
			{
				printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);		
				i++;
			}
		}else if(strcmp(course, "Physic")==0)
		{
			if(p1->Physic >= scope1 && p1->Physic <= scope2)
			{
				printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);		
				i++;
			}
		}
		p1 = p1->next;
	}
		if(i == 0)
		{
			printf("no the student adopt to adition\n");
		}

}

void Search()
{
	int op;
	do
	{
		ShowMenu3();
		printf("\tplease choose the option:: ");
		scanf("%d", &op);

		switch(op)
		{
			case 1: SearchByRank(); break;

			case 2: SearchByClass(); break;

			case 3: SearchByScore(); break;

			case 4: break;

			default : printf("input error!\n");
		} 
	}while(op != 4);

}
void ShowMenu4()
{
	printf("\t****************************************\n");

	printf("\t1-------------- 總計\n");

	printf("\t2-------------- 分班統計\n");

	printf("\t3----------exit\n");

	printf("\t****************************************\n");

}
void StatByStu()
{	
	struct Student *head, *p1;

	head = File_stuNode();

	p1 = head;
	while(p1 != NULL)
	{
		p1->Sum = p1->Math + p1->English + p1->Physic;

		p1 = p1->next;
	}

	Record(head);
	
}

void StatByClass()
{
	char Class[10][10] = {0};
	int ClassSum[10] = {0};
	FILE *fp;
	struct Student *head, *p1;
	int i = 0;  //  記錄class總數
	int j;
	int tag = 1;

	head = File_stuNode();
	p1 = head;

	while(p1 != NULL)
	{
		tag = 1;
		for(j = 0; j < i; j++)
		{
			if(strcmp(Class[j] , p1->Sclass) == 0)
			{
				tag = 0; break;
			}
		}
		if(tag == 1)
		{
			strcpy(Class[i++], p1->Sclass);	
		}
		p1 = p1->next;
	}

	p1 = head;
	while(p1 != NULL)
	{
		for(j = 0; j < i; j++)
		{
			if(strcmp(p1->Sclass, Class[j]) == 0)
			{
				ClassSum[j] += p1->Sum;
			}
		}
		p1 = p1->next;
	}



	if((fp = fopen("D:\\Course\\student1.txt", "w+")) == NULL)
	{
		printf("cannot open this file\n");
		return;
	}
	
	for(j = 0; j < i; j++)
	{
		printf("%s %d\n", Class[j], ClassSum[j]);
	}
}
void Statistics()
{
	int op;
	do
	{
		ShowMenu4();
		printf("\tplease choose the option:: ");
		scanf("%d", &op);

		switch(op)
		{
			case 1: StatByStu(); break;

			case 2: StatByClass(); break;

			case 3: break;

			default : printf("input error!\n");
		} 
	}while(op != 3);

}
struct Student * RankSort(struct Student * head)
{
	int i;
	struct Student *p1, *p2;

	p1 = head;
	while(p1 != NULL)
	{
		i = 1;
		p2 = head;
		while(p2 != NULL)
		{
			if(p1->Sum < p2->Sum)
			{
				i++;
			}
			p2 = p2->next;
		}
		p1->rank = i;
		p1 = p1->next;

	}

	return head;
}

/* 
	對連結串列 按 rank 實現從小到大排序, 並返回連結串列的頭指標。
*/
struct Student * SortByRank(struct Student *head)
{
	struct Student *p1, *p2, *p3;

	struct Student t;

    for(p1 = head; p1 != NULL; p1 = p1->next)
    {
		p3 = p1;
		for(p2 = p1->next; p2 != NULL; p2 = p2->next)
		{
			if(p3->Sum < p2->Sum)
			{
				p3 = p2;   

			}
		}
		if(p3 != p1)
		{

			t.English = p1->English;
			t.Math = p1->Math;
			t.Physic = p1->Physic;
			t.Sum = p1->Sum;
			strcpy(t.Sclass, p1->Sclass);
			strcpy(t.Sid, p1->Sid);
			strcpy(t.Sname, p1->Sname);

			p1->English = p3->English;
			p1->Math = p3->Math;
			p1->Physic = p3->Physic;
			p1->Sum = p3->Sum;
			strcpy(p1->Sclass, p3->Sclass);
			strcpy(p1->Sid, p3->Sid);
			strcpy(p1->Sname, p3->Sname);

			p3->English = t.English;
			p3->Math = t.Math;
			p3->Physic = t.Physic;
			p3->Sum = t.Sum;
			strcpy(p3->Sclass, t.Sclass);
			strcpy(p3->Sid, t.Sid);
			strcpy(p3->Sname, t.Sname);
	
		}
    }
	RankSort(head);   
    return head;
}


/* 
	對連結串列 按 Sid 實現從小到大排序, 並返回連結串列的頭指標。
*/
struct Student * SortBySid(struct Student *head)
{
	struct Student *p1, *p2, *p3;

	struct Student t;

    for(p1 = head; p1 != NULL; p1 = p1->next)
    {
		p3 = p1;
		for(p2 = p1->next; p2 != NULL; p2 = p2->next)
		{
			if(strcmp(p3->Sid, p2->Sid) > 0)
			{
				p3 = p2;
			}
		}
		if(p3 != p1)
		{
			t.English = p1->English;
			t.Math = p1->Math;
			t.Physic = p1->Physic;
			strcpy(t.Sclass, p1->Sclass);
			strcpy(t.Sid, p1->Sid);
			strcpy(t.Sname, p1->Sname);

			p1->English = p3->English;
			p1->Math = p3->Math;
			p1->Physic = p3->Physic;
			strcpy(p1->Sclass, p3->Sclass);
			strcpy(p1->Sid, p3->Sid);
			strcpy(p1->Sname, p3->Sname);

			p3->English = t.English;
			p3->Math = t.Math;
			p3->Physic = t.Physic;
			strcpy(p3->Sclass, t.Sclass);
			strcpy(p3->Sid, t.Sid);
			strcpy(p3->Sname, t.Sname);
	
		}
    }

	// 統計總分;
	p1 = head;
	while(p1 != NULL)
	{
		p1->Sum = p1->Math + p1->English + p1->Physic;

		p1 = p1->next;
	}
	
	head = RankSort(head);   //  排名;
    return head;
}

void ShowSortBy(int i)
{
	struct Student *head, *p1;
	SortRank(); 
	head = File_stuNode();

	p1 = head;

	if(i == 1)
	{
		p1 = SortByRank(p1);	
	}
	else if(i == 2)
	{
		p1 = SortBySid(p1);
	}
	while(p1 != NULL)
	{

		printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);		
		p1 = p1->next;
	}
}

void ShowSort()
{
	int op;
	do
	{
		printf("\t****************************************\n");

		printf("\t1----------sort by the rank for all student\n");

		printf("\t2----------sort by the Sid for all student\n");

		printf("\t3----------Exit!\n");

		printf("\t****************************************\n");

		printf("\tplease choose the option:: ");
		scanf("%d", &op);

		switch(op)
		{
			case 1: ShowSortBy(op); break;

			case 2: ShowSortBy(op); break;

			case 3: break;

			default : printf("input error!\n");
		} 
	}while(op != 3);

}
void Delete()
{
	struct Student *p1, *head, *p2;
	char stuid[10] = {0};
	int tag = 0;

	head = File_stuNode();

	printf("please input the deleted stuSid:\n");
	scanf("%s", stuid);

	p1 = head;
	p2 = NULL;
	while(p1 != NULL)
	{
		if(strcmp(p1->Sid, stuid) == 0)
		{
			if(p2 == NULL)
			{
				head = p1->next; // 刪除是 head 結點
				tag = 1;
				break;
			}else{

				if(p1->next == NULL)
				{
					p2->next = NULL; // 刪除是 尾節點 
				    tag = 1;
					break;
				}
				p2->next = p1->next;
				tag = 1;
				break;
			}
		}
		p2 = p1;
		p1 = p1->next;
	}
	if(tag == 0)
	{
		printf("no found the student!\n");
	}else{
		Record(head);
	}

}

void  Add()
{
	struct Student * head1, *head2, *p1, *head;

	head1 = File_stuNode();
	head2 = in_stuNode();

	p1 = head1;
	if(p1 != NULL)
	{
		while(p1->next != NULL)
		{
			p1 = p1->next;
		}
		head = head1;
		p1->next = head2;
		
	}
	else
	{
		head = head2;	
	}
	if(head == NULL){
		printf("Y");}
	Record(head);

}

void CopyFile(){
	FILE *fp, *fp1;
	char filename[30] = {0};
	char filename1[30] = {0};
	struct  Student *p1, stu;

	printf("please input the fileName\n");
	scanf("%s", filename);

	getchar();
	printf("please input the fileName path\n");
	scanf("%s", filename1);
	getchar();

	if((fp = fopen(filename, "r")) == NULL)
	{
		printf("open file error");
		return;
	}

	if((fp1 = fopen(filename1, "w")) == NULL)
	{
		printf("open file error");
		return;
	}

	p1 = &stu;

	while(!feof(fp))
	{
		fscanf(fp, "%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d", p1->Sid, p1->Sclass, p1->Sname, &p1->Math, &p1->English, &p1->Physic, &p1->Sum, &p1->rank);
		fprintf(fp1, "%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);
	}

	fclose(fp);
	fclose(fp1);
}

void SaveFile() 
{
	FILE *fp;
	char filename[30] = {0};
	struct  Student *head, *p1;

	scanf("%s", filename);
	getchar();
	if((fp = fopen(filename, "w")) == NULL)
	{
		printf("open file error");
		return;
	}

	head = File_stuNode();
	
	p1 = head;
	while(p1 != NULL)
	{
		fprintf(fp, "%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p1->Sid, p1->Sclass, p1->Sname, p1->Math, p1->English, p1->Physic, p1->Sum, p1->rank);

		p1 = p1->next;
	}

	free(head);
	free(p1);
	fclose(fp);
}

int main()
{
	int option;
	
	do
	{
		ShowMenu();
		printf("\tplease choose the option: ");
		scanf("%d", &option);
		getchar();

		switch(option)
		{
			case 1: Record(in_stuNode()); break;

			case 2: Show_stuNode(); break;

			case 3: Search(); break;

			case 4: Statistics(); break;

			case 5: Delete(); break;

			case 6: Add(); break;

			case 7: ShowSort(); break;

			case 8: SaveFile();break;

			case 9: CopyFile();break;

			case 10: break;

			default : printf("input error!");

		}
	}while(option != 10);

	return 0;
}



相關文章