学堂在线 郑莉C++ 习题4

C5-1 “鱼额宝

(100/100 分数)
题目描述

请实现一个“鱼额宝”类,下面已给出代码模板,请根据main函数中对该类的操作,补充类实现部分完成代码。

“鱼额宝”类可以记录账户余额、存钱、取钱、计算利息。该类中有一个私有静态成员变量profitRate存储“鱼额宝”的利率,可以用共有静态成员函数setProfitRate修改利率的值。程序输入为第1天至第n天连续n天的账户操作,每天只能进行一次账户操作,或存或取,每一天产生的利息是前一天的账户余额与“鱼额宝”利率的乘积,产生的利息当天也将存入账户余额,由于第1天之前账户不存在,所以第1天一定是新建账户并存钱,且当天不会有利息存入余额。程序在接受n天操作的输入后,要求计算出第n天操作完成后的账户余额并输出。

输入描述

每个测例共 n+2 行

第 1 行输入一个整数 n ,表示接下来有 n 天的操作

第 2 行输入一个实数,为 “ 鱼额宝 ” 的利率, n 天中利率不变

接下来有 n 行,代表 n 天的操作,每行有 2 个数,第 1 个数或 0 或 1 , 0 表示存钱, 1 表示取钱,第二个实数为存取的金额

1 <= n <= 20

 

输出描述

对于每一个测例,输出第n天操作完成后的账户余额

样例输入

3
0.1
0 10
0 10
1 10

样例输出

13.1

#include <iostream>
using namespace std;

class Yuebao
{
private:
    static double profit_rate;
    double balance;
public:
    static void setProfitRate(double rate);
    Yuebao(int t):balance(t){}
    void addProfit();
    void deposit(double t);
    void withdraw(double t);
    double getBalance();
};

double Yuebao::profit_rate ;

void Yuebao::setProfitRate(double rate)
{
    Yuebao::profit_rate = rate;
}

void Yuebao::addProfit()
{
    balance += balance * Yuebao::profit_rate;
}

void Yuebao::deposit(double t)
{
    balance += t;
}

void Yuebao::withdraw(double t)
{
    balance -= t;
}

double Yuebao::getBalance()
{
    return balance;
}

int main()
{
    int n;
    while (cin >> n)
    {
        double profitRate;
        cin >> profitRate;
        Yuebao::setProfitRate(profitRate);//设定鱼额宝的利率
        Yuebao y(0); //新建鱼额宝账户,余额初始化为0
        int operation;//接受输入判断是存还是取
        double amount;//接受输入存取金额
        for (int i = 0; i < n; ++i)
        {
            y.addProfit();//加入前一天余额产生的利息
            cin >> operation >> amount;
            if (operation == 0)
                y.deposit(amount);//存入金额
            else
                y.withdraw(amount);//取出金额
        }
        cout << y.getBalance() << endl;//输出最终账户余额
    }
    system("pause");
    return 0;
}

 

POINT个数

 
(100/100 分数)
题目描述

 

通过本题目的练习可以掌握静态数据成员和静态成员函数的用法
要求设计一个点类Point,它具有两个double型的数据成员x,y。
和一个静态数据成员count ,用以记录系统中创建点对象的数目。
为该类设计构造函数和析构函数,在其中对count的值做修改,体现点的数目的动态变化。
并为其添加一个静态成员函数用以输出count的值;成员函数showPoint()用于输出点的信息。
输入描述
输入点的x坐标和y坐标,当x、y均为0的时候停止输入
输出描述
输出输入点的个数
样例输入
1 1
2 2
3 3
4 4
0 0
样例输出
4

 

#include <iostream>
using namespace std;

class Point {
public:
    Point(double xx = 0.0, double yy = 0.0) :x(xx), y(yy) { count++; }
    Point(const Point &p);
    ~Point();
    static int show_count();
    void show_point();
private:
    double x, y;
    static int count;
};

int Point::count = 0;

Point::Point(const Point&p)
{
    x = p.x; //可以访问p的私有元素?
    y = p.y;
    count++;
}

Point::~Point()
{
    count--;
}

int Point::show_count()
{
    return Point::count;
}

void Point::show_point()
{
    cout << x << " " << y << endl;
}

int main()
{
    int a, b;
    Point *save_point[100];
    int i = 0;
    while (cin >> a >> b)
    {
        if (a == b && a == 0)
        {
            cout << Point::show_count() << endl;
            break;
        }
        save_point[i] = new Point(a, b);
        i++;
    }
    system("pause");
    return 0;
}

 

输出日期时间-友元类

 
(100/100 分数)
题目描述
设计一个日期类和时间类,编写display函数用于显示日期和时间。要求将Time类声明为Date类的友元类,通过Time类中的Display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒

输入描述
年月日以及时分秒
输出描述
格式化的年月日以及时分秒
样例输入
2013 12 23
14 23 50
样例输出
2013/12/23
14:23:50

#include <iostream>
using namespace std;

class Date;

class Time
{
public:
    Time(int hh, int mm, int ss) :hour(hh), minute(mm), second(ss){}
    void display(Date &d);
private:
    int hour;
    int minute;
    int second;
};

class Date
{
public:
    friend class Time;
    Date(int yy, int mm, int dd): year(yy), month(mm), day(dd) {}
private:
    int year;
    int month;
    int day;
};

void Time::display(Date &d)
{
    cout << d.year << "/" << d.month << "/" << d.day << endl;
    cout << hour << ":" << minute << ":" << second << endl;
}

int main()
{
    int year, month, day;
    cin >> year >> month >> day;
    Date d1(year, month, day);
    int hour, minute, second;
    cin >> hour >> minute >> second;
    Time t1(hour, minute, second);
    t1.display(d1);
    return 0;
}

 

转载于:https://www.cnblogs.com/jjakj1/p/10083225.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值