首页 技术 正文
技术 2022年11月12日
0 收藏 325 点赞 3,634 浏览 3483 个字
这是一个简易的学籍管理系统,大一时居然三个人写了一千多行......年少无知啊!欢迎摘果实!  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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297