Итак, у меня есть одна моя функция, но функции, чтобы проверить, содержит ли введенная пользователем строка только римские цифры (M, D, V, X, C, L, I) и преобразовать римские цифры в арабские числа (1,2,3,4 и т. Д.) Не работают. Может кто-нибудь помочь ?? Мне нужно отформатировать в C ++.
Я проверил функцию romanValue, и она прекрасно работает, но мне нужно отредактировать функцию convertRomantoArabic, чтобы получить входную строку, проверить ее с помощью функции romanValue и суммы значений римских цифр и отобразить общее значение для экран.
это вся моя программа:
#include <iostream>
#include <string>
#include <cassert>
#include <cstddef>
using namespace std;
//This function searches in the string to see if the string only contains
//Roman Numeral values
//Pre:none
//Post:none
string isRoman(string roman){
for(const auto& c:roman){
if(!(c == 'M' || c == 'D' || c == 'C' ||
c == 'L' || c == 'X' || c == 'V' || c == 'I')){
cout<<"Not Roman";
}
cout<<"Is Roman";
}
}
//This Function finds the Roman Numeral and returns the value
//Pre: none
//Post: none
int romanValue(char roman){
switch(roman){
case'M':
return 1000;
case'D':
return 500;
case'C':
return 100;
case'L':
return 50;
case 'X':
return 10;
case'V':
return 5;
case'I':
return 1;
default:
cout<<"Not a Roman Value: "<<roman<<endl;
return 0;
}
}
//This Function Takes the Roman Numerals entered, finds their value, Adds up
//Adds up their value and then converts them to Arabic numbers.
//Pre: none
//Post: None
int convertRomantoArabic(string arabic){
int i,ans =0, p=0;
int n = arabic.length()-1;
for(i = n; i > 0;i--){
if(romanValue(arabic[i] >= p)){
ans = ans + romanValue(arabic[i]);
}
ans = ans - romanValue(arabic[i]);
p = romanValue(arabic[i]);
}
return ans;
}
int main()
{
string romanLet;
int getRomanValue;
cout<<"Please enter a Roman Numeral: ";
cin>>romanLet;
string findRoman = isRoman(romanLet);
cout<<findRoman;
//getRomanValue = convertRomantoArabic(romanLet);
//cout<<getRomanValue;
return 0;
}