01-0001 C + + implementation of student information management system [Part 1]

Keywords: Attribute

C + + implementation of student information management system [interface + data processing]

1. Simple problem description

1. Self design student information management system
 2. Realize the functions of student information entry, deletion, search and browsing [this demand is really disgusting]
3. Use EasyX graphic library to realize the interface
 4. Reference video: https://www.bilibilili.com/video/av13231926

2. Problems encountered

2.1 repeated inclusion

Note: add a more complete description of the problem

2.2 declare and initialize an array of type LPCTSTR under the Unicode character set

LPCTSTR s[5] = { L"Student ID",L"Full name",L"English?",L"Mathematics",L"Chinese" };

Note: LP [pointer], C [const], W [wide character], T [indicates that there is a macro in the Win32 environment]
Reference link: 1. LPCTSTR LPCWSTR LPCSTR meaning
2. The source and significance of LPTSTR, LPCSTR, LPCTSTR and LPSTR error C2440 in VS2005: a summary of "const char [N]" cannot be converted to "LPCWSTR" [extranet]

2.3 initialization of lptstr

LPTSTR temp=new TCHAR[10];

Look at the definition of LPTSTR to see how to initialize, as follows:

#ifdef _UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif // _UNICODE

typedef const TCHAR* LPCTSTR;
typedef TCHAR* LPTSTR

In fact, TCHAR is the wide character type, while LPTSTR is the pointer of the wide character type. There are only two ways to initialize, new TCHAR, or new TCHAR[SIZE]. The analogy is as follows

char* a=new char;
char* b=new char[100]
//According to the standard, char occupies 8 bits, that is, a byte

2.4 CString to LPTSTR, CString string connection, other types to CString

CString str;
//int to CString
int a=1;
str.Format(_T("%d"), a);
//float to CString
float b=1.2;
str.Format(_T("%f"), b);
//char* to CString
char* name="Liming";
str=name;

//CSting to LPTSTR
LPTSTR out_str;
out_str=str.GetBuffer();

//Connection of CString string
CString str1;
CString str2;
str2+=str1;

2.5 in order to return data without const, you need to end const and return

//Studet is a student class with a name attribute
//Store the Student object in the vector
//Define the search method to return the pointer to the corresponding data when it is found
Student* search(const vector<Student>& v, const char* name) {
    int count = 0;
    for (auto it : v) {
        if (!strcmp(it.name,name)){
            return const_cast<Student*>(&v[count]);
        }
        count++;
    }
    return NULL;
}

//The following statement cancels const to enable the pointer to return
//Do not copy all the code, just copy the function part above
return const_cast<Student*>(&v[count]);

2.6 there is a pointer to an element of an array or vector. Judge whether this element is the first

vector<int> v;//Life is a vector that needs to be initialized, but there is no
int* a=&v[3];//Pointer to fourth element
int rank=a-&v[0];//Get the rank of this element
v.erase(v.begin()+(a-&v[0]));//Release the element

2.7 debug, heap.cpp, line904, crtisvalidheappointer don't know what the reason is

	//debug_heap.cpp line904 _crtisvalidheappointer
    LPTSTR temp = new TCHAR[10];
    InputBox(temp, 10, L"Please enter the student's name:");
    outtextxy(183, 86, temp);
    USES_CONVERSION;
    char* c_temp=new char[20];
    c_temp = T2A(temp);
    stu.name = new char[strlen(c_temp) + 1];
    strcpy_s(stu.name, strlen(c_temp) + 1, c_temp);
    delete[] c_temp;//There will be mistakes here. I don't know why
    //Presumably, the same memory space is delete d repeatedly

3. source code

Address: Original engineering documents You can also copy the following source code and create a new project by yourself.
File 1: School.h

#ifndef SCHOOL_H_
#define SCHOOL_H_

#include <iostream>
#include <vector>
#include <graphics.h>
#include <mmstream.h>//Header file containing multimedia device interface
#pragma comment(lib,"winmm.lib")//Library file containing multimedia device interface
#include "Student.h"
using namespace std;
const int win_width = 600;
const int win_hight = 400;
IMAGE bkimg, butimg;

//Initialization of management system interface
void iniInterface();
//Initializing: managing system sounds
void iniSound();
//Initializing: managing system text
void iniText();
//Overall initialization
void beginItf();
//insert data
void insertIfo(vector<Student>& v);
//Detect mouse related events
void butPress(vector<Student>& v);
//Search data
void searchIfo(const vector<Student>& v);
//Browse data
void scanIfo(const vector<Student>& v);
//Delete data
void deleteIfo(vector<Student>& v);
//Overloading two lookup functions
//You can use student ID or name to search
Student* search(const vector<Student>& v, const int id);
Student* search(const vector<Student>& v, const char* name);
//Overloaded text output function in EasyX
void outtextxy(int x, int y, Student s);
#endif // !SCHOOL_H_
File 2: Student.h
#pragma once
#include<iostream>
class Student {
public:
	int id;
	char *name;
	float s_english;
	float s_math;
	float s_chinese;
};
//char * is used to facilitate the following type conversion
File 3: School.cpp
#include "School.h"
#include <atlconv.h>
#include <atlstr.h>
#include <cstring>
int main()
{
    //Data storage location
    vector<Student> stu_vct;//Using vectors to store student data
    //Vector < student > * stu P = & stu VCT; / / declares that the pointer points to a vector that stores student data to save space
    Student s1={1111,(char*)"Zhang San",123,123,123};
    Student s2={2222,(char*)"Li Si",123,123,123 };
    stu_vct.push_back(s1);
    stu_vct.push_back(s2);

    //Initialization
    initgraph(win_width, win_hight);
    beginItf();
    butPress(stu_vct);

    //Function tail, card screen, close picture stream
    std::cin.get();
    closegraph();
    cout << stu_vct[0].id << endl;
    std::cout << "Hello World!\n";
}


//Detect mouse related events
void butPress(vector<Student>& v) {
    MOUSEMSG msg{};
    while (true) {
        msg = GetMouseMsg();
        switch (msg.uMsg) {
        case WM_LBUTTONDOWN://Press the left mouse button
            if (msg.x > 200 && msg.x < 400 && msg.y>80 && msg.y < 130) {
                //Input information
                insertIfo(v);
                beginItf();
            }
            if (msg.x > 200 && msg.x < 400 && msg.y>140 && msg.y < 190) {
                //find information
                searchIfo(v);
                beginItf();
            }
            if (msg.x > 200 && msg.x < 400 && msg.y>200 && msg.y < 250) {
                //Delete information
                deleteIfo(v);
                beginItf();
            }
            if (msg.x > 200 && msg.x < 400 && msg.y>260 && msg.y < 310) {
                //Browsing information
                scanIfo(v);
                beginItf();
            }
            break;
        case WM_MOUSEMOVE://Mouse movement
            if (msg.mkLButton) {
                //What to do when you click the left mouse button during the move
                //...
            }
            break;
        default:
            break;
        }
    }
}

