Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <cstdlib>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <fstream>
  8. #include <math.h>
  9. #include <cstdio>
  10. using namespace std;
  11.  
  12. int value(char Roman) //Roman is the character that is presented from the integer value.
  13. {
  14.  
  15. switch (Roman) //Switch statement that presents a number in return of the user input
  16. {
  17. case 'I':
  18. return 1; //If I is presented, 1 is returned.
  19. case 'V':
  20. return 5; //If V is presented, 5 is returned.
  21. case 'X':
  22. return 10; //If X is presented, 10 is returned.
  23. case 'L':
  24. return 50; //If L is presented, 50 is returned.
  25. case 'C':
  26. return 100; //If C is presented, 100 is returned.
  27. case 'D':
  28. return 500; //If D is presented, 500 is returned.
  29. case 'M':
  30. return 1000; //If M is presented, 1000 is returned.
  31. }
  32. }
  33. int convert1(string roman) { //The function "convert1" showcases how the position each roman numeral is in changes the result.
  34. int SecNum = value(roman[1]);//the variables of the function
  35. int FirNum = value(roman[0]);
  36. if (FirNum < SecNum) { //If the first number of the code is smaller than the second, they will produce a negative.
  37. return SecNum - FirNum;
  38. }
  39. else return SecNum + FirNum; //However if the first number is larger, they will produce a positive.
  40. }
  41. int convert2(string roman) { //The function "convert2" will work for any roman numeral with more than two characters and for the 'I' variable.
  42. int sum = value(roman[roman.length() - 1]);
  43. int CurNum;
  44. 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.
  45.  
  46. for (int a = roman.length() - 2; a >= 0; a--) {
  47. CurNum = value(roman[a]);
  48. if (LastNum > CurNum) {
  49. sum = sum - CurNum; //If the last number in the code is larger than the current number, they will take away from eachother.
  50. }
  51. else if (LastNum <= CurNum) {
  52. sum = sum + CurNum; //However, if the last number is smaller or equal to the previous, they will add.
  53. }
  54. LastNum = CurNum;
  55. }
  56. return sum; //The sum is returned.
  57.  
  58. }
  59. int main()
  60. {
  61. FILE *file_in; //Defining the file variable
  62. file_in = fopen("roman.txt", "r"); //Reading the file that all the numbers are on
  63. int Number;
  64. char Numeral[20];
  65. fscanf(file_in, "%s, %d", Numeral, &Number);
  66. string select;
  67. string A = "1. Batch Mode";
  68. string B = "2. User Input mode";
  69. cout << "Please select from the menu" << endl; //Allows the user to select between Batch Mode and Input Mode
  70. cout << A << endl;
  71. cout << B << endl;
  72. cin >> select;
  73. {
  74. if (select == "1") //If 1 is selected, the Batch Mode will be enabled.
  75. {
  76. ofstream file;
  77. file.open("convert.txt"); //Opening the file that the answer will be printed on.
  78. string Roman; //Compiling the string Roman to the final result.
  79. printf("Enter the Roman Numeral you would like to convert: ");
  80. getchar(); //Gets the characters from the printf
  81. 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.
  82.  
  83. 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.
  84. file << value(Roman[0]) << "\n"; //We use file here so it gets printed out into the file.
  85. }
  86. if (Roman.length() == 2) {
  87. 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.
  88. }
  89. 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.
  90. file << convert2(Roman) << "\n";
  91. }
  92. return 0;
  93. _getch;
  94. file.close(); //Making sure the files are closed.
  95. }
  96. }
  97.  
  98. if (select == "2") //If 2 is selected, the Input Mode will be enabled.
  99. {
  100.  
  101. string Roman;
  102. printf("Enter the Roman Numeral you would like to convert: ");
  103. getchar();
  104. 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.
  105. if (Roman.length() == 1) {
  106. 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.
  107. }
  108. if (Roman.length() == 2) {
  109. 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.
  110. }
  111. if (Roman.length() > 2) {
  112. 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.
  113. }
  114. return 0;
  115. _getch;
  116. }
  117. }
  118.  
  119. /*
  120. switch (Roman) - This switch statement allows the user's inputted roman numeral to be converted depending on what it is.
  121. convert1 - The function that will change the final result depending on the variables.
  122. SecNum = The Second Number in the Roman Numeral
  123. FirNum = The First Number in the Roman Numeral
  124. Roman[0] = If the Roman Numeral only has 1 digit, then that is the only thing printed/converted.
  125. convert2 - The second function that will change the final result that has more than 2 roman numeral digits.
  126. Sum = The final sum created by the for loop.
  127. CurNum = Current Number that is being converted
  128. LastNum = The previous number that will be added/subtracted depending on its position
  129. string Roman = The string from the previous functions that will be displayed at the end.
  130. getline = Taking the line from Roman and presenting it out on the screen.
  131. if Roman.length = 1 : The first number will be presented
  132. if Roman.length = 2 : The convert1 function will be used.
  133. if Roman.length = > 2 : The convert2 function will be used.
  134. file << : All of these make the presented final number appear inside a file instead of the screen.
  135. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement