Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <cstdlib>
- #include <string>
- #include <stdio.h>
- #include <iostream>
- #include <conio.h>
- #include <fstream>
- #include <math.h>
- #include <cstdio>
- using namespace std;
- int value(char Roman) //Roman is the character that is presented from the integer value.
- {
- switch (Roman) //Switch statement that presents a number in return of the user input
- {
- case 'I':
- return 1; //If I is presented, 1 is returned.
- case 'V':
- return 5; //If V is presented, 5 is returned.
- case 'X':
- return 10; //If X is presented, 10 is returned.
- case 'L':
- return 50; //If L is presented, 50 is returned.
- case 'C':
- return 100; //If C is presented, 100 is returned.
- case 'D':
- return 500; //If D is presented, 500 is returned.
- case 'M':
- return 1000; //If M is presented, 1000 is returned.
- }
- }
- int convert1(string roman) { //The function "convert1" showcases how the position each roman numeral is in changes the result.
- int SecNum = value(roman[1]);//the variables of the function
- int FirNum = value(roman[0]);
- if (FirNum < SecNum) { //If the first number of the code is smaller than the second, they will produce a negative.
- return SecNum - FirNum;
- }
- else return SecNum + FirNum; //However if the first number is larger, they will produce a positive.
- }
- int convert2(string roman) { //The function "convert2" will work for any roman numeral with more than two characters and for the 'I' variable.
- int sum = value(roman[roman.length() - 1]);
- int CurNum;
- int LastNum = value(roman[roman.length() - 1]); //The variables that showcase the current number, last number and the overall sum of the roman numerals converted to produce the final number.
- for (int a = roman.length() - 2; a >= 0; a--) {
- CurNum = value(roman[a]);
- if (LastNum > CurNum) {
- sum = sum - CurNum; //If the last number in the code is larger than the current number, they will take away from eachother.
- }
- else if (LastNum <= CurNum) {
- sum = sum + CurNum; //However, if the last number is smaller or equal to the previous, they will add.
- }
- LastNum = CurNum;
- }
- return sum; //The sum is returned.
- }
- int main()
- {
- FILE *file_in; //Defining the file variable
- file_in = fopen("roman.txt", "r"); //Reading the file that all the numbers are on
- int Number;
- char Numeral[20];
- fscanf(file_in, "%s, %d", Numeral, &Number);
- string select;
- string A = "1. Batch Mode";
- string B = "2. User Input mode";
- cout << "Please select from the menu" << endl; //Allows the user to select between Batch Mode and Input Mode
- cout << A << endl;
- cout << B << endl;
- cin >> select;
- {
- if (select == "1") //If 1 is selected, the Batch Mode will be enabled.
- {
- ofstream file;
- file.open("convert.txt"); //Opening the file that the answer will be printed on.
- string Roman; //Compiling the string Roman to the final result.
- printf("Enter the Roman Numeral you would like to convert: ");
- getchar(); //Gets the characters from the printf
- getline(cin, Roman); //Asking the user to input the Roman Numeral they are trying to convert and scanning the string so that the final result can be displayed.
- if (Roman.length() == 1) { //If the final value is equal to 1 roman letter, the function of Roman[0], which is the first number, is presented.
- file << value(Roman[0]) << "\n"; //We use file here so it gets printed out into the file.
- }
- if (Roman.length() == 2) {
- file << convert1(Roman) << "\n"; //If the final value is equal to 2 roman letters, the function of "convert1" is presented, which adds or takes away depending on the number order.
- }
- if (Roman.length() > 2) { //If the final value is more than 2 roman letters, the function of "convert2" is presented, which uses the code LastNum to complete the code.
- file << convert2(Roman) << "\n";
- }
- return 0;
- _getch;
- file.close(); //Making sure the files are closed.
- }
- }
- if (select == "2") //If 2 is selected, the Input Mode will be enabled.
- {
- string Roman;
- printf("Enter the Roman Numeral you would like to convert: ");
- getchar();
- getline(cin, Roman); //Asking the user to input the Roman Numeral they are trying to convert and scanning the string so that the final result can be displayed.
- if (Roman.length() == 1) {
- cout << value(Roman[0]) << "\n"; //If the final value is equal to 1 roman letter, the function of Roman[0], which is the first number, is presented.
- }
- if (Roman.length() == 2) {
- cout << convert1(Roman) << "\n"; //If the final value is equal to 2 roman letters, the function of "convert1" is presented, which adds or takes away depending on the number order.
- }
- if (Roman.length() > 2) {
- cout << convert2(Roman) << "\n"; //If the final value is more than 2 roman letters, the function of "convert2" is presented, which uses the code LastNum to complete the code.
- }
- return 0;
- _getch;
- }
- }
- /*
- switch (Roman) - This switch statement allows the user's inputted roman numeral to be converted depending on what it is.
- convert1 - The function that will change the final result depending on the variables.
- SecNum = The Second Number in the Roman Numeral
- FirNum = The First Number in the Roman Numeral
- Roman[0] = If the Roman Numeral only has 1 digit, then that is the only thing printed/converted.
- convert2 - The second function that will change the final result that has more than 2 roman numeral digits.
- Sum = The final sum created by the for loop.
- CurNum = Current Number that is being converted
- LastNum = The previous number that will be added/subtracted depending on its position
- string Roman = The string from the previous functions that will be displayed at the end.
- getline = Taking the line from Roman and presenting it out on the screen.
- if Roman.length = 1 : The first number will be presented
- if Roman.length = 2 : The convert1 function will be used.
- if Roman.length = > 2 : The convert2 function will be used.
- file << : All of these make the presented final number appear inside a file instead of the screen.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement