获取英寸的输出
Getting output for inches
下面是我编写的代码,用于将用户输入的英寸转换为英里,码,英尺。我唯一的问题是,我希望输出的格式也要求输出具有"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;
这可能更接近您想要的:
// ...
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;
// ...
我的猜测是您希望计算级联,例如它需要英寸并将其发挥到尽可能大的单位。 所以 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;
}
相关文章:
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 我无法获取以下代码的输出
- 从C++应用获取PowerShell脚本输出
- 我无法从 C# 获取 PInvoke 的输入参数以C++ DLL 以用作 IntPtr 的输出
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 使用提升过程获取 shell 命令的标准输出
- 从 exprTk 获取向量作为输出
- 如何使用 c++ libboost 运行进程并获取其输出?
- 从用户获取输入/输出
- 从 c++ 中的实例化进程获取输出的可靠方法是什么?
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- TFLite 隔离错误,通过获取C++输入和输出
- 在集成终端上运行vscode lldb调试器时,如何获取程序的输出?
- 使用 boost::regex 从目录中获取带有一些正则表达式的文件名称时出现意外输出
- 使用 qt 或 c++ 从桌面获取输入和输出
- 如何从amd gpus的显示输出中获取图像数据
- 获取使用p4api运行的Perforce命令的输出
- 获取C++输出流中元素的大小
- 如何从进程获取实时、非阻塞输出
- 异步输入和输出/获取前缀后的输入