| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 929 人关注过本帖
标题:求各位大神们指点啊!!!关于类的模板问题。。。。
只看楼主 加入收藏
smile康师傅
Rank: 2
等 级:论坛游民
帖 子:34
专家分:53
注 册:2012-6-10
结帖率:71.43%
收藏
 问题点数:0 回复次数:10 
求各位大神们指点啊!!!关于类的模板问题。。。。
这是代码:
/*题目三:学生信息管理系统设计
学生信息包括:学号,姓名,年龄,性别,出生年月,地址,电话,E-mail等。试设计一学生信息管理系统,使之能提供以下功能:
系统以菜单方式工作
学生信息录入功能 ---输入
学生信息浏览功能---输出查询、排序功能---算法按学号查询
按姓名查询
学生信息的删除与修改
*/


#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
const string file="student.txt";


void select_menu();
void judge(int cnt);                                      //admin_pass的辅助函数
class student;
class student1;
ostream& operator<<(ostream& os,const student&s);
istream& operator>>(istream&is,student&s);
void main_menu();
void add_menu();
void build_data();
void inquire_menu();
void index_menu();
void modify_menu();
void delete_menu();




class student{                                             //基类
    friend ostream& operator<<(ostream& os,const student&s);//重载<<操作符输出学生个人信息
    friend istream& operator>>(istream&is,student&s);//重载>>操作符输入学生个人信息
public:
    student(){sno=name=sex=birthday=address=telnum='*';age=grade=0;}//对所有数据进行初始化
    string sno;
    string name;
    int age;
    string sex;
    string birthday;
    string address;
    string telnum;
    int grade;
};
bool operator<(student&s1,student&s2)
{
    return s1.sno<s2.sno;
}

class student1:public student{                              //大一学生
public:
    student1():student(){grade=1;}
};


class student2:public student{                              //大二学生
public:
    student2():student(){grade=2;}
};


class student3:public student{                              //大三学生
public:
    student3():student(){grade=3;}
};


istream& operator>>(istream&is,student&s)
{
    is>>s.sno>>s.name>>s.sex>>s.age>>s.birthday>>s.address>>s.telnum;
    if (s.sno.length()!=10||(s.sex!="男"&&s.sex!="女")||(s.age>100||s.age<0)||s.telnum.length()!=11)
    {
        string s;
        throw  s="输入错误!!!请返回重新输入。。。。";
    }
    return is;
}
ostream& operator<<(ostream& os,const student&s)
{
    os<<s.sno<<'\t'<<s.name<<'\t'<<s.sex<<'\t'<<s.age<<'\t'<<s.birthday<<'\t'<<s.address<<'\t'<<s.telnum<<endl;
    return os;
}
vector<student> stu;                                //申明vector全局变量


//选择学生所在年级
void choose_grade()
{
    cout<<"******************************************************************************"<<endl
        <<"请选择学生所在年级:                                                           "<<endl
        <<"*  1.大一                                                                     "<<endl
        <<"*  2.大二                                                                     "<<endl
        <<"*  3.大三                                                                     "<<endl
        <<endl;
}
//管理员登陆密码检验
bool admin_pass()
{
    const string userword="123456";                       //管理员登陆密码
    char ch,password[20];
    int cnt=0,i=0,j=0;
    system("cls");
    while (cnt<3)
    {
    cout<<"******************************************************************************"<<endl
        <<"***************************************^_^************************************"<<endl;
        cout<<"请输入登陆密码:"<<endl;
        while (1)
        {
            ch=getch();
            if (ch!=13&&ch!=8)
            {
                cout<<'*';
                password[i]=ch;
                i++;
            }
            else if (ch==8)
            {
                system("cls");
                cout<<"请输入登陆密码:"<<endl;
                string s(--i,'*');
                cout<<s;
            }
            else
                break;
        }
        cnt++;
        if (i==userword.length())                        //判断输入密码与所设定的密码是否长度相等
        {
            while (userword[j]==password[j])
            {
                ++j;
            }
            if (j==userword.length())
            {
                break;
            }
            else
            {
                judge(cnt);                               //调用judge函数对输入错误进行相关处理
                i=j=0;
            }
        }
        else
        {
            judge(cnt);
            i=j=0;
        }
    }
    if (cnt<=3)
        return true;
    else
        return false;
}

inline void judge(int cnt)                                        //admin_pass的辅助函数
{
    if ((3-cnt)>0)
    {
        cout<<"\n密码输入错误!!!你还剩"<<3-cnt<<"次输入机会!!!请返回重新输入。。。。"<<endl;
    }
    else
    {
        cout<<"\n抱歉啦!!!该退出程序了。。。。"<<endl;
        system("cls");
        exit(0);
    }
}
//选择菜单
inline void select_menu()
{
    cout<<"请进行选择:"<<endl;
}


//返回主菜单
inline void come_back()
{
    char ch;
    cout<<"确认返回主菜单吗?(Y/N)"<<endl;
    cin>>ch;
    if (ch=='Y'||ch=='y')
    {
        system("cls");
        main_menu();
    }
    else
        exit(0);
}
//输入学生姓名
inline const string input_name()
{
    string name;
    cout<<"请输入学生姓名:"<<endl;
    cin>>name;
    return name;
}
//输入学生学号
inline const string input_sno()
{
    string sno;
    cout<<"请输入学生学号:"<<endl;
    cin>>sno;
    return sno;
}



void main_menu()
{
    cout<<"******************************欢迎来到学生管理系统****************************"<<endl
        <<"******************************************************************************"<<endl
        <<"1.添加学生信息                                                                "<<endl
        <<"2.查询学生信息                                                                "<<endl
        <<"3.浏览学生信息                                                                "<<endl
        <<"4.修改学生信息                                                                "<<endl
        <<"5.删除学生信息                                                                "<<endl
        <<"6.退出                                                                        "<<endl
        <<endl;
    select_menu();
    char select;
head:cin>>select;
    if (select>'0'&&select<='6')
    {
        switch(select)
        {
            case '1':
                {
                    system("cls");
                    add_menu();
                    build_data();
                    come_back();
                    break;
                }
            case '2':
                {
                    system("cls");
                    inquire_menu();
                    come_back();
                    break;
                }
            case '3':
                {
                    system("cls");
                    index_menu();
                    come_back();
                    break;
                }
            case '4':
                {
                    system("cls");
                    modify_menu();
                    come_back();
                    break;
                }
            case '5':
                {
                    system("cls");
                    delete_menu();
                    come_back();
                    break;
                }
            case '6':
                {
                    exit(0);
                    break;
                }
        }   
    }
    else
    {
        cout<<"选择错误!!!请返回重新输入。。。"<<endl;
        goto head;
    }
}


//添加学生信息
void add_menu()
{
    char select;
    choose_grade();
    /*cout<<"**************************************************************"<<endl
        <<"请选择学生所在年级:                                             "<<endl
        <<"*  1.大一                                                       "<<endl
        <<"*  2.大二                                                       "<<endl
        <<"*  3.大三                                                       "<<endl
        <<endl;*/
    select_menu();
head:cin>>select;
    if (select>'0'&&select<='3')
    {
        switch(select)
        {
            case '1':
                {
                    student stu1;
                L1:    try
                    {
                        cout<<"请输入学生学号-姓名-性别-年龄-出生年月-地址-联系电话:"<<endl;
                        cin>>stu1;
                    }
                    catch (string s)
                    {
                        cerr<<s<<endl;
                        goto L1;
                    }
                    stu.push_back(stu1);
                    break;
                }
            case '2':
                {
                    student stu2;
                L2:try
                    {
                           cout<<"请输入学生学号-姓名-性别-年龄-出生年月-地址-联系电话:"<<endl;
                        cin>>stu2;
                    }
                    catch (string s)
                    {
                        cerr<<s<<endl;
                        goto L2;
                    }
                    stu.push_back(stu2);
                    break;
                }
            case '3':
                {
                    student stu3;
                L3:try
                    {
                           cout<<"请输入学生学号-姓名-性别-年龄-出生年月-地址-联系电话:"<<endl;
                        cin>>stu3;
                    }
                    catch (string s)
                    {
                        cerr<<s<<endl;
                        goto L3;
                    }
                    stu.push_back(stu3);
                    break;
                }
        }
   
    }
    else
    {
        cout<<"选择错误!!!请返回重新输入。。。。。"<<endl;
        goto head;
    }
}






//建立学生文件
void build_data()
{
    if (stu.empty())
    {
        char ch;
        cout<<"文件内容为空,需要现在建立吗?(Y/N)"<<endl;
        cin>>ch;
        if (ch=='Y'||ch=='y')
        {
            system("cls");
            add_menu();
        }
        else
        {
            ofstream out(file.c_str());
            for (vector<student>::iterator it=stu.begin();it<=stu.end();it++)
                out<<*it<<endl;
            exit(0);
        }
    }
    else
    {
        sort(stu.begin(),stu.end());
        ofstream out(file.c_str());
        for (vector<student>::iterator it=stu.begin();it<=stu.end();it++)
            out<<*it<<endl;
    }
}




//学生信息查询菜单
void inquire_menu()
{
    char ch,select;
    cout<<"******************************************************************************"<<endl
        <<"请选择查询方式:                                                               "<<endl
        <<"* 1.姓名                                                                       "<<endl
        <<"* 2.学号                                                                     "<<endl
        <<"* 3.返回                                                                      "<<endl
        <<endl;
    select_menu();
  L:cin>>select;
    if (select>'0'&&select<='3')
    {
        if (select>'0'&&select<='2')
        {
            switch(select)
            {
                case '1':
                    {
                        string name=input_name();
                        vector<student>::iterator it=stu.begin();
                        while (it<=stu.end())
                        {
                            if (it->name==name)
                            {
                                cout<<"该学生的个人信息为:"<<endl;
                                cout<<*it<<endl;
                                it++;
                            }
                            else
                                it++;
                        }
                        break;
                    }
                case '2':
                    {
                        string sno=input_sno();
                        vector<student>::iterator it=stu.begin();
                        while (it<=stu.end())
                        {
                            if (it->sno==sno)
                            {
                                cout<<"该学生的个人信息为:"<<endl;
                                cout<<*it<<endl;
                                it++;
                            }
                            else
                                it++;
                        }
                        break;
                    }
            }
        }
        else
            come_back();
    }
    else
    {
        cout<<"选择错误!!!请返回重新输入。。。。。"<<endl;
        goto L;
    }
    cout<<"你想继续查询学生信息吗?(Y/N)"<<endl;
    cin>>ch;
    if (ch=='Y'||ch=='y')
    {
        system("cls");
        inquire_menu();
    }
}




//浏览学生信息
void index_menu()
{
    char select,ch;
    choose_grade();
    /*cout<<"**************************************************************"<<endl
        <<"请选择学生所在年级:                                             "<<endl
        <<"*  1.大一                                                       "<<endl
        <<"*  2.大二                                                       "<<endl
        <<"*  3.大三                                                       "<<endl
        <<"*  4.返回                                                       "<<endl
        <<endl;*/
    select_menu();
  K:cin>>select;
    if (select>'0'&&select<='4')
    {
        switch(select)
        {
            case '1':
                {
                    cout<<"大一的学生信息:"<<endl;
                    vector<student>::iterator it=stu.begin();
                    while (it<=stu.end())
                    {
                        if (it->grade==1)
                        {
                            cout<<*it<<endl;
                            it++;
                        }
                        else
                            it++;
                    }
                    break;
                }
            case '2':
                {
                    cout<<"大二的学生信息:"<<endl;
                    vector<student>::iterator it=stu.begin();
                    while (it<=stu.end())
                    {
                        if (it->grade==2)
                        {
                            cout<<*it<<endl;
                            it++;
                        }
                        else
                            it++;
                    }
                    break;
                }
            case '3':
                {
                    cout<<"大三的学生信息:"<<endl;
                    vector<student>::iterator it=stu.begin();
                    while (it<=stu.end())
                    {
                        if (it->grade==3)
                        {
                            cout<<*it<<endl;
                            it++;
                        }
                        else
                            it++;
                    }
                    break;
                }
            case 4:
                {
                    come_back();
                    break;
                }
        }
    }
    else
    {
        cout<<"选择错误!!!请返回重新输入。。。。。"<<endl;
        goto K;
    }
    cout<<"你想继续浏览学生信息吗?(Y/N)"<<endl;
    cin>>ch;
    if (ch=='Y'||ch=='y')
    {
        system("cls");
        index_menu();
    }
}



//修改学生信息
void modify_menu()
{
    char ch,select;
    cout<<"**************************************************************"<<endl
        <<"请选择修改的查找方式:                                         "<<endl
        <<"* 1.姓名                                                      "<<endl
        <<"* 2.学号                                                      "<<endl
        <<"* 3.返回                                                      "<<endl
        <<endl;
    select_menu();
  P:cin>>select;
    if (select>'0'&&select<='3')
    {
        if (select>'0'&&select<='2')
        {
            switch(select)
            {
                case '1':
                    {
                        string name=input_name();
                        vector<student>::iterator it=stu.begin();
                        while (it<=stu.end())
                        {
                            if (it->name==name)
                            {
                                cin>>*it;
                                it++;
                            }
                            else
                                it++;
                        }
                        break;
                    }
                case '2':
                    {
                        string sno=input_sno();
                        vector<student>::iterator it=stu.begin();
                        while (it<=stu.end())
                        {
                            if (it->sno==sno)
                            {
                                cin>>*it;
                                it++;
                            }
                            else
                                it++;
                        }
                        break;
                    }
            }
        }
        else
            come_back();
    }
    else
    {
        cout<<"选择错误!!!请返回重新输入。。。。。"<<endl;
        goto P;
    }
    cout<<"你想继续修改学生信息吗?(Y/N)"<<endl;
    cin>>ch;
    if (ch=='Y'||ch=='y')
    {
        system("cls");
        modify_menu();
    }
}


//删除学生个人信息
void delete_menu()
{
    char ch,select;
    cout<<"******************************************************************************"<<endl
        <<"请选择删除的查找方式:                                                         "<<endl
        <<"* 1.姓名                                                                      "<<endl
        <<"* 2.学号                                                                      "<<endl
        <<"* 3.返回                                                                      "<<endl
        <<endl;
    select_menu();
  Q:cin>>select;
    if (select>'0'&&select<='3')
    {
        if (select>'0'&&select<='2')
        {
            switch(select)
            {
                case '1':
                    {
                        string name=input_name();
                        vector<student>::iterator it=stu.begin();
                        while (it<=stu.end())
                        {
                            if (it->name==name)
                            {
                                stu.erase(it);
                                --it;
                            }
                            else
                                it++;
                        }
                        break;
                    }
                case '2':
                    {
                        string sno=input_sno();
                        vector<student>::iterator it=stu.begin();
                        while (it<=stu.end())
                        {
                            if (it->sno==sno)
                            {
                                stu.erase(it);
                                --it;
                            }
                            else
                                it++;
                        }
                        break;
                    }
            }
        }
        else
            come_back();
    }
    else
    {
        cout<<"选择错误!!!请返回重新输入。。。。。"<<endl;
        goto Q;
    }
    cout<<"你想继续删除学生信息吗?(Y/N)"<<endl;
    cin>>ch;
    if (ch=='Y'||ch=='y')
    {
        system("cls");
        delete_menu();
    }
}






void main()
{
    ifstream infile;
    string stu_ifo;
    infile.open(file.c_str());
    while(getline(infile,stu_ifo))
    {   
        student s;
        istringstream sin(stu_ifo);
        sin>>s.sno>>s.name>>s.sex>>s.age>>s.birthday>>s.address>>s.telnum>>s.grade;  //为什么不能sin>>s;重载了为什么没用啊!!!
        stu.push_back(s);
    }
    sort(stu.begin(),stu.end());                 //将读入的学生按年级学号排序,怎样控制排列次序????
    if (admin_pass())
    {
        system("cls");
        main_menu();
    }
    else
        exit(0);
}
搜索更多相关主题的帖子: 信息 学号查询 include file 
2012-12-30 13:33
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:0 
这代码真是够长的。。。直接告诉楼主 。。。我懒得看。。。因为连分都没有

有心者,千方百计;无心者,千难万难。
2012-12-31 21:32
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
楼主想累死人啊,这么长。类模板template<class Type>
函数void ss(Type a,Type *b){}函数调用时程序自己去匹配a ,*b的类型
带返回值的Type    ss(Type a,Type *b){ return Type;}
//只会这个,要是不满足 ,楼主自己看书去

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-01-01 20:20
zk2012
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-12-23
收藏
得分:0 
程序有点点长,waiting  。。。
2013-01-01 21:58
smile康师傅
Rank: 2
等 级:论坛游民
帖 子:34
专家分:53
注 册:2012-6-10
收藏
得分:0 
各位,我这个问题差不多解决了的。。。。谢谢啦!!!
2013-01-08 21:30
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
一般长程序懒得看...
针对你红字部分...既然你会用std::sort...
那么你尝试一下sort的第三个参数吧...赋函数指针进去...

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-01-09 19:16
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
void sort(RanIt first, RanIt last, Pred pr);
函数原型

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-01-09 19:17
锋了
Rank: 7Rank: 7Rank: 7
来 自:向日葵幼儿园
等 级:黑侠
威 望:2
帖 子:306
专家分:586
注 册:2012-10-27
收藏
得分:0 
楼主不用这么抠吧,竟然一分都不给,贴这么长的程序,你不理解模板,就贴出你不理解的模板就得啦。。。


是在是太抠了。。。
2013-01-14 21:20
小糊涂神c30
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:198
专家分:809
注 册:2012-4-25
收藏
得分:0 
你真幸运,版主竟然没有封杀你的帖子!!!
2013-01-14 22:23
liu137800
Rank: 2
等 级:论坛游民
帖 子:21
专家分:24
注 册:2012-12-25
收藏
得分:0 
如果不理解模板的真正含义最好去学习学习再整,帖子太长了!
2013-01-15 14:43
快速回复:求各位大神们指点啊!!!关于类的模板问题。。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040955 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved