1. 程式人生 > >用c++模擬實現一個學生成績管理系統

用c++模擬實現一個學生成績管理系統

題目:用c++模擬實現一個學生成績的資訊管理系統,要求能新增、刪除、修改、檢視和儲存學生的資訊等功能

原始碼如下:

#define  _CRT_SECURE_NO_WARNINGS

#include<iostream>
using namespace std;
#include<string.h>
#include<fstream>

class student
{
private:
	student* next;
public:
	char stu_num[15];                  //學號
	char stu_name[30];                //姓名
	float stu_score;                      //成績

	void afterInsert(student *p);//在該節點後插入一個節點
	void afterDelete();//在該節點後刪除一個節點
	
	student *getNext()//獲得下一個節點的指標
	{ 
		return next; 
	}

	/***********查詢學生資訊************/
	void getMage();

	/******學生資訊修改******/
	void changeMage(int n, char *ptr);
	void changegrade(float p);

	/******構造*****/
	student(char *num, char *name, float score);
	student();
};

void student::changegrade(float p)
{
	stu_score = p;
}

student::student()           //構造
{
	strcpy(stu_num, "\0");
	strcpy(stu_name, "\0");
	stu_score = 0;
	next = '\0';
}

student::student(char *num, char *name, float score)
{
	strcpy(stu_num, num);
	strcpy(stu_name, name);
	stu_score = score;
	next = '\0';
}

void student::afterInsert(student *p)//插入節點
{
	p->next = next;
	next = p;
}

void student::afterDelete()        //刪除節點
{
	student *p = next;
	next = p->next;
	delete p;
}

void student::getMage()             //獲得資訊
{
	cout << "學號:" << stu_num << "      姓名:" << stu_name;
	cout << "      c++成績:" << stu_score << endl;
}

void student::changeMage(int n, char *ptr)
{
	switch (n)
	{
	case 1: strcpy(stu_num, ptr); 
		break;
	case 2: strcpy(stu_name, ptr);
	}
}

//建立連結串列函式
void  construct_list(student *tail)
{
	student *p = new student;
	char very[20];
	float achieve;
	cout << "請輸入學號:" << endl;
	cin >> very;
	p->changeMage(1, very);
	cout << "請輸入姓名:" << endl;
	cin >> very;
	p->changeMage(2, very);
	cout << "請輸入c++成績:" << endl;
	cin >> achieve;
	p->changegrade(achieve);
	system("cls");
	cout << "資訊輸入完畢" << endl;

	for (; tail->getNext() != '\0';)
	{
		tail = tail->getNext();
	}

	tail->afterInsert(p);
}

/*********查詢資訊*********/
student *findmege(student *head)
{
loop:
	cout << "1--按姓名查詢           2--按學號查詢              q--返回上一級選單" << endl;
	char p[5], ptr[20];
	student *mid = head;
	cin >> p;

	if (p[0] != '1'&&p[0] != '2'&&p[0] != 'q' || strlen(p)>1)
	{
		system("cls");
		cout << "對不起,你的輸入有誤,請重新輸入!" << endl;
		goto loop;
	}

	switch (p[0])
	{
	case '1':
	{
		system("cls");
		cout << "請輸入要查詢的姓名:" << endl;
		cin >> ptr;

		for (; strcmp(ptr, mid->stu_name) != 0; mid = mid->getNext())
		{
			if (mid->getNext() == '\0')
			{
				cout << "對不起,你要查詢的人不存在,請確認你的輸入是否正確!" << endl;
				goto loop;
			}
		}
		return mid;
	}
	case '2':
	{
		system("cls");
		cout << "請輸入您要查詢的學號:" << endl;
		cin >> ptr;
		for (; strcmp(ptr, mid->stu_num) != 0; mid = mid->getNext())
		{
			if (mid->getNext() == '\0')
			{
				cout << "對不起,您要查詢的內容不存在,請確認您的輸入是否正確!" << endl;
				goto loop;
			}
		}
		return mid;
	}
	case 'q': 
	{
		return '\0';
	}
	default:
	{
		system("cls");
		cout << "對不起,您的輸入有誤,請重新輸入!" << endl;
		goto loop;
	}
	}
}

