C++ 铺瓷砖问题

题目:(全英文)

我们的题目
下面附上中文翻译:

你在一家铺瓷砖的公司工作,他们需要一个程序来估算一份工作需要多少箱瓷砖。一项工作是通过计算每个房间的英尺和英寸的尺寸,然后将这些尺寸转换为瓷砖大小的倍数(取整数的部分倍数),然后再相乘得到房间的瓷砖数量。一个盒子里有20块瓷砖,所以需要将总数除以20,四舍五入得到盒子的数量。假定这些瓦片是方形的。该程序最初应该提示用户瓷砖的尺寸(以英寸为单位)和要输入的房间数量。然后它应该输入每个房间的尺寸,并输出该房间所需的瓷砖。输入最后一个房间后,程序还应该输出所需瓷砖的总数、所需盒子的数量以及剩余的瓷砖的数量。

下面是一个run可能出现的例子:
输入房间数:2
输入瓷砖的尺寸(英寸):12
输入房间宽度(英尺和英寸,间隔空间):17 4 1
输入房间长度(英尺和英寸,间隔空间):9 3
房间需要180块瓷砖。
输入房间宽度(英尺和英寸,间隔空间):11 6
输入房间长度(英尺和英寸,间隔空间):11 9
房间需要144块瓷砖
总共需要324块瓷砖。
需要的箱子数量是17个
将会有额外的16个瓷砖。

使用功能分解来解决这个问题,并在任何有意义的地方使用函数来编写解决方案。您的程序应该检查无效数据,如非正尺寸、小于1的房间数、大于11的英寸数,等等。当检测到无效输入时,它应该提示正确的输入。现在你的程序正在变成。更复杂的是,更重要的是要使用正确的缩进和样式、有意义的标识符和适当的注释。

下面是翻译截图:
在这里插入图片描述

这里是代码区:

// source code of programming problem 7.2 ,我们书上第七章的题目
#include<iostream>
#include<cmath>
using namespace std;

void Getroomnum(int& roomnum)//这里用引用,直接修改roomnum的值
{
  cout<<"Enter number of rooms:";
  cin>>roomnum;
  
  while(roomnum<=1)
  {
    cout<<"Invalid data,enter again :";
    cin>>roomnum;
  }
}
 
void Getroomsize(int& size)//同上一个注释,这里瓷砖尺寸
{
    cout<<endl<<"Enter size of tile :";
    cin>>size;
    
    while(size<=0)
      {
        cout<<"Invalid data,enter again:";
        cin>>size;
      }
}

//房间宽度、长度,这个函数以及它的下边的函数
void Getroomwidth(int& widfeet,int& widinch)
{
     cout<<endl<<"Enter room width(feet and inches):";
     cin>>widfeet>>widinch;
     
     while(widfeet<=0)
      {
      cout<<endl<<"Invalid data,enter again:";
      cin>>widfeet;
      }
      
      while(widinch<=0||widinch>=12)
      {
      cout<<endl<<"Invalid data,enter again:";
       cin>>widinch;
       }
}

void Getroomlength(int& lengthfeet,int& lengthinch)
{
   cout<<endl<<"Enter room length(feet and inches):";
   cin>>lengthfeet>>lengthinch;
   
   while(lengthfeet<=0)
    { 
      cout<<endl<<"Invalid data,enter again:";
      cin>>lengthfeet;
     }
     
    while(lengthinch<=0||lengthinch>=12)
    {
      cout<<endl<<"Invalid data,enter again:";
      cin>>lengthinch;
     }
}

int main()
{
    int totaltile=0,roomnum,size,widfeet,widinch,lengthfeet,lengthinch;
    int extratile,width,length;
    float tilenum,boxnum;

    Getroomnum(roomnum);
     Getroomsize(size);
     
     while(roomnum)
      {
          Getroomwidth(widfeet,widinch);
          Getroomlength(lengthfeet,lengthinch);
          
          //1英尺=12英寸
          if( (widfeet*12+widinch) % size == 0 )
              width=widfeet*12+widinch;
          else
            width=( int( (widfeet*12+widinch) / size) +1 )*12;
            
           if( (lengthfeet*12+lengthinch) % size == 0 )
               length=lengthfeet*12+lengthinch;
           else
               length=( int( (lengthfeet*12+lengthinch) / size)+1 )*12;

           tilenum=(width*length) / (size*size);
           cout<<endl<<"Room requirs "<<tilenum<<" tiles."<<endl;
           totaltile=totaltile+tilenum;
          roomnum--;
      }
      
       cout<<"Total tiles required is "<<totaltile<<"."<<endl;
       if(totaltile%20 == 0)
           boxnum = totaltile / 20;
       else
           boxnum = int(totaltile/20) + 1;
           
        cout<<"Number of boxes needed is "<<boxnum<<"."<<endl;

        extratile = boxnum*20 - totaltile;
        
        cout<<"There will be "<<extratile<<" extra tile."<<endl;//就是不满一箱的多余出来的
        
        return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

懒回顾,半缘君

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

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

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

打赏作者

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

抵扣说明:

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

余额充值