簡體   English   中英

如何格式化我的兩個函數輸出,使它們彼此相鄰?

[英]How do I format my two function outputs so they are next to each other?

我正在為課堂做這件事,但我一直無法獲得正確的輸出。

#include<iostream>
using namespace std;
double celsiusToFahrenheit (double);
double fahrenheitToCelsius (double);
int main ()
{
  double f;
  double c;
  cout.setf (ios::fixed);
  cout.precision (2);
  cout << "Celsius \t" << "Fahrenheit \t" << "| \t" << "Fahrenheit \t" << "Celsius" << endl;
  cout << fahrenheitToCelsius (c) << "\t\t" << celsiusToFahrenheit (c) <<
    endl;

  return 0;
}

double celsiusToFahrenheit (double f)
{
  double fahrenheit;

  for (double celsius = 40.0; celsius >= 31.0; celsius--)
    {
      fahrenheit = (9.0 / 5.0) * celsius + 32.0;
      cout << celsius << "\t\t" << fahrenheit << "\t\t|" << endl;
    }
  return fahrenheit;
}

double fahrenheitToCelsius (double c)
{
  double celsius;
  for (double fahrenheit = 120.0; fahrenheit >= 30.0;
       fahrenheit = fahrenheit - 10)
    {
      celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
      cout << fahrenheit << "\t\t" << celsius << endl;
    }
  cout << endl;
  return celsius;
}

當我運行代碼時我得到了什么

攝氏華氏| 華氏度
40.00 104.00 |
39.00 102.20 |
38.00 100.40 |
37.00 98.60 |
36.00 96.80 |
35.00 95.00 |
34.00 93.20 |
33.00 91.40 |
32.00 89.60 |
31.00 87.80 |
120.00 48.89
110.00 43.33
100.00 37.78
90.00 32.22
80.00 26.67
70.00 21.11
60.00 15.56
50.00 10.00
40.00 4.44
30.00 -1.11

-1.11 87.80

您的方法存在一個設計缺陷。

您為轉換函數“celsiusToFahrenheit”和“fahrenheitToCelsius”分配了額外的任務。 在您的方法中,它們也會生成輸出。 功能/任務應該在您的程序中分開。 您甚至可能已經注意到,您沒有使用函數參數。

所以,讓我們重新設計和重構代碼並做一些改進:

  1. 我們從函數中消除了所有 const 文字,並將它們定義為程序頂部的 constexpr 值。 有了這個,我們可以在一個中心位置更改一個值,它將對所有功能產生影響。
  2. 輸出屏幕上的列寬是一個計算值。 這取決於使用時間最長的文本。
  3. 我們在函數 main 上面定義了轉換函數。 這消除了對前向聲明的需要
  4. 轉換功能被簡化為它們的基本功能
  5. 我們將所有 ios 標志與所有其他操縱器一起放入輸出流中
  6. 我們為在一行中輸出 2 個不同的長列表定義了一個邏輯。 基本上,我們檢查是否應該打印轉換。 如果攝氏度到華氏度部分完成,那么我們將為當前行打印空格
  7. 我們在不同的地方計算下一個要轉換的值
  8. 我們在一個循環中運行所有這些。 在循環開始時,我們假設我們已經完成(並且應該停止循環),但是,如果我們打印一個值,那么我們將繼續循環。

請注意。 有許多不同的解決方案。 其他解決方案也非常短。 但我創建了這個“詳細”的解決方案,以便您更好地理解。 請看我在代碼中添加了很多注釋。 應該始終這樣做。 否則沒有人會理解編碼的內容,甚至一個月后您也不會理解。 . .

#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>

// The ranges for that we will do the calculations
// For Fahrenheit values to be converted into Celcius
constexpr double FahrenheitStart = 120.0;       // Start value (inclusive)
constexpr double FahrenheitEnd = 0.0;           // End value (inclusive)
constexpr double FahrenheitStep = 5;            // Decrement step

// For Celcius values to be converted into Fahrenheit
constexpr double CelsiusStart = 30.0;           // Start value (inclusive)
constexpr double CelsiusEnd = -5.0;             // End value (inclusive)
constexpr double CelsiusStep = 1.0;             // Decrement step

// Global texts that may be used in different places
constexpr std::string_view Fahrenheit{ "Fahrenheit" };
constexpr std::string_view Celcius{ "Celcius" };
// The delimiter
constexpr std::string_view Delimiter{ " | " };

// The field width of the columns shall be the maximum text length +1 
constexpr size_t FieldWidth = std::max(Fahrenheit.length(), Celcius.length()) + 1;


// Conversion functions
double celsiusToFahrenheit(const double celsius) {
    return (9.0 / 5.0) * celsius + 32.0;
}

double fahrenheitToCelsius(const double fahrenheit) {
    return (fahrenheit - 32.0) * 5.0 / 9.0;
}

// Print a conversion list for temperatures in Fahrenheit and Celcius
int main()
{
    // Set ios flags and print header
    std::cout << std::right << std::setiosflags(std::ios::fixed) << std::setprecision(2) <<
        std::setw(FieldWidth) << Celcius << std::setw(FieldWidth) << Fahrenheit << Delimiter <<
        std::setw(FieldWidth) << Fahrenheit << std::setw(FieldWidth) << Celcius << "\n";

    // Set start values for conversion functions
    double f = FahrenheitStart;
    double c = CelsiusStart;

    // We want to run a loop;
    bool doCalculation = true;

    // Calculate all values
    while (doCalculation) {

        // We assume that we are maybe done with all values
        doCalculation = false;

        // Check, if we have printed all Celcius values
        if ((c >= CelsiusEnd)) {

            // Print Celcius values and its conversions
            std::cout << std::setw(FieldWidth) << c << std::setw(FieldWidth) << celsiusToFahrenheit(c) << Delimiter;

            // Calculate next value to convert
            c -= CelsiusStep;

            // No, not done, continue the loop
            doCalculation = true;
        }
        else {
            // If all Celcius values have been printed already, then print spaces, if needed
            if (f >= FahrenheitEnd) std::cout << std::setw(FieldWidth * 2) << "" << Delimiter;
        }

        // Check, if we have printed all Fahrenheit values
        if (f >= FahrenheitEnd) {

            // Print Fahrenheit values and its conversions
            std::cout << std::setw(FieldWidth) << f << std::setw(FieldWidth) << fahrenheitToCelsius(f);

            // Calculate next value to convert
            f -= FahrenheitStep;

            // No, not done, continue the loop
            doCalculation = true;
        }
        std::cout << "\n";
    }
    return 0;
}

暫無
暫無

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

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