Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rom nums c++.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- char input[99];
- //bool op = false;
- int i = 0;
- int num[99];
- char op;
- int inp(char input[],int num[], int i)
- {
- //Converts array values to numbers
- int tot = 0; //total
- for (i = 0; i <= 99; i++)
- {
- switch (input[i])
- {
- case 'm':
- case 'M':
- num[i] = 1000;
- break;
- case 'D':
- case 'd':
- num[i] = 500;
- break;
- case 'C':
- case 'c':
- num[i] = 100;
- break;
- case 'L':
- case 'l':
- num[i] = 50;
- break;
- case 'X':
- case 'x':
- num[i] = 10;
- break;
- case 'V':
- case 'v':
- num[i] = 5;
- break;
- case 'I':
- case 'i':
- num[i] = 1;
- break;
- case 0:
- num[i] = 0;
- break;
- default:
- cout << "You have not input a valid numeral. Please input a Roman numeral using the following numbers in descending order. Maximum value 4000."
- << endl << "M = 1000 \n D = 500 \n C = 100 \n L = 50 \n X = 10 \n V = 5 \n I = 1\n";
- tot = -1; goto flop;
- break;
- }
- }
- //Code to output all input numerals and all the numbers they mean
- //for (i = 0; i <= 99; i++)
- //{
- // cout << input[i] << " - " << num[i] << endl;
- //}
- //Checks for descending order
- for (i = 0; i <= 99; i++)
- {
- if (num[i] < num[i + 1])
- {
- cout << "The numeral must be input in descending order." << endl;
- tot = -1; goto flop;
- }
- }
- //checks for use of smaller numbers where a bigger number could be used
- int I = 0;
- int V = 0;
- int X = 0;
- int L = 0;
- int C = 0;
- int D = 0;
- int M = 0;
- for (i = 0; i <= 99; i++)
- {
- if (num[i] == 1)
- {
- I = I + 1;
- }
- if (num[i] == 5)
- {
- V = V + 1;
- }
- if (num[i] == 10)
- {
- X = X + 1;
- }
- if (num[i] == 50)
- {
- L = L + 1;
- }
- if (num[i] == 100)
- {
- C = C + 1;
- }
- if (num[i] == 500)
- {
- D = D + 1;
- }
- if (num[i] == 1000)
- {
- M = M + 1;
- }
- }
- if ((I >= 5) || (V >= 2) || (X >= 5) || (L >= 2) || (C >= 5) || (D >= 2))
- {
- cout << "You have input more numerals than needed - for example, 5 'I's - where it would be appropriate to use a 'V' instead" << endl;
- goto flop; tot = -1;
- }
- //adds all the numbers up
- for (i = 0; i <= 99; i++)
- {
- tot = tot + num[i];
- }
- cout << "Your number is: " << tot << endl;
- flop:
- return(tot);
- //Asks if the user wishes to restart the program
- // cout << "Input 'start' to restart, or anything else to quit";
- // char end[99];
- // cin >> end; cout << endl;
- // if (end == "start"){ goto start; }
- // return 0;
- }
- int main()
- {
- //second input
- start:
- cout << "Please input your first number and press enter" << endl; //Prints a request for the input, and then starts a new line
- start2:
- cout << "Input: "; cin >> input; //Takes the input
- int num1 = inp(input, num, 0);
- if (num1 == -1) {goto start;}
- //first input
- cout << "Please input your second number and press enter" << endl; //Prints a request for the input, and then starts a new line
- cout << "Input: "; cin >> input; //Takes the input
- int num2 = inp(input, num, 0);
- if (num2 == -1) {goto start;}
- oper:
- cout << "Would you like to add or subtract?" << endl << "Input 'a' to add or 's' to subtract'" << endl;
- cin >> op;
- int total = 0;
- switch (op)
- {
- case 'a':
- case 'A':
- total = num1 + num2;
- break;
- case 's':
- case 'S':
- total = num1 - num2;
- if (total <= 0) {cout <<"The answer cannot be zero or negative." << endl << "The second number is subtracted from the first."; goto start;}
- break;
- }
- cout << "The answer is " << total;
- char out[20];
- for (i = 0; total<=0; i++)
- {
- if(total>=1000) {total = total-1000; out[i] = 'M';}
- else if(total>=500) {total = total-500; out[i] = 'D';}
- else if(total>=100) {total = total-100; out[i] = 'C';}
- else if(total>=50) {total = total-50; out[i] = 'L';}
- else if(total>=10) {total = total-10; out[i] = 'X';}
- else if(total>=5) {total = total-5; out[i] = 'V';}
- else if(total>=1) {total = total-1; out[i] = 'I';}
- else {out[i] = ' ';}
- }
- cout << " or ";
- for(i = 1; i <=20, i++)
- {cout << out[i];}
- cout endl;
- goto start;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment