在c++中要将文本中的东西按成绩排名,并可以在模式1中查询到,求救
#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;
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.m_Name = name;
ofs << name << " ";
cout << "请输入学生的学号:" << endl;
cin >> studentid;
abs.personArray.m_studentid = studentid;
ofs << studentid << " ";
cout << "请输入学生的总成绩:" << endl;
cin >> scores;
abs.personArray.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.m_scores > abs.personArray.m_scores)
{
string temp1 = abs.personArray.m_Name;
abs.personArray.m_Name = abs.personArray.m_Name;
abs.personArray.m_Name= temp1;
int temp2 = abs.personArray.m_scores;
abs.personArray.m_scores = abs.personArray.m_scores;
abs.personArray.m_scores = temp2;
int temp3 = abs.personArray.m_studentid;
abs.personArray.m_studentid = abs.personArray.m_studentid;
abs.personArray.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;{:5_96:}
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;
} 掉那条河里啦?{:5_94:},出什么错误了? jhq999 发表于 2022-3-18 06:42
掉那条河里啦?,出什么错误了?
排序后在输出程序中没有排序
页:
[1]