c++ - 罗马数字到阿拉伯语 C++
问题描述
我在这里有这个程序,它将罗马数字转换为阿拉伯语,但是我有一些如何使它成为一个循环并且在让它不成为一个连续循环方面遇到了问题。我只希望它运行用户输入罗马数字,然后输出阿拉伯数字。例如,我输入V,它显示5,然后程序结束。我怎样才能做到这一点。我尝试在下面删除 int main() { 中的 while 部分,但它似乎没有多大作用。有什么办法让它不循环?
#include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void error(const string& a) {
throw runtime_error(a);
}
class Roman {
public:
string roman = "";
int number;
int as_int() { return number; }
string int_to_roman(int number);
void think(const char& a, const char& b, const char& c, int& divided, const int & dividee); //meant to ensure the roman permissible
void roman_to_int(Roman a);
};
void Roman::think(const char& a, const char& b, const char& c, int& divided, const int & dividee) {
string name = "";
int number = divided;
number /= dividee;
if (number < 1) return;
if (number == 1)
name += a;
if (number == 2)
name = name+a+a;
if (number == 3)
name = name+a+a+a;
if (number == 4)
name = name + a+b;
if (number == 5)
name = b;
if (number == 6)
name = name + b+a;
if(number==7)
name = name + b+a+a;
if(number==8)
name = name + b+a+a+a;
if(number==9)
name = name + a+c;
if(number== 10)
name += c;
int z = divided;
divided = (z - (number * dividee));
roman += name;
}
string Roman::int_to_roman(int number) {
if (number > 4999) error("Doesn't support numbers higher than 5000");
think('C', 'D', 'M', number, 100);
think('X', 'L', 'C', number, 10);
think('I', 'V', 'X', number, 1);
return roman;
}
void Roman::roman_to_int(Roman a){
}
ostream& operator << (ostream& os, Roman a) {
return os << a.roman;
}
int test(char ch, int how_much) {
if (cin.get() == ch) return how_much;
cin.unget();
return 0;
}
void dis(char const& a, char const& b, int& sum, int times, string& numeral) {
sum += (1*times);
int number = sum;
sum += test(a, (3*times));
if (number != sum) {
numeral += a;
return;
}
sum += test(b, (8*times));
if (number != sum) numeral += b;
}
int get_number(Roman &a) {
int sum = 0;
string numeral = "";
while (cin.get()!='\n') {
cin.unget();
char ch = cin.get();
numeral += ch;
switch (ch) {
case'I':
dis('V', 'X', sum, 1, numeral);
break;
case'V':
sum += 5;
break;
case'X': dis('L', 'C', sum, 10, numeral);
break;
case'L': sum += 50;
break;
case'C': dis('D', 'M', sum, 100, numeral);
break;
case'D': sum += 500;
break;
case'M': sum += 1000;
break;
}
}
if (numeral != a.int_to_roman(sum)) error("Impermissible roman, try again.\n");
return sum;
}
int main() {
while (true) {
try {
while (true) {
Roman a;
a.number = get_number(a);
cout << a.number << endl;
}
}
catch (exception& e) {
cerr << "error: \n" << e.what() << '\n ';
//return 1; // 1 - indicates failure
}
}
}
解决方案
推荐阅读
- c++ - 在 C++ 中重复 for 循环迭代的最佳方法是什么?
- c++ - Why do libraries create macro aliases for c++ keywords?
- reactjs - React Hook useEffect 缺少与 Useeffect 挂钩的依赖项。忽略此警告是不好的做法吗?
- python - 如何通过调用值来条件变量?
- python - 如何从python中的列表中过滤值?
- visual-studio-code - 如何使用 VSCode 调试 Lua Love2D?
- reactjs - ElevationScroll 'children' 道具需要一个'ReactElement'类型的子级