【问题标题】:How do I format my two function outputs so they are next to each other?如何格式化我的两个函数输出,使它们彼此相邻?
【发布时间】:2020-02-08 22:49:48
【问题描述】:

我正在为班级工作,但我一直无法获得正确的输出。

#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

【问题讨论】:

  • 使用 2 个字符串流,每个都用作单个页面的“列”。当每一列中的所有行都完成时,然后从每一列适当地 cout 到 std::cout。

标签: c++ function formatting format


【解决方案1】:

您的方法存在一个设计缺陷。

您为转换函数“celsiusToFahrenheit”和“fahrenheitToCelsius”分配了额外的任务。在您的方法中,它们也会生成输出。功能/任务应该在您的程序中分开。您甚至可能已经注意到,您不使用函数参数。

所以,让我们重新设计和重构代码并做一些改进:

  1. 我们从函数中删除所有 const 字面量,并在程序顶部将它们定义为 constexpr 值。这样,我们就可以在一个中心位置更改一个值,这将对所有功能产生影响。
  2. 输出屏幕上的列宽是计算值。这取决于使用时间最长的文本。
  3. 我们在main函数上面定义了转换函数。这消除了前向声明的需要
  4. 转换函数被精简为它们的基本功能
  5. 我们将所有 ios 标志与所有其他操纵器一起放入输出流中
  6. 我们为一行中的 2 个不同的长列表的输出定义了一个逻辑。基本上,我们检查是否应该打印转换。如果完成了摄氏度到华氏度的部分,那么我们将为当前行打印空格
  7. 我们计算下一个要在不同地方转换的值
  8. 我们在一个循环中运行所有这些。在循环开始时,我们假设我们已经完成(并且应该停止循环),但是,如果我们打印一个值,那么我们将继续循环。

请注意。有许多不同的解决方案。其他解决方案也非常短。但我创建了这个“冗长”的解决方案,以便您更好地理解。请看我在代码中添加了很多 cmets。应该始终这样做。否则其他人都看不懂编码的内容,一个月后你也看不懂。 . .

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多