Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > С/С++: Кроссплатформенное программирование, Qt/Gtk+/wxWidgets > Парсер, нужна помощь.


Автор: SpAwN3D 20.5.2012, 01:17
Помогите с парсером, не пойму почему не работает или работает, но не правильно особенно с отрицательными числами( в примере это число -10 ).
Парсер использую для высчитывания математических уравнений. 


main
Код

int y;
QByteArray eq;
eq = -10;
//eq = 2*-2;
pars.InsertQueue(equazione.data());
        pars.PrintQueue();
        if(pars.PrintQueue()==0)
        {
            pars.FinishWork();
            pars.outputQueue();
            y = pars.ResolveEquation();
            pars.output.clear();
        }


парсер.h
Код

#ifndef PARSER_H
#define PARSER_H
#include <iostream>
#include <stack>
#include <deque>
#include <queue>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <math.h>
using namespace std;
class parser
{
public:
    parser();
    stack<string> s;
    queue<string> q;
    deque<string> output;
    string b;
    void InsertQueue(string str);
    int PrintQueue();
    void confronta();
    void outputQueue();
    void FinishWork();
    double ResolveEquation();
};

#endif // PARSER_H



парсер.cpp
Код

#include "parser.h"

parser::parser()
{
}

void parser::outputQueue()
{
    deque<string>::iterator it;
    it=output.begin();
    while(it!=output.end())
        cout<<" "<<*it++;
}
void parser::InsertQueue(string str)
{
    string s;
    for(int i=0;i<str.length();i++)
    {
        char c = str[i];
        if(c=='+'|| c=='-' || c=='/'|| c=='*' || c=='^' || c=='(' || c==')')
        {
            if(!s.empty())
            {
                q.push(s);
            }
            s = c;
            q.push(s);
            s.clear();
        }
        else
        {
            s += c;
            if((i+1)==str.length())//se e' in fondo;
            {
                q.push(s);
            }
        }
    }
}
int parser::PrintQueue()
{
    int NumParentesi=0;
    while (!q.empty())
    {
        int exit=0,flag=0;
        cout<<q.front()<<" ";
        if(q.front()==")")
        {
            while(s.top()!="(")
            {
                output.push_back(s.top());
                s.pop();
            }
            s.pop();
            exit=1,flag=0,NumParentesi--;
        }
        while(exit==0)
        {
            if(q.front()=="+"||q.front()=="-"||q.front()=="/"||q.front()=="*" || q.front()=="^")
            {
                if(!s.empty())
                {
                    if(s.top()=="*"||s.top()=="-"||s.top()=="+"||s.top()=="/" || s.top()=="^")
                    {
                        if((s.top()=="+"||s.top()=="-")&&( q.front()=="*"||q.front()=="/"||q.front()=="^" ))
                        {
                            if(flag==0)
                                s.push(q.front());
                        }
                        else if(s.top()=="*"||s.top()=="/" && q.front()=="^")
                        {
                            if (flag==0)
                                s.push(q.front());
                        }
                        else
                        {
                            output.push_back(s.top());
                            s.pop();
                            s.push(q.front());
                        }
                    }
                    else
                        s.push(q.front());
                }
                else
                    s.push(q.front());
            }
            else if(q.front()=="(")
            {
                flag=1,NumParentesi++;
                s.push("(");
            }
            else
                output.push_back(q.front());
            exit=1;
        }
        q.pop();
    }
    if(NumParentesi!=0)
    {
        cout<<"error"<<endl;
        return 1;
    }
    else
        return 0;
}
void parser::FinishWork()
{
    while(!s.empty())
    {
        if(s.top()=="+"||s.top()=="-"||s.top()=="/"||s.top()=="*" || s.top()=="^")
            output.push_back(s.top());
        s.pop();
    }
}
double parser::ResolveEquation()
{
    deque<string>::iterator it;
    it=output.begin();
    char* cstr=new char(2);
    string str;
    int count=0;
    float tmp;
    while (output.size()!=1)
    {
        if(*it=="*"||*it=="+"||*it=="/"||*it=="-" || *it=="^")
        {
            string sign=*it;
            if(*it-->"-9999"||*it<"99999999"&&*it-->"-9999"||*it<"9999999999")
            {
                *it++,*it++;
                output.erase(output.begin()+count);
                it=output.begin()+(count-1);
                str=*it;
                strcpy(cstr,str.c_str());
                tmp=atoi(cstr);
                output.erase(output.begin()+(count-1));
                if(!output.empty())
                {
                    it=output.begin()+(count-2),str=*it;
                    strcpy(cstr,str.c_str());
                    output.erase(output.begin()+(count-2));
                }
                else
                {
                    str='0';
                    strcpy(cstr,str.c_str());
                }
                if(sign=="+")
                    tmp=tmp+atof(cstr);
                else if(sign=="-")
                    tmp=atof(cstr)-tmp;
                else if(sign=="*")
                    tmp=atof(cstr)*tmp;
                else if (sign=="/")
                    tmp=atof(cstr)/tmp;
                else if(sign=="^")
                    tmp=pow(atof(cstr),tmp);
                itoa(tmp,cstr,10);
                str=cstr;
                if(!output.empty())
                {
                   output.insert(output.begin()+(count-2),str);
                }
                else
                {
                   output.insert(output.begin(),str);
                }
                it=output.begin();
                count=0;
            }
        }
        *it++,count++;
    }
    double z;
    string s;
    s = output.front();
    z = atof(s.c_str());
    return z;
}



Автор: SpAwN3D 20.5.2012, 15:57
немного исправил код,
но всё ещё есть проблемы,
использовал уравнение для примера 2*(-10) с помощью парсера получается 2,10,-,*
как исправить?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)