资源简介
将输入的表达式由中缀变后缀 后输出结果 完全用C语言实现的
代码片段和文件信息
#include
#include
#define MAXNUM 100
typedef double DataType;
struct SeqStack
{ DataType s[MAXNUM];
int t;
};
typedef struct SeqStack *PSeqStack;
PSeqStack createEmptyStack_seq()
{
PSeqStack pastack;
pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack == NULL)
printf(“Out of space!!\n“);
else
pastack->t = -1;
return pastack;
}
int isEmptyStack_seq(PSeqStack pastack)
{
return pastack->t == -1;
}
void push_seq(PSeqStack pastack DataType x)
{
if (pastack->t >= MAXNUM - 1)
printf(“Overflow!\n“);
else
{
pastack->t = pastack->t + 1;
pastack->s[pastack->t] = x;
}
}
void pop_seq(PSeqStack pastack)
{
if (pastack->t == -1)
printf(“Underflow!\n“);
else
pastack->t = pastack->t - 1;
}
DataType top_seq(PSeqStack pastack)
{
return pastack->s[pastack->t];
}
double chartonum(char *strint *i)
{
double x=0y=0;
double j=10.0;
while(str[*i]>=‘0‘&&str[*i]<=‘9‘)
{
x=x*10+(str[*i]-‘0‘);
(*i)++;
}
if(str[*i]==‘.‘)
{
(*i)++;
while (str[*i]>=‘0‘&&str[*i]<=‘9‘)
{
y+=(str[*i] -‘0‘)/j;
j*=10;
(*i)++;
}
}
x+=y;
return x;
}
infixtoSuffix(char *infix char *suffix)
{
int shuqian = 0;
char cc2;
PSeqStack ps = createEmptyStack_seq();
int i j = 0;
if (infix[0] == ‘\0‘)
return 0;
for (i = 0; infix[i] != ‘\0‘; i++)
{
c = infix[i];
if(c==‘ ‘)
{
if (shuqian == 1)
suffix[j++] = ‘ ‘;
shuqian = 0;
}
if(c>=‘0‘&&c<=‘9‘||c==‘.‘)
{
shuqian = 1;
suffix[j++] = c;
}
if(c== ‘(‘)
{if (shuqian == 1)
suffix[j++] = ‘ ‘;
shuqian = 0;
push_seq(ps c);
}
if(c== ‘)‘)
{if (shuqian == 1)
suffix[j++] = ‘ ‘;
shuqian = 0;
c2 = ‘)‘;
while (!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);
pop_seq(ps);
if (c2 == ‘(‘)
break;
suffix[j++] = c2;
}
if (c2 != ‘(‘)
{
free(ps);
suffix[j++] = ‘\0‘;
return 0;
}
}
if(c== ‘+‘||c==‘-‘)
{
if (shuqian == 1)
suffix[j++] = ‘ ‘;
shuqian = 0;
while(!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);
if (c2 == ‘+‘ || c2 == ‘-‘ || c2 == ‘*‘ || c2 == ‘/‘)
{
pop_seq(ps);
suffix[j++] = c2;
}
else if(c2==‘(‘) break;
}
push_seq(ps c);
}
if(c== ‘*‘||c==‘/‘)
{
if (shuqian == 1)
suffix[j++] = ‘ ‘;
shuqian = 0;
while (!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);
if (c2 == ‘*‘ || c2 == ‘/‘)
{
pop_seq(ps);
suffix[j++] = c2;
}
else if(c2==‘+‘||c2==‘-‘||c2==‘(‘) break;
}
push_seq(ps c);
}
}
if (shuqian == 1) suffix[j++] = ‘ ‘;
while (!isEmptyStack_seq(ps))
{
c2 = top_seq(ps);
pop_seq(ps);
if (c2 == ‘(‘)
{
free(ps);
suffix[j++] = ‘\0‘;
return 0;
}
suffix[j++] = c2;
}
free(ps);
suffix[j++] = ‘\0‘;
return 1;
}
double calculateSuffix( char * suffix double * presult)
{
PSeqStack ps = createEmptyStack_seq();
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4017 2009-10-22 10:42 扫描两遍\saomiaoliangbian.c
文件 35840 2009-10-22 10:41 扫描两遍\扫描两遍.doc
目录 0 2009-11-04 18:50 扫描两遍
----------- --------- ---------- ----- ----
39857 3
- 上一篇:LBM溃坝算例代码
- 下一篇:用C语言实现DPSK数字信号调制
评论
共有 条评论