|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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");
}
//3、进行排序
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;
}
}
}
}
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),pai(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;
}
在按排序的时候没有进行排序,在文本中的顺序也没有发生变化,不知道怎么改好,求解。希望有代码帮忙,谢谢了
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #define MAX 100 // 最大储存人数
- using std::cout, std::cin, std::endl;
- // 学生个资
- class Student {
- public:
- std::string m_name;
- int m_id, m_score;
- Student();
- Student(std::string, int, int);
- ~Student();
- };
- Student::Student() {
- m_name = "";
- m_id = -1;
- m_score = -1;
- }
- Student::Student(std::string name, int id, int score) : m_name(name), m_id(id), m_score(score) {}
- Student::~Student() {}
- // 联络列表
- class ContactBooks {
- public:
- Student list[MAX];
- size_t m_size = 0;
- };
- // 读取文件:ifstream
- void readFile() {
- std::string message;
- std::ifstream file;
- file.open("text.txt");
- while (getline(file, message))
- {
- cout << message << endl;
- }
- file.close();
- }
- // 更新文件:ofstream
- void updateFile(ContactBooks &student) {
- std::ofstream file;
- int N = student.m_size;
- file.open("text.txt", std::ios::trunc);
- for (int i = 0; i < N; i++) {
- file
- << student.list[i].m_id << " "
- << student.list[i].m_name << " "
- << student.list[i].m_score << " "
- << endl;
- }
- file.close();
- }
- // 写入文件:ofstream
- void writeFile(Student student, ContactBooks **Book) {
- std::ofstream file;
- ContactBooks* p = *Book;
- p->list[p->m_size++] = student;
- file.open("text.txt", std::ios::app);
- file
- << student.m_id << " "
- << student.m_name << " "
- << student.m_score << " "
- << endl;
- file.close();
- }
- // 冒泡排序 - 根据学生成绩 score 进行排序
- void sort(ContactBooks& Book) {
- Student temp;
- // 排序列表里的元素
- int N = static_cast<int> (Book.m_size);
- for (int i = 0; i < N - 1; i++) {
- for (int j = i + 1; j < N; j++) {
- if (Book.list[i].m_score > Book.list[j].m_score) {
- temp = *(Book.list + i);
- *(Book.list + i) = *(Book.list + j);
- *(Book.list + j) = temp;
- }
- }
- }
- updateFile(Book); // 更新文件内容
- }
- // 输入学生资料
- void insert(ContactBooks &Book) {
- cout
- << "模式一: 输入数据"
- << endl << endl;
- std::string name;
- int id, score;
- cout
- << "请输入学生的姓名: "
- << endl;
- cin >> name;
- cout
- << "请输入学生的学号: "
- << endl;
- cin >> id;
- cout
- << "请输入学生的总成绩: "
- << endl;
- cin >> score;
- // 存入文件 text.txt 和列表 ContactBooks 里
- ContactBooks* p = &Book;
- writeFile({ name, id, score }, &p);
- cout << "导入成功!!!" << endl;
- system("pause");
- system("cls");
- }
- // 菜单
- void menu() {
- cout
- << std::setfill('*') << std::setw(25) << endl
- << std::setfill(' ') << std::setw(15)
- << " | 请选择你的模式 |" << endl
- << std::setfill(' ') << std::setw(15)
- << " | 1、输入数据 |" << endl
- << std::setfill(' ') << std::setw(15)
- << " | 2、输出数据 |" << endl
- << std::setfill(' ') << std::setw(15)
- << " | 3、进行排序 |" << endl
- << std::setfill(' ') << std::setw(15)
- << " | 4、退出程序 |" << endl
- << std::setfill('*') << std::setw(25) << endl;
- cout << "请输入你的模式(1 ~ 4)" << endl;
- }
- // 指令
- int command(ContactBooks Book) {
- while (true) {
- menu();
- int cmd;
- cin >> cmd;
- switch (cmd)
- {
- case 1:
- insert(Book);
- break;
- case 2:
- readFile();
- break;
- case 3:
- sort(Book);
- break;
- case 4:
- cout << "欢迎下次使用" << endl;
- return 0;
- default:
- cout << "请输入有效的数字。" << endl;
- system("pause");
- system("cls");
- break;
- }
- }
- }
- int main() {
- ContactBooks Book; // 创建列表
- return command(Book);
- }
复制代码
|
|