c++ 类与对象应用题

#include<bits/stdc++.h>
using namespace std;
class tank
{
    private:
        int F;
        int vin;
        int vout;
        int gravity;
    public:
        tank():F(0),vin(0),vout(0),gravity(0){    }
        ~tank(){
		};
        void set(int f,int in,int out)
        {
            F=f;
            vin=in;
            vout=out;
        }
        int getin(int t0,int te)
        {
            gravity+=(te-t0)*vin;
            return gravity;
        }
        int getout(int t0,int te)
        {
            gravity-=(te-t0)*vout;
            if(gravity<=0)
            {
                gravity=0;
            }
               return gravity;
        }
        int getresult()
        {
            if(gravity<F)
            {
                return gravity;
            }
            else
            {
                return F;
            }
        }
}; 
int main()
{
    tank t1;
    t1.set(10000,200,160);
    tank t2;
    t2.set(5000,150,100);
    tank t3;
    t3.set(5000,100,120);
    t1.getin(0,30);
    t2.getin(20,50);
    t3.getin(40,50);
    t3.getout(60,70);
    t1.getout(60,80);
    int res=t1.getresult()+t2.getresult()+t3.getresult();
    cout<<res;
    return 0;
    
}
假设某水果店销售 4 种水果:苹果 A (每个重量 0.1kg 10 /kg )、梨 B (每个重量 0.1kg
12 /kg )、桃 C (每个重量 0.05kg 14 /kg )、菠萝 D (每个重量 0.4kg 16 /kg )。例如一顾客
拿出一张 100 元面额的纸钞,要购买一批水果: AAAAABBBBCCCDD ,即 5 个苹果、 4 个梨、 3
个桃、 2 个菠萝,水果店需要给该批水果称重、计算应付金额、找零钱。
请设计水果店 完成称重、计算应付金额、找零钱等功能,顾客 完成购买商品的行为;请
设 计 有 关 代 码 模 拟 实 现 水 果 店 与 该 顾 客 之 间 的 交 易 。 顾 客 购 买 的 商 品 模 拟 为 字 符 串
AAAAABBBBCCCDD ”,请将该字符串作为输入参数。

 

#include<bits/stdc++.h>
using namespace std;
class CFruit_stand
{
	public:
		CFruit_stand();
		~CFruit_stand();
		double weight;
		double price;
		void charge(char*,double);
		void print();
};
CFruit_stand::CFruit_stand()
{
	weight=0;
	price=0;
}
CFruit_stand::~CFruit_stand()
{
	
} 
void CFruit_stand::charge(char *p,double money)
{
	while(*p!='\0')
	{
		if(*p=='A')
		{
			weight+=0.1;
			price+=0.1*10;
		}
		else if(*p=='B')
		{
			weight+=0.1;
			price+=0.1*12;
		}
		else if(*p=='C')
		{
			weight+=0.05;
			price=0.05*14;
		}
		else if(*p=='D')
		{
			weight+=0.4;
			price+=0.4*16;
		}
		p++;
	}
	cout<<"收顾客钱"<<money<<endl;
	cout<<"水果的总价"<<price<<endl;
	cout<<"水果的总重"<<weight<<endl;
	cout<<"应找钱"<<money-price<<endl;
}
int main()
{
	CFruit_stand a;
	a.charge("AAAAABBBBCCCDD",100);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值