这是一个简易的学籍管理系统,大一时居然三个人写了一千多行......年少无知啊!欢迎摘果实!

  1 #include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std; /*
*信息结构
*/
/*struct stu
{
string id;
string name;
string gender;
int age;
string magor;
string prize;
};*/ bool isFind = false;//是否找到的标识 void find(const char *file, const int id);
void del(const char *file, const int id);
void change(const char *file, const int id); void add(const char *file)
{
ofstream out;
out.open(file, ios::app);
if (!out.is_open())
{
cout << "文件打开失败" << endl;
return;
}
else
{
int id, age;
string name, major, prize, gender;
cout << "输入学号、姓名、性别、年龄、专业、奖项:" << endl;
cin >> id >> name >> gender >> age >> major >> prize;
find(file, id);
if(!isFind)
out << id << "\n" << name << "\n" << gender << "\n" << age << "\n" << major << "\n" << prize << "\n";
else
{
cout << "该学号已存在..." << endl;
return;
}
cout << "添加成功" << endl;
out.close();
}
} void find(const char *file, const int id)
{
ifstream in;
in.open(file, ios::in | ios::binary);
if (!in.is_open())
{
cout << "打开文件错误" << endl;
return ;
}
while(in.peek() != EOF)
{
int temp;
stringstream s;
string line;
getline(in, line);//读字符串
s << line;
s >> temp;
if (temp == id)
{
isFind = true;//找到
int age;
string name, major, prize, gender;
cout << "找到记录:"<< endl;
getline(in, line);//读名字
s << line; s >> name;
getline(in, line);//读性别
s << line; s >> gender;
getline(in, line);//读年龄
s << line; s >> age;
getline(in, line);//读专业
s << line; s >> major;
getline(in, line);//读奖项
s << line; s >> prize; cout << "学号:" << temp << " ";
cout << "姓名:" << name << " ";
cout << "性别:" << gender << " ";
cout << "年龄:" << age << " ";
cout << "专业:" << major << endl;
cout << "奖项:" << prize << endl;
}
}
in.close();
} void del(const char *file, const int id)
{
isFind = false;
find(file, id);//找到要删的位置
if(isFind)
cout << "正在删除..." << endl;
else
{
cout << "无此纪录!" << endl;
isFind = false;
return;
}
ifstream in;
in.open(file, ios::in | ios::binary);
if (!in.is_open())
{
cout << "打开文件错误" << endl;
return;
}
string tempStr;
while (in.peek() != EOF)
{
int temp;
string line;
getline(in, line);//读字符串
stringstream s;
s << line;
s >> temp;
if (temp == id)
{
int delLine = ;
while (delLine--)
getline(in, line);
}
else
{
//getline(in, line);//读字符串
tempStr += line;
tempStr += "\n";
} }
in.close();
//重新写入文件
ofstream out;
out.open(file, ios::out);
if (!out.is_open())
{
cout << "打开文件错误,删除失败!" << endl;
return;
}
else
{
out << tempStr;//重新写入
cout << "删除完成!" << endl;
}
out.close();
} void change(const char *file, const int id)
{
isFind = false;
find(file, id);//找到要改的目标
if (isFind)
{
int age;
string name, major, prize, gender;
cout << "输入新的姓名、性别、年龄、专业、奖项:" << endl;
cin >> name >> gender >> age >> major >> prize;
ifstream in;
in.open(file, ios::in | ios::binary);
if (!in.is_open())
{
cout << "文件打开失败!" << endl;
return;
}
string tempStr;
while (in.peek() != EOF)
{
int temp;
string line;
stringstream s;
getline(in, line);
s << line;
s >> temp;
if (temp == id)
{
tempStr += to_string(id) + "\n";
tempStr += name + "\n";
tempStr += gender + "\n";
tempStr += to_string(age) + "\n";
tempStr += major + "\n";
tempStr += prize + "\n";//加入新信息
int delLine = ;
while (delLine--)
getline(in, line);//跳过旧信息
}
else
{
tempStr += line;
tempStr += "\n";
}
}
in.close();//别忘记 //重新写入文件
ofstream out;
out.open(file, ios::out);
if (!out.is_open())
{
cout << "打开文件错误,删除失败!" << endl;
return;
}
else
{
out << tempStr;//重新写入
cout << "修改完成!" << endl;
}
out.close();
}
} int main()
{
const char *file = "D:\\homework\\DB\\test.txt";//文件地址
while (true)
{
int ans;
cout << "--------------------------------" << endl;
cout << "--> 1.插入信息 <--" << endl;
cout << "--> 2.查找信息 <--" << endl;
cout << "--> 3.删除信息 <--" << endl;
cout << "--> 4.修改信息 <--" << endl;
cout << "--> 0.退出Demo <--" << endl;
cout << "--------------------------------" << endl;
cout << "输入指令~$ ";
cin >> ans;
switch (ans)
{
case :
{
cout << "已退出!" << endl;
return ;
}
break;
case :
{
add(file);
}
break;
case :
{
cout << "输入要查找的学号:";
int id;
cin >> id;
find(file, id);
}
break;
case :
{
cout << "输入要删除的学生学号:";
int id;
cin >> id;
del(file, id);
}
break;
case :
{
cout << "输入要修改的学生学号:";
int id;
cin >> id;
change(file, id);
}
default:
{
cout << "输入有误!" << endl;
}
break;
}
} return ;
}
04-16 07:11
查看更多