Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- static int counter = 0;
- int leseAusdruck(string ausdruck);
- int leseFaktor(string ausdruck);
- int leseSummand(string ausdruck);
- int leseFaktor(string ausdruck) {
- int result = 0;
- char vorzeichen;
- if(ausdruck[counter] == '-' || ausdruck[counter] == '+') {
- vorzeichen = ausdruck[counter];
- counter++;
- if((result = leseFaktor(ausdruck))) {
- if(vorzeichen == '-') {
- result *= (-1);
- }
- }
- }
- else {
- if(isdigit(ausdruck[counter])) {
- while(isdigit(ausdruck[counter])) {
- result *= 10;
- result += (ausdruck[counter] - '0');
- counter++;
- }
- }
- else {
- if(ausdruck[counter] == '(') {
- counter++;
- if((result = leseAusdruck(ausdruck))) {
- if(ausdruck[counter] == ')') {
- counter++;
- }
- }
- }
- }
- }
- return result;
- }
- int leseSummand(string ausdruck) {
- int faktor1 = 0, faktor2 = 0;
- char zeichen;
- if((faktor1 = leseFaktor(ausdruck))) {
- while(ausdruck[counter] == '*' || ausdruck[counter] == '/') {
- zeichen = ausdruck[counter];
- counter++;
- if((faktor2 = leseFaktor(ausdruck))) {
- if(zeichen == '*') {
- faktor1 *= faktor2;
- }
- else {
- faktor1 /= faktor2;
- }
- }
- }
- }
- return faktor1;
- }
- int leseAusdruck(string ausdruck) {
- int summand1 = 0, summand2 = 0;
- char zeichen;
- if((summand1 = leseSummand(ausdruck))) {
- while(ausdruck[counter] == '+' || ausdruck[counter] == '-') {
- zeichen = ausdruck[counter];
- counter++;
- if((summand2 = leseSummand(ausdruck))) {
- if(zeichen == '+') {
- summand1 += summand2;
- }
- else {
- summand1 -= summand2;
- }
- }
- }
- }
- return summand1;
- }
- int main() {
- string ausdruck("(13+7)*5-(2*3+7)/(-8)");
- /*cout << "Geben Sie einen Ausdruck an: ";
- cin >> ausdruck;*/
- cout << "Ergebnis zum Ausdruck: " << ausdruck << " = " << leseAusdruck(ausdruck) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement