|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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)
{
for (int i = 0;i < 3;i++)
{
cout << abs.personArray[i].m_Name << " ";
cout << abs.personArray[i].m_scores << endl;
}
system("pause");
system("cls");
}
void pai(Addbooks& abs)
{
for (int i = 0; i < abs.m_Size; i++)
{
for (int j = 0; j < i - abs.m_Size - 1; j++)
{
if (abs.personArray[j].m_scores > abs.personArray[j + 1].m_scores)
{
Person temp = abs.personArray[j];
abs.personArray[j] = abs.personArray[j + 1];
abs.personArray[j + 1] = temp;
}
}
}
}
//3、进行排序
void Sort( ifstream &ifs, Addbooks& abs)
{
ifs.open("text.txt", ios::in);
string name;
int studentid;
int scores;
while (ifs >> name && ifs >> studentid && ifs >> scores)
{
abs.personArray[abs.m_Size].m_Name = name;
abs.personArray[abs.m_Size].m_scores = scores;
abs.personArray[abs.m_Size].m_studentid = studentid;
}
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(ifs,abs);
break;
case 4:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
case 5:pai(abs);
break;
default:
cout << "请输入有效的数字。" << endl;
system("pause");
system("cls");
}
}
}
int main()
{
ofstream ofs;
Addbooks abs;
ifstream ifs;
SelMode(ofs, abs, ifs,Menu);
system("pause");
return 0;
}
要在文本中进行排序,但我这里面利用冒泡排序后文本中依然没有改变,不知道错在哪里,请各位大神帮帮忙最好把代码发出来,球球了
|
|