#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include <windows.h>
using std::endl;
using std::cin;
using std::cout;
#define NAMESIZE 20
#define FILENAME "学生成绩.dat"
const char* subject[6] = { "语文","数学","英语","物理","化学","生物" };
class Human {
public:
char name[NAMESIZE] = { '\0' };
int age;
double height;
double weight;
};
typedef class Student:public Human {
public:
int info=0;
int arr[6]={0};
struct Student* next;
}SN, * SNL;
class stuSystem {
private:
SNL shead;
public:
stuSystem();
int ADD();
int SORT();
int SORT(int index);
int SEARCH();
int CHANGE();
int FILEREAD();
int FILEWRITE();
int SHOW();
int MENU();
int DEL();
SNL INIT();
int wait();
int WrongInput(const char* warn);
int color(int x);
int AddScore(const int info, const int arr[6], const char name[NAMESIZE]);
};
int delwar;
int System();
int main(int argc, char** argv)
{
System();
return 0;
}
stuSystem::stuSystem() {
this->shead = INIT();
for (int i = 0; i < 6; i++)
this->shead->arr[i] = 0;
this->shead->info = 0;
this->shead->next = NULL;
}
int stuSystem::SORT()
{
SNL shead = this->shead;
if (!shead || !(shead->next))
{
WrongInput("链表头不存在,无法排序");
return -1;
}
else {
SNL s1, s2;
s1 = shead->next;
while (s1)
{
s2 = s1->next;
while (s2)
{
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < 6; i++)
{
sum1 += s1->arr[i];
sum2 += s2->arr[i];
}
if (sum1 < sum2)
{
int temp;
temp = s1->info;
s1->info = s2->info;
s2->info = temp;
for (int i = 0; i < 6; i++)
{
int temp;
temp = s1->arr[i];
s1->arr[i] = s2->arr[i];
s2->arr[i] = temp;
}
char name1[NAMESIZE] = { '\0' };
char name2[NAMESIZE] = { '\0' };
strcpy(name1, s1->name);
strcpy(name2, s2->name);
for (int i = 0; i < NAMESIZE; i++)
{
s1->name[i] = s2->name[i] = '\0';
}
strcpy(s1->name, name2);
strcpy(s2->name, name1);
}
s2 = s2->next;
}
s1 = s1->next;
}
cout << endl;
cout << "\t\t\t\t\t";
cout << "排序成功";
cout << endl;
}
return 0;
}
int stuSystem::SORT(int index)
{
SNL shead = this->shead;
if (!shead || !(shead->next))
{
WrongInput("链表头不存在,无法排序");
return -1;
}
else {
SNL s1, s2;
s1 = shead->next;
while (s1)
{
s2 = s1->next;
while (s2)
{
if (s1->arr[index-1] < s2->arr[index-1])
{
int temp;
temp = s1->info;
s1->info = s2->info;
s2->info = temp;
for (int i = 0; i < 6; i++)
{
int temp;
temp = s1->arr[i];
s1->arr[i] = s2->arr[i];
s2->arr[i] = temp;
}
char name1[NAMESIZE] = { '\0' };
char name2[NAMESIZE] = { '\0' };
strcpy(name1, s1->name);
strcpy(name2, s2->name);
for (int i = 0; i < NAMESIZE; i++)
{
s1->name[i] = s2->name[i] = '\0';
}
strcpy(s1->name, name2);
strcpy(s2->name, name1);
}
s2 = s2->next;
}
s1 = s1->next;
}
cout << endl;
cout << "\t\t\t\t\t";
cout << "排序成功";
cout << endl;
}
return 0;
}
int stuSystem::DEL()
{
int info;
SNL shead = this->shead;
cout << endl << endl << endl;
cout << "\t\t\t\t\t";
cout << "输入学号: ";
cin >> info;
while (shead->next)
{
if (shead->next->info == info)
{
SNL delnode = shead->next;
shead->next = shead->next->next;
free(delnode);
cout << endl;
cout << "\t\t\t\t\t";
cout<<"删除成功";
cout << endl;
return 0;
}
shead = shead->next;
}
WrongInput("查无此人");
return 0;
}
int stuSystem::SEARCH()
{
int info;
SNL shead = this->shead;
cout << endl << endl << endl;
cout << "\t\t\t\t\t";
cout<<"输入学号: ";
cin >> info;
while (shead->next)
{
if (shead->next->info == info)
{
printf("\n\t\t %10d%8s%8d%8d%8d%8d%8d%8d",
shead->next->info,
shead->next->name,
shead->next->arr[0],
shead->next->arr[1],
shead->next->arr[2],
shead->next->arr[3],
shead->next->arr[4],
shead->next->arr[5]);
cout << endl;
cout << "\t\t\t\t\t";
cout << "查询成功";
cout << endl;
return 0;
}
shead = shead->next;
}
WrongInput("查无此人");
return 0;
}
int stuSystem::CHANGE()
{
int info;
SNL shead = this->shead;
cout << endl << endl << endl;
cout << "\t\t\t\t\t";
cout<<"输入学号: ";
cin >> info;
while (shead->next)
{
if (shead->next->info == info)
{
cout << endl;
cout << endl;
cout << endl;
cout << "\t\t\t\t\t";
cout << "输入姓名: ";
for (int i = 0; i < NAMESIZE; i++) {
shead->next->name[i] = '\0';
}
cin>>shead->next->name;
for (int i = 0; i < 6; i++)
{
cout << "\t\t\t\t\t";
cout<<"输入"<<subject[i]<<"成绩: ";
cin>>shead->next->arr[i];
}
cout << endl;
cout << "\t\t\t\t\t";
cout<<"修改成功";
cout << endl;
return 0;
}
shead = shead->next;
}
WrongInput("查无此人");
return 0;
}
int stuSystem::SHOW()
{
SNL shead = this->shead;
if (!shead || !(shead->next)) {
WrongInput("无链表头或无数据,无法显示");
return -1;
}
printf("\n\n\t\t\t学号\t姓名\t语文\t数学\t英语\t物理\t化学\t生物\n");
while (shead->next)
{
printf("\n\t\t %10d%8s%8d%8d%8d%8d%8d%8d",
shead->next->info,
shead->next->name,
shead->next->arr[0],
shead->next->arr[1],
shead->next->arr[2],
shead->next->arr[3],
shead->next->arr[4],
shead->next->arr[5]);
shead = shead->next;
}
return 0;
}
int stuSystem::ADD()
{
SNL shead = this->shead;
if (!shead)
{
WrongInput("无链表头,无法添加");
return -1;
}
int info;
int arr[6];
char name[NAMESIZE] = { '\0' };
cout << endl;
cout << endl;
cout << endl;
cout<<"\t\t\t\t\t输入学号: ";
delwar = scanf("%d", &info);
cout<<"\t\t\t\t\t输入姓名: ";
delwar = scanf("%s", name);
for (int i = 0; i < 6; i++)
{
printf("\t\t\t\t\t输入%s成绩: ", subject[i]);
delwar = scanf("%d", &arr[i]);
}
AddScore(info, arr, name);
cout<<"\t\t\t\t\t添加成功\n";
return 0;
}
SNL stuSystem::INIT()
{
SNL node = new SN;
node->next = NULL;
return node;
}
int stuSystem::color(int x) //设置字体颜色
{
if (x >= 0 && x <= 15) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
return 0;
}
int stuSystem::wait()
{
cout << endl;
cout << endl;
cout << endl;
cout << "\t\t\t\t\t";
cout<<"输入回车结束";
cout << endl;
char wait = getchar();
wait = getchar();
return 0;
}
int stuSystem::MENU()
{
int choice;
cout<<"\n\n\n";
cout<<"\t\t\t\t\t██████████████████████████\n";
cout<<"\t\t\t\t\t█ █\n";
cout<<"\t\t\t\t\t█ < 菜单 > █\n";
cout<<"\t\t\t\t\t█ 1.增加成绩 █\n";
cout<<"\t\t\t\t\t█ 2.删除成绩 █\n";
cout<<"\t\t\t\t\t█ 3.修改成绩 █\n";
cout<<"\t\t\t\t\t█ 4.查询成绩 █\n";
cout<<"\t\t\t\t\t█ 5.显示成绩 █\n";
cout<<"\t\t\t\t\t█ 6.排序成绩 █\n";
cout<<"\t\t\t\t\t█ 0.退出 █\n";
cout<<"\t\t\t\t\t█ █\n";
cout<<"\t\t\t\t\t██████████████████████████\n";
cout<<"\n\t\t\t\t\t选择> ";
cin >> choice;
system("cls");
return choice;
}
int stuSystem::WrongInput(const char* warn)
{
system("cls");
color(4);
cout << endl << endl;
cout << endl << endl;
cout << endl << endl << endl;
cout << "\t\t\t\t\t\t";
cout<<warn;
cout << endl;
color(7);
return 0;
}
int stuSystem::AddScore(const int info, const int arr[6], const char name[NAMESIZE])
{
SNL shead = this->shead;
if (!shead)
{
WrongInput("无链表头,无法添加");
return 0;
}
while (shead->next) {
shead = shead->next;
}
SNL snode = INIT();
shead->next = snode;
snode->info = info;
for (int i = 0; i < 6; i++) {
snode->arr[i] = arr[i];
}
strcpy(snode->name, name);
return 0;
}
int stuSystem::FILEREAD()
{
if (!shead)
{
WrongInput("链表头不存在,读取失败");
return -1;
}
FILE* file = fopen(FILENAME, "r");
if (!file)
{
WrongInput("文件不存在,读取失败");
return -1;
}
while (!feof(file))
{
int info = -1;
int arr[6];
char name[NAMESIZE] = { '\0' };
delwar = fscanf(file, "%d%s%d%d%d%d%d%d",
&info,
name,
&arr[0],
&arr[1],
&arr[2],
&arr[3],
&arr[4],
&arr[5]);
if (info != -1) {
AddScore(info, arr, name);
}
}
fclose(file);
return 0;
}
int stuSystem::FILEWRITE()
{
if (!shead)
{
WrongInput("链表头不存在,写入失败");
return -1;
}
FILE* file = fopen(FILENAME, "w");
if (!file)
{
WrongInput("文件写入失败");
return -1;
}
while (shead->next)
{
fprintf(file, "%d %s %d %d %d %d %d %d\n",
shead->next->info,
shead->next->name,
shead->next->arr[0],
shead->next->arr[1],
shead->next->arr[2],
shead->next->arr[3],
shead->next->arr[4],
shead->next->arr[5]);
shead = shead->next;
}
return 0;
}
int System()
{
stuSystem* SYSTEM = new stuSystem();
SYSTEM->FILEREAD();
while (true)
{
switch (SYSTEM->MENU())
{
case 1:
SYSTEM->ADD();
break;
case 2:
SYSTEM->DEL();
break;
case 3:
SYSTEM->CHANGE();
break;
case 4:
SYSTEM->SEARCH();
break;
case 5:
SYSTEM->SHOW();
break;
case 6:
SYSTEM->SORT();
break;
case 0:
cout<<"\n\n\n\n\n";
cout<<"\t\t\t\t\t\t";
cout<<"成功退出";
SYSTEM->FILEWRITE();
exit(0);
break;
default:
SYSTEM->WrongInput("错误输入警告");
break;
}
SYSTEM->wait();
system("cls");
}
}
C++学生管理系统
最新推荐文章于 2024-03-15 17:54:13 发布