Advertisement
Guest User

kranke rechner

a guest
Nov 27th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. static int counter = 0;
  6.  
  7. int leseAusdruck(string ausdruck);
  8. int leseFaktor(string ausdruck);
  9. int leseSummand(string ausdruck);
  10.  
  11.  
  12. int leseFaktor(string ausdruck) {
  13. int result = 0;
  14. char vorzeichen;
  15. if(ausdruck[counter] == '-' || ausdruck[counter] == '+') {
  16. vorzeichen = ausdruck[counter];
  17. counter++;
  18. if((result = leseFaktor(ausdruck))) {
  19. if(vorzeichen == '-') {
  20. result *= (-1);
  21. }
  22. }
  23. }
  24.  
  25. else {
  26. if(isdigit(ausdruck[counter])) {
  27. while(isdigit(ausdruck[counter])) {
  28. result *= 10;
  29. result += (ausdruck[counter] - '0');
  30. counter++;
  31. }
  32. }
  33.  
  34. else {
  35. if(ausdruck[counter] == '(') {
  36. counter++;
  37. if((result = leseAusdruck(ausdruck))) {
  38. if(ausdruck[counter] == ')') {
  39. counter++;
  40. }
  41. }
  42. }
  43. }
  44. }
  45. return result;
  46. }
  47.  
  48. int leseSummand(string ausdruck) {
  49. int faktor1 = 0, faktor2 = 0;
  50. char zeichen;
  51. if((faktor1 = leseFaktor(ausdruck))) {
  52. while(ausdruck[counter] == '*' || ausdruck[counter] == '/') {
  53. zeichen = ausdruck[counter];
  54. counter++;
  55. if((faktor2 = leseFaktor(ausdruck))) {
  56. if(zeichen == '*') {
  57. faktor1 *= faktor2;
  58. }
  59.  
  60. else {
  61. faktor1 /= faktor2;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. return faktor1;
  68. }
  69.  
  70. int leseAusdruck(string ausdruck) {
  71. int summand1 = 0, summand2 = 0;
  72. char zeichen;
  73. if((summand1 = leseSummand(ausdruck))) {
  74. while(ausdruck[counter] == '+' || ausdruck[counter] == '-') {
  75. zeichen = ausdruck[counter];
  76. counter++;
  77. if((summand2 = leseSummand(ausdruck))) {
  78. if(zeichen == '+') {
  79. summand1 += summand2;
  80. }
  81.  
  82. else {
  83. summand1 -= summand2;
  84. }
  85. }
  86. }
  87. }
  88.  
  89. return summand1;
  90. }
  91.  
  92.  
  93. int main() {
  94. string ausdruck("(13+7)*5-(2*3+7)/(-8)");
  95.  
  96. /*cout << "Geben Sie einen Ausdruck an: ";
  97. cin >> ausdruck;*/
  98.  
  99. cout << "Ergebnis zum Ausdruck: " << ausdruck << " = " << leseAusdruck(ausdruck) << endl;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement