第16周作业

第一题

这一个题目的意义是想启发同学们继承的实际应用场景,当然最后结果那里出了乌龙,分和角的位置反了,大家看看类图即可,(这里的类图和题目要求的有所不同,chicken和soda并没有作为新的类,实际上如果要实际应用这种结构是应该要单独列出的,但这题反正难度也不大,随意一点也可)
在这里插入图片描述
对应的代码如下:
foodStore.h

#ifndef FOODSTORE_H
#define FOODSTORE_H
#include <iostream>
#include "food.h"
using namespace std;
class foodStore{
    public:
        food chicken;
        food soda;
        int total = 0;
        void setChickenPrice(int yuan, int jiao)
        {
            chicken.setPrice(yuan, jiao);
        }
        void setSodaPrice(int yuan, int jiao)
        {
            soda.setPrice(yuan, jiao);
        }
        void setAllPrice(int y1, int j1, int y2, int j2)
        {
            chicken.setPrice(y1, j1);
            soda.setPrice(y2, j2);
        }
        void calPrice(int numChicken, int numSoda)
        {
            cout <<"欢迎光临!"<<endl;
            total = 0;
            total += numChicken * chicken.p.totalInFen;
            total += numSoda * soda.p.totalInFen;
        }
};
class K:public foodStore
{
    public:
    void calPrice(int numChicken, int numSoda)
        {
            foodStore::calPrice(numChicken, numSoda);
            if(total>10000)
            {
            total -= 1800;
            }
            else  if(total>5000)
            {
                total -= 600;
            }
            else if(total>2000)
            {
                total -= 200;
            }
            price tem;
            tem.cal(total);
            printf("你的商品在K记需要%d元%d分%d角\n", tem.yuan, tem.jiao, tem.fen);
        }
};
class M:public foodStore
{
    public:
    void calPrice(int numChicken, int numSoda)
        {
            foodStore::calPrice(numChicken, numSoda);
            if(total>4000)
            {
                total = 4000 + 0.9 * (total - 4000); 
            }
            price tem;
            tem.cal(total);
            printf("你的商品在M记需要%d元%d分%d角", tem.yuan, tem.jiao, tem.fen);
        }
};
#endif

food.h

#ifndef FOOD_H
#define FOOD_H
#include "price.h"
class food
{
    public :
        price p;
        food(){};
        food(int yuan, int jiao):p(yuan, jiao, 0){}
        void setPrice(int yuan, int jiao)
        {
            p.reset(yuan, jiao, 0);
        }
};
#endif

main.cpp

#include <iostream>
#include "food.h"
#include "foodStore.h"
#include "price.h"
int main()
{
    K kfc;
    M mcdonalds;
    int a,b,c,d;
    scanf("%d元%d角 %d元%d角", &a, &b, &c, &d);
    kfc.setAllPrice(a,b,c,d);
    scanf("%d元%d角 %d元%d角", &a, &b, &c, &d);
    mcdonalds.setAllPrice(a,b,c,d);
    cin >>a>>b;
    kfc.calPrice(a, b);
    mcdonalds.calPrice(a, b);
}

第二题

ifstream里面有个函数eof()可以用来判断是否已经读取到文件的末尾,利用这个对读取到的数据进行循环判断,每次读取一个int并按照要求相加即可,对应的代码如下:

#include <iostream>
#include <fstream>
using namespace std;
//函数count:统计文件fin中每种服装的销售总额,并写入文件fout中
//参数fin:文件每种服装的销售情况,fout:每种服装销售总额的写入文件
//返回值:无
//说明:文件fin中,每种服装信息占一行,分别为服装编号,销售件数,每件的销售价格(整型)。
//文件fout:每种服装统计信息占一行,分别为服装编号,销售总额(整型),中间用一个空格隔开。
void count(ifstream & fin, ofstream & fout)
{
    // 请在此添加代码,补全函数count
    /********** Begin *********/
    char s[100];
    fin>>s;
    while(!fin.eof())
    {
        int i,n,c=0,t;
        fin>>n;
        for(i=0;i<n;i++)
        {
            fin>>t;
            c+=t;
        }
        fout<<s<<" "<<c<<endl;
        fin>>s;
    }
    /********** End **********/
}
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值