OJ Problem B: 老师的工资

Problem B: 老师的工资

Description:
假设高中老师和大学老师的工资分别是这么计算的:
1.高中老师的工资是基本工资+奖金,其中奖金是升学的学生数乘以100。
2.大学老师的工资是基本工资+绩效。其中当教学的小时数不到240小时时,每少1个小时,扣20元;当小时数多于240小时时,每多1个小时,多发40元。
如:王老师是一个高中老师,他的基本工资是1000元,有5个学生成功升学,则其工资是1000+5100=1500元。
刘老师是一个大学老师,基本工资是2000元,如果只完成了200小时的授课,则工资是2000-40
20=1200元;如果完成了250小时的授课,则工资是2000+10*40=2400元。
现在,定义Teacher、HighSchoolTeacher、UniversityTeacher的构造函数、析构函数、getSalary方法,使得main函数能正确执行并获得样例所示的结果。
Input:输入有多行。
第1行N>0,表示后面有N个测试用例。
每个测试占1行,包括1个字符、1个double数和1个int数,均为正数。
Output:见样例。

Sample Input
4
h 2900.13 20
u 3911.23 210
u 3911.34 250
h 3911.15 10

Sample Output
Teacher’s constructor.
HighSchoolTeacher’s constructor.
4900.13
HighSchoolTeacher’s deconstructor.
Teacher’s deconstructor.
Teacher’s constructor.
UniversityTeacher’s constructor.
3311.23
UniversityTeacher’s deconstructor.
Teacher’s deconstructor.
Teacher’s constructor.
UniversityTeacher’s constructor.
4311.34
UniversityTeacher’s deconstructor.
Teacher’s deconstructor.
Teacher’s constructor.
HighSchoolTeacher’s constructor.
4911.15
HighSchoolTeacher’s deconstructor.
Teacher’s deconstructor.

Append Code:

int main()
{
    int N, i, m;
    char ch;
    double basis;
    Teacher *t;
    cin>>N;
    for (i = 0; i < N; i++)
    {
        cin>>ch>>basis>>m;
        if (ch == 'h')
            t = new HighSchoolTeacher(basis, m);
        else if (ch == 'u')
            t  = new UniversityTeacher(basis, m);
        cout<<setprecision(2)<<setiosflags(ios::fixed)<<t->getSalary()<<endl;
        delete t;
    }
    return 0;
}

code:

#include <iostream>
#include <iomanip>

using namespace std;

class Teacher
{
public:
     Teacher()
     {cout<<"Teacher's constructor."<<endl;}
     virtual ~Teacher()
     {cout<<"Teacher's deconstructor."<<endl;}
     virtual double getSalary()=0;
};

class HighSchoolTeacher:public Teacher
{
public:
    double bas;
    int x;
    HighSchoolTeacher(double b,int m):bas(b),x(m)
    {cout<<"HighSchoolTeacher's constructor."<<endl;}
    ~HighSchoolTeacher()
    {
        cout<<"HighSchoolTeacher's deconstructor."<<endl;
    }
    double getSalary()
    {
        return bas+100*x;
    }
};

class UniversityTeacher:public Teacher
{
public:
    double bas;
    int x;
    UniversityTeacher(double b,int m):bas(b),x(m)
    {
        cout<<"UniversityTeacher's constructor."<<endl;
    }
    ~UniversityTeacher()
    {
       cout<<"UniversityTeacher's deconstructor."<<endl;
    }
    double getSalary()
    {
        double s=0;
        if(x<240)
        {
            s=bas-20*(240-x);
        }
        else if(x>240)
        {
            s=bas+40*(x-240);
        }
        return s;
    }
};

int main()
{
    int N, i, m;
    char ch;
    double basis;
    Teacher *t;
    cin>>N;
    for (i = 0; i < N; i++)
    {
        cin>>ch>>basis>>m;
        if (ch == 'h')
            t = new HighSchoolTeacher(basis, m);
        else if (ch == 'u')
            t  = new UniversityTeacher(basis, m);
        cout<<setprecision(2)<<setiosflags(ios::fixed)<<t->getSalary()<<endl;
        delete t;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题目的大致思路如下: 1. 首先读入原有文件中的数据,并将其保存到一个数组中; 2. 读入要插入的数据,并将其插入到数组中相应的位置; 3. 将数组中的数据写回到原有文件中。 下面是一个可能的实现: ```c++ #include <iostream> #include <fstream> #include <vector> using namespace std; struct Record { int id; string name; int age; }; int main() { // 读入原有文件中的数据 vector<Record> records; ifstream fin("data.txt"); if (fin.is_open()) { int id, age; string name; while (fin >> id >> name >> age) { records.push_back({id, name, age}); } fin.close(); } // 读入要插入的数据 Record new_record; cin >> new_record.id >> new_record.name >> new_record.age; // 将新数据插入到数组中相应的位置 int pos = -1; for (int i = 0; i < records.size(); ++i) { if (records[i].id > new_record.id) { pos = i; break; } } if (pos == -1) { records.push_back(new_record); } else { records.insert(records.begin() + pos, new_record); } // 将数组中的数据写回到原有文件中 ofstream fout("data.txt"); if (fout.is_open()) { for (const auto& record : records) { fout << record.id << " " << record.name << " " << record.age << "\n"; } fout.close(); } return 0; } ``` 其中,我们定义了一个 `Record` 结构体来表示每一条记录,然后使用一个 `vector` 来保存所有的记录。在读入原有文件中的数据时,我们使用了文件读取流 `ifstream`,在写回到文件中时,我们使用了文件写入流 `ofstream`。读入要插入的数据时,我们直接使用标准输入流 `cin`。 在将新数据插入到数组中时,我们首先需要找到相应的位置。这里我们使用了一种简单的方法,即遍历数组,找到第一个 ID 大于新数据 ID 的位置,然后将新数据插入到该位置。如果没有找到这样的位置,说明新数据 ID 是最大的,我们将其追加到数组末尾即可。在将新数据插入到数组中时,我们使用了 `vector` 的 `insert` 方法。 最后,我们将数组中的数据写回到原有文件中。在写回到文件中时,我们使用了 `ofstream` 的输出流运算符 `<<`。由于每条记录都需要以一行的形式写入文件,因此我们在输出时需要加上换行符 `\n`。 希望这个解答能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值