简体   繁体   中英

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

Im working on this for class but I am getting stuck with getting the right output.

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

what i get when i run the code

Celsius Fahrenheit | Fahrenheit 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

There is one design flaw in your approach.

You assigned additional tasks to your conversion functions "celsiusToFahrenheit" and "fahrenheitToCelsius". In your approach they do also generate output. The functionalities / tasks should be separated in your program. You may have even noticed, that you do not use the functions parameters.

So, let us redesign and refactor the code and do some improvements:

  1. We eliminate all const literals from the functions and define them on the top of the program as constexpr values. With that, we can change a value on exactly one central place and it will have an impact in all functions.
  2. The column width on the output screen is a calculated value. It depends on the longest used text.
  3. We define the conversion functions above function main. That eliminates the need for forward declarations
  4. The conversion functions are stripped down to their essential functionality
  5. We put all ios flags into the output stream, together with all other manipulators
  6. We define a logic for the output of 2 different long lists in one line. Basically, we check, if a conversion should be printed or not. If the celsius to fahrenheit part is done, then we will print spaces instead for the current line
  7. We calculate the next value to be converted in different places
  8. We run all this in a loop. At the beginning of the loop, we assume that we are done (and should stop the loop), but, if we print a value, then we will continue the loop.

Please note. There are many many different solutions. Other solutions are also very much shorter. But I created this "verbose" solution for you to get a better understanding. Please see that I added many comments in the code. This should always be done. Otherwise nobody else will understand what is coded, and even you will not understand it one month later. . .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM