用罗马数字做一个程序,把它们变成整数
doing a program using Roman numerals and making them int
可能重复:
我的输出正确输出
这是一道家庭作业题。我知道有些罗马数字不正确。只要数值正确,我的老师就不在乎如何正确地写数字。
我对这个程序有一些问题。除了要求用户按"E"之外,我不知道还有什么其他方法可以摆脱while循环。
一旦我这样做,输出就变为cout << "The first number is " << RomanNum1 << endl;
是E,而不是V或VI或其他什么。
我应该使用函数,我不担心制作这些函数。程序的底部是我的老师希望输出的样子。任何帮助都将不胜感激。
//This programs reads in Roman numerals and outputs the correct numerical value.
//This program will do simple math using Roman numerals and output a numerical value
#include <iostream>
using namespace std;
const char One = 'I';
const char Five = 'V';
const char Ten = 'X';
const char Fifty = 'L';
const char OneHundred = 'C';
const char FiveHundred = 'D';
const char OneThousand = 'M';
const char EXIT = 'E';
const char Plus = '+';
const char Minus = '-';
const char Times = '*';
const char Divide = '/';
const int ValueOfOne = 1;
const int ValueOfFive = 5;
const int ValueOfTen = 10;
const int ValueOfFifty = 50;
const int ValueOfOneHundred = 100;
const int ValueOfFiveHundred = 500;
const int ValueOfOneThousand = 1000;
int main (){
char RomanNum1, RomanNum2;
char Operation;
int Answer;
string Response;
int ICount = 0;
int VCount = 0;
int XCount = 0;
int LCount = 0;
int CCount = 0;
int DCount = 0;
int MCount = 0;
int Sum = 0;
cin >> RomanNum1;
while (RomanNum1 != EXIT){
if (RomanNum1 == One){
ICount++;
}
else if (RomanNum1 == Five){
VCount++;
}
else if (RomanNum1 == Ten){
XCount++;
}
else if (RomanNum1 == Fifty){
LCount++;
}
else if (RomanNum1 == OneHundred){
CCount++;
}
else if (RomanNum1 == FiveHundred){
DCount++;
}
else if (RomanNum1 == OneThousand){
MCount++;
}
else {
RomanNum1 = EXIT;
}
cin >> RomanNum1;
}
cin >> RomanNum2;
while (RomanNum2 != EXIT){
if (RomanNum2 == One){
ICount++;
}
else if (RomanNum2 == Five){
VCount++;
}
else if (RomanNum2 == Ten){
XCount++;
}
else if (RomanNum2 == Fifty){
LCount++;
}
else if (RomanNum2 == OneHundred){
CCount++;
}
else if (RomanNum2 == FiveHundred){
DCount++;
}
else if (RomanNum2 == OneThousand){
MCount++;
}
else {
RomanNum2 = EXIT;
}
cin >> RomanNum2;
}
cin >> Operation;
if (Operation == Plus){
Answer = RomanNum1 + RomanNum2;
Response = "sum";
}
else if (Operation == Minus){
Answer = RomanNum1 - RomanNum2;
Response = "difference";
}
else if (Operation == Times){
Answer = RomanNum1 * RomanNum2;
Response = "product";
}
else {
Answer = RomanNum1 / RomanNum2;
Response = "quotient";
}
Sum = ValueOfOne * ICount + ValueOfFive * VCount + ValueOfTen * XCount
+ ValueOfFifty * LCount + ValueOfOneHundred * CCount + ValueOfFiveHundred
* DCount + ValueOfOneThousand * MCount;
cout << "The first number is " << RomanNum1 << endl;
cout << "The second number is " << RomanNum2 << endl;
cout << "Arithmetic operation is " << Operation << endl;
cout << "The " << Response << " of " << RomanNum1 << " and " << RomanNum2 << " is " << Sum << endl;
return 0;
}
/* here is what the output should be
Input for Run 1:
MCCXXVI
LXVIIII
+
DCX
MCI
-
LXVI
CCLXI
/
MD
XXX
/
LXVIIII
XXVIIII
*
The output for Test Run 1:
MCCXXVI
The first number is 1226
LXVIIII
The second number is 69
+
Arithmetic operation is +
The sum of 1226 and 69 is MCCLXXXXV (1295)
DCX
The first number is 610
MCI
The second number is 1101
-
Arithmetic operation is -
The difference of 610 and 1101 is -CCCCLXXXXI (-491)*/
嗨,关于罗马数字的提示。当较小的值先于较大的值时,会从较大的值中减去较小的值,并将结果添加到总数中。例如IV=4。有关更多详细信息,请查看相关的维基百科页面:http://en.wikipedia.org/wiki/Roman_numerals
所以总结部分应该修改。
一般来说,每当您在代码中看到一些重复时,请毫不犹豫地将其提取到方法中。
如果作业必须使用c++,那么是时候创建一个RomanNumeral类了:)
相关文章:
- 内联程序集printf将整数解释为地址
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 编写一个读取五个整数并执行一些任务的C++程序
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- 16 位系统中的程序如何访问大于 65535 的整数,但不能访问地址
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 在控制台上输出我从文件中读取的整数,将程序插入无限循环
- 向文件读取和写入整数的程序
- MongoDB C++ 驱动程序 - 8 位和 16 位整数?
- 编写一个程序,输入整数 n 并打印该数字的字符数(符号除外)
- 我的程序不断四舍五入到最接近的整数
- 为什么在此C++程序上添加负整数会导致正整数?
- 调用 Parse int 函数时编译 parse 整数程序时出现问题
- C++程序,将整数输入转换为字符的问题
- 我的C++程序有问题.涉及动态调整整数数组的大小
- 程序终止,而不是继续整数输入验证循环
- 查找混合整数程序的绑定约束