Home > Backend Development > C#.Net Tutorial > Using C++ to implement data management functions

Using C++ to implement data management functions

little bottle
Release: 2019-04-02 16:25:52
Original
2731 people have browsed it


This article mainly talks about adding, deleting, querying and sorting data through C.


Using C++ to implement data management functions

Here, I use student data management to give an example:

Requires the following functions:


1. Information entry: enter student performance information (including student number, name, grades of each course, etc.);

2. Information query: enter student No. to query the students' scores in each course and display them.

3. Sorting: Sort by the average score of each course and display it.

4. Information deletion and modification - enter the student number to delete the student's grade information.

Step-by-step implementation:

1. Preliminarily complete the overall design, set up the framework, determine the interface for human-computer dialogue, and determine the number of functions.

2. Create a file, write each student's information into the file and display it on the screen.

3. Complete the above information query (student ID number, name, etc.), sorting, information deletion and modification functions.

#include <iostream>
#include <string.h>
using namespace std;
#define MAX 100
class CStudent
{private:    char *name;              // 姓名
    bool sex;                // 性别
    CDate date;              // 出生日期,类对象作数据成员
    public:    static int num;          // 学生人数
    CStudent();    void InputData();    
    friend void Sort();      // 排序
    friend void FindName();  // 按姓名查询
    friend void Statistic(); // 按性别统计
    friend void Display();   // 显示全部信息} stu[MAX];
    int CStudent::num=0;    //static 初始化
    CStudent::CStudent() {} //构造函数// 输入信息
void CStudent::InputData()
{    
    int p;    char s[41];    
    cout<<"请输入学生信息(NO."<<num<<"):\n";    
    cout<<"姓名:";    cin>>s;
    name=new char[strlen(s)+1];    
    strcpy(name,s);    
    cout<<"性别(1-男,0-女):";    
    cin>>p;    
    if (p)  sex=true;    
    else sex=false;    cin>>date;//操作符重载->istream &operator>>(istream &in,CDate &d)
    cout<<endl;
}// 排序 根据data从小到大排序
void Sort()
{    
    int i,j,p,num;    char *tn;    bool ts;
    CDate td;
    num=CStudent::num;    
    for(i=1; i<num; i++)
    {
        p=i;        
    for(j=i+1; j<=num; j++)            
    if (stu[j].date<stu[p].date) p=j;//找到当前未排序元素中年龄最小的对象的下标
        if (p==i) continue;        //下面交换stu[i]和stu[p]
        tn=stu[i].name;
        stu[i].name=stu[p].name;
        stu[p].name=tn;
        ts=stu[i].sex;
        stu[i].sex=stu[p].sex;
        stu[p].sex=ts;
        td=stu[i].date;
        stu[i].date=stu[p].date;
        stu[p].date=td;
    }
}// 按姓名查询
void FindName()
{    
    char name[41];    
    int i,num;    
    cout<<"请输入姓名:";    
    cin>>name;
    num=CStudent::num;    
    for(i=1; i<=num; i++)        
    if (strcmp(stu[i].name,name)==0) break;    
    if (i>num)
    {        
    cout<<"查无此人!"<<endl<<endl;        
    return;
    }    //如果查到了,显示学生信息
    cout<<"姓名:"<<stu[i].name<<endl;    
    cout<<"性别:";    if (stu[i].sex)        
    cout<<"男"<<endl;    else
    cout<<"女"<<endl;    
    cout<<"生日:"<<stu[i].date<<endl;    
    cout<<endl;
}// 按性别统计
void Statistic()
{    
    int i,num,s1,s0;
    num=CStudent::num;
    s1=0;
    s0=0;    for(i=1; i<=num; i++)        
    if (stu[i].sex==1)s1++;        
    else s0++;    
    cout<<"男生人数:"<<s1<<endl;    
    cout<<"女生人数:"<<s0<<endl;    
    cout<<endl;
}// 显示全部信息
void Display()
{    
    int i,num;
    num=CStudent::num;    for(i=1; i<=num; i++)
    {        
    cout<<stu[i].name<<"\t";        
    if (stu[i].sex)            
    cout<<"男";        
    else cout<<"女";        
    cout<<"\t"<<stu[i].date<<endl;
    }    
    cout<<endl;
}
    int main1()
{    char *menu[]= { "","输入信息","排序","按姓名查询","按性别统计","显示全部信息","退出" };    
    int i,p;    bool end;
    end=false;    while(!end)
    {        for(i=1; i<7; i++)            
    cout<<i<<"  "<<menu[i]<<endl;        
    cin>>p;        
    switch(p)
        {        
    case 1:                          // 输入信息
            CStudent::num++;
            stu[CStudent::num].InputData();            break;        
    case 2:                          // 排序
            Sort();            break;        
    case 3:                          // 按姓名查询
            FindName();            break;        
    case 4:                          // 按性别统计人数
            Statistic();            break;        
    case 5:                          // 显示全部信息
            Display();            break;        
    case 6:                          // 退出
            end=true;            break;
        }
    }    return 0;
}
Copy after login

The above are the details about the data management implementation of C. For more information, please pay attention to other related articles on the php Chinese website!

【Recommended course: C Video Tutorial


The above is the detailed content of Using C++ to implement data management functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template