//Insert a student message
void insertIfo(vector<Student>& v) {
    //Perfect function:
    //  1. Cannot insert repeatedly. If the student information already exists, you need to give a prompt
    //  2. Detect the improper input until the user inputs the correct content
    //  3. Support direct return. Press the back key, etc. to return directly
    //  4. File writing is supported. If it can be inserted, the content needs to be written to the file
    Student stu;
    putimage(0, 0, &bkimg);//Keep the background unchanged
    //char s[][5] = {"student number", "name", "English", "Mathematics", "Chinese"};
    //TCHAR * s [5] = {L "student number", "name", "English", "Mathematics", "Chinese"};
    LPCTSTR s[5] = { L"Student ID",L"Full name",L"English?",L"Mathematics",L"Chinese" };
    for (int i = 0; i < 5; i++) {
        setlinecolor(RED);
        rectangle(100, 50 + i * 35, 500, 85 + i * 35);
        outtextxy(100, 51 + i * 35, s[i]);
    }
    line(180, 50, 180, 225);
    
    //LPSTR: 32bit pointer points to a string, each character occupies 1 byte, equivalent to char 
    LPTSTR temp=new TCHAR[10];

    InputBox(temp, 10, L"Please input students id: ");
    outtextxy(183, 51, temp);
    stu.id = _wtoi(temp);


    InputBox(temp, 10, L"Please enter the student's name:");
    outtextxy(183, 86, temp);
    USES_CONVERSION;
    char* c_temp;
    c_temp= T2A(temp);
    stu.name = new char[strlen(c_temp) + 1];
    strcpy_s(stu.name,strlen(c_temp)+1, c_temp);

    InputBox(temp, 10, L"Please enter your English score:");
    outtextxy(183, 121, temp);
    stu.s_english = _wtof(temp);

    InputBox(temp, 10, L"Please enter your math score:");
    outtextxy(183, 156, temp);
    stu.s_math = _wtof(temp);

    InputBox(temp, 10, L"Please enter your math score:");
    outtextxy(183, 191, temp);
    stu.s_chinese = _wtof(temp);

    delete[] temp;//Instead of the C? temp above, it should be cleared
    v.push_back(stu);
}
//Search for a student message
void searchIfo(const vector<Student>& v) {
    //Support to find id or name
    //The following two statements about the interface should be encapsulated into one
    putimage(0, 0, &bkimg);//Keep the background unchanged
    settextcolor(RGB(93, 107, 153));//Adjust text color
    settextstyle(15, 0, L"Regular script");

    LPTSTR temp = new TCHAR[10];
    InputBox(temp, 10, L"Please input students id Or student name:");
    int t_id;
    char* t_name;
    Student* s;
    if (t_id= _wtoi(temp)) {
        //You can use find if
        s=search(v, t_id);
        if (s != NULL) {
            outtextxy(0, 0, *s);
            outtextxy(0, 16, L"Please press any key to return...");
            cin.get();
        }
        else {
            outtextxy(0, 0, L"No such student,Please press any key to return...");
            cin.get();
        }
    }
    else {
        USES_CONVERSION;
        t_name = W2A(temp);
        s = search(v, t_name);
        if (s != NULL) {
            outtextxy(0, 0, *s);
            outtextxy(0, 16, L"Please press any key to return...");
            cin.get();
        }
        else {
            outtextxy(0, 0, L"No such student,Please press any key to return...");
            cin.get();
        }
    }
}
//Delete student information
void deleteIfo(vector<Student>& v) {
    putimage(0, 0, &bkimg);//Keep the background unchanged
    settextcolor(RGB(93, 107, 153));//Adjust text color
    settextstyle(15, 0, L"Regular script");
    Student* s;
    int t_id;
    LPTSTR temp = new TCHAR[10];
    InputBox(temp, 10, L"Please input students id: ");
    if (t_id = _wtoi(temp)) {
        //You can use find if
        s = search(v, t_id);
        if (s != NULL) {
            outtextxy(0, 0, *s);
            v.erase(v.begin() + (s - &v[0]));//According to the principle that the element block can be added and subtracted by pointer, the erase() function is called
            outtextxy(0,16,L"Successfully deleted,Please press any key to return...");
            cin.get();
        }
        else {
            outtextxy(0, 0, L"No such student,Please press any key to return...");
            cin.get();
        }
    }

}

void scanIfo(const vector<Student>& v) {
    //You need to set the scrolling effect. If you ignore it, you don't want to write it. It's disgusting
}

//Encapsulate the following initializations
void beginItf() {
    iniSound();
    iniInterface();
    iniText();
}

//Initialization: management system interface
void iniInterface() {

    //Loading pictures
    loadimage(&bkimg, L"./res/bkimg.jpg", 600, 400);//The last two parameters are scaling
    loadimage(&butimg, L"./res/butimg.jpg", 200, 50);//The last two parameters are scaling
    //Load image (null, l ". / RES / bkimg. JPG", 600, 400); / / directly output the image to the console, but the test did not

    //Paste pictures -- output pictures to a stream
    putimage(0, 0, &bkimg);//The coordinate system is in the upper left corner, facing down
    putimage(200, 80, &butimg);
    putimage(200, 140, &butimg);
    putimage(200, 200, &butimg);
    putimage(200, 260, &butimg);
}

//Initializing: managing system sounds
void iniSound() {
    //Open and play mp3 file
    //MciSendString (L "open. / RES / bkmusic. MP3 alias BGM", 0, 0, 0); / / add background music
    //mciSendString(L"play bgm repeat", 0, 0, 0); / / play background music

    //Play wav file
    //Playsound (L ". / RES / music. Wav", null, snd | filename | snd | loop | snd | async); / / the last parameter controls asynchronous playback
}

//Initializing: managing system text
void iniText() {
    //Style text
    settextstyle(30, 0, L"Regular script");
    //Set text color
    settextcolor(RGB(204, 213, 240));
    //Set text mode
    setbkmode(TRANSPARENT);
    //Output text at specified location
    outtextxy(180, 20, L"XXX School management system");
    outtextxy(247, 88, L"Input information");
    outtextxy(247, 148, L"find information");
    outtextxy(247, 208, L"Delete information");
    outtextxy(247, 268, L"Browsing information");
}

Student* search(const vector<Student>& v, const int id) {
    int count=0;
    for (auto it : v) {
        if (it.id == id) {
            return const_cast<Student*>(&v[count]);
        }
        count++;
    }
    return NULL;
}

Student* search(const vector<Student>& v, const char* name) {
    int count = 0;
    for (auto it : v) {
        if (!strcmp(it.name,name)){
            return const_cast<Student*>(&v[count]);
        }
        count++;
    }
    return NULL;
}

void outtextxy(int x, int y, Student s) {
    //Connect to the whole string and output uniformly
    LPTSTR out_LPT;
    CString str;
    CString out_str;
    str.Format(_T("%d"), s.id);
    out_str =out_str + "ID: "  + str + " ";
    str = s.name;
    out_str = out_str+ "Full name:" + str + " ";
    str.Format(_T("%.1f"), s.s_english);
    out_str = out_str + "English?" +  str + " ";
    str.Format(_T("%.1f"), s.s_math);
    out_str = out_str + "Mathematics:" + str + " ";
    str.Format(_T("%.1f"), s.s_chinese);
    out_str = out_str + "Chinese:" + str;
    out_LPT = out_str.GetBuffer();
    cout << "name" << endl;
    outtextxy(0, 0, out_LPT);
}
Effect

4. Tucao

Originally, I wanted to find a management system to do a good job. I've been watching c++ primer plus for a long time. Then I saw this tutorial. I didn't find it at the beginning, but I'll work later. But later, the more I heard it, the more disgusting it was. The most important thing is that it's not fully functional, and it's very old, and it's very angry. In the middle, the teacher only knows what function to use, that's why, Jane It's bad to die. I'm convinced. The urge to scold is already burning. If I can be a teacher in the future, I'll fight against this family.

.
The next time you write code, you should take notes as you write, instead of finding that you don't seem to make any mistakes.
.
When you encounter something of type conversion, you can't panic or be confused. You don't want to read it when you see a long article. That's not the way it is, but it's the way it is.
.
In addition, it needs to realize the function of detecting input and writing files
Remarks: Original engineering documents [in fact, there is an additional entrance for future use]
Note: if reprinted, please use well-known source and author.

Published 14 original articles, won praise 9, visited 4191
Private letter follow

Posted by jawaking00 on Sun, 12 Jan 2020 22:11:41 -0800