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

本文实例为大家分享了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 InsertSort(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 << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    outfile << stu.length << endl;

    for (i = 1; i <= stu.length; i++) {
        outfile << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
    }
    cout << "学生信息数:" << stu.length << endl;
    outfile.close();
    cout << stu.length;
}

//创建学生信息(只能创建一次,不然会被刷新)
void InitList(SqList &stu)
{

    int i;
    cout << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    for (i = 1; i <= stu.length; i++) {
        cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
    }
    InsertSort(stu);
}

//增加学生信息
void Addstudent(SqList &stu)
{
    int n;
    int i = stu.length + 1;
    cout << "输入增加学生人数" << endl;
    cin >> n;
    cout << "学号" << setw(8) << "姓名" << setw(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;
    InsertSort(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.close();
        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 << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }

        infile.close();//关闭磁盘文件

    }
    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.close();
        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 << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }

        infile.close();//关闭磁盘文件
    }
    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.close();
        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 << setw(8) << stu.r[i].name << setw(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.close();
        for (i = 1; i <= stu.length; i++)//先找到再修改
        {
            if (stu.r[i].id == id)
            {
                stu.r[i].name = name;
                InsertSort(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.close();
        for (i = 1; i <= stu.length; i++)//先找到再修改
        {
            if (stu.r[i].id == id)
            {
                stu.r[i].dormid = dormid;
                InsertSort(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 << setw(8) << b << setw(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 << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
    }
    infile.close();
}

//删除宿舍信息
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.close();
    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--;
            InsertSort(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)
        {
        case 1:
            cout << "输入学生人数" << endl;
            cin >> stu.length;
            InitList(stu);
            cout << "###########################################" << endl;
            break;
        case 2:
            Addstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 3:
            Findstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 4:
            Showstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 5:
            Renewstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 6:
            Deletstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 0:
            cout << "您已退出系统!" << endl;
            return 0;
        }

    }
    system("pause");
    return 0;
}

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

(0)

相关推荐

  • C++实现学生宿舍管理系统

    本文实例为大家分享了C++实现学生宿舍管理系统的具体代码,供大家参考,具体内容如下 非常简易,完成个作业够用,仅供初学者参考,不喜勿喷. #include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> /*先用结构体定义学生信息*/ struct stud  {     int   num;            //学号      char  name[10];     

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

    本文实例为大家分享了C++实现宿舍管理查询系统的具体代码,供大家参考,具体内容如下 C++使用IO流关联.txt文件 各模块之间的调用关系如下: 函数的调用关系反映了演示程序的层次结构: 代码如下: #include<iostream> #include<fstream> #include<iomanip> #include<cstdlib> #include<string> using namespace std; #define MAXSIZ

  • C语言实现宿舍管理系统设计

    本文实例为大家分享了C语言实现宿舍管理系统的具体代码,供大家参考,具体内容如下 设计目的 <数据结构>课程主要介绍最常用的数据结构,进行数据结构课程设计要达到以下目的: (1)了解并掌握数据结构与算法的设计方法,具备初步的独立分析和设计能力:(2)初步掌握软件开发过程的问题分析.系统设计.程序编码.测试等基本方法和技能:(3)提高综合运用所学的理论知识和方法独立分析和解决问题的能力:(4)训练用系统的观点和软件开发一般规范进行软件开发,培养软件工作者所应具备的科学的工作方法和作风. 任务概述

  • 利用Vue.js框架实现火车票查询系统(附源码)

    前言 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得我们能够快速地上手并使用Vue.js 今天,我就要基于这个库来搭建一个火车票查询系统.首先我们的机器上得有NodeJS环境,并且安装了npm包管理工具.因为vue是跑在node环境下的,并且我们需要用npm来安装vue. 在终端输入npm install --global vue-cli我们来全局安装vue-cli

  • 基于Python制作公交车站查询系统

    目录 一.设计目的 1.教学目的 2.教学要求 二.需求分析 1.问题 2.系统 3.运行要求 三.系统模块设计 四.详细设计 五.需要设计的函数 六.Python源码 七.运行效果 一.设计目的 1.教学目的 本课程设计是学生学习完<Python程序设计>课程后,进行的一次全面的综合训练,通过课程设计,更好地掌握使用Python语言进行程序设计的方法,加深对Python语言特点和使用Python语言进行程序设计开发过程的理解,加强动手能力.其主要目的是: (1)进一步培养学生Python程序

  • C语言实现宿舍管理课程设计

    本文实例为大家分享了C语言实现宿舍管理的具体代码,供大家参考,具体内容如下 和本编前几个程序结构差不多,比较简单直观,希望可以给你带来帮助. #include <stdio.h> #include <stdlib.h> #include <string.h> #include<Windows.h> typedef struct student {     int might1;     int might2;     char user[20];//账号  

  • 一个简单的java学生寝室查询系统

    本文实例为大家分享了java学生寝室查询系统的具体代码,供大家参考,具体内容如下 前端部分: index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AHPU Freshman dormitory inquiry</title> <script src="confirm.js

  • Python脚本实现12306火车票查询系统

    最近我看到看到使用python实现火车票查询,我自己也实现了,感觉收获蛮多的,下面我就把每一步骤都详细给分享出来.(注意使用的是python3) 首先我将最终结果给展示出来: 在cmd命令行执行:python tickets.py -dk shanghai chengdu 20161007 > result.txt 意思是:查询 上海--成都 2016.10.07 的D和K开头的列车信息,并保存到 result.txt文件中:下面就是result.txt文件中的结果: 下面的将是实现步骤: 1.

  • asp空间奸商查询系统

    看到很多朋友无缘无故的被骗,特发布此查询系统,以免再次上当! 使用方法非常简单: 直接输入对方的域名或者网站名称,支持中英文!点查询即可 复制代码 代码如下: <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <% On Error Resume Next ff="垃圾,是垃圾,骗子,无耻,不要买,骗钱,垃圾空间,垃圾中的极品,垃圾中的垃圾,经常掉线,无缘无故的关闭" if Request.Form

  • IP查询系统的异步回调案例

    话不多说,请看代码: package com.lxj.demo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; public class Http extends Thread{ // 下载结束的回调接口 public interface

  • vue实现后台管理权限系统及顶栏三级菜单显示功能

    •效果演示地址 项目demo展示 重要功能总结 权限功能的实现 权限路由思路: 根据用户登录的roles信息与路由中配置的roles信息进行比较过滤,生成可以访问的路由表,并通过router.addRoutes(store.getters.addRouters)动态添加可访问权限路由表,从而实现左侧和顶栏菜单的展示. 实现步骤: 1.在router/index.js中,给相应的菜单设置默认的roles信息: 如下:给"权限设置"菜单设置的权限为:meta:{roles: ['admin

随机推荐