C++ 学生数据管理

C++ 学生数据管理

教育机构使用一个名为学生管理系统的简单C++应用程序来处理与学生相关的数据以及其他学生在学校中的数据需求。用户可以在这个学生管理系统项目中创建、查看和编辑内容。

如今,数据库在我们生活的各个方面都被使用。全球各地的服务器包含着万亿字节的数据。其中一个使用这种数据库的最简单方式是使用SQL语言。但是,你是否曾考虑过使用C++来管理这样的数据库?在本文中,我们将讨论如何根据用户类型创建基于文本文件的多个视图并相应地进行调整。

此代码存储了以下信息:

  1. 姓名和注册号码
  2. CSE10001科目的成绩
  3. CSE10002科目的成绩
  4. 导师ID

程序分解

# include < fstream >
# include < iostream >
# include < stdio.h >
# include < string.h >
using namespace std ; 

我们在这个程序中包含了所有必要的库,以避免任何类型的编译错误。

int main ( )
{
    char data [ 15 ] ;
    int n = 0 , option = 0 , count_n = 0 ;
    // This is the initial mark alloted to a subject.
    string empty = "00" ;
    string proctor = "" ;
    ifstream f ( "Example.txt" ) ;
    string line ; 

我们考虑输入数据的最大长度为15。并且我们将访问文件”Example.txt”,其中的数据库是排序过的。

for (int i = 0 ; std :: getline ( f , line ) ; + + i ) {
        count_n + + ;
    }

上述 for 循环用于计算文件中的行数。

    while ( option ! = 6 ) {
        // This prints out all the available options in the
        // DB
        cout << " \ n Available operations: \ n1. Add New "
                "Students \ n2 . "
            << "Student Login \ n3 . Faculty Login \ n4 . "
                "Proctor Login \ n5 . Admin View \ n "
            << "6. Exit\nEnter option: " ;
        cin >> option ;

在上面的while循环中,只有当选择的选项不等于6时才会执行,并逐行打印可用操作,添加新学生、学生登录、教职工登录、监考员登录、管理员查看、第6个选项为退出并输入选项。

if ( option == 1 ) {
            cout << "Enter the number of students: " ; 
            cin >> n ; 
            count_n = count_n + n ; 
            for ( int i = 0 ; i < n ; i + + ) {
                ofstream outfile ;
                outfile.open ( "Example.txt" , ios :: app ) ;
                // The entire data of a single student is
                // stored line-by-line.
                cout << "Enter your registration number: " ;
                cin >> data ;
                outfile << data << " \ t " ;
                cout << "Enter your name: " ;
                cin >> data ;
                int len = strlen ( data ) ; 
                while ( len < 15 ) {
                    data [ len ] = ' ' ;
                    len = len + 1 ;
                }

上述代码仅在用户选择1作为选项时执行。在此过程中,它会要求用户输入学生的数量。学生的全部数据将逐行存储,然后要求输入注册号和姓名。

else if ( option = = 2 ) {
            char regno [ 9 ] ;
            cout << "Enter your registration number: " ;
            cin >> regno ;
            ifstream infile ;
            int check = 0 ;
            infile.open ( "Example.txt" , ios :: in ) ;

            // This loop prints out the data according to
            // the registration number specified.
            while ( infile > > data ) {
                if (strcmp ( data , regno ) = = 0 ) {
                    cout
                        << " \ n Registration Number: " << data
                        << endl ;
                    infile >> data ;
                    cout << "Name: " << data << endl ;

                    infile >> data ;
                    cout << "CSE10001 mark: " << data
                        << endl ;

                    infile >> data ;
                    cout << "CSE10002 mark: " << data
                        << endl ;

                    infile >> data ;
                    cout << "Proctor ID: " << data << endl ;

                    infile.close ( ) ;
                    check = 1 ;
                }
            }

在上述代码示例中,当用户选择第二个选项2时,else if循环将被执行。注册号码有9个字符的大小,要求用户输入注册号码,然后显示注册号码。接下来,它会要求输入科目的分数。

if ( check = = 0 ) {
                cout << "No such registration number found!"
                    << endl ;
            }
        }

如果用户选择了0作为选项,它将显示“没有找到该注册号码”。

else if ( option == 3 ) {
            char subcode [ 7 ] ;
            cout << "Enter your subject code: ";
            cin >> subcode ;
            string code1 = "CSE10001" ,  code2 =  "CSE10002" ,
                mark = "" ;
            ifstream infile ;
            int check = 0 ;

            cout << " \ n Available operations: \ n1 . Add data "
                    "about marks \ n "
                << "2. View data\nEnter option: " ;
            cin >> option ;

以上的 if 循环只有在用户选择了第三个选项时才会执行。在这种情况下,用户将被要求输入科目代码、可用操作以及查看数据。基本上,这个循环用于查看或添加学生数据库的成绩。

if ( option = = 1 ) {
                cout
                    << "Warning! You would need to add mark"
                    << "details for all the students!"
                    << endl ;
                for ( int i = 0 ; i < count_n ; i + + ) {
                    fstream file( "Example.txt" ) ;

                    // The seek in file has been done
                    // according to the length
                    // of the data being inserted. It needs
                    // to adjusted accordingly for different
                    // lengths of data.
                    if ( strcmp ( subcode , code1.c_str ( ) )
                        == 0 ) {
                        file.seekp ( 26 + 37 * i ,
                                std : : ios_base : : beg ) ;
                        cout << " Enter the mark of student# "
                            << ( i + 1 ) << " : " ;
                        cin >> mark ;
                        file.write ( mark.c_str ( ) , 2 ) ;
                    }

在上面的代码中,如果用户选择了选项1,它将显示一条消息,警告您需要添加分数;文件中的搜索操作是根据插入数据的长度完成的。需要根据不同数据的长度进行相应的调整。

        else if ( option == 2 ) {
                infile.open ( " Example.txt " , ios : : in ) ;
                if ( strcmp ( subcode , code1.c_str ( ) ) == 0 ) {
                    cout << " Registration number - Marks \ n "
                        << endl ;
                    while ( infile >> data ) {
                        cout << data ;
                        infile >> data ;
                        infile >> data;
                        cout << " - " << data << endl ;
                        infile >> data ;
                        infile >> data ;
                        check = 1 ;
                    }
                }
                infile.close ( ) ;
                infile.open ( " Example.txt " , ios : : in ) ;
                if ( strcmp ( subcode , code2.c_str ( ) ) = = 0 ) {
                    cout << " Registration number - Marks \ n"
                        << endl ;
                    while ( infile >> data ) {
                        cout << data ;
                        infile >> data ;
                        infile >> data ;
                        infile >> data ;
                        cout << " - " << data << endl ;
                        infile >> data ;
                        check = 1 ;
                    }
                }
            }   infile.close ( ) ;

            if ( check == 0 ) {
                cout << " No such subject code found! "
                    << endl ;
            }
        }

在上述代码中,else循环只有在用户选择了选项2时才会执行。该循环用于查看学生的分数。额外的infile命令被用来仅获取特定的分数,因为数据是通过制表符分隔的。

    else if ( option = = 4 ) {
            char procid [ 7 ] ;
            cout << " Enter your proctor ID : " ;
            cin >> procid ;
            int check = 0 ;
            char temp1 [ 100 ] , temp2 [ 100 ] , temp3 [ 100 ] ;
            char temp4 [ 100 ] , id [ 100 ] ;
            ifstream infile ;
            infile.open ( " Example.txt " , ios : : in ) ;

            while ( infile >> temp1 ) {
                infile >> temp2 ;
                infile >> temp3 ;
                infile >> temp4 ;
                infile >> id ;

                if ( strcmp ( id , procid ) = = 0 ) {
                    cout << " \ n Registration Number: "
                        << temp1 << endl ;
                    cout << " Name : " << temp2 << endl ;
                    cout << " CSE1001 Mark : " << temp3
                        << endl ;
                    cout << " CSE1002 Mark : " << temp4
                        << endl ;
                    check = 1 ;
                }
            }

            if ( check == 0 ) {
                cout << " No such proctor ID found! " << endl ;
            }
        }

在上面的代码中,循环显示了具有相同监督员ID的所有学生的详细信息。

// This loop acts as an admin view to see all the
        // data in the file. 
        else if ( option = = 5 ) {
            char password [ 25 ] ;
            cout << " Enter the admin password : " ;
            cin >> password ;

            // This variable value can be changed according
            // to your requirement of the administrator
            // password.

            string admin_pass = " admin " ;

            if ( strcmp ( password , admin_pass.c_str ( ) ) = = 0 ) {
                cout << " Reg No.    "
                        " \ t Name \ t CSE10001 \ t CSE10002 \ t Proctor "
                        " ID "
                    << endl ;
                ifstream infile ;
                infile.open ( " Example.txt " , ios : : in ) ;
                char data [ 20 ] ; 
                while ( infile >> data ) {
                    cout << data << " \ t " ;
                    infile >> data ;
                    cout << data << " \ t " ;
                    infile >> data ;
                    cout << data << " \ t " ;
                    infile >> data ;
                    cout << data << " \ t " ;
                    infile >> data ;
                    cout << data << endl ;
                }
            }
        }
    }
}

此循环充当管理员查看文件中所有数据的视图。根据管理员密码的要求,可以更改此变量的值。

C++学生数据库管理程序

// Include all the necessary libraries.
#include 
#include 
#include 
#include 
using namespace std;
int main()
{
    // Considering the max length of data entered (name) to
    // be 15.
    char data[15];
    int n = 0, option = 0, count_n = 0;
    // This is the initial mark alloted to a subject.
    string empty = "00";
    string proctor = "";
    // Name of the file in which DB is stored.
    ifstream f("Example.txt");
    string line;

    // The following for loop counts the total number of
    // lines in the file.
    for (int i = 0; std::getline(f, line); ++i)
    {
        count_n++;
    }

    while (option != 6)
    {
        // This prints out all the available options in the
        // DB
        cout << "\nAvailable operations: \n1. Add New "
                "Students\n2."
             << "Student Login\n3. Faculty Login\n4. "
                "Proctor Login\n5. Admin View\n"
             << "6. Exit\nEnter option: ";
        cin >> option;

        if (option == 1)
        {
            cout << "Enter the number of students: ";
            cin >> n;

            count_n = count_n + n;

            for (int i = 0; i < n; i++)
            {
                ofstream outfile;
                outfile.open("Example.txt", ios::app);
                // The entire data of a single student is
                // stored line-by-line.
                cout << "Enter your registration number: ";
                cin >> data;
                outfile << data << "\t";

                cout << "Enter your name: ";
                cin >> data;
                int len = strlen(data);

                while (len < 15)
                {
                    data[len] = ' ';
                    len = len + 1;
                }
                outfile << data << "\t";
                // Inserting empty data initially into the
                // file
                outfile << empty << "\t";
                outfile << empty << "\t";

                cout << "Enter your proctor ID: ";
                cin >> proctor;

                outfile << proctor << endl;
            }
        }

        else if (option == 2)
        {
            char regno[9];
            cout << "Enter your registration number: ";
            cin >> regno;
            ifstream infile;
            int check = 0;
            infile.open("Example.txt", ios::in);

            // This loop prints out the data according to
            // the registration number specified.
            while (infile >> data)
            {
                if (strcmp(data, regno) == 0)
                {
                    cout
                        << "\nRegistration Number: " << data
                        << endl;
                    infile >> data;
                    cout << "Name: " << data << endl;

                    infile >> data;
                    cout << "CSE1001 mark: " << data
                         << endl;

                    infile >> data;
                    cout << "CSE1002 mark: " << data
                         << endl;

                    infile >> data;
                    cout << "Proctor ID: " << data << endl;

                    infile.close();
                    check = 1;
                }
            }

            if (check == 0)
            {
                cout << "No such registration number found!"
                     << endl;
            }
        }

        // This loop is used to view and add marks to the
        // database of a student.
        else if (option == 3)
        {
            char subcode[7];
            cout << "Enter your subject code: ";
            cin >> subcode;
            string code1 = "CSE1001", code2 = "CSE1002",
                   mark = "";
            ifstream infile;
            int check = 0;

            cout << "\nAvailable operations: \n1. Add data "
                    "about marks\n"
                 << "2. View data\nEnter option: ";
            cin >> option;

            if (option == 1)
            {
                cout
                    << "Warning! You would need to add mark"
                    << "details for all the students!"
                    << endl;
                for (int i = 0; i < count_n; i++)
                {
                    fstream file("Example.txt");

                    // The seek in file has been done
                    // according to the length
                    // of the data being inserted. It needs
                    // to adjusted accordingly for different
                    // lengths of data.

                    if (strcmp(subcode, code1.c_str()) == 0)
                    {
                        file.seekp(26 + 37 * i,
                                   std::ios_base::beg);
                        cout << "Enter the mark of student#"
                             << (i + 1) << " : ";
                        cin >> mark;
                        file.write(mark.c_str(), 2);
                    }

                    if (strcmp(subcode, code2.c_str()) == 0)
                    {
                        file.seekp(29 + 37 * i,
                                   std::ios_base::beg);
                        cout << "Enter the mark of student#"
                             << (i + 1) << " : ";
                        cin >> mark;
                        file.write(mark.c_str(), 2);
                    }
                }
            }

            // This loop is used to view marks of a student.
            // The extra infile commands have been used to
            // get a specific mark only since the data has
            // been separated by a tabspace.

            else if (option == 2)
            {
                infile.open("Example.txt", ios::in);
                if (strcmp(subcode, code1.c_str()) == 0)
                {
                    cout << "Registration number - Marks\n"
                         << endl;
                    while (infile >> data)
                    {
                        cout << data;
                        infile >> data;
                        infile >> data;
                        cout << " - " << data << endl;
                        infile >> data;
                        infile >> data;
                        check = 1;
                    }
                }

                infile.close();
                infile.open("Example.txt", ios::in);

                if (strcmp(subcode, code2.c_str()) == 0)
                {
                    cout << "Registration number - Marks\n"
                         << endl;
                    while (infile >> data)
                    {
                        cout << data;
                        infile >> data;
                        infile >> data;
                        infile >> data;
                        cout << " - " << data << endl;
                        infile >> data;
                        check = 1;
                    }
                }
            }

            infile.close();

            if (check == 0)
            {
                cout << "No such subject code found!"
                     << endl;
            }
        }

        // This loop displays all the details of students
        // under the same proctor ID.

        else if (option == 4)
        {
            char procid[7];
            cout << "Enter your proctor ID: ";
            cin >> procid;
            int check = 0;
            char temp1[100], temp2[100], temp3[100];
            char temp4[100], id[100];
            ifstream infile;
            infile.open("Example.txt", ios::in);

            while (infile >> temp1)
            {
                infile >> temp2;
                infile >> temp3;
                infile >> temp4;
                infile >> id;

                if (strcmp(id, procid) == 0)
                {
                    cout << "\nRegistration Number: "
                         << temp1 << endl;
                    cout << "Name: " << temp2 << endl;
                    cout << "CSE1001 Mark: " << temp3
                         << endl;
                    cout << "CSE1002 Mark: " << temp4
                         << endl;
                    check = 1;
                }
            }

            if (check == 0)
            {
                cout << "No such proctor ID found!" << endl;
            }
        }

        // This loop acts as an admin view to see all the
        // data in the file.

        else if (option == 5)
        {
            char password[25];
            cout << "Enter the admin password: ";
            cin >> password;

            // This variable value can be changed according
            // to your requirement of the administrator
            // password.

            string admin_pass = "admin";

            if (strcmp(password, admin_pass.c_str()) == 0)
            {
                cout << "Reg No.     "
                        "\tName\tCSE1001\tCSE1002\tProctor "
                        "ID"
                     << endl;
                ifstream infile;
                infile.open("Example.txt", ios::in);
                char data[20];

                while (infile >> data)
                {
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << endl;
                }
            }
        }
    }
}

输出:

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 1
Enter the number of students: 2
Enter your registration number: 12BCE2083
Enter your name: Prateek
Enter your proctor ID: 10001
Enter your registration number: 15BCE2022
Enter your name: shubham
Enter your proctor ID: 10002

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 3
Enter your subject code: CSE10001

Available operations: 
1. Add data about marks
2. View data
Enter option: 1
Warning! You would need to add mark details for all the students!
Enter the mark of student#1 : 54
Enter the mark of student#2 : 90
No such subject code found!

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 5
Enter the admin password: admin
Reg No.       Name    CSE1001    CSE1002    Proctor ID
15BCE2083    Prateek    54         00         10001
15BCE2082    Shubham    89         00         10002

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 6 
- - - - - - - - - - - - - - - - - - - - - - - 
(Program exited with code: 0)
Press return to continue

请注意,指针位置已经根据我们添加到文本文件的数据的长度进行了调整。我们假设注册号始终是9个字符长,科目代码要么是CSE10001,要么是CSE10002,监考员ID是4个字符长,而标签只有2个字符长。如果您想输入不同类型的数据,您将需要相应地修改代码。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程