首页 > 作文

C++实现宿舍管理查询系统

更新时间:2023-04-06 04:21:28 阅读: 评论:0

本文实例为大家分享了c++实现宿舍管理查询系统的具体代码,供大家参考,具体内容如下

c++使用io流关联.txt文件

各模块之间的调用关系如下:

函数的调用关系反映了演示程序的层次结构:

代码如下:

#include<iostream>#include<fstream>#include<iomanip>#include<cstdlib>#include<string>using namespace std;#define maxsize 100   //顺序表的最大长度typedef struct {  string name;    //姓名  string id;     //学号  string dormid;   //宿舍号}student;typedef struct {  student r[maxsize + 1];   //r[0]做单元哨兵  int length;//长度}sqlist;//用直接插入排序存入到student.txt文件中void inrtsort(sqlist &stu){  int i, j;  for (i = 2; i <= stu.length; i++)    if (stu.r[i].id < stu.r[i - 1].id)    {      stu.r[0] = stu.r[i];      stu.r[i] = stu.r[i - 1];      for (j = i - 2; stu.r[0].id < stu.r[j].id; j--)        stu.r[j + 1] = stu.r[j];      stu.r[j + 1] = stu.r[0];    }  ofstream outfile("student.txt", ios::out);  if (!outfile) {               //如果文件打开失败    cout << "文件打开失败" << endl;    exit(1);  }  //outfile << "学号" << tw(8) << "姓名" << tw(8) << "宿舍号" << endl;  outfile << stu.length << endl;  for (i = 1; i <= stu.length; i++) {    outfile << stu.r[i].id << tw(8) << stu.r[i].name << tw(8) << stu.r[i].dormid << endl;  }  cout << "学生信息数:" << stu.length << endl;  outfile.clo();  cout << stu.length;}//创建学生信息(只能创建一次,不然会被刷新)void initlist(sqlist &stu){  int i;  cout << "学号" << tw(8) << "姓名" << tw(8) << "宿舍号" << endl;  for (i = 1; i <= stu.length; i++) {    cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;  }  inrtsort(stu);}//增加学生信息void addstudent(sqlist &stu){  int n;  int 强直性脊柱炎的护理i = stu.length + 1;  cout << "输入增加学生人数" << endl;  cin >> n;  cout << "学号" << tw(8) << "姓名" << tw(8) << "宿舍号" << endl;  stu.length = stu.length + n;  for (i; i <= stu.length; i++)    cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;  inrtsort(stu);}//查询学生信息void findstudent(sqlist &stu){  string a, b, c;  string name, id, dormid;  cout << "1.学号查询 2.姓名查询 3.宿舍号查询" << endl;  cout << "请输入你的查询选择(1~3)" << endl;  int i;  int n;  cin >> n;  if (n < 1 && n>3)  {    cout << "您输入有误,请重新输入:" << endl;    findstudent(stu);  }  if (1 == n)  {英语四级词组    cout << "请输入学生学号:" << endl;    cin >> id;    ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"    infile >> stu.length;    for (i = 1; i <= stu.length; i++) {      infile >> stu介入放射学.r[i].id >> stu.r[i].name >> stu.r[i].dormid;    }    infile.clo();    for (i = 1; i <= stu.length; i++)    {      infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;      if (stu.r[i].id == id)        cout << stu.r[i].id << tw(8学历填什么) << stu.r[i].name << tw(8) << stu.r[i].dormid << endl;    }    infile.clo();//关闭磁盘文件  }  if (2 == n)  {    cout << "请输入学生姓名:" << endl;    cin >> name;    ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"    infile >> stu.length;    for (i = 1; i <= stu.length; i++) {      infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;    }    infile.clo();    for (i = 1; i <= stu.length; i++)    {      infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;      if (stu.r[i].name == name)        cout << stu.r[i].id << tw(8) << stu.r[i].name << tw(8) << stu.r[i].dormid << endl;    }    infile.clo();//关闭磁盘文件  }  if (3 == n)  {    cout << "请输入学生宿舍号:" << endl;    cin >> dormid;    ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"    infile >> stu.length;    for (i = 1; i <= stu.length; i++) {      infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;    }    infile.clo();    for (i = 1; i <= stu.length; i++)    {      infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;      if (stu.r[i].dormid == dormid)        cout << stu.r[i].id << tw(8) << stu.r[i].name << tw(8) << stu.r[i].dormid << endl;    }  }}//修改学生信息void renewstudent(sqlist &stu){  int n;  string id, name, dormid;  cout << "1.姓名 2.宿舍号" << endl;  cout << "请输入您的选择(1~2):" << endl;  cin >> n;  cout << "请输入需要修改学生的学号" << endl;  cin >> id;  if (n != 1 && n != 2)  {    cout << "输入有误,请重新输入:" << endl;    renewstudent(stu);  }  if (1 == n)  {    cout << "请输入修改姓名" << endl;    cin >> name;    int i = 0;    ifstream infile("student.txt", ios::in);    infile >> stu.length;    for (i = 1; i <= stu.length; i++) {      infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;    }    infile.clo();    for (i = 1; i <= stu.length; i++)//先找到再修改    {      if (stu.r[i].id == id)      {        stu.r[i].name = name;        inrtsort(stu);        return;      }    }  }  if (2 == n)  {    int i;    cout << "请输入修改宿舍号" << endl;    cin >> dormid;    ifstream infile("student.txt", ios::in);    infile >> stu.length;    for (i = 1; i <= stu.length; i++) {      infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;    }    infile.clo();    for (i = 1; i <= stu.length; i++)//先找到再修改    {      if (stu.r[i].id == id)      {        stu.r[i].dormid = dormid;        inrtsort(stu);        return;      }    }  }}//显示宿舍信息void showstudent(sqlist &stu){  string a, b, c;  int i;  cout << "学生的信息如下:" << endl;  cout << "**********************************" << endl;  ifstream infile("student.txt", ios::in);  if (!infile) {               //如果文件打开失败    cout << "文件打开失败" << endl;    exit(1);  }  /*infile >> a >> b >> c;//从磁盘文件读入  cout << a << tw(8) << b << tw(8) << c << endl;//在显示器上显示*/  infile >> stu.length;  for (i = 1; i <= stu.length; i++) {    infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;    cout << stu.r[i].id << tw(8) << stu.r[i].name << tw(8) << stu.r[i].dormid << endl;  }  infile.clo();}//删除宿舍信息void deletstudent(sqlist &stu){  int i, j;  string a, b, c, id;  cout << "请输入删除学生学号" << endl;  cin >> id;  ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"  infile >> stu.length;  for (i = 1; i <= stu.length; i++) {    infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;  }  infile.clo();  for (i = 1; i <= stu.length; i++)//先找到再删除  {    if (stu.r[i].id == id)    {      for (j = i; j<stu.length; j++)        stu.r[j] = stu.r[j + 1];      stu.length--;      inrtsort(stu);      return;    }  }}//主函数int main(){  sqlist stu;  int n;  for (;;)  {    cout << "**************************宿舍管理查询软件***************于从政***********" << endl;    cout << "1. 创建学生信息" << endl;            //initlist    cout << "2. 增加学生信息" << endl;            //addstudent      cout << "3. 查询学生信息" << endl;            //findstudent    cout << "4. 显示学生信息" << endl;            //showstudent    cout << "5. 修改学生信息" << endl;            //renewstudent    cout << "6. 删除学生信息" << endl;            //deletstudent    cout << "0. 退出系统" << endl;    cout << "*******************************************************************" << endl;    cout << "请输入你需要的操作(0~6):" << endl;    cin >> n;    switch (n)    {    ca 1:      cout << "输入学生人数" << endl;      cin >> stu.length;      initlist(stu);      cout << "###########################################" << endl;      break;    ca 2:      addstudent(stu);      cout << "###########################################" << endl;      break;    ca 3:      findstudent(stu);      cout << "###########################################" << endl;      break;    ca 4:      showstudent(stu);      cout << "###########################################" << endl;      break;    ca 5:      renewstudent(stu);      cout << "###########################################" << endl;      break;    ca 6:      deletstudent(stu);      cout << "###########################################" << endl;      break;    ca 0:      cout << "您已退出系统!" << endl;      return 0;    }  }  system("pau");  return 0;}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-06 04:21:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/188e00d7e59c0324765ae6c44fe1d4ef.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C++实现宿舍管理查询系统.doc

本文 PDF 下载地址:C++实现宿舍管理查询系统.pdf

标签:文件   请输入   学生   学号
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图