num->string

C++:

int Number = 123;
string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();

std::string s = std::to_string(42);


#include <iostream>; using namespace std; #include "studentManager.h" #include "student.h" #include "info.h" int main() { studentManager st; int choice = 0; // 用户的选择 while (true) { st.showMenu(); // 展示菜单界面 cout << "请输入你的选择:" << endl; cin >> choice; switch (choice) { case 0: // 退出系统 st.backsystem(); break; case 1: // 增加学生 st.addStu(); break; case 2: // 显示学生 st.showStu(); break; case 3: // 删除学生 st.deletStu(); break; case 4: // 修改学生 st.editStu(); break; case 5: // 查找学生 st.findStu(); break; case 6: // 排序学生 st.sortStu(); break; default: // 输入其他数字-清空屏幕 system("cls"); break; } } system("pause"); return 0; } #include "info.h" Info::Info(string num, string name, string sex, int age, string adress, string major) { this->sNum = num; this->sName = name; this->sSex = sex; this->sAge = age; this->sAdress = adress; this->sMajor = major; } void Info::showInfo() { cout << this->sNum << " " << this->sName << " " << this->sSex << " " << this->sAge << " " << this->sAdress << " " << this->sMajor<< endl; } #include "studentManager.h"; // 构造函数 studentManager::studentManager() { // 初始化 fstream File; File.open("student.txt", ios::in); // 1 文件不存在的初始化 if (!File.is_open()) { cout << "文件不存在" << endl; this->sCount = 0; // 初始化学生人数和数组 this->sStuArray = NULL; this->isFileEmpty = true; // 文件为空为真 File.close(); return; } // 2 文件存在但为空 char ch; File >> ch; if (File.eof()) { cout << "文件为空" << endl; this->sCount = 0; this->sStuArray = NULL; this->isFileEmpty = true; File.close(); return; } // 3 文件存在 并记录数据 int count = this->getCount(); // 获取学生人数 this->sCount = count; // 更新学生人数 this->sStuArray = new Student * [this->sCount]; this->initStu(); // 保存文件中数据到数组中 } // 展示菜单 void studentManager::showMenu() { cout << "********************************************" << endl; cout << "********** 欢迎使用学生管理系统! *********" << endl; cout << "************* 0.退出管理程序 *************" << endl; cout << "************* 1.增加学生信息 *************" << endl; cout << "************* 2.显示学生信息 *************" << endl; cout << "************* 3.删除离校学生 *************" << endl; cout << "************* 4.修改学生信息 *************" << endl; cout << "************* 5.查找学生信息 *************" << endl; cout << "************* 6.按照学号排序 *************" << endl; cout << "********************************************" << endl; cout << endl; } // 退出系统 void studentManager::backsystem() { cout << "您已退出系统,欢迎下次使用" << endl; system("pause"); exit(0); // 退出程序 } // 添加学生 void studentManager::addStu() { cout << "请输入您想添加的学生人数" << endl; int addCount = 0; cin >> addCount; if (addCount > 0) { int newCount = this->sCount + addCount; // 最新人数 = 原本人数 + 新增人数 Student** newSpace = new Student * [newCount]; // 新数组空间 if (this->sStuArray != NULL) { // 添加原来的职工 for (int i = 0; i < this->sCount; i++) { newSpace[i] = this->sStuArray[i]; } } for (int i = 0; i < addCount; i++) { string Num; // 学号 string Name; // 姓名 string Sex; // 性别 int Age; // 年龄 string Adress; // 住址 string Major; // 专业 cout << "请输入要添加的第" << i + 1 << "个学生的学号" << endl; cin >> Num; int res = this->isStuExit(Num); // 判断该学号是否已存在 if (res == -1) { // 学号不存在 可以添加 string splitNum; // 要切割出来的学号的前n个数字 splitNum = getNchar(Num, 4); // 保存学号的前4个数字(年份) if (Num.length() == 12 && splitNum >= "2004" && splitNum <= "2023") { cout << "请输入要添加的第" << i + 1 << "个学生的姓名" << endl; cin >> Name; cout << "请输入要添加的第" << i + 1 << "个学生的性别" << endl; cin >> Sex; if (Sex == "男" || Sex == "女") { cout << "请输入要添加的第" << i + 1 << "个学生的年龄" << endl; cin >> Age; if (Age >= 14 && Age <= 30) { cout << "您是否要录入该学生的住址" << endl; cout << "1 是" << endl; cout << "2 否" << endl; int choose; cin >> choose; if (choose == 1) { cout << "请输入要添加的第" << i + 1 << "个学生的住址" << endl; cin >> Adress; cout << "您是否要录入该学生的专业" << endl; cout << "1 是" << endl; cout << "2 否" << endl; int choose; cin >> choose; if (choose == 1) { cout << "请输入要添加的第" << i + 1 << "个学生的专业" << endl; cin >> Major; } else { Major = " "; } } else { Adress = " "; cout << "您是否要录入该学生的专业" << endl; cout << "1 是" << endl; cout << "2 否" << endl; int choose; cin >> choose; if (choose == 1) { cout << "请输入要添加的第" << i + 1 << "个学生的专业" << endl; cin >> Major; } else { Major = " "; } } /*fstream File; File.open("student.txt", ios::out | ios::app); File << Num << " " << Name << " " << Sex << " " << Age << " " << Adress << " " << Major << " " << endl; File.close();*/ Student* stu = NULL; stu = new Info(Num, Name, Sex, Age, Adress, Major); newSpace[this->sCount + i] = stu; this->sCount = newCount; // 更新人数为最新人数 this->isFileEmpty = false; // 文件为空的标志为假 cout << "添加成功" << endl; delete[] this->sStuArray; this->sStuArray = newSpace; this->saveStu(); } else { cout << "您输入的年龄有误 请重新输入" << endl; break; } } else { cout << "性别只能输入男或女 请重新输入" << endl; break; } } else { cout << "您输入的学号有误 请重新输入" << endl; break; } } else { cout << "该学号已存在 请重新输入" << endl; } } } else { cout << "您输入的数字有误,请重新输入" << endl; system("pause"); system("cls"); } system("pause"); system("cls"); } // 返回字符串的前n个字符 string studentManager::getNchar(string str, int n) { string nChars; if (n < 0) { n = 0; cout << "输入有误, 请输入大于0的n" << endl; } if (n > str.size()) { n = str.size(); } for (size_t i = 0; i < n; i++) { nChars.push_back(str[i]); } return nChars; } // 保存学生 void studentManager::saveStu() { fstream File; File.open("student.txt", ios::out); for (int i = 0; i < this->sCount; i++) { File << this->sStuArray[i]->sNum << " " << this->sStuArray[i]->sName << " " << this->sStuArray[i]->sSex << " " << this->sStuArray[i]->sAge << " " << this->sStuArray[i]->sAdress << " " << this->sStuArray[i]->sMajor << " " << endl; } File.close(); } // 获取学生人数 int studentManager::getCount() { fstream File; File.open("student.txt", ios::in); string Num; // 学号 string Name; // 姓名 string Sex; // 性别 int Age; // 年龄 string Adress; // 住址 string Major; // 专业 int Count = 0; // 学生人数 while (File >> Num && File >> Name && File && File >> Sex && File >> Age && File >> Adress && File >> Major) { Count++; // 读到一条就加一条 } File.close(); return Count; } // 初始化学生数组 void studentManager::initStu() { fstream File; File.open("student.txt", ios::in); string Num; // 学号 string Name; // 姓名 string Sex; // 性别 int Age; // 年龄 string Adress; // 住址 string Major; // 专业 int index = 0; while (File >> Num && File >> Name && File && File >> Sex && File >> Age && File >> Adress && File >> Major) { Student* stu = NULL; stu = new Info(Num, Name, Sex, Age, Adress, Major); this->sStuArray[index] = stu; index++; } } // 展示学生信息 void studentManager::showStu() { if (this->isFileEmpty) { cout << "文件不存在或为空" << endl; } else { for (int i = 0; i < this->sCount; i++) { this->sStuArray[i]->showInfo(); } } system("pause"); system("cls"); } // 判断学号是否存在 存在则返回学生在数组中位置 不存在则返回-1 int studentManager::isStuExit(string num) { int index = -1; // 默认为-1 for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sNum == num) { index = i; break; } } return index; } // 删除学生 void studentManager::deletStu() { if (this->isFileEmpty) { // 文件不存在或无数据 cout << "文件不存在或为空" << endl; } else { // 文件有数据 cout << "请输入您想要删除的学生学号" << endl; string num; cin >> num; int index = this->isStuExit(num); // 判断该学号学生是否存在 if (index != -1) { // 学生存在 删除index位置的学生 for (int i = index; i < this->sCount - 1; i++) { // index后一个覆盖前一个依次类推 this->sStuArray[i] = this->sStuArray[i + 1]; } this->sCount--; // 更新学生人数 this->saveStu(); // 更新学生信息到文件 cout << "成功删除学号为" << num << "的学生" << endl; } else { // 学生不存在 cout << "删除失败 该学号学生不存在" << endl; } } system("pause"); system("cls"); } // 修改学生 void studentManager::editStu() { if (this->isFileEmpty) { // 文件不存在或无数据 cout << "文件不存在或为空" << endl; } else { // 有数据 cout << "请输入您要修改的学生学号" << endl; string num; cin >> num; int index = this->isStuExit(num); // 判断学生是否存在 if (index != -1) { // 学生存在 进行修改 string newNum; // 学号 string newName; // 姓名 string newSex; // 性别 int newAge; // 年龄 string newAdress; // 住址 string newMajor; // 专业 cout << "查找成功 请输入您要修改的学生信息" << endl; cout << "1 修改学号" << endl; cout << "2 修改姓名" << endl; cout << "3 修改性别" << endl; cout << "4 修改年龄" << endl; cout << "5 修改住址" << endl; cout << "6 修改专业" << endl; int choose; cin >> choose; switch(choose) { case 1: { cout << "请输入您要修改的新学号" << endl; cin >> newNum; int res = this->isStuExit(newNum); // 判断要修改的新学号是否存在 if (res == -1) { // 学号不存在 可以修改 this->sStuArray[index]->sNum = newNum; cout << "修改成功" << endl; break; } else { cout << "修改失败 该学号已存在" << endl; break; } } case 2: { cout << "请输入您要修改的新姓名" << endl; cin >> newName; this->sStuArray[index]->sName = newName; cout << "修改成功" << endl; break; } case 3: { cout << "请输入您要修改的新性别" << endl; cin >> newSex; this->sStuArray[index]->sSex = newSex; cout << "修改成功" << endl; break; } case 4: { cout << "请输入您要修改的新年龄" << endl; cin >> newAge; this->sStuArray[index]->sAge = newAge; cout << "修改成功" << endl; break; } case 5: { cout << "请输入您要修改的新住址" << endl; cin >> newAdress; this->sStuArray[index]->sAdress = newAdress; cout << "修改成功" << endl; break; } case 6: { cout << "请输入您要修改的新专业" << endl; cin >> newMajor; this->sStuArray[index]->sMajor = newMajor; cout << "修改成功" << endl; break; } default: // 输入其他数字-清空屏幕 system("cls"); break; } this->saveStu(); // 更新数组到文件中 } else { cout << "修改失败 该学号学生不存在" << endl; } } system("pause"); system("cls"); } // 查询学生 void studentManager::findStu() { if (this->isFileEmpty) { cout << "文件不存在或为空" << endl; } else { cout << "请输入您想要查找的方式" << endl; cout << "1 按学号查找" << endl; cout << "2 按姓名查找" << endl; cout << "3 按性别查找" << endl; cout << "4 按年龄查找" << endl; cout << "5 按住址查找" << endl; cout << "6 按专业查找" << endl; cout << "7 多条件查找" << endl; cout << "8 模糊查找" << endl; int choose; cin >> choose; if (choose == 1) { // 按学号查找 cout << "请输入您要查找的学号" << endl; string num; cin >> num; this->findNum(num); } else if (choose == 2) { // 按姓名查找 cout << "请输入您要查找的姓名" << endl; string name; cin >> name; this->findName(name); } else if (choose == 3) { // 按性别查找 cout << "请输入您要查找的性别" << endl; string sex; cin >> sex; this->findSex(sex); } else if (choose == 4) { // 按年龄查找 cout << "请输入您要查找的年龄" << endl; int age; cin >> age; this->findAge(age); } else if (choose == 5) { // 按住址查找 cout << "请输入您要查找的住址" << endl; string adress; cin >> adress; this->findAdress(adress); } else if (choose == 6) { // 按专业查找 cout << "请输入您要查找的专业" << endl; string major; cin >> major; this->findMajor(major); } else if (choose == 7) { // 多条件查找 cout << "请输入2-7的数字组合 如按姓名+性别查找 输入23" << endl; int choose; cin >> choose; if (choose == 23) { // 按姓名+性别查找 cout << "请输入您要查找的姓名" << endl; string name; cin >> name; cout << "请输入您要查找的性别" << endl; string sex; cin >> sex; int count = 0; for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sName == name && this->sStuArray[i]->sSex == sex) { this->sStuArray[i]->showInfo(); count++; } } cout << "符合条件的人数为" << count << endl; } if (choose == 345) { // 性别+年龄+住址 cout << "请输入您要查找的性别" << endl; string sex; cin >> sex; cout << "请输入您要查找的年龄" << endl; int age; cin >> age; cout << "请输入您要查找的住址" << endl; string adress; cin >> adress; int count = 0; for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sSex == sex && this->sStuArray[i]->sAge == age && this->sStuArray[i]->sAdress == adress) { this->sStuArray[i]->showInfo(); count++; } } cout << "符合条件的人数为" << count << endl; } } else if (choose == 8) { // 模糊查找 cout << "请输入您要查询的姓名中含的某字 如陈" << endl; char name; cin >> name; int count = 0; // 符合条件的人数 for (int i = 0; i < this->sCount; i++) { for (int j = 0; j < this->sStuArray[i]->sName.length(); j++) { if (this->sStuArray[i]->sName[j] == name) { this->sStuArray[i]->showInfo(); count++; } } } cout << "符合条件的人数为" << count << endl; } else { cout << "查询失败 您输入的数字有误" << endl; } } system("pause"); system("cls"); } // 1 按学号查找 void studentManager::findNum(string num) { int res = this->isStuExit(num); // 判断该学号学生是否存在 if (res != -1) { // 学生存在 this->sStuArray[res]->showInfo(); } else { cout << "查找失败 该学号学生不存在" << endl; } } // 2 按姓名查找 void studentManager::findName(string name) { int flag = false; // 是否查找成功的标志 int count = 0; // 符合条件的学生人数 for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sName == name) { this->sStuArray[i]->showInfo(); flag = true; count++; } } if (flag == true) { cout << "该姓名的学生人数为" << count << endl; } else if (flag == false) { cout << "查找失败 该姓名学生不存在" << endl; } } // 3 按性别查找 void studentManager::findSex(string sex) { int count = 0; // 符合条件的学生人数 if (sex == "男" || sex == "女") { for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sSex == sex) { this->sStuArray[i]->showInfo(); count++; } } cout << "该性别的学生人数为" << count << endl; } else { cout << "查找失败 性别只能输入男或女" << endl; } } // 4 按年龄查找 void studentManager::findAge(int age) { int flag = false; // 是否查找成功的标志 int count = 0; // 符合条件的学生人数 for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sAge == age) { this->sStuArray[i]->showInfo(); flag = true; count++; } } if (flag == true) { cout << "该年龄的学生人数为" << count << endl; } else if (flag == false) { cout << "查找失败 该年龄学生不存在" << endl; } } // 5 按住址查找 void studentManager::findAdress(string adress) { int flag = false; // 是否查找成功的标志 int count = 0; // 符合条件的学生人数 for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sAdress == adress) { this->sStuArray[i]->showInfo(); flag = true; count++; } } if (flag == true) { cout << "该住址的学生人数为" << count << endl; } else if (flag == false) { cout << "查找失败 该住址学生不存在" << endl; } } // 6 按专业查找 void studentManager::findMajor(string major) { int flag = false; // 是否查找成功的标志 int count = 0; // 符合条件的学生人数 for (int i = 0; i < this->sCount; i++) { if (this->sStuArray[i]->sMajor == major) { this->sStuArray[i]->showInfo(); flag = true; count++; } } if (flag == true) { cout << "该专业的学生人数为" << count << endl; } else if (flag == false) { cout << "查找失败 该专业学生不存在" << endl; } } // 按学号大小排序 void studentManager::sortStu() { for (int i = 0; i < this->sCount; i++) { // 按前4位数(年份)排序 string splitNum4i; // 要切割出来的学号的前4个数字 splitNum4i = getNchar(this->sStuArray[i]->sNum, 4); // 保存学号的前4个数字(年份) int min = i; // 目前最小值的下标 for (int j = i + 1; j < this->sCount; j++) { string splitNum4j; // 要切割出来的学号的前n个数字 splitNum4j = getNchar(this->sStuArray[j]->sNum, 4); // 保存学号的前4个数字(年份) if (splitNum4i > splitNum4j) { min = j; } } if (min != i) { Student* temp = this->sStuArray[i]; this->sStuArray[i] = this->sStuArray[min]; this->sStuArray[min] = temp; } // 按学号排序 int min2 = i; // 目前最小值的下标 for (int j = i + 1; j < this->sCount; j++) { if (this->sStuArray[i]->sNum > this->sStuArray[j]->sNum) { min2 = j; } } if (min2 != i) { Student* temp = this->sStuArray[i]; this->sStuArray[i] = this->sStuArray[min2]; this->sStuArray[min2] = temp; } } cout << "排序成功 最新的数据为" << endl; this->saveStu(); this->showStu(); system("pause"); system("cls"); } // 析构函数 studentManager::~studentManager() { // 手动释放堆区数据 if (this->sStuArray != NULL) { delete[] this->sStuArray; this->sStuArray = NULL; } } #pragma once #include <iostream> using namespace std; #include <string> class Student { public: virtual void showInfo() = 0; // 显示学生信息 string sNum; // 学号 string sName; // 姓名 string sSex; // 性别 int sAge; // 年龄 string sAdress; // 住址 string sMajor; // 专业 }; #pragma once #include <iostream> using namespace std; #include "student.h" #include "info.h" #include <string> #include <fstream> #include <ctype.h> #include <vector> class studentManager { public: studentManager(); // 构造函数 void showMenu(); // 展示菜单 void backsystem(); // 退出系统 void addStu(); // 添加学生 int sCount; // 原本学生人数 Student** sStuArray; // 记录学生的数组 string getNchar(string str, int n); // 返回输入的前n个字符 void saveStu(); // 保存学生 bool isFileEmpty; // 标志文件是否为空 int getCount(); // 获取学生人数 void initStu(); // 初始化学生数组 void showStu(); // 展示学生信息 int isStuExit(string num); // 根据学号判断该学生学生是否存在 void deletStu(); // 删除学生 void editStu(); // 修改学生 void findStu(); // 查找学生 void findNum(string num); // 1 按学号查找 void findName(string name); // 2 按姓名查找 void findSex(string sex); // 3 按性别查找 void findAge(int age); // 4 按年龄查找 void findAdress(string adress); // 5 按住址查找 void findMajor(string major); // 6 按专业查找 void sortStu(); // 排序 ~studentManager(); // 析构函数 }; #pragma once #include <iostream> using namespace std; #include "student.h" class Info: public Student { public: // 构造函数 Info(string num, string name, string sex, int age, string adress, string major); // 展示学生信息 virtual void showInfo(); };
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值