C++ Primer Plus P52 程序清单3.12(使用整数除法来计算合多少英石,再用求模运算符来计算余下多少磅)——中职

C++ Primer Plus P52 程序清单3.12
该程序使用整数除法来计算合多少英石,再用求模运算符来计算余下多少磅

将磅转换为英石,一英石等于14磅。

/*
C++ Primer Plus P52 程序清单3.12
该程序使用整数除法来计算合多少英石,再用求模运算符来计算余下多少磅

将磅转换为英石,一英石等于14磅。
*/

//头文件
#include<iostream>

//主函数
int main(void)
{
    
    
	using namespace std;						//编译指令
	const int Lbs_per_stn = 14;					//定义常变量(磅)为14
	int lbs;

	cout << "Enter your weight in pounds:";		//提示用户输入
	cin >> lbs;									//输入信息
	
	int stone = lbs / Lbs_per_stn;				//计算合算多少英石
	int pounds = lbs % Lbs_per_stn;				//计算余下多少磅

	cout << lbs << " pounds are " << stone << " stone, " << pounds << " pound(s)." << endl;			//输出值

	return 0;
}

本文重点是取余运算

	int stone = lbs / Lbs_per_stn;				//计算合算多少英石
	int pounds = lbs % Lbs_per_stn;				//计算余下多少磅

取余运算

	int pounds = lbs % Lbs_per_stn;				//计算余下多少磅

感谢观看

再次感谢~

猜你喜欢

转载自blog.csdn.net/qq_51212951/article/details/113428492