|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#define MAX 100
class Person
{
public:
string m_Name;
int m_studentid = 0;
int m_scores = 0;
};
class Addbooks
{
public:
Person personArray[MAX];
int m_Size = 0;
};
//1、输入模式
void Intput(Addbooks &abs, ofstream & ofs)
{
cout << "模式一:输入数据" << endl;
ofs.open("text.txt", ios::out|ios::app);
string name;
int studentid;
int scores;
cout << "请输入学生的姓名: " << endl;
cin >> name;
abs.personArray[abs.m_Size].m_Name = name;
ofs << name << " ";
cout << "请输入学生的学号: " << endl;
cin >> studentid;
abs.personArray[abs.m_Size].m_studentid = studentid;
ofs << studentid << " ";
cout << "请输入学生的总成绩: " << endl;
cin >> scores;
abs.personArray[abs.m_Size].m_scores = scores;
ofs << scores <<endl;
abs.m_Size++;
ofs.close();
cout << "导入成功!!!" << endl;
system("pause");
system("cls");
}
//2、输出模式
void Output(Addbooks& abs)
{
ifstream ifs;
ifs.open("text.txt", ios::in);
string buf;
while (getline(ifs, buf))
{
cout << buf << endl;
}
ifs.close();
system("pause");
system("cls");
}
//3、进行排序
void Sort(Addbooks& abs, ifstream &ifs)
{
ifs.open("text.txt", ios::in);
for (int i = 0; i < abs.m_Size; i++)
{
for (int j = 0; j < abs.m_Size - i - 1; j++)
{
if (abs.personArray[j].m_scores > abs.personArray[j + 1].m_scores)
{
string temp1 = abs.personArray[j].m_Name;
abs.personArray[j].m_Name = abs.personArray[j + 1].m_Name;
abs.personArray[j + 1].m_Name= temp1;
int temp2 = abs.personArray[j].m_scores;
abs.personArray[j].m_scores = abs.personArray[j + 1].m_scores;
abs.personArray[j + 1].m_scores = temp2;
int temp3 = abs.personArray[j].m_studentid;
abs.personArray[j].m_studentid = abs.personArray[j + 1].m_studentid;
abs.personArray[j + 1].m_studentid = temp3;
}
}
}
ifs.close();
system("pause");
system("cls");
}
void Menu()
{
cout << "*************************" << endl;
cout << " | 请选择你的模式 |" << endl;
cout << " | 1、输入数据 |" << endl;
cout << " | 2、输出数据 |" << endl;
cout << " | 3、进行排序 |" << endl;
cout << " | 4、退出程序 |" << endl;
cout << "*************************" << endl;
cout << "请输入你的模式(1 - 4)" << endl;
}
//选择模式
int SelMode(ofstream & ofs, Addbooks &abs, ifstream &ifs, void Menu())
{
while (true)
{
Menu();
int choose = 0;
cin >> choose;
switch (choose)
{
case 1:Intput(abs, ofs);
break;
case 2:Output(abs);
break;
case 3:Sort(abs, ifs);
break;
case 4:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "请输入有效的数字。" << endl;
system("pause");
system("cls");
break;
}
}
}
int main()
{
ofstream ofs;
Addbooks abs;
ifstream ifs;
SelMode(ofs, abs, ifs,Menu);
system("pause");
return 0;
} |
|