help me

im trying this code

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# include <stdio.h>
# include <iostream.h>
# include <math.h>
# define SIZE 100
class Stack
{
    private:
  
 char st[SIZE];
 int Top;
  
 public:
 
  Stack()
 {
  Top=0;
 }
 
 ~Stack()
  {
 
  }
 
 char pop()
 {
  if(!isEmpty())
  {
  
   --Top;
   return st[Top];
   
  
  }
  else
  {
   cout<<"Sorry ,This is Empty Stack \n\n";
   return NULL;
  
  
  }
 
 }
 void push(char v)
 {
  if(isFull())
  {
   cout<<"Sorry,Stack is Overload \n\n";
   
  }
  else
  {
   st[Top]=v;
   Top++;
   
  }
 }
 int isFull()
 {
  return Top>=SIZE;
 }

 int isEmpty()
 {
  return Top<=0;
 }
 
 
};

class EvalStack
{
    private:
  
 int Ev[SIZE];
 int Top;
  
 public:
 
  EvalStack()
 {
  Top=0;
 }
 
 ~EvalStack()
  {
 
  }
 
 int pop()
 {
  if(!isEmpty())
  {
  
   --Top;
   return Ev[Top];
   
  
  }
  else
  {
   cout<<"Sorry ,This is Empty Stack \n\n";
   return NULL;
  
  
  }
 
 }
 void push(int v)
 {
  if(isFull())
  {
   cout<<"Sorry,Stack is Overload \n\n";
   
  }
  else
  {
   Ev[Top]=v;
   Top++;
   
  }
 }
 int isFull()
 {
  return Top>=SIZE;
 }

 int isEmpty()
 {
  return Top<=0;
 }
 
 
};

int isOperator(char T)
{
 if(T=='+'|| T=='/'|| T=='*' || T=='-' || T=='^' || T=='%')
  return 1;
 else
  return 0;
}
int prio(char T)
{
 if(T== '/' || T=='*' || T=='%')
  return 100;
 else if(T=='^') 
  return 1000;
 else if(T=='+' || T=='-')
  return 50;
 
 else if(T==')' || T=='(')
  return 1500;
}
int Evaluat(int x, int y,char z )  
{
  int result;
  if(z=='+')
  {
  result=x+y;
  return result;
  }
 else if(z=='-')
  {
  result=y-x;
  return result;
  }
 else if(z=='*')
  {
  result=x*y;
  return result;
  }
 else if(z=='/')
  {
  result=y/x;
  return result;
  }
 else if(z=='%')
  {
  result=pow(y,x);;
  return result;
  }
 else if(z=='^')
  {
  result=fmod(y,x);
  return result;
  }
 
}
 
 
void main()
{
 EvalStack Eval;
 Stack S;
 int i=0;
 char exp[100];
 char postFix[100];
    
    printf("Please enter Expression to evaluate:\n");
 gets(exp);
 char *p=exp;
 while(*p)
 {
  char T=*p++;
  
  if(!isOperator(T))
  {
 // cout<<T;
  postFix[i]=T;
  i++;
  }
  else if(isOperator(T))
  {
   if(S.isEmpty())
   S.push(T); 
   else
   {
    char v=S.pop();
    while(prio(T)<= prio(v)&&S.isEmpty()S)
    {   
 //       cout<<v;
     postFix[i]=v;
     i++;
     v=S.pop();
    }
         if(prio(T)>prio(v))
    S.push(v);
    S.push(T);
    
   }
  }
 }
  while(!S.isEmpty())
 {
  
  char z=S.pop();
  postFix[i]=z;
  i++;
 // cout<<z;
  cout<<"\n\n";
  
 
  
 }
     postFix[i]=NULL;
  cout<<postFix<<"\n";
  int j=0;
  while(postFix[j]!=NULL)
  {
   if(!isOperator(postFix[j]))
   {
    Eval.push(postFix[j]-'0');
     
   }
   else
  
   {
    char z=postFix[j];
    int x=Eval.pop();
    int y=Eval.pop();
   // int f=x-'0';
   // int s=y-'0';
    int result=Evaluat(x,y,z);
    Eval.push(result);

   }
   j++;
  }
  int q=Eval.pop();
  
  cout<<"The Final result for this Expression:"<<" "<<exp<<" "<<"is equal:"<<q<<"\n";

     if(!Eval.isEmpty())
  cout<<"syntax Erorr"<<"\n";
 
  
 
 
 
 
}



