程序阅读:简单C++学生信息管理系统

简介: 课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接【程序阅读】阅读并运行下面的程序,找出其中出现构造函数、友元函数、运算符重载、静态数成员语法现象出现的位置,仔细体会其用法,在以后的设计中能够灵活应用有关方法和技巧#include <iostream>#include &

课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接


【程序阅读】阅读并运行下面的程序,找出其中出现构造函数、友元函数、运算符重载、静态数成员语法现象出现的位置,仔细体会其用法,在以后的设计中能够灵活应用有关方法和技巧

#include <iostream>
#include <string.h>
using namespace std;
#define MAX 100

class CDate  // 定义日期类
{
private:
    unsigned short int year;   // 年
    unsigned short int month;  // 月
    unsigned short int day;    // 日
public:
    CDate(int y=0,int m=0,int d=0);
    bool operator < (CDate d);
    friend istream & operator >> (istream &in,CDate &d);
    friend ostream & operator<<(ostream &out,CDate &d);
    friend bool CheckValid(CDate d);
    friend bool LeapYear(int year);
    void SetDate(int y,int m,int d);
};
CDate::CDate(int y,int m,int d):year(y),month(m),day(d) {}
// 设置日期
void CDate::SetDate(int y,int m,int d)
{
    year=y;
    month=m;
    day=d;
}
// 重载输入运算符>>
istream &operator>>(istream &in,CDate &d)
{
    char ch1,ch2;
    cout<<"请输入日期(输入格式:YYYY-MM-DD):";
    while(1)
    {
        cin>>d.year>>ch1>>d.month>>ch2>>d.day;
        if (ch1=='-' && ch2=='-')
            if (CheckValid(d)) break;
        cerr<<"时间格式或取值不正确! 请重新输入\n";
    }
    return cin;
}
// 重载输出运算符<<
ostream &operator<<(ostream &out,CDate &d)
{
    out<<d.year<<"年"<<d.month<<"月"<<d.day<<"日";
    return out;
}
// 判断日期d1<d2
bool CDate::operator < (CDate d)
{
    if (year<d.year) return true;
    if (year>d.year) return false;
    if (month<d.month) return true;
    if (month>d.month) return false;
    if (day<d.day) return true;
    return false;
}

// 检查是否为闰年
bool LeapYear(int year)
{
    if (year%4==0 && year%100 || year%400==0)
        return true;
    return false;
}

// 检查日期合法性
bool CheckValid(CDate d)
{
    int n;
    if (d.month<1 || d.month>12) return false;
    if (d.day<1) return false;
    n=31;
    switch(d.month)
    {
    case 2:
        if (LeapYear(d.year))
            n=29;
        else
            n=28;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        n=30;
        break;
    }
    if (d.day>n) return false;
    return true;
}

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;
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;
    cout<<endl;
}
// 排序
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 main()
{
    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;
}
【扩展提示】你是否可以在如上设计基础上,增加文件保存数据,使其趋向于真正实用的系统?


==================== 迂者 贺利坚 CSDN博客专栏=================
|== IT学子成长指导专栏 专栏文章的分类目录(不定期更新) ==|
|== C++ 课堂在线专栏  贺利坚课程教学链接(分课程年级) ==|
|== 我写的书——《逆袭大学——传给IT学子的正能量》    ==|
===== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =====



目录
相关文章
|
23天前
|
存储 缓存 算法
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
111 0
|
1月前
|
编译器 Linux C++
3C++程序的编写和实现
3C++程序的编写和实现
17 2
|
1月前
|
C++ 开发者
2C++的程序的构成和书写形式
2C++的程序的构成和书写形式
16 2
|
4月前
|
JavaScript 前端开发 Serverless
函数计算只支持Node.js,我用C++写的程序怎么运行?
函数计算只支持Node.js,我用C++写的程序怎么运行?
90 1
|
12天前
|
存储 人工智能 机器人
【C/C++】C++学籍信息管理系统(源码+报告)【独一无二】
【C/C++】C++学籍信息管理系统(源码+报告)【独一无二】
|
18天前
|
存储 缓存 C++
C++链表常用的函数编写(增查删改)内附完整程序
C++链表常用的函数编写(增查删改)内附完整程序
|
30天前
|
缓存 编译器 程序员
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
60 0
|
30天前
|
缓存 编译器 程序员
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
25 0
|
30天前
|
缓存 算法 编译器
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
36 0
|
1月前
|
自然语言处理 编译器 调度
深入gcc编译器:C/C++代码如何变为可执行程序
深入gcc编译器:C/C++代码如何变为可执行程序
75 0