wheelsmanx

CPS 171 Machine Problem 4

Oct 17th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7. #include <wctype.h>
  8. using namespace std;
  9.  
  10. //had already completed this with out adding functions - and so i added two of the examples to show that i understand how the functions work
  11.  
  12. char digit;
  13. char prevdigit;
  14. char prevprevdigit = ' ';
  15. float firstnumber = 0;
  16. string firststring;
  17. float secondnumber = 0;
  18. string secondstring;
  19. float currentnumber = 0;
  20. string currentstring;
  21. float answer;
  22. int switchstatement = 0;
  23. char op;
  24.  
  25.  
  26.  
  27.  
  28. string reinsist(int newanswer) {
  29. string finalromannumber;
  30. int final = newanswer;
  31.  
  32. while (final > 0) {
  33. while (final > 1000) {
  34. final = final - 1000;
  35. finalromannumber += 'M';
  36. }
  37. while (final > 500) {
  38. final = final - 500;
  39. finalromannumber += 'C';
  40. }
  41. while (final > 100) {
  42. final = final - 100;
  43. finalromannumber += 'D';
  44. }
  45. while (final > 50) {
  46. final = final - 50;
  47. finalromannumber += 'L';
  48. }
  49. while (final > 10) {
  50. final = final - 10;
  51. finalromannumber += 'X';
  52. }
  53. while (final > 5) {
  54. final = final - 5;
  55. finalromannumber += 'V';
  56. }
  57. while (final >= 1) {
  58. final = final - 1;
  59. finalromannumber += 'I';
  60. }
  61. }
  62. return finalromannumber;
  63. }
  64.  
  65.  
  66.  
  67. void resetcurrent() {
  68. currentstring = "";
  69. currentnumber = 0;
  70. }
  71. void addsomethingfirst(int a) {
  72. firstnumber += a;
  73. }
  74. void resetall() {
  75. switchstatement = 0;
  76. currentnumber = 0;
  77. secondnumber = 0;
  78. firstnumber = 0;
  79. firststring = "";
  80. secondstring = "";
  81. currentstring = "";
  82. answer = 0;
  83. }
  84.  
  85. int main()
  86. {
  87. ifstream fileInput; //here we are going to define the file that we are trying to stream
  88. fileInput.open("c:\\temp\\mp4romanletrdata.txt");
  89.  
  90. while (!fileInput.eof())
  91. {
  92. fileInput >> std::noskipws >> digit;
  93. //cout << digit << endl;
  94. if (digit == 'I') {
  95. currentnumber = currentnumber + 1; currentstring += digit;
  96. }
  97. if (digit == 'V') {
  98. currentnumber = currentnumber + 5; currentstring += digit;
  99. }
  100. if (digit == 'X') {
  101. currentnumber = currentnumber + 10; currentstring += digit;
  102. }
  103. if (digit == 'L') {
  104. currentnumber = currentnumber + 50; currentstring += digit;
  105. }
  106. if (digit == 'C') {
  107. currentnumber = currentnumber + 100; currentstring += digit;
  108. }
  109. if (digit == 'D') {
  110. currentnumber = currentnumber + 500; currentstring += digit;
  111. }
  112. if (digit == 'M') {
  113. currentnumber = currentnumber + 1000; currentstring += digit;
  114. }
  115.  
  116. if (digit == (char)' ' && prevdigit != (char)' ' && switchstatement == 0) {
  117. addsomethingfirst(currentnumber); // this is my example of a return function
  118. firststring = currentstring;
  119. resetcurrent(); // this is my void function
  120. switchstatement = 1;
  121. }
  122. else
  123. if (digit == (char)' ' && prevdigit != (char)' ' && switchstatement == 1) {
  124. secondnumber += currentnumber;
  125. secondstring = currentstring;
  126. resetcurrent();
  127. switchstatement = 2;
  128. }
  129. if (digit == (char)'\n' && switchstatement == 2) {
  130. switchstatement = 0;
  131. op = prevdigit;
  132. if (op == '+') { answer = firstnumber + secondnumber; }
  133. if (op == '-') { answer = firstnumber - secondnumber; }
  134. if (op == '*') { answer = firstnumber * secondnumber; }
  135. if (op == '/') { answer = firstnumber / secondnumber; }
  136. cout << firstnumber << op << secondnumber << "=" << answer << endl;
  137. cout << "The first number is: " << firststring << "(" << firstnumber << ")" << endl;
  138. cout << "The second number is: " << secondstring << "(" << secondnumber << ")" << endl;
  139. cout << "The answer is: " << answer << endl;
  140. cout << endl << "Answer In Roman Numerals : " << "<" << reinsist(answer) << ">" << endl;
  141. cout << "----------------------------------------" << endl;
  142. resetall();
  143. }
  144. prevdigit = digit;
  145. prevprevdigit = prevdigit;
  146. }
  147. system("pause");
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment