stl::stack(标准模板库stack使用)

作者在 2011-12-13 23:00:51 发布以下内容
标题:2、基于stl模版类生成新的模版类
时 限:1000 ms
内存限制:10000 K
总时限:3000 ms
描述:存在学生记录(studentRecord{string name;string num;int age})和老师记录(teacherRecord{string name;double sage})两种数据类型,请利用list::stack生成学生记录和老师记录栈的pushpop操作(push为空输出stuStack is Empty!tckStack is Empty!
输入:

总共欲执行的操作次数

执行操作种类 相应的记录

注意操作种类中

1 表示学生记录的push

2表示老师记录的push

3表示学生记录的pop

4表示老师记录的pop

输出:

push或pop的相关信息

全部的学生信息(pop出来)

全部老师信息(pop出来)
输入样例:

6

3

3

1 刘天福 0102431 21

4

2 张天福 2100

2 张浩 2313

输出样例:

stuStack is Empty!

stuStack is Empty!

tckStack is Empty!

Stu:Name 刘天福 Num 0102431 Age 21

Teacher:Name 张浩   Sage 2313

Teacher:Name 张天福 Sage 2100

(注意:

学生输出格式"Stu:"<<"Name "<

提示:

6-------总共欲执行的操作次数

3--------- pop学生记录,为空,输出stuStack is Empty!

3--------- pop学生记录,为空,输出stuStack is Empty!

1---------push 学生记录

4----------pop老师记录,为空输出tckStack is Empty!

2-------push 老师记录

2-------push 老师记录

最后 输出栈中的记录,先输出学生栈,再输出老师栈。

Stu:Name 刘天福 Num 0102431 Age 21

Teacher:Name 张浩   Sage 2313

Teacher:Name 张天福 Sage 2100
#include <iostream>
#include <string>
#include <stack>

using namespace std;

class StudentRecord
{
private:
    string name;
    string num;
    int age;
public:
    StudentRecord()
    {
        name = "";
        num = "";
        age = 0;
    }
    StudentRecord(string _name,string _num,int _age )
    {
        name=_name;
        num=_num;
        age=_age;
    }
    string GetName()
    {
        return name;
    }
    string GetNum()
    {
        return num;
    }
    int GetAge()
    {
        return age;
    }
    void Show()
    {
        cout << "Stu:" << "Name " << name << " Num " << num << " Age " << age << endl;
    }
    ~StudentRecord()
    {

    }
};
class TeacherRecord
{
private:
    string name;
    long int tage;

public:
    TeacherRecord()
    {
        name = "";
        tage = 0;
    }
    TeacherRecord(string _name,long int _tage)
    {
        name=_name;
        tage=_tage;
    }
    string GetName()
    {
        return name;
    }
    long int Getage()
    {
        return tage;
    }
    void Show()
    {
        cout << "Teacher:Name " << name << " Sage " << tage << endl;
    }
    ~TeacherRecord()
    {

    }
};


int main()
{
    stack<StudentRecord> SStack;
    stack<TeacherRecord> TStack;

    StudentRecord SRecord;
    TeacherRecord TRecord;
    int amount, manipulate, age;
    string num,name;
    long int tage;

    cin >>amount;
    for (int i = 0; i < amount; ++i)
    {
        cin >> manipulate;
        switch(manipulate)
        {
        case 1:
            cin >> name >> num >> age;
            SStack.push(StudentRecord::StudentRecord(name, num, age));
            break;
        case 2:
            cin >> name >> tage;
            TStack.push(TeacherRecord::TeacherRecord(name, tage));
            break;
        case 3:
            if (!SStack.empty())
            {
                SStack.pop();
            }
            else cout << "stuStack is Empty!" << endl;
            break;
        case 4:
            if (!TStack.empty())
            {
                TStack.pop();
            }
            else cout << "tckStack is Empty!" << endl;
            break;
        }
    }
    while (!SStack.empty())
    {
        SRecord = SStack.top();
        SRecord.Show();
        SStack.pop();
    }
    while (!TStack.empty())
    {
        TRecord = TStack.top();
        TRecord.Show();
        TStack.pop();
    }
    return 0;
}
郑重声明:所有日志内的题目原型均来自学校OJ系统,未经允许不得非法引用,源码部分不受此限制。
 
c++ | 阅读 1908 次
文章评论,共0条
游客请输入验证码
浏览69238次