Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- using namespace std;
- int main() {
- char input; //holds the input value
- string line;
- int output = 0; //holds the output value of the result
- int n;
- while (cin.get(input)) { // loop until end of file
- input = toupper(input); // if the input is lowercase convert to upper case
- //value of the first symbol if it is Thousand (M)
- if (input == 'M')
- output = output + 1000;
- //check if the first symbol is Five hundred(D)
- else if (input == 'D') {
- input = cin.peek(); //read next character in the input stream
- input = toupper(input);
- //if the next symbol is of a higher value then subtract, else add the value to the result
- if (input == 'M') {
- output = output - 500;
- continue;
- } else {
- output = output + 500;
- continue;
- }
- }
- //check if the first symbol is Hundred (C)
- else if (input == 'C') {
- input = cin.peek(); //read next
- input = toupper(input);
- if (input == 'M' || input == 'D') {
- output = output - 100;
- continue;
- } else {
- output = output + 100;
- continue;
- }
- }
- //check if the first symbol is Fifty (L)
- else if (input == 'L') {
- input = cin.peek();
- input = toupper(input);
- output = output + 50;
- continue;
- }
- //check if the first symbol is Ten (X)
- else if (input == 'X') {
- input = cin.peek(); //read next
- input = toupper(input); //convert to upper case
- //check if next symbol is M or D or C or L
- if (input == 'C' || input == 'L') {
- output = output - 10;
- continue;
- } else {
- output = output + 10;
- continue;
- }
- }
- //check if the first symbol is Five (V)
- else if (input == 'V') {
- input = cin.peek();
- input = toupper(input);
- output = output + 5;
- continue;
- }
- //check if the first symbol is One(I)
- else if (input == 'I') {
- input = cin.peek();
- input = toupper(input);
- if (input == 'X' || input == 'V') {
- output = output - 1;
- continue;
- } else {
- output = output + 1;
- continue;
- }
- } else break;
- }
- cout << output << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement