Cod sursa(job #317662)

Utilizator alexandru92alexandru alexandru92 Data 24 mai 2009 19:43:18
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.07 kb
//programul nu-mi apartine, doar il testez ;)
#include <fstream>
#include <string>
#include <vector>
#include <iostream>

using namespace std;
fstream f;
fstream g;

vector<float> var(100);
int i;
char c;
string expr;
int variableCount;
string operation;
int searchPos;
bool exprSolved;
float aux;
string x;

int valid(string op)
{
   if(op[0]=='+'||op[0]=='-'||op[0]=='*'||op[0]=='/')
      return 0;
   if(op[1]=='+'||op[1]=='-'||op[1]=='*'||op[1]=='/')
      return 0;
   if(op[2]!='+'&&op[2]!='-'&&op[2]!='*'&&op[2]!='/')
      return 0;
   return 1;    
}

int main()
{
 f.open("evaluare.in",fstream::in);
 g.open("evaluare.out",fstream::out);
 f >> expr;
 while(!f.eof())
 {
    f >> c;if(f.eof()) break;
    f >> var[(int)c-97];
    variableCount++;               
 }  
 
 searchPos = expr.length()-1;
  
  while(searchPos>=0)
 {       
     operation = "";      
     operation = expr.substr(searchPos,3);
     if(valid(operation))
        {
         
                
         switch(operation[2])
         {
            case '+':
                aux = var[operation[0]-97]+var[operation[1]-97];  
                   break;
            case '-':
                aux = var[operation[0]-97]-var[operation[1]-97];  
                   break;
            case '*':
                  aux = var[operation[0]-97]*var[operation[1]-97];
                   break;
            case '/':   
                  if(var[operation[0]-97]==0)
                    aux = 0;
                    else   
                  aux = var[operation[0]-97]/var[operation[1]-97];
       
                   break;                         
         }
         
         var[variableCount++] = aux;
         
         x = "";
         x.append(1,(char)(variableCount+96));
         
         x.append(expr,searchPos+3,100);
         expr.replace(searchPos,x.length(),x);
         searchPos = expr.length()-1;      
         
        }
     else
       searchPos--;
 }
 g << var[variableCount-1] << "\n";
 g.close();
 f.close();
 return 0;
    
}