A. 旅馆顾客统计(静态成员)

题目描述

编写程序,统计某旅馆住宿客人的总数和收入总额。要求输入客人的姓名,输出客人编号(2015+顺序号,顺序号4位,如第1位为0001,第2位为0002,依此类推)、姓名、总人数以及收入总额。总人数和收入总额用静态成员,其他属性采用普通的数据成员。旅馆类声明如下:

class Hotel
{
private:
    static int totalCustNum; // 顾客总人数
    static float totalEarning; // 旅店总收入
    static float rent; // 每个顾客的房租
    char *customerName; // 顾客姓名
    int customerId; // 顾客编号
public:
    // totalCustNum++,customerId按照totalCustNum生成
    Hotel(char* customer);
    ~Hotel(); //记得delete customerName
    void Display(); //相应输出顾客姓名、顾客编号、总人数、总收入
};

输入

第1行:输入旅馆单个顾客房租

第2行开始,依次输入顾客姓名,0表示输入结束, 姓名的最大字符长度为20

 

 


输出

每行依次输出顾客信息和当前旅馆信息。包括顾客姓名,顾客编号,旅馆当前总人数,旅馆当前总收入。

 

输入样例1 
150  
张三 李四 王五 0

输出样例1
张三 20150001 1 150
李四 20150002 2 300
王五 20150003 3 450

该题主要考察在类中对静态变量的使用,主要注意使用静态变量记得初始化 

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;

class Hotel
{
private:
    static int totalCustNum; // 顾客总人数
    static float totalEarning; // 旅店总收入
    float rent; // 每个顾客的房租
    char* customerName; // 顾客姓名
    int customerId; // 顾客编号
public:
    // totalCustNum++,customerId按照totalCustNum生成
    Hotel(char* customer,float evmey);
//    ~Hotel(); //记得delete customerName
    void Display(); //相应输出顾客姓名、顾客编号、总人数、总收入
};

int Hotel::totalCustNum = 0;
float Hotel::totalEarning = 0;//  记得初始化静态变量

Hotel::Hotel(char* customer,float evmey)
{
//	customerName=new char[30]; 
    rent = evmey;
    customerName = customer;
    totalCustNum++;
    customerId = totalCustNum;
    totalEarning = totalCustNum * rent;
}

//Hotel::~Hotel()
//{
//    delete []customerName;
//}

void Hotel::Display()
{
    cout << customerName<<" 2015";
    if (totalCustNum == 0) cout << "0000" << ' ';
    else if (totalCustNum > 0 && totalCustNum < 10) cout << "000" << totalCustNum << ' ';
    else if (totalCustNum >= 10 && totalCustNum < 100)cout << "00" << totalCustNum << ' ';
    else if (totalCustNum >= 100 && totalCustNum < 1000)cout << "0" << totalCustNum << ' ';
    else if (totalCustNum >= 1000 && totalCustNum <= 9999)cout << totalCustNum << ' ';
    cout << totalCustNum << ' ' << totalEarning << endl;
}
int main()
{
    float evmey;
    char name[30];
    cin >> evmey;
    while (1)
    {
        cin >> name;
        if (name[0] == '0') break;
        Hotel cust(name,evmey);
        cust.Display();
        cust.~Hotel();
    }
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZZWWWFFF_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值