Parse Method

Wilhelm Korrengk DerBadejunge at web.de
Mon Apr 4 19:20:10 CEST 2005


Hello,

I tried to put the following parser to show a gmp int afterwards.
But this did not work due to the fact that array cannot be returned by a 
function....
Is ther a Way to make this work?
I already tried to convert every returnable value as a const char* cstring.
This wasn`t that nice becaue mpz_get_str just returne a char* which seems to 
be the same but doen`t work either...

What would you do?
The demo-parser on the webpage was really horrible to take out of the source 
code....
regards wilhelm Korrengk

-----------

#include<iostream>
#include<cctype>
#include "parse.h"
using namespace std;

int main() {





cout << "Bitte einen Term" << '\n' << endl;
string x;
getline(cin, x);
const char* ch = x.c_str();
cout << '\n' << ausdruck(ch) << endl;

return 0;
}

int i = 0;

long ausdruck(const char* c) {

 long a;
 
 if(c[i] == '-') {
 i++;
 a = -summand(c);
 }
 
 else {
  if(c[i] == '+')
  i++;
  a = summand(c);
 }
 
 while(c[i] == '+' || c[i] == '-')
  if(c[i] == '+') {
  i++;
  a += summand(c);
  }
  else {
  i++;
  a -= summand(c);
  }
 return a;
} 


long summand(const char* c) {
 long s = faktor(c);
 while(c[i] == '*' || c[i] == '/' || c[i] == '^')
  if(c[i] == '*'){
  i++;
  s *= faktor(c);
  }
  else{
   if(c[i] == '/') {
   i++;
   s /= faktor(c);
   }
   else{
    if(c[i] == '^') {
    i++;
   
    long t = faktor(c);
    long p = 2;
    long sold = s; 
     while(p <= t){
   
     s = s * sold;
     p++;
     }
    }
   }
  }
 
 return s;
} 


long faktor(const char* c){
 long f;
 if(c[i] == '(') {
 i++;
 f = ausdruck(c);
 
 if(c[i] != ')'){cout << "Syntax Error" << '\n' << endl;}
 else i++;
 }
 else f = zahl(c);
 return f;
} 

long zahl(const char* c){
 long z = 0;
 while(isdigit(c[i])){
 z = 10*z + static_cast<long>(c[i]-'0'); 
 i++;
 }
return z; 
} 
  


More information about the gmp-discuss mailing list