Skip to content

Instantly share code, notes, and snippets.

@LizardLeliel
Last active August 29, 2015 14:07
Show Gist options
  • Save LizardLeliel/37a4aa92b4595b65880b to your computer and use it in GitHub Desktop.
Save LizardLeliel/37a4aa92b4595b65880b to your computer and use it in GitHub Desktop.
This program takes a roman numeral string as a command-line parameter, and outputs its value as an integer. It does not check for the validity of the roman numeral, but it does not crash (non-roman numerals are treated as zeros; values less then the highest value to values on their right are treated as subtracted values (which is correct for iv,…
#include <stack>
#include <string>
#include <iostream>
#include "RomanConversionTools.hpp"
using namespace std;
int main (int argv, char* argc[]) {
// Examine arg count and exit if there's an incorrect parameter ammount
if (argv != 2) {
cout << "Wrong ammount of parameters" << endl;
cout << "Usage: " << argc[0] << " <valid roman numeral>" << endl;
return 1;
}
// Initalize objects and values; includes one string, stack, and two integers
string romanNumeral;
stack<int> romeValues;
unsigned int lastChar;
int lastHighValue = 0;
// Assign the second paramter to a c++ string and record its length
romanNumeral = argc[1];
lastChar = romanNumeral.size();
// Push each value (left to right) into the stack, converting each letter to an integer
for (int i = 0; i < lastChar; ++i){
romeValues.push(romeCharToInt(romanNumeral[i]));
}
// Initialize highest value variable to what's currently on the top of the stack
lastHighValue = romeValues.top();
// Take the top two values, add (or subtract) the second value to the top,
// and place the top back on the stack for each char in romeCount
for (int i = romeValues.size(); i != 1; --i) {
romeCount(romeValues, lastHighValue);
}
// Output the number and pause
cout << romanNumeral << " is equal to " << romeValues.top() << endl;
cout << "Press enter to terminate"; cin.ignore();
}
#ifndef ROME
#include <stack>
// Takes a char and returns its magnitude if it were a roman digit
int romeCharToInt(char rome);
// Take the top two numbers, add or subtract the second value to the top,
// and place the top back onto the stack
void romeCount(std::stack<int> &romanStack, int &lastHighValue);
#define ROME
#endif
#include <stack>
using namespace std;
int romeCharToInt(char rome) {
//--------------- V converts lower-case to upper-case
switch (rome > 90 ? rome-32 : rome) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
void romeCount(stack<int> &romanStack, int &lastHighValue) {
int total = romanStack.top(); romanStack.pop();
int next = romanStack.top(); romanStack.pop();
if (next > lastHighValue) lastHighValue = next;
if (next < lastHighValue) next *= -1;
total += next;
romanStack.push(total);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment