i need some help in my calculator again

finally i can solve how to change infix to postfix ......... but one more problem is i ,my cal can run but cant give me right result :( ....... i need some help again

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
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.14
struct STACK
{
	int *array;
	int Max;	
	int Top;
};

bool InitStack(STACK &s,int MaxNumItem)
{
	s.Max=MaxNumItem;
	s.array=new int[s.Max];
		if(s.array==NULL) return false;
			s.Top=-1;
			return true;
}
bool IsStackEmpty(const STACK &s)
{
	if(s.Top<0) return true;
	else return false;
}
bool IsStackFULL(const STACK &s)
{
	if(s.Top==s.Max-1) return true;
	else return false;
}
void printStack(const STACK &s)
{
	if(IsStackEmpty(s)==true)
		{
			printf("Stack is empty! No data to show!\n");
			return;
		}
	for(int i=0;i<=s.Top;i++)
		{
			printf("%5d ",s.array[i]);
		}
	printf("\n");
}
int Pushstack2(STACK &s,int InItem)
{
	if(IsStackFULL(s)==true) return false;
		s.array[++s.Top]=InItem;
		return true;
}
int Popstack2(STACK &s)
{
	int OutItem;
	if(IsStackEmpty(s)==true) return -1;
	else
		{
			OutItem=s.array[s.Top--];
			return OutItem;
		}
}
//bool Pushstack(STACK &s,char InItem)
//{
//	if(IsStackFULL(s)==true) return false;
//	s.array[++s.Top]=InItem;
//	return true;
//}
//bool Popstack(STACK &s,char &OutItem)
//{
//	if(IsStackEmpty(s)==true) return false;
//	else
//	{
//	 OutItem=s.array[s.Top--];
//	 return true;
//	}
//}
int IsGreaterOrEqual(char c)
{
	switch(c)
		{
			case 'c': return 3;
			case 's': return 3;
			case '*': return 2;
			case '/': return 2;
			case '+': return 1;
			case '-': return 1;
			case '(': return 0;
		}
}
void infixtopostfix(char* p1,char* p2,int &p)
{
	STACK s;
	InitStack(s,100);
	char temp,c;
	int e=0;
	for(int i=0;i<=strlen(p1);i++)
	{
		c=p1[i];
		switch(c)
			{
				case '(': {Pushstack2(s,c); break;}
				case 'c': goto sosanh;
				case 's': goto sosanh;
				case '+': goto sosanh;
				case '-': goto sosanh;
				case '/': goto sosanh;
				case '*': goto sosanh;
				case ')': 
				{	while(s.array[s.Top]!='('&&IsStackEmpty(s)==false)
					{
						temp=char(Popstack2(s));
						p2[e++]=temp;
					}
				if(s.array[s.Top]=='(') 
					{
						temp=char(Popstack2(s)); 
						break;
					}
				}
			default: 
				{
					if(c>=48&&c<=57&&c!='c'&&c!='s')
						{
							p2[e++]=c; break;
						}
					break;
				}
			sosanh:
				{
					if(IsStackEmpty(s)==true) {Pushstack2(s,c); break;}
					else
						{
							temp=char(Popstack2(s));
							if(IsGreaterOrEqual(temp)>=IsGreaterOrEqual(c)) {p2[e++]=temp; Pushstack2(s,c); break;}
							else {Pushstack2(s,temp); Pushstack2(s,c);break;}
						}
				}
			}
	/*printf("Show stack: ");
	for(int i=0;i<=s.Top;i++)
	{
	printf("%c",s.array[i]);
	}
	printf("\n");*/
}
while(IsStackEmpty(s)==false)
{
	temp=char(Popstack2(s));
	p2[e++]=temp;
}
p=e;
//printf("Convert done!\n");
//printf("Output: ");
for(int j=0;j<e;j++)
{
	printf("%c",p2[j]);
}
//printf("\nCalculating...\n");
}
void cal(char* p2,int p)
{
	STACK s;
	InitStack(s,100);
	char c;
	int kq,temp1,temp2,flag=1;
	float goc1,goc2;
	for(int i=0;i<p;i++)
		{
			c=p2[i];
			switch (c)
				{
					case '+':
					{
						temp1=Popstack2(s);
						temp2=Popstack2(s);
						kq=temp1+temp2;
						Pushstack2(s,kq);
						break;
					}
					case '-':
					{
						temp1=Popstack2(s);
						temp2=Popstack2(s);
						kq=temp2-temp1;
						Pushstack2(s,kq);
						break;
					}
					case '*':
					{
						temp1=Popstack2(s);
						temp2=Popstack2(s);
						kq=temp1*temp2;
						Pushstack2(s,kq);
						break;
					}	
					case '/':
					{
						temp1=Popstack2(s);
						temp2=Popstack2(s);
						if(temp1==0) {flag=0; goto Result;}
						else
						{
							kq=temp2/temp1;
							Pushstack2(s,kq);
						}
						break;
					}
					case 's':
					{
						temp1=Popstack2(s);
						temp2=Popstack2(s);
						goc1=temp2*10+temp1;
						kq=sin(goc1*PI/180);
						Pushstack2(s,kq);
					}
					/*case 'c':
					{
					temp1=Popstack2(s);
					temp2=Popstack2(s);
					goc2=temp2*10+temp1;
					kq=cos(goc2*PI/180);
					Pushstack2(s,kq);
					}*/
					default:
					{
						if(c>=48&&c<=57&&c!='c'&&c!='s')
						{
							Pushstack2(s,atoi(&c));
							break;
						}
					}
				}
		}
		Result:
		{
			if(flag==0) printf("Math error!\n (Can't devide by zero)\n");
			else
				{
					kq=Popstack2(s);
					printf("ket qua la: %d\n",kq);
				}
		}
}
void main()
{
	int loop;
	do
		{
			char p1[100];
			char p2[100];
			int number,p;
			printf("nhap chuoi: ");
			fflush(stdin);
			gets_s(p1,100);
			fflush(stdin);
			//printf("Input: ")
			puts(p1);
			//printf("Converting...\n");
			infixtopostfix(p1,p2,p);
			cal(p2,p);
			printf("ban hay thu lai? \n nhap so: ");
			scanf_s("%d",&loop);
		}
	while(loop==1);
}
Yikes when programs get this big and you want help, it's amazingly helpful to have comments on your code.

Also, the .h versions of many headers are outdated. You should replace your includes with the up to date versions. I think PI should not be #defined but declared as a const double and you should remove your goto statements and replace them with something more sensible. For instance, each operation could be its own function and when an operation is selected, it could call up the correct function. This would make your program much more readable.

Some other problems here too:

-void main() must be changed to int main(). Which compiler are you using for this? It seems to be out of date.
-You are not using some of those headers, so why include them in your code? It's inflating your program unnecessarily.

Anyway, if you can comment your code and/or narrow down your error to something more than 'can't give me right result' then I will help you fix your problem.
thanks u to mention to my post but right now i dont need to make it work 100% :D i must convert it into class ,cus this is my Object-oriented programming project ......... can u help me to convert it
Topic archived. No new replies allowed.