簡體   English   中英

使用C ++的編程原理和實踐第4章鑽取,第7步

[英]Programming Principles and Practice Using C++ Chapter 4 Drill, Step 7

我正在使用C ++進行編程原理和實踐,目前仍處於第4章練習的第7步。 我在這里發現了類似的問題,但是在轉換單位/值並查看更大/更小的值時,某些方法無法正常工作。 該程序運行正常,但由於某些原因,某些轉換無法正確返回,例如,如果我輸入2 m,然后2 ft。2 ft作為較大的值返回。

我知道代碼看起來可能很難看,如果可以使轉換生效,我會將其轉換為函數。 提前致謝。

int main() {
double doubNum = 0;
double smallestNum = ' ';
double largestNum = 0;
string unitOfDistance = " ";
double testNum = 0;

cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";

while (cin >> doubNum >> unitOfDistance) { //while tests to see if the input is a double and unit is legal

    //check the unitOfDistance and convert all values to cm and hold in testNum for comparison
    if (unitOfDistance == "in") { //in to cm
        testNum = doubNum * 2.54;
    }
    else if (unitOfDistance == "ft") { //ft to cm
        testNum = (doubNum * 12) * 2.54;
    }
    else if (unitOfDistance == "cm") { //cm
        testNum = doubNum;
    }
    else if (unitOfDistance == "m") { //m to cm
        testNum = doubNum * 100;
    }
    else {
        cout << "I don't know that unit.\n";
        return 0;
    }

    //check to see if testNum (the converted version of doubNum) is the smallest/largest/same value entered so far
    if (testNum < smallestNum) {
        smallestNum = doubNum;
        cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";

    }
    else if (testNum > largestNum) {
        largestNum = doubNum;
        cout << largestNum << " " << unitOfDistance << " is the largest distance entered so far.\n";

    }
    else {
        cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";
        cout << largestNum << " " << unitOfDistance << " is the largest distance entered so far.\n";
    }

    cout << "Enter another distance with unit: \n";
}}

嘗試這個

#include <iostream>
#include <limits>
using namespace std;

int main() {
    double num, result, smallest, largest;
    smallest = numeric_limits<double>::max();
    largest = numeric_limits<double>::min();
    string unit;
    cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";
    while (cin >> num >> unit) {
        if (unit == "in")       // in to cm
            result = num * 2.54;
        else if (unit == "ft")  // ft to cm
            result = (num * 12) * 2.54;
        else if (unit == "cm")  // cm
            result = num;
        else if (unit == "m")   // m to cm
            result = num * 100;
        else {
            cout << "I don't know that unit.\n";
            break;
        }
        smallest = min(smallest, result);
        largest = max(largest, result);
        cout << smallest << " cm is the smallest distance entered so far.\n";
        cout << largest << " cm is the largest distance entered so far.\n";
    }
    return 0;
}

輸入項

2 m
3 ft
6 in

輸出量

Enter a distance with a unit of measure (ft, in, cm, m):
200 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
91.44 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
15.24 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.

您的代碼有幾個問題:
1.應該用double smallestNum = DBL_MAX (或非常大的值)替換double smallestNum = DBL_MAX double smallestNum = ' '
2.就像您使用了largestNumsmallestNum來跟蹤最大值和最小值一樣,您還需要使用unitOfLargestDistanceunitOfSmallestDistance來跟蹤其對應的單位。
3。

if (testNum < smallestNum) {
    smallestNum = doubNum;
    cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";
}

在這里,你需要更新smallestNumtestNumdoubNum 所以應該像這樣:

if (testNum < smallestNum) {
    smallestNum = testNum;
    cout << doubNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";
}

對於其他2個條件,情況類似。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM