C ++阿拉伯语数字给Roman Numeral& s;反之亦然

时间:2018-05-09 19:29:26

标签: c++ merge arabic roman-numerals

我正在编写一个C ++程序,可以在阿拉伯语和罗马语编号系统之间进行转换。我写了一个程序,将阿拉伯语转换为罗马语,并有另一个程序将罗马语转换为阿拉伯语 问题是我无法弄清楚如何将它们合并到一个程序中,以便用户可以输入阿拉伯语或罗马数字,因此程序会将所述数字转换为另一个。

我的问题是如何将这两个程序合并为一个?

罗马语到阿拉伯语的代码

#include <iostream>
using namespace std;
int main()
{
    char roman_Numeral;
    int arabic_Numeral = 0;

    cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
    while (cin.get(roman_Numeral))
    {

        if (arabic_Numeral > 100)
        {
            cout << "\nInvalid Value. Number must be  between I and C" << endl;
            return 0;
        }

        else if (roman_Numeral == 'C')
        {
            roman_Numeral = cin.peek();
            if (roman_Numeral == 'M' || roman_Numeral == 'D')
            {
                arabic_Numeral = arabic_Numeral - 100;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 100;
            }

        }

        else if (roman_Numeral == 'L')
        {
            roman_Numeral = cin.peek();
            if (roman_Numeral == 'M' || roman_Numeral == 'D'
                    || roman_Numeral == 'C')
            {
                arabic_Numeral = arabic_Numeral - 50;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 50;
                continue;
            }
        }

        else if (roman_Numeral == 'X')
        {
            roman_Numeral = cin.peek();
            if (roman_Numeral == 'M' || roman_Numeral == 'D'
                    || roman_Numeral == 'C' || roman_Numeral == 'L')
            {
                arabic_Numeral = arabic_Numeral - 10;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 10;
                continue;
            }
        }

        else if (roman_Numeral == 'V')
        {
            roman_Numeral = cin.peek();
            if (roman_Numeral == 'M' || roman_Numeral == 'D'
                    || roman_Numeral == 'C' || roman_Numeral == 'L'
                    || roman_Numeral == 'X')
            {
                arabic_Numeral = arabic_Numeral - 5;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 5;
                continue;
            }
        }

        else if (roman_Numeral == 'I')
        {
            roman_Numeral = cin.peek();
            if (roman_Numeral == 'M' || roman_Numeral == 'D'
                    || roman_Numeral == 'C' || roman_Numeral == 'L'
                    || roman_Numeral == 'X' || roman_Numeral == 'V')
            {
                arabic_Numeral = arabic_Numeral - 1;
                continue;
            }
            else
            {
                arabic_Numeral = arabic_Numeral + 1;
                continue;
            }
        }

        else
            break;
    }
    cout << arabic_Numeral << endl;
    return 0;
}

阿拉伯语到罗马的代码

#include <iostream>
#include <string>

#include <iomanip>
using namespace std;

#define MAX_INPUT 100 // These constants hold high and low integer numbers,
#define MIN_INPUT 1
#define ARRAY_SIZE 4   // and the array size declarator.

string convert(int digit, string low, string mid, string high);

int main()
{
    string answers[ARRAY_SIZE] = { "", "", "", "" }; //An  array of string to hold the output from the convert function.
    int accumulator = 0; // Variable to hold number of arabic numbers converted.
    int userNum = 0;
    string strUserNum;

    do
    {
        cout << "";
        cout << "Enter an arabic number between 1 and 100: ";
        cin >> strUserNum;

        userNum = std::stoi(strUserNum);

        if (userNum == 0 || userNum > MAX_INPUT)
        {
            cout << "\nInvalid Value. Number must be between 1 and 100" << endl;
            return 0;
        }
        else if (userNum == 'end')
        {
            cout << "Exiting program:";
            break;
        }

        int thous = userNum / 1000;

        int hund = userNum % 1000 / 100;

        int tens = userNum % 100 / 10;

        int ones = userNum % 10 / 1;

        answers[0] = convert(thous, "M", "M", "M");
        answers[1] = convert(hund, "C", "D", "M");

        answers[2] = convert(tens, "X", "L", "C");
        answers[3] = convert(ones, "I", "V", "X");

        cout << answers[0] << answers[1] << answers[2];
        cout << answers[3] << endl;
        cout << endl;
        break;

    } while (userNum != 'end');

    system("PAUSE");
    return 0;

}
string convert(int digit, string low, string mid, string high)
{

    if (digit == 1)
    {
        return low;
    }
    if (digit == 2)
    {
        return low + low;
    }
    if (digit == 3)
    {
        return low + low + low;
    }
    if (digit == 4)
    {
        return low + mid;
    }
    if (digit == 5)
    {
        return mid;
    }
    if (digit == 6)
    {
        return mid + low;
    }
    if (digit == 7)
    {
        return mid + low + low;
    }
    if (digit == 8)
    {
        return mid + low + low + low;
    }
    if (digit == 9)
    {
        return low + high;
    }
    if (digit == 0)
    {
        return "";
    }
}

1 个答案:

答案 0 :(得分:0)

将罗马字到阿拉伯语的代码(在主块内)放入void procedureRomanToArabic() 罗马到阿拉伯语也一样。将其放入void procedureArabicToRoman()。 删除并删除ArabicToRoman执行时,并将其移至main。

void procedureRomanToArabic(){
    char roman_Numeral;
    int arabic_Numeral = 0;

    //...

    cout << arabic_Numeral << endl;
}




void procedureArabicToRoman()
{
    string answers[ARRAY_SIZE] = { "", "", "", "" }; //An  array of string to hold the output from the convert function.
    int accumulator = 0; // Variable to hold number of arabic numbers converted.
    int userNum = 0;
    string strUserNum;

    //remove do while here
    cout << "";
    cout << "Enter an arabic number between 1 and 100: ";
    cin >> strUserNum;

    //...

    answers[2] = convert(tens, "X", "L", "C");
    answers[3] = convert(ones, "I", "V", "X");

    cout << answers[0] << answers[1] << answers[2];
    cout << answers[3] << endl;
    cout << endl;
}


int main(){
  char type;
  do{

    cout<<"2 for Arabic to Roman, 1 for Roman to Arabic, or anything else for exit";
    cin>>type;
    if(type=='1')
        procedureArabicToRoman();
    else if(type =='2')
        procedureRomanToArabic();
    else break; //end program
  } while(true);     

}