// 学生成绩管理系统.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iomanip>
#include<stdlib.h>//system
#include<iostream>
#include<string>
#include<algorithm>//算法头文件
const int MAX_SIZE=500;
using namespace std;
class Student
{
private:
char name[12];//姓名
char id[10];//学号
float math;//数学成绩
float english;//英语成绩
float computer;//计算机成绩
public:
Student(){}//构造函数(跟函数名一样的函数)1(跟默认的一样)
Student(char*n) {strcpy_s(name, n);}//构造函数二只输入name
Student(char*n, char*i) {strcpy_s(name, n); strcpy_s(id,i); }//构造函数三输入name和id 实参传入时,形参就会指向实参的地址,也就不是野指针了。
//以下是设置信息
void SetName(char*n){strcpy_s(name, n);}//设置名字
void SetMath(float m){math = m; }//设置数学成绩
void SetEnglish(float e){english = e;}//设置英语成绩
void SetComputer(float c){computer = c; }//设置计算机成绩
void SetAllScores(float m, float e, float c)
{
math = m; english = e; computer = c;
}
//以下是获取信息
string GetName(){return name;}//获取name返回一个字符串
string GetID() { return id; }//获取id返回一个字符串
float GetMath() { return math; }
float GetEnglish() { return english; }
float GetComputer() { return computer; }
float Getsum() { return math + english + computer; }//获得总成绩
bool Match(char *str, short flag)//flag为1,与姓名匹配,flag为2,与id进行匹配
{
return flag == 1 ? strcmp(name, str) == 0 : strcmp(id, str) == 0;//一致的话返回0不一致返回非零
}
void Print()//打印格式输出
{
cout << name << '\t' << id << endl;
cout << "数学:" << setprecision(1) << setiosflags(ios::fixed) << math << '\t' << "英语" << english << '\t' << "计算机" << '\t' << computer << endl;//setprecision()精确到小数点后1位,setiosflags(ios::fixed)小数点后一位
}
};
bool Compare1(Student st1, Student st2)
{
return st1.GetID() < st2.GetID();
}
bool Compare2(Student st1, Student st2)
{
return st1.Getsum()>st2.Getsum();
}
class DataBase//数据集一个对象里有所有学生
{
private:
Student stu[MAX_SIZE];
short size;//实际现在有多少个学生
public:
DataBase() { size = 0; }//构造函数
bool Push(char *n, char*i, float m, float e, float c)//添加一个学生,姓名、学号、数学、英语、计算机成绩
{
if (size == MAX_SIZE) return false;
Student st(n, i); st.SetAllScores(m, e, c);
stu[size++] = st;
return true;
}
bool Push()//函数重载,允许用户手动输入。
{
if (size == MAX_SIZE)
{
cout << "系统不能容纳更多学生。\n";
system("pause");
return false;
}
char n[12], i[10];
cout << "输入学生姓名:";
cin >> n;
int idx;//循环变量
do
{
cout << "输入学号:";
cin >> i;
for (idx= 0; idx < size; idx++)
{
if (stu[idx].Match(i,2))
{
cout << "学号存在不能重复输入!\n";
break;
}
}
} while (idx<size);
Student stu_tmp(n, i);
float m, e, c;
cout << "请输入学生数学成绩:"; cin >> m;
cout << "请输入学生英语成绩:"; cin >> e;
cout << "请输入学生计算机成绩:"; cin >> c;
stu_tmp.SetAllScores(m, e, c);
stu[size++] = stu_tmp;
cout << "添加成功!\n";
system("pause");
return true;
}
short AimedSearch(short start_id, char*str, short flag)//flag为1,与姓名匹配,flag为2,与id进行匹配
{
for (short i = start_id; i < size; i++)
{
if (stu[i].Match(str,flag))
{
stu[i].Print();//显示信息
return i;
}
}
return -1;//返回-1表示没有找到
}
short Search()//实现查找功能,提供用户输入接口
{
short choice;//接受用户输入按姓名还是id匹配
do
{
cout << "请问按什么条件搜索?1.姓名2.学号";
cin >> choice;
} while (choice!=1&&choice!=2);
cout << "请输入要找的" << (choice == 1 ? "姓名:" : "学号:");
char match[12];
cin >> match;
short result = 0;
char nod;
while (true)
{
result = AimedSearch(result, match, choice);
if (result==-1)
{
cout << "未找到匹配信息。\n";
system("pause");
return result;
}
cout << "这是要找的人么?(y/n)";
cin >> nod;
if (nod=='y'||nod=='Y')
{
return result;
}
else result++;
}
}
bool Delete()
{
short result;
result = Search();
if (result==-1)
{
return false;
}
cout << "是否要删除这条信息?(y/n)";
char choice;
short idx;
cin >> choice;
if (choice=='y'||choice=='Y')
{
for ( idx = result; idx < size-1; idx++)
{
stu[idx] = stu[idx + 1];
size--;
cout << "删除成功!\n";
system("pause");
return true;
}
}
cout << "信息未删除。\n";
system("pause");
return false;
}
bool Alter()
{
short result;
result=Search();
if (result == -1)
{
return false;
}
cout << "是否要修改这条信息?(y/n)";
char choice;//是否接受修改
char subchoie;//修改内容的选择
char rename[12];//修改名字
float rescore;//新成绩
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
cout << "修改什么:1.名字2.数学成绩3.英语成绩4.计算机程序\n";
cin >> subchoie;
switch (subchoie)
{
case '1':
cout << "输入一个新名字"; cin >> rename;
stu[result].SetName(rename); break;
cout << "修改已成功!";
system("pause");
case '2':
cout << "输入数学成绩:"; cin >> rescore;
stu[result].SetMath(rescore); break;
cout << "修改已成功!";
system("pause");
case '3':
cout << "输入英语成绩:"; cin >> rescore;
stu[result].SetEnglish(rescore); break;
cout << "修改已成功!";
system("pause");
case '4':
cout << "输入计算机成绩:"; cin >> rescore;
stu[result].SetComputer(rescore); break;
cout << "修改已成功!";
system("pause");
default:
break;
}
}
return true;//只要找到了,不管是否修改都为true
cout << "信息未修改。\n";
system("pause");
}
void Display()
{
cout << endl << setw(12) << setiosflags(ios::left) << "姓名" << setw(12) << "学号" << setw(8) << "数学" << setw(8) << "英语" << setw(8) << "计算机" << endl<<endl;
cout << setprecision(1) << setiosflags(ios::fixed);//设置小数点精度
for (int i = 0; i < size; i++)
{
cout << setw(12) << stu[i].GetName() << setw(12) << stu[i].GetID() << setw(8) << stu[i].GetMath() << setw(8) << stu[i].GetEnglish() << setw(8) << stu[i].GetComputer() << endl;
}
cout << resetiosflags(ios::left);
system("pause");
}
void Sort()//排序
{
char choice;
do
{
cout << "请输入排序方式:1学号升序,2总成绩降序";
cin >> choice;
} while (choice!='1'&&choice!='2');
if (choice=='1')//按学号排序
{
sort(&stu[0], &stu[0] + size, Compare1);
}
else//按总成绩排序
{
sort(&stu[0], &stu[0] + size, Compare2);
}
cout << "重新排序完成,如下:\n";
Display();
}
char ShowMenu()
{
char choice;
do
{
system("cls");
cout << "--------------欢迎使用成绩管理系统-----------------\n\n";
cout << "1.添加学生\n";
cout << "2.查找学生\n";
cout << "3.删除学生\n";
cout << "4.重新排序\n";
cout << "5.显示全部\n";
cout << "6.修改信息\n";
cout << "7.退出\n";
cin >> choice;
} while (choice<'1'||choice>'7');
return choice;
}
};
int main()
{
DataBase db;
char choice;
bool quit = false;//quit是false不退出,true退出
db.Push("张五","0005",50,65,75);
db.Push("张三", "0003", 62, 615, 275);
db.Push("张一", "0001", 520, 765, 725);
db.Push("张四", "0004", 570, 565, 705);
db.Push("张七", "0007", 250, 625, 745);
while (!quit)//quit是false不退出,ture退出
{
choice = db.ShowMenu();
switch (choice)
{
case '1':
{
db.Push();
}break;
case '2':
{
db.Search();
}break;
case '3':
{
db.Delete();
}break;
case '4':
{
db.Sort();
}break;
case '5':
{
db.Display();
}break;
case '6':
{
db.Alter();
}break;
case '7':
{
quit = true;
}break;
}
}
return 0;
}
学生成绩管理系统
最新推荐文章于 2022-07-21 09:32:09 发布