C++ Lessons

Roman Numbers Example


/*

See lesson 8.3 last paragraph for suggestions on how to use this example as a
learning exercise.  Note that I have included some error checking in the code 
below just to give you a chance to see how it might look.  In CS 10 
you are allowed to assume that the user enters valid data unless I specify 
otherwise, so you may not be using these techniques in your programs.  Because 
the error checking makes main look significantly more complex, I have provided a 
"no error checking" version of main.

Note also that there may be other approaches to the problem that are equally
good solutions.  Ask yourself if your solution is more complex than this, or
requires functions with more lines of code than this.

*/

#include <iostream>
using namespace std;

void readRomanToInt(int &num1);
void performOp(int num1, char op, int num2, int &answer);
void writeIntToRoman(int num);
void getOperands(int &num1, int &num2);
void getOperator(char &op);
void writeAnswer(int num1, int num2, char op, int answer);







/*

int main()                 // the no error checking version
{
    int num1, num2, answer;
    char op;
    
    getOperands(num1, num2);
    getOperator(op);
    performOp(num1, op, num2, answer);
    writeAnswer(num1, num2, op, answer);
}


*/







int main()
{
    int num1,num2,answer;
    char op;
    
    getOperands(num1, num2);
    if (num1 >= 0 && num2 >= 0){
        getOperator(op);
        if (op == '+' || op == '-' || op == '*' || op == '/'){
            performOp(num1, op, num2, answer);
            writeAnswer(num1, num2, op, answer);
        } else {
            cout << "invalid operator." << endl;
        }
    } else {
        cout << "invalid input." << endl;
    }
    cout << "end of program." << endl;
}









void getOperands(int &num1, int &num2)
{
    cout << "enter the first number: ";
    readRomanToInt(num1);
    if (num1 >= 0){
        cout << "The first number is " << num1 << endl;
        cout << "enter the second number: ";
        readRomanToInt(num2);
        if (num2 >= 0){
            cout << "The second number is " << num2 << endl;
        }
    }
}









void getOperator(char &op)
{
    cout << "Enter the desired arithmetic operation: ";
    cin >> op;
}










void writeAnswer(int num1, int num2, char op, int answer)
{
    cout << "The ";
    switch (op) {
        case '+': cout << "sum";        break;
        case '-': cout << "difference"; break;
        case '*': cout << "product";    break;
        case '/': cout << "quotient";   break;
    }
    cout << " of ";
    writeIntToRoman(num1);
    cout << " and ";
    writeIntToRoman(num2);
    cout << " is ";
    writeIntToRoman(answer);
    cout << "(" << answer << ")" << endl;
}










void readRomanToInt(int &num)
{
    char ch;
    bool error = false;
    
    num = 0;        
    cin.get(ch);    
    while (ch != '\n' && !error){
        switch (ch){
            case 'M':num += 1000;   break;
            case 'D':num += 500;    break;
            case 'C': num += 100;   break;
            case 'L': num += 50;    break;
            case 'X': num += 10;    break;
            case 'V': num += 5;     break;
            case 'I': num += 1;     break;
            default: num = -1;
                     error = true;
        }
        cin.get(ch);
    }
}
    
    
    
    
    
    
    
    
    
// This function should actually be a value returning function...
    
void performOp(int num1, char op, int num2, int &answer)
{
    switch(op){
        case '+': answer = num1 + num2; break;
        case '-': answer = num1 - num2; break;
        case '*': answer = num1 * num2; break;
        case '/': answer = num1 / num2; break;
    }
}









void writeIntToRoman(int num)
{
    while (num > 0){
        if (num >= 1000){
            num -= 1000;
            cout << 'M';
        } else if (num >= 500){
            num -= 500;
            cout << 'D';
        } else if (num >= 100){
            num -= 100;
            cout << 'C';
        } else if (num >= 50){
            num -= 50;
            cout << 'L';
        } else if (num >= 10){
            num -= 10;
            cout << 'X';
        } else if (num >= 5){
            num -= 5;
            cout << 'V';
        } else {
            num -= 1;
            cout << 'I';
        }
    }
}

Valid XHTML 1.0 Strict