Java - postfijo

 
Vista:

postfijo

Publicado por postfijos (1 intervención) el 25/06/2016 23:46:16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <cstdlib>
#include <iostream>
#include <string.h>
 
using namespace std;
 
int validar(char c)
{
    if(c>=48 && c<=57)
    return 1;
    else
    {
        if(c>=65 && c<=90)
        return 2;
        else
        {
            if(c>=97 && c<=122)
            return 3;
            else
            return 4;
        }
    }
}
 
typedef struct nodo
{float dato;
 struct nodo*sgt;
 struct nodo*ant;
}tnodo;
 
typedef struct pila
{struct nodo*p1;
 struct nodo*p2;
 int n;
}tpila;
 
tpila*crearpila()
{tpila*p=(tpila*)malloc(sizeof(struct pila));
 p->p1=NULL;
 p->p2=NULL;
 p->n=0;
 return p;
}
 
void push(float dato,tpila*pila)
{tnodo*nodo=NULL;
 nodo=(tnodo*)malloc(sizeof(struct nodo));
 nodo->dato=dato;
 nodo->ant=NULL;
 nodo->sgt=NULL;
 if(pila->p1==NULL)
 {pila->p1=nodo;
  pila->p2=nodo;
 }
 else
 {pila->p2->sgt=nodo;
  nodo->ant=pila->p2;
  pila->p2=nodo;
 }
 pila->n++;
}
 
void pop(tpila*pila)
{tnodo*ultimo=NULL;
 ultimo=pila->p2;
 if(pila->p1!=NULL)
 {pila->p2=pila->p2->ant;
  if(pila->p2==NULL)
   pila->p1=NULL;
  else
   pila->p2->sgt=NULL;
  free(ultimo);
  pila->n--;
 }
}
 
float cima(tpila*pila)
{float dato=0;
 dato=pila->p2->dato;
 return dato;
}
 
bool esvacia(tpila*pila)
{bool respuesta=false;
 if(pila->p1==NULL)
 respuesta=true;
 return respuesta;
}
 
int main()
{
    //pila
    tpila*pila=NULL;
    pila=crearpila();
    //push(8,pila);
 
    char *c=(char*)malloc(sizeof(char));
    cin>>c;
    int nc=strlen(c);
    float result=0,b;
    int stado=0,caso;
    int sfinal=3;
    bool error=false;
    int i=0;
    while(!error && stado!=3)
    {
 
        if(validar(c[i])==1 && stado==0)
        {
            push(c[i]-48,pila);
            i++;
        }
        else
        {
            if(validar(c[i])==4 && stado==0 && !esvacia(pila) && !error)
            {
 
 
                if(c[i]=='+')
                    caso=1;
                else
                {
                    if(c[i]=='-')
                        caso=2;
                    else
                    {
                        if(c[i]=='*')
                            caso=3;
                        else
                        {
                            if(c[i]=='/')
                                caso=4;
                            else
                                error=true;
                        }
                    }
                }
 
                if(!error)
                {
                    b=cima(pila);
                    pop(pila);
                    stado=1;
 
                    switch(caso)
                    {
                        case 1:
                                if(!esvacia(pila))
                                {
                                    result=cima(pila)+b;
                                    pop(pila);
                                    push(result,pila);
                                    stado=2;
                                }
                                else
                                error=true;
                                break;
 
                        case 2:
                                if(!esvacia(pila))
                                {
                                    result=cima(pila)-b;
                                    pop(pila);
                                    push(result,pila);
                                    stado=2;
                                }
                                else
                                error=true;
                                break;
 
                        case 3:
                                if(!esvacia(pila))
                                {
                                    result=cima(pila)*b;
                                    pop(pila);
                                    push(result,pila);
                                    stado=2;
                                }
                                else
                                error=true;
                                break;
 
                        case 4:
                                if(!esvacia(pila))
                                {
                                    result=cima(pila)/b;
                                    pop(pila);
                                    push(result,pila);
                                    stado=2;
                                }
                                else
                                error=true;
                                break;
 
                        default: cout<<"error de sistema :p";
                    }
                }
                i++;
            }
            else
            {
                if(i<nc && stado==2 && !esvacia(pila) && !error)
                stado=0;
                else
                {
                    if(stado==2 && !esvacia(pila)&& !error)
                    {
                        result=cima(pila);
                        pop(pila);
                        stado=3;
                        if(!esvacia(pila))
                        error=true;
                    }
                    else
                        error=true;
                }
            }
        }
    }
    if(!error)
    cout<<result;
    else
    cout<<"ecuacion invalida";
    system("PAUSE");
    return EXIT_SUCCESS;
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

postfijo

Publicado por saul (1 intervención) el 26/06/2016 01:41:45
?????????????
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar