zoukankan      html  css  js  c++  java
  • Windows下freopen函数使用与学生顺序表练习

    该工程分为四个文件

    main.cpp  主函数main所在文件,主要执行文件

    student.h 各种函数(接口)的声明

    student.cpp 各种函数(接口)的实现

    studentInfo.txt  学生信息存储文件

    1、freopen函数主要是重定向输入,

    freopen("studentInfo.txt","r",stdin);//打开文件输入,以标准输入方式输入,重定向到studentInfo.txt
    在C语言标准库中,并没有关闭freopen函数的方法,所以当要关闭时只能把它重定向到"CON"文件(DOS系统和Windows)

    freopen("CON","r",stdin); //关闭文件输入,重定向到控制台

    当出现freopen函数无法关闭的情况可能是,你定义一个指针,没有分配空间,就执行写入操作,这会导致freopen出错(其实也是本身代码块出错),

    2、主要代码

      main.cpp

    #include <iostream>
    #include "stdint.h"
    #include "student.h"
    #include "string.h"

    using namespace std;


    int main() {
    SqList L;
    int n, id;
    float grade;
    char name[10];
    Position position;
    Student *student = new Student;

    cout << "Input student number:";
    cin >> n;

    // 初始化
    init(L);

    // 对顺序表进行数据初始化
    initData(L, n); //以文件的方式传入学生信息
    // 显示顺序表中数据信息
    printList(L); //显示所有学生信息

    // 删除测试
    cout << "Delete test:input a student's position which is you want to delete:";
    cin >> position;
    if (deleteStudent(L, position) == OK) cout << "Delete student who position =" << position << " ";

    // 显示顺序表中数据信息
    printList(L); //显示所有学生信息

    //插入测试
    cout << "Input a id which is you want to insert student's id:";
    cin >> id;
    cout << "Input a grade which is you want to insert student's grade:";
    cin >> grade;
    cout << "Input a name which is you want to insert student's name:";
    cin >> name;
    cout << "Input a position which is you want to insert student position:";
    cin >> position;
    student->id = id;
    student->grade = grade;
    strcpy(student->name, name);
    if (insert(L, student, position) == OK) cout << "insert successful! ";
    else "This order table was full";
    // 显示顺序表中数据信息
    printList(L); //显示所有学生信息


    // 查找测试
    cout << "Input a id which is you want to seek out student's name:";
    cin >> name;
    student = seek(L, name);
    if (student == NULL) cout << "Can't find student who is student's name=" << name << " ";
    else
    cout << "Found the student: " << "student's id:" << student->id << " " << "student's grade:" << student->grade
    << " " << "student's name:" << student->name << " ";

    // 返回顺序表长度
    cout << "The student's number is equal to:" << studentNum(L) << " ";


    // 添加元素测试
    Student *student1 = new Student;
    cout << "Input a id which is you want to append student's id:";
    cin >> id;
    cout << "Input a grade which is you want to append student's grade:";
    cin >> grade;
    cout << "Input a name which is you want to append student's name:";
    cin >> name;
    student1->id = id;
    student1->grade = grade;
    strcpy(student1->name, name);
    if (append(L, student1) == OK) cout << "append student successful ";
    else "This order table was full ";
    // 显示顺序表中数据信息
    printList(L); //显示所有学生信息

    // 返回顺序表长度
    cout << "The student's number is equal to:" << studentNum(L);
    delete student1;
    delete student;
    }

      student.h
    #define MAXSIZE 10
    enum Status{
    OK,
    ERROR
    };
    typedef int Position;
    typedef struct {
    int id;
    char name[10];
    float grade;
    } Student;

    typedef struct {
    Student *student;
    int length;
    } SqList;
    /**
    * This function is called only at the beginning of the project;
    * @param L 传入顺序表的引用;
    * @return 返回是否成功创建一个空顺序表 return value: enum Status{OK,ERROR};
    */
    Status init(SqList &L);

    /**
    * This function is only used when the element is not full;
    * @param L 传入一个顺序表
    * @param student 传入一个student指针,该指针所指位置分配了空间存储操作的数据
    * @return 返回是否成功添加 return value: enum Status{OK,ERROR};
    */
    Status append(SqList &L,Student* student);

    /**
    * This function prints the data passed in to the sequence table;
    * @param L 传入一个顺序表
    */
    void printList(SqList &L);

    /**
    * This function is used to insert data into the order table;
    * @param L 传入一个顺序表
    * @param student 传入一个student指针,该指针所指位置分配了空间存储操作的数据
    * @param position 传入要操作的位置,注意该位置是从1开始,即student[0] belong to position one;
    * @return 返回是否成功插入 return value: enum Status{OK,ERROR};
    */
    Status insert(SqList &L,Student* student,Position position);

    /**
    *This function is used to delete the data with the specified position in the order tables;
    * @param L 传入一个顺序表
    * @param id 传入一个position 按照position delete student;
    * @return 返回是否成功删除 return value: enum Status{OK,ERROR};
    */
    Status deleteStudent(SqList &L,Position position);

    /**
    * This function is used to seek the data with specified id in the order tables;
    * @param L 传入一个顺序表
    * @param id 传入一个student's name 按照name查询数据
    * @return 返回一个student指针,该指针指向的内存分配了空间,存储返回的数据;
    */
    Student* seek(SqList &L,char name[10]);
    /**
    * This function is used to init order table;
    * @param L 传入一个顺序表
    * @param n 传入一个number,指代要录入的学生个数
    * @return 返回是否成功初始化数据 return value: enum Status{OK,ERROR};
    */
    Status initData( SqList &L,int n);
    /**
    * This function is used to return order table's length;
    * @param L 传入顺序表
    * @return 返回学生个数
    */
    int studentNum(SqList &L);


    student.cpp
    #include "student.h"
    #include "string.h"
    #include <iostream>
    using namespace std;

    Status init(SqList &L) {
    L.student = new Student[MAXSIZE];
    if (!L.student) return ERROR;
    L.length = 0;
    return OK;
    }

    Status append(SqList &L, Student *student) {
    if (student == NULL) return ERROR;
    if (L.length == MAXSIZE) return ERROR;
    L.student[L.length].id = student->id;
    L.student[L.length].grade = student->grade;
    strcpy(L.student[L.length].name, student->name);
    L.length++;
    return OK;
    }

    void printList(SqList &L) {
    for (int i = 0; i < L.length; ++i) {
    cout << "id:" << L.student[i].id << " grade:" << L.student[i].grade << " name:" << L.student[i].name << " ";
    }
    }

    Status insert(SqList &L, Student *student, Position position) {
    int i = L.length;
    if (student == NULL) return ERROR;
    if (L.length == MAXSIZE) return ERROR;
    if (position > L.length || position < 0) return ERROR;
    while (position <= i) {
    L.student[i] = L.student[i - 1];
    i--;
    }
    L.student[position - 1].id = student->id;
    L.student[position - 1].grade = student->grade;
    strcpy(L.student[position - 1].name, student->name);
    L.length++;
    return OK;
    }

    Status deleteStudent(SqList &L, Position position) {
    if (position<=0||position>L.length) return ERROR;
    while (position<L.length){
    L.student[position-1] = L.student[position];
    position++;
    }
    L.length--;
    return OK;
    }

    Student* seek(SqList &L, char name[10]) {
    Student *student = new Student;
    for (int i = 0; i < L.length; ++i) {
    if (strcmp(L.student[i].name,name)==0) {
    student->id = L.student[i].id;
    student->grade = L.student[i].grade;
    strcpy(student->name, L.student[i].name);
    return student;
    }
    }
    return NULL;
    }

    Status initData(SqList &L,int n){
    Student *student = new Student;
    int id;
    float grade;
    char name[10];
    freopen("studentInfo.txt","r",stdin);//打开文件输入
    for (int i = 0; i < n; ++i) {
    cout << "Input student's id:";
    cin >> id;
    cout << "Input student's grade:";
    cin >> grade;
    cout << "Input student's name:";
    cin >> name;
    cout << " ";
    student->id = id;
    student->grade = grade;
    strcpy(student->name, name);
    if (append(L, student) == ERROR) {
    cout << i;
    break;
    }
    }
    freopen("CON","r",stdin); //关闭文件输入
    return OK;
    }

    int studentNum(SqList &L){
    return L.length;
    }


    student.txt
    1 56.5 tom
    2 67.4 yom
    3 98.5 uom
    4 89.5 iom
    5 90.5 oom
    6 99.5 pom
    7 34.7 gom
    8 65.2 fom
    9 112.5 nom
    10 67.8 mom



  • 相关阅读:
    LUA之面向对象
    LUA笔记之表
    LUA笔记之字符串
    STM32模拟I2C
    php(1)-php5.6启动命令
    ip地址变更对tomcat和nginx的影响
    解决 nginx: [alert] kill(189, 1) failed (3: No such process)
    linux(16)-yum安装提示“没有可用软件包”
    性能测试监控分析(17) 负载和CPU使用率低高负载的原因
    Codeforces Round #588 (Div. 2)C(思维,暴力)
  • 原文地址:https://www.cnblogs.com/youlingdada-top/p/13885567.html
Copyright © 2011-2022 走看看