/******************刪除連結串列 節點***********************/
void delete_list(student *head)
{
	student *p = '\0';
	char selet[4];
	system("cls");
	cout << "在刪除前,系統會根據您的提示找到您要刪除的學生資訊!" << endl;
	p = findmege(head);
	if (p != '\0')
	{
		cout << "確認要刪除嗎(yes/任意鍵返回)" << endl;
		cin >> selet;

		if (strcmp(selet, "yes") == 0)
		{
			for (; head->getNext() != p; head = head->getNext());
			head->afterDelete();
			system("cls");
			cout << "該資訊刪除成功!" << endl;
		}
	}
}

/*******************修改節點資訊********************/
void change_info(student *head)
{
	system("cls");
	cout << "在您修改前,系統會根據您提供的資訊找的您要修改的資訊:" << endl;
	student *p = '\0';

	float achieve;
	p = findmege(head);
	if (p != '\0')
	{
		cout << "請輸入c++成績:" << endl;
		cin >> achieve;
		p->changegrade(achieve);
		system("cls");
		cout << "修改成功!" << endl;
	}

}

/**************輸出學生成績資訊**************/
void output(student *head)
{
	system("cls");
	cout << "1-檢視指定學生資訊;2-檢視所有學生資訊;3-分段輸出學生資訊" << endl;
	char ch;
	int n = 0;
	head = head->getNext();
	cin >> ch;
	switch (ch)
	{
	case '1': 
		head = findmege(head);
		if (head == '\0')
		{
			break;
		}
		head->getMage();
		break;
	case '2': 
	while (head)
	{
		head->getMage();
		head = head->getNext();
	}
	break;
	case '3': 
		cout << "a-60分以下;b-60~70分之間;c-70~80分之間;d-80~90分之間;e-90~100分之間:" << endl;
		cin >> ch;
		switch (ch)
		{
		case 'a':
		while (head)
		{
			if (head->stu_score <= 60)
			{
				head->getMage();
				n++;
			}
			head = head->getNext();
		}
		 break;
		case 'b': 
		while (head)
		{
			if (head->stu_score>60 && head->stu_score <= 70) 
			{ 
				head->getMage();
				n++; 
			}
			head = head->getNext();
		}
		break;
		case 'c': 
		while (head)
		{
			if (head->stu_score>70 && head->stu_score <= 80)
			{ 
				head->getMage(); 
				n++; 
			}
			head = head->getNext();
		}
		break;
		case 'd': 
		while (head)
		{
			if (head->stu_score>80 && head->stu_score <= 90)
			{
				head->getMage();
				n++;
			}
			head = head->getNext();
		}
		break;
		case 'e': 
		while (head)
		{
			if (head->stu_score>90 && head->stu_score <= 100)
			{ 
				head->getMage();
				n++;
			}
			head = head->getNext();
		}
		}
		if (n == 0)
		{
			cout << "該分段內沒有您要找的學生資訊" << endl;
		}
	}
}

/*****************主選單************************/
void mainmenu(student *head)
{
	char selet[10];
	int n = 1;
	ofstream outfile;
	ifstream infile;
	student *p, *ptr;
	student *test = head, *mid;
	cout << "*************************歡迎進入學生資訊管理系統*************************" << endl;
	do {
		cout << "**************************************************************************" << endl;
		cout << "1.插入資訊;   2.刪除資訊;  3.修改資訊; 4.檢視資訊; 5.儲存  " << endl;
		cout << "按'q'鍵退出      " << endl;
		cout << "**************************************************************************" << endl;
		cin >> selet;
		if (((selet[0]<'1' || selet[0]>'6') && selet[0] != 'q') || strlen(selet)>1)
		{
			system("cls");
			cout << "您的輸入有誤,請重新輸入!" << endl;
			break;
		}
		switch (selet[0])
		{

		case '1':
			construct_list(head);
			break; 
		case '2': 
			delete_list(head); 
			break;
		case '3': 
			change_info(head);
			break;
		case '4': 
			output(head);
			break;
		case '5':  
			outfile.open("students.txt", ios::out | ios::app);
			for (p = head->getNext(); p != '\0'; p = p->getNext())
			{
				outfile << p->stu_name << ' ';
				outfile << p->stu_num << ' ';
				outfile << p->stu_score << ' ';
				outfile << endl;
			}
			outfile.close();
			system("cls");
			cout << "儲存成功!" << endl;
			break;
		case 'q': 
			break;
		}
	} while (selet[0] != 'q');
}

void main()
{
	student head;
	mainmenu(&head);
}
執行結果部分截圖: