【问题标题】:How to break a for loop and convert integers?如何打破for循环并转换整数?
【发布时间】:2013-11-18 14:43:05
【问题描述】:

我的程序需要为客户计算成本 以每码 5 美元的价格更换地毯进行安装,各种填充选项, 地毯成本和总计四舍五入到最近的院子。 填充是成本基于:

  1. 好 - 每码 3 美元 - 1-3 年保修
  2. 更好 - 每码 4 美元 3-5 年保修
  3. 最好 - 每码 5 美元 - 5-10 年保修
  4. 优秀 - 每码 7 美元,10-20 年保修

操作:

  1. 提示用户每个房间的房间数量:
  2. 提示每个房间数的长度大于宽度
  3. 计算平方英尺 a。将平方英尺转换为平方码并向上取整 b.平方码 = 房间 c 所需的码数。按平方码计算安装成本*$5
  4. 提示用户选择填充。一种。填充成本乘以房间的平方码
  5. 提示用户每平方米房间的地毯成本:通过将输入乘以所需的平方码来计算成本
  6. 需要输出总码数
  7. 输出安装成本
  8. 输出填充成本
  9. 输出地毯成本
  10. 输出总成本 = + 安装 + 填充 + 地毯
  11. 总计 = 每个房间的费用

******************/

到目前为止我有 5 个问题:

  1. 如何将整数填充选项转换为质量成本
  2. 地板环不会在房间之间断开
  3. 当它显示房间号时,它从 0 开始
  4. 如何让美元显示到小数点后 2 位?
  5. 我如何将每个房间的总数存储为双打以获得总计?

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <conio.h>
    #include <string>
    
    using namespace std;
    
    const float INSTALL_COST = 5;
    const float GOOD_PAD = 3;
    const float BETR_PAD = 4;
    const float BEST_PAD = 5;
    const float EXC_PAD = 7;
    const double SQU_FT_YD = 9;
    
    int main () 
    
    {
    
    int padding, rooms, numreq, squareYards;
    double length, width, squareFeet,priceSquareYard;
    double paddingCost, installFee, totalCost, carpetCost;
    
    //step 1:
    
    cout << "Enter number of rooms: ";
    cin >> numreq;
    cout << endl;
    
    //Step 2
    cout << "Enter length of room: ";
    cin >> length;
    cout << endl;
    cout << "Enter width of room: ";
    cin >> width; 
    cout << endl; 
    
    //step 3
    
    cout << "Select quality of padding:<1-4> ";
    cout << "\n1. Good - $3 per yard  - 1-3 year warranty \n2. Better - $4 per yard 3-5 year warranty \n3. Best- $5 per yard -  5-10 year warranty \n4. Excellent - $7 per yard 10-20 year warranty: ";
    cin >> padding;
    cout << "Enter price of carpeting per square yard of room: ";
    cin >> priceSquareYard;
    //step3
    
    for(int x = 0; x < numreq; x++)
    {   
    squareFeet = length * width; 
        squareYards = ((squareFeet / SQU_FT_YD) + 0.5);
        if (squareYards > 0)
            squareYards++;
        installFee = squareYards * INSTALL_COST;
        carpetCost = priceSquareYard * squareYards;
        paddingCost = squareYards * padding;
        totalCost = carpetCost + installFee + paddingCost;
        cout << "\n Room " << x << " Yards Required = " << squareYards;
        cout << "\n Room " << x << " Installation = $"  <<installFee;
        cout << "\n Room " << x << " Padding Cost = $" << paddingCost;
        cout << "\n Room " << x << " Carpet Cost = $" << carpetCost;
        cout << "\n Room " << x << " Total Cost = $" << totalCost; 
    }
    
    
    _getch();
    return 0;
    

    }

【问题讨论】:

  • 似乎第 2 步和第 3 步也应该在 for 循环内。如果您将每个房间的所有数据分组到一个类或结构中并将它们添加到一个向量中,那么以后计算和显示数据会容易得多。

标签: c++ for-loop


【解决方案1】:

要在循环内获取总数,只需在循环外存储一个变量,从 0 开始,并在必要时在循环内递增。

您可以使用break; 完全跳出循环。此外,如果您需要,continue; 会让您停止当前迭代并直接跳转到 for 循环的下一次迭代。

对于第 4 点,请查看: NumberFormat/DecimalFormat treats certain floating-point values as longs instead of doubles

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多