Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

c++ - Converting integer to Roman Numeral

I have a test due in about four hours and one of the questions asks us to convert a user-inputed integer up to 100 into a roman numeral. I think my code is very close (I found a youtube video that I sort of used as a guide) but alas my code simply will not work :(. Can anyone spot the errors? EDIT: Ah sry sry sry, the problem is that when it compiles, it gives no Roman numerals. As in I enter a value and it gives me a blank.

    #include "stdafx.h"
#include <iostream>
#include <string>
    using namespace std;


int main()
{
    string romnum;
    int input;
    int num;
    cout << "Type in an integer: ";
    cin >> input;
    if(( input >= 101) || (input <= 0)) // <-- this is the upper bound
    {
        cout << "
 INVALID INPUT";
    }
    else
    {
        if(input = 100)
        {
            romnum + 'C';

        }
        input %= 100; // gets the remainder after dividing by 100

        if(input <= 10)
        {
            num = (input/10); // now we are dealing with number in 10s place
            if(num == 9)
            {
                romnum += "XC";
            }
            else if(num >= 5)
            {
                romnum += 'L';

                for(int i=0; i < num - 5;i++)
                {
                    romnum += 'X';
                }
            }
            else if(num == 4)
            {
                romnum += "XL";
            }
            else if(num >= 1)
            {
                for(int i=0; i>num; i++)
                {
                    romnum += 'X';
                }
                input %= 10;
            }
            if(num >= 1)
            {
                num = input; //  now we are dealing with number in ones place
                if(num == 9)
                {
                    romnum += "IX";
                }
                else if(num >= 5)
                {
                    romnum += 'V';
                    for(int i=0; i < num - 5; i++)
                    {
                        romnum += 'I';
                    }
                }
                else if(num == 4)
                {
                    romnum += "IV";
                }
                else if(num >= 1)
                {
                    for(int i = 0; i < num; i++)
                    {
                        romnum += 'I';
                    }
                }
        cout << "The Roman Numeral is: " << romnum;

            }
        }



        cout << "The Roman Numeral is: " << romnum;
    }
    int f;
    cin >> f;
    return 0;
}

enter code here
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B

std::string to_roman(unsigned int value)
{
    struct romandata_t { unsigned int value; char const* numeral; };
    const struct romandata_t romandata[] =
    {
        {1000, "M"}, {900, "CM"},
        {500, "D"}, {400, "CD"},
        {100, "C"}, { 90, "XC"},
        { 50, "L"}, { 40, "XL"},
        { 10, "X"}, { 9, "IX"},
        { 5, "V"}, { 4, "IV"},
        { 1, "I"},
        { 0, NULL} // end marker
    };

    std::string result;
    for (const romandata_t* current = romandata; current->value > 0; ++current)
    {
        while (value >= current->value)
        {
            result += current->numeral;
            value -= current->value;
        }
    }
    return result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...