You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
7.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<iostream>
#include<fstream>
#include"xianzhi.h"
using namespace xiaozu;
using namespace std;
#define MAX 100//先定义一个最大值,便于改动人数的时候直接改变
#define max 150//定义考试成绩的最高分
struct Student
{
int m_num;
string m_name;
int m_score;
};
struct Studentbooks
{
struct Student StudentArray[MAX];
int m_size;
};
void show_Menu();//展示操作的菜单栏
void addstudent(Studentbooks* sbs)//添加学生信息
{
if (sbs->m_size == MAX)
{
cout << "当前信息系统已满!" << endl;
return;
}
else
{
//编号
cout << "请输入学生编号:";
int num = 0;
cin >> num;
sbs->StudentArray[sbs->m_size].m_num = num;
//姓名
string name;
cout << "请输入学生姓名:";
cin >> name;
sbs->StudentArray[sbs->m_size].m_name = name;
//成绩
cout << "请输入学生的成绩:";
int score{ get_line(0,max) };
sbs->StudentArray[sbs->m_size].m_score = score;
//更新信息系统数据统计
sbs->m_size++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
}
void showstudent(Studentbooks* sbs)//显示学生信息
{
if (sbs->m_size == 0)
{
cout << "当前信息系统为空!" << endl;
}
else
{
//输出每个学生的编号,姓名和成绩
double highest{ 0 }, lowest{ 10000 }, average{ 0 }, fangcha{ 0 };
for (int i = 0; i < sbs->m_size; i++)
{
cout << "学生编号:" << sbs->StudentArray[i].m_num<< endl;
cout << "学生姓名:" << sbs->StudentArray[i].m_name << endl;
cout << "学生成绩:" << sbs->StudentArray[i].m_score << endl;
cout << endl;
average += sbs->StudentArray[i].m_score;
highest= highest > sbs->StudentArray[i].m_score ? highest : sbs->StudentArray[i].m_score;
lowest= lowest < sbs->StudentArray[i].m_score ? lowest : sbs->StudentArray[i].m_score;
}
average = average / sbs->m_size;
for (int i = 0; i < sbs->m_size; i++)
{
fangcha += (sbs->StudentArray[i].m_score - average) * (sbs->StudentArray[i].m_score - average);
}
//输出平均分,最高分,最低分,方差
cout << "平均分:" << average << "\n";
cout << "最高分:" << highest << "\n";
cout << "最低分:" << lowest << "\n";
cout << "方差:" << fangcha << "\n";
cout << "成绩范围:" << lowest << "——" << highest << "\n";
}
system("pause");
system("cls");
}
int isExist1(Studentbooks* sbs, int num)//删除学生信息(检测)
{
for (int i = 0; i < sbs->m_size; i++)
{
if (sbs->StudentArray[i].m_num == num)
{
return i;
}
}
return -1;
}
int isExist2(Studentbooks* sbs, string name)//删除学生信息(检测)
{
for (int i = 0; i < sbs->m_size; i++)
{
if (sbs->StudentArray[i].m_name == name)
{
return i;
}
}
return -1;
}
void deletestudent(Studentbooks* sbs)
{
int ret = 0;
cout << "0编号删除" << endl;
cout << "1姓名删除" << endl;
int choice{ get_line(0,1) };
if (choice == 0)
{
int num = 0;
cout << "请输入要删除的学生编号:" << endl;
cin >> num;
ret = isExist1(sbs, num);
}
if (choice == 1)
{
string name;
cout << "请输入要删除的学生姓名:" << endl;
cin >> name;
ret = isExist2(sbs, name);
}
if (ret != -1)
{
for (int i = ret; i < sbs->m_size; i++)
{
//数据前移
sbs->StudentArray[i] = sbs->StudentArray[i + 1];
}
sbs->m_size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void findstudent(Studentbooks* sbs)
{
int ret = 0;
cout << "0编号查找" << endl;
cout << "1姓名查找" << endl;
int choice{ get_line(0,1) };
if (choice == 0)
{
int num = 0;
cout << "请输入要查找的学生编号:" << endl;
cin >> num;
ret = isExist1(sbs, num);
}
if (choice == 1)
{
string name;
cout << "请输入要查找的学生姓名:" << endl;
cin >> name;
ret = isExist2(sbs, name);
}
if (ret != -1)
{
cout <<"学生编号:" << sbs->StudentArray[ret].m_num << endl;
cout <<"学生姓名:" << sbs->StudentArray[ret].m_name << endl;
cout <<"学生成绩:" << sbs->StudentArray[ret].m_score << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
void modifystudent(Studentbooks* sbs)
{
int ret = 0;
cout << "0编号修改" << endl;
cout << "1姓名修改" << endl;
int choice{ get_line(0,1) };
if (choice == 0)
{
int num = 0;
cout << "请输入要修改的学生编号:" << endl;
cin >> num;
ret = isExist1(sbs, num);
}
if (choice == 1)
{
string name;
cout << "请输入要修改的学生姓名:" << endl;
cin >> name;
ret = isExist2(sbs, name);
}
if (ret != -1)
{
//编号
cout << "请输入学生编号:";
int num = 0;
cin >> num;
sbs->StudentArray[ret].m_num = num;
//姓名
string name;
cout << "请输入学生姓名:";
cin >> name;
sbs->StudentArray[ret].m_name = name;
//成绩
cout << "请输入学生的成绩:";
int score{ get_line(0,max) };
sbs->StudentArray[ret].m_score = score;
cout << "修改成功!!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
void clearstudent(Studentbooks* sbs)
{
sbs->m_size = 0;
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
int main()
{
struct Studentbooks sbs;
sbs.m_size = 0;
ifstream ifile;
ifile.open("Information Manager System.txt", ios::in);
if (ifile.is_open())
{
cout<<"是否读取文件?" << "\n" << "0:是" << "\n" << "1:否" << "\n";
cout << "请输入:";
int isread{ get_line(0,1) };
if (!isread)
{
while (ifile >> sbs.StudentArray[sbs.m_size].m_num && ifile >> sbs.StudentArray[sbs.m_size].m_name && ifile >> sbs.StudentArray[sbs.m_size].m_score)
sbs.m_size++;
}
}
else
{
cout << "无现有文件!" << endl;
system("pause");
system("cls");
}
ifile.close();
ofstream ofile;
ofile.open("Information Manager System.txt", ios::trunc);
while (1)
{
system("cls");
show_Menu();
int select(get_line(0, 6));
switch (select)
{
case 1://添加学生
addstudent(&sbs);
break;
case 2://显示学生(包括成绩以及最高分,平均分,最低分,方差)
showstudent(&sbs);
break;
case 3://删除学生
deletestudent(&sbs);
break;
case 4://查找学生
findstudent(&sbs);
break;
case 5://修改学生
modifystudent(&sbs);
break;
case 6:
clearstudent(&sbs);
break;
case 0://退出系统
for (int i{ 0 }; i < sbs.m_size; i++)
{
ofile << sbs.StudentArray[i].m_num << " ";
ofile << sbs.StudentArray[i].m_name << " ";
ofile << sbs.StudentArray[i].m_score << " ";
ofile << "\n";
}
ofile.close();
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
default:
break;
}
}
system("pause");
return 0;
}
void show_Menu()
{
cout << "************************" << endl;
cout << "****** 1.添加学生 ******" << endl;
cout << "****** 2.显示学生 ******" << endl;
cout << "****** 3.删除学生 ******" << endl;
cout << "****** 4.查找学生 ******" << endl;
cout << "****** 5.修改学生 ******" << endl;
cout << "****** 6.清空学生 ******" << endl;
cout << "****** 0.退出系统 ******" << endl;
cout << "************************" << endl;
}