C++ 摄氏温度和华氏温度的转换

(摄氏温度和华氏温度)实现下面的整数函数:

a)celsius 函数返回华氏温度相应的摄氏温度。

b)fahrenheit函数返回摄氏温度相应的华氏温度。

c)利用上面两个函数编写一个程序,打印0~100之间所有摄氏温度对应的华氏温度的图表和32~212之间所有华氏温度对应的摄氏温度的图表。

要求在保证可读性的前提下尽量减少输出的行数,把输出结果打印成整齐的表格形式。

实现代码如下:

//华氏温度与摄氏温度
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int celsius( int j )//return celsius(摄氏温度 )
{
	return 5*(j-32)/9;	
}
int fahrenheit( int k )//return fahrenheit(温度 )
{
	return 32+k*9/5;
}
	
int main()
{
	int celsius( int j );
	int fahrenheit( int k );
	
	cout << "Celsius corresponding to Fahrenheit:" << endl << endl;
	
	for( int i=0 ; i<100 ; i++ )
	{
		cout << "F:" <<setw(3)<< i <<" "<< "C:" <<setw(3)<< celsius(i) << "  " ;
		if(i%9==0)
		cout << endl;
	}
	cout << endl;
	
	cout << "Celsius corresponding to Fahrenheit:" << endl << endl;
		for( int i=32 ; i<=212 ; i++ )
	{
		cout << "C:" <<setw(3)<< i <<" "<< "F:" <<setw(3)<< fahrenheit(i) << "  " ;
		if(i%9==0)
		cout << endl;
	}
	return 0;
}  

Guess you like

Origin blog.csdn.net/weixin_74287172/article/details/130635289