for this questiom


enter the equation then :-
1- change the equation from infix to postfix then print the result
2- Solve the equation and then print the result
Allows the use of:
stack
queue
strtok
array



Figures that fall may be from one status or more
Possible equation contains arcs


I Analyzed the question, but I analyzed the question for number from status one and does not contain arcs .. Please help me
Please do not spam, you already have a thread:
http://www.cplusplus.com/forum/general/56656/
A notes:

1. Abstain from using gets -- it has several flaws in design. Use getline instead. Also, if you are using C++ then it would be better to use C++ streams instead of a mix of both C and C++.
2. Line 219-225. The loop condition is not proper. You require the stack to be empty to enter the loop and yet you pop items from the stack inside the loop, even though it is empty?
3. I would suggest you to include more comments in your file in the future.
4. You really shouldn't be using strtok(). The assignment requires you to use it? I'm surprised.
5. In standard C++ the header file should be <iostream> not <iostream.h>.
6. Rather than using fixed length C Strings, use the std::string class from <string>. It, at least, is easier to use.
7. Use switch cases wherever you can, instead of using a long if... else if... branch.
8. Organise the implementations of Stack in different headers and source files. E.g., Stack.h, Stack.c.

Finally, a post script:
In deciphering expressions I would suggest you first learn a bit about BNF. It will be of immense help to you if you wish to tokenize and handle such expressions in an easier way.
Let's try and divert comments to one thread: http://www.cplusplus.com/forum/general/56656/
;)
this thread http://www.cplusplus.com/forum/general/56656/
doesnt open i dont know why plz delete it and divert comments to this thread http://www.cplusplus.com/forum/general/56715/
now what i do ??? can anyone help me and edit my programme ??
All right, I'll answer in this thread then.

Allows the use of:
stack
queue
strtok
array

If you are allowed to use these, why are you trying to make them yourself?

For stacks, just #include <stack> and you can use std::stack<> as described here: http://www.cplusplus.com/reference/stl/stack/
For queues, just #include <queue> and you can use std::queue<> as described here: http://www.cplusplus.com/reference/stl/queue/
Last edited on
i know > I analyzed the question and the code above in the subject, but I need anyone that I want him to become adjusted to the number from status of one or more .. and solve the problem arcs
Is the code you show provided or your own?
I have done this code, but it reads a number from one status and does not solve the problem of arcs .. Knowing that in the question can contain arcs
Enter the Basic mathematical equation such as 2 +3 * 4% 3

Possible contain calculations +-/^%*

Converted from infix to postfix

And print after conversion

the numbers we entering may be from one status or more ( 23+445*4%8 )

The equation can contain arcs

And calculates the output and marked by
By 'arcs' do you mean 'parenthesis'?

Also, I recommend deleting that code you wrote for the stack class. Just use std::stack instead, as you are allowed to do so.
http://www.cplusplus.com/reference/stl/stack/

As for actually doing the program, I'd recommend looking at one character at a time and using recursion for the ().
Edit: irrelevant if the arc actually means parenthesis. It doesn't seem like I can delete this post.

Are you having trouble with parsing the user input or with calculating?
Parsing user input text can be tricky, that is why I had suggested the use of BNF in my earlier post.
Last edited on
The equation can contain arcs
as (23*2)+(445*4-5)%8
I have a problem in that the solution >> can enter without equivalent arcs and I want to enter the equation with arcs .. And that the figures in the equation introduced from one status, such as 2. 3. 5, and I want it from one or more status such as 2. 45. 66. 676
If there are mismatched parenthesis, just assume they all close at the end:
(4+(3-1)*5
you could assume it has a ) at the end.

As for different number of digits, why would that be any sort of problem?
i dont understand any thing i need anyone edit my programme please >>> pleaseeeeeeeeeee
Topic archived. No new replies allowed.