下面是我编写的用于将用户输入英寸转换为英里,码,英尺的代码。我唯一的问题是我希望输出的格式要求输出有" 0英寸"同样。
对于我的生活,我无法想象那么多。我尝试将一个新的int值设置为英寸并让它返回0,但这让事情更加困惑。
感谢您的帮助。
#include <iostream>
using namespace std;
int
main ()
{
double m;
double y;
double f;
double i;
cout << " Enter the Length in inches:";
cin >> i;
m = i / 63360; // Convert To Miles
y = 1760 * m; // Convert To Yards
f = 3 * y; // Convert to Feet
i = 12 * f; // Convert to Inches
cout << i << "inches =" << " " << m << " " << "(mile)s," << " " <<
y << " " << "yards," << " " << f << " " << "feet," << " " << i <<
" " << "inches." << endl;
return 0;
答案 0 :(得分:2)
这可能更接近你想要的东西:
// ...
int m;
int y;
int f;
int i;
int len;
cout << " Enter the Length in inches:";
cin >> len;
cout << len << "inches = ";
m = len / 63360; // Miles
len -= m * 63360;
y = len / 36; // Yards
len -= y * 36;
f = len / 12; // Feet
i = len % 12; // Inches
if (m)
cout << m << " (mile)s, ";
if (y)
cout << y << " yards, ";
if (f)
cout << f << " feet, ";
cout << i << " inches." << endl;
// ...
答案 1 :(得分:0)
我的猜测是你希望计算级联,例如它需要几英寸,并将其作用到可能的最大单位。所以15“会去1'3”。如果我猜错了,请忽略这个答案。
#include <iostream>
using namespace std;
static const int INCHES_PER_MILE = 63360;
static const int INCHES_PER_YARD = 36;
static const int INCHES_PER_FOOT = 12;
int main ()
{
int inches, m, y, f ,i, remainder; //int so that we dont get decimal values
cout << " Enter the Length in inches:";
cin >> inches;
m = inches / INCHES_PER_MILE ; // Convert To Miles -- shouldn't be 'magic numbers'
remainder= inches % INCHES_PER_MILE ; // % stands for modulo -- (i.e. take the remainder)
y = remainder / INCHES_PER_YARD; // Convert To Yards
remainder = remainder % INCHES_PER_YARD;
f = remainder / INCHES_PER_FOOT; // Convert to Feet
remainder = remainder % INCHES_PER_FOOT;
i = remainder; // Convert to Inches
cout << inches << " inches = " << m <<" (mile)s, " <<
y << " yards, " << f << " feet, " << i << " inches." << endl;
return 0;
}