📄 application.cpp
字号:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include "Application.h"
using namespace std;
void Application::PrintMenu() const
{
cout << "**************简易学生信息管理系统**************" << endl;
cout << setfill(' ');
cout << setw(14) << "" << "0、退出整个系统" << endl;
cout << setw(14) << "" << "1、添加学生的信息" << endl;
cout << setw(14) << "" << "2、查找学生信息:已知学号" << endl;
cout << setw(14) << "" << "3、查找学生信息:已知姓名" << endl;
cout << setw(14) << "" << "4、修改学生信息:已知学号" << endl;
cout << setw(14) << "" << "5、修改学生信息:已知姓名" << endl;
cout << setw(14) << "" << "6、对学生成绩排序" << endl;
cout << setw(14) << "" << "7、保存学生信息到文件中" << endl;
cout << setw(14) << "" << "8、从文件中读入学生信息" << endl;
cout << "************************************************" << endl;
}
void Application::Process()
{
unsigned int id;
char name[80];
cout << "请输入功能序号: " << endl;
char ch = cin.get(); //getchar();
cout << endl;
switch(ch) {
case '0':
cout << "您选择的是: 退出整个系统!" <<endl;
exit(0);
case '1':
cout << "您选择的是: 添加学生信息!" <<endl;
Add();
break;
case '2':
cout << "您选择的是: 根据学号查找学生信息!" <<endl;
cout << "Please enter id:";
cin >> id;
Find(id);
break;
case '3':
cout << "您选择的是: 根据姓名查找学生信息!" <<endl;
cout << "Please enter name:";
cin.getline(name, 80);
Find(name);
break;
case '4':
cout << "您选择的是: 根据学号修改学生信息!" <<endl;
cout << "Please enter id:";
cin >> id;
Update(id);
break;
case '5':
cout << "您选择的是: 根据姓名修改学生信息!" <<endl;
cout << "Please enter name:";
cin.getline(name, 80);
Update(name);
break;
case '6':
cout << "您选择的是: 按学生成绩排序!" <<endl;
Sort();
break;
case '7':
cout << "您选择的是保存学生信息到文件中!" <<endl;
Save();
break;
case '8':
cout << "您选择的是从文件中载入学生信息!" <<endl;
Load();
break;
default:
cout << "输入有误!" << endl;
}
cout << endl;
getchar();
}
int Application::GetResponse() const
{
while (true) {
cout << "按回车键继续, q键或者x键退出!" << endl;
char ch = cin.get(); //getchar();
if (ch == '\n') {
system("cls");
return 1;
}
else if (ch == 'q' || ch == 'Q' || ch == 'x' || ch == 'X')
return 0;
}
}
void Application::Add()
{
cout << "Here to add one student-information." << endl;
}
int Application::Find(char *name)
{
for (int i = 0; i < stuNumber; ++i) {
if (stricmp(name, s[i].name) == 0) {
cout << "Just Find him!" << endl;
return i;
}
}
return -1;
}
int Application::Find(unsigned int id)
{
for (int i = 0; i < stuNumber; ++i) {
if (id == s[i].id) {
cout << "Just Find him!" << endl;
return i;
}
}
return -1;
}
void Application::Update(char *name)
{
int idx = Find(name);
if (idx == -1) {
cout << name << "doesnot exist." << endl;
cout << "would you like add him?" << endl;
Add();
return;
}
cout << "Just Update his information." << endl;
}
void Application::Update(unsigned int id)
{
int idx = Find(id);
if (idx == -1) {
cout << id << "doesnot exist." << endl;
cout << "would you like add him?" << endl;
Add();
return;
}
cout << "Just Update his information." << endl;
}
void Application::Sort()
{
for (int i = 0; i < stuNumber - 1; ++i) {
for (int j = i + 1; j < stuNumber; ++j) {
if (s[i] < s[j]) {
Student t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
}
void Application::Save()
{
cout << "Just save all information." << endl;
ofstream ofs("Student.txt");
if (!ofs) {
cout << "Error: failed to open file." << endl;
exit(1);
}
int i = 0;
while (ofs && i < stuNumber) {
ofs.write((char*)&s[i], sizeof(s[i]));
++i;
}
ofs.close();
}
void Application::Load()
{
cout << "Just load all information." << endl;
ifstream ifs("Student.txt");
if (!ifs) {
cout << "Error: failed to open file." << endl;
exit(1);
}
int i = 0;
while (ifs && i < stuNumber) {
ifs.read((char*)&s[i], sizeof(s[i]));
++i;
}
ifs.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -