/*
* stack demo, evaluation for arithmetic expression
* Time-stamp: <2011-08-24 15:17:31 root>
*
* snallie@tom.com
*/
#include <iostream>
using namespace std;
template < class T > class stk {
#define SIZE 100
public:
stk():sp(0) {
stk_base = new T[SIZE];
stk_bottom = stk_base + SIZE - 1;
sp = SIZE;
}
~stk() {
delete stk_base;
}
void push(T a) {
if (!stk_full())
stk_base[--sp] = a;
}
T pop() {
if (!stk_empty())
return stk_base[sp++];
}
bool stk_empty() {
return (sp == SIZE);
}
bool stk_full() {
return (sp == 0);
}
T get_top() {
return stk_base[sp];
}
void stk_dump() {
int i;
for (i = SIZE - 1; i >= sp; i--) {
cout << stk_base[i] << " ";
}
cout << endl;
}
private:
int sp;
T *stk_base;
T *stk_bottom;
};
int main()
{
int i, j;
char a[] = "(5*(((9+8)*(4/6))-7))";
char c[40];
stk < char >b;
int N = strlen(a);
printf("%s\n", a);
for (i = 0, j = 0; i < N; i++) // convert a[] to postsuffix
{
if (a[i] == ')')
printf("%c ", (c[j++] = b.pop()));
if ((a[i] == '+') || (a[i] == '*') || (a[i] == '-')
|| (a[i] == '/'))
b.push(a[i]);
if ((a[i] >= '0') && (a[i] <= '9'))
printf("%c ", (c[j++] = a[i]));
}
printf("\n");
c[j] = 0;
printf("%s\n", c);
stk < float >d;
int k = 0;
while (c[k] != 0) {
printf("%c# ", c[k]);
switch (c[k]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
d.push(c[k] - 0x30);
d.stk_dump();
break;
case '+':
d.push(d.pop() + d.pop());
d.stk_dump();
break;
case '-':
d.push(-(d.pop() - d.pop()));
d.stk_dump();
break;
case '/':
{
float oprnd1 = d.pop();
float oprnd2 = d.pop();
d.push(oprnd2 / oprnd1);
d.stk_dump();
break;
}
case '*':
d.push(d.pop() * d.pop());
d.stk_dump();
break;
default:
break;
}
k++;
}
cout << a << "=" << d.pop() << endl;
return 0;
}
* stack demo, evaluation for arithmetic expression
* Time-stamp: <2011-08-24 15:17:31 root>
*
* snallie@tom.com
*/
#include <iostream>
using namespace std;
template < class T > class stk {
#define SIZE 100
public:
stk():sp(0) {
stk_base = new T[SIZE];
stk_bottom = stk_base + SIZE - 1;
sp = SIZE;
}
~stk() {
delete stk_base;
}
void push(T a) {
if (!stk_full())
stk_base[--sp] = a;
}
T pop() {
if (!stk_empty())
return stk_base[sp++];
}
bool stk_empty() {
return (sp == SIZE);
}
bool stk_full() {
return (sp == 0);
}
T get_top() {
return stk_base[sp];
}
void stk_dump() {
int i;
for (i = SIZE - 1; i >= sp; i--) {
cout << stk_base[i] << " ";
}
cout << endl;
}
private:
int sp;
T *stk_base;
T *stk_bottom;
};
int main()
{
int i, j;
char a[] = "(5*(((9+8)*(4/6))-7))";
char c[40];
stk < char >b;
int N = strlen(a);
printf("%s\n", a);
for (i = 0, j = 0; i < N; i++) // convert a[] to postsuffix
{
if (a[i] == ')')
printf("%c ", (c[j++] = b.pop()));
if ((a[i] == '+') || (a[i] == '*') || (a[i] == '-')
|| (a[i] == '/'))
b.push(a[i]);
if ((a[i] >= '0') && (a[i] <= '9'))
printf("%c ", (c[j++] = a[i]));
}
printf("\n");
c[j] = 0;
printf("%s\n", c);
stk < float >d;
int k = 0;
while (c[k] != 0) {
printf("%c# ", c[k]);
switch (c[k]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
d.push(c[k] - 0x30);
d.stk_dump();
break;
case '+':
d.push(d.pop() + d.pop());
d.stk_dump();
break;
case '-':
d.push(-(d.pop() - d.pop()));
d.stk_dump();
break;
case '/':
{
float oprnd1 = d.pop();
float oprnd2 = d.pop();
d.push(oprnd2 / oprnd1);
d.stk_dump();
break;
}
case '*':
d.push(d.pop() * d.pop());
d.stk_dump();
break;
default:
break;
}
k++;
}
cout << a << "=" << d.pop() << endl;
return 0;
}



被折叠的 条评论
为什么被折叠?



