69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
typedef struct node
{
char data;
struct node *next;
}snode,*slink;
int Emptystack(slink S)
{
if(S==NULL)return 1;
else return 0;
}
char Pop(slink top)
{
char e;
slink p;
if(Emptystack(top))return -1;
else
{
e=(top)->data;
p=top;top=(top)->next;
free(p);return e;
}
}
void Push(slink top,char e)
{
slink p;
p=(slink)malloc(sizeof(snode));
p->data=e;
p->next=top;
top=p;
}
void Clearstack(slink top)
{
slink p;
while(top!=NULL)
{
p=(top)->next;
Pop(top);
top=p;
}
top=NULL;
}
char Getstop(slink S)
{
if(S!=NULL)return(S->data);
return 0;
}
int Precede(char x,char y) /*比较x是否大于y*/
{int a,b;
switch(x)
{
case '(':a=0;break;
case '+':
case '-':a=1;break;
case '*':
case '/':a=2;break;
default:printf("字符错误");
}
switch(y)
{
case '(':b=0;break;
case '+':
case '-':b=1;break;
case '*':
case '/':b=2;break;
default:printf("字符错误");
}
if(a>=b)return 1;
else return 0;
}
void mid_post(char post[],char mid[]) //中(mid)、后(post)缀表达式转换
{
int i=0,j=0;
char x;
slink S;
S = (slink)malloc(sizeof(snode));
Clearstack(S);
Push(S,'#'); //‘#’入栈
do
{
x=mid[i++] ; //扫描当前表达式分量
switch(x)
{
case '#': {while(!Emptystack(S))
post[j++]=Pop(S);
}break; //输出栈顶运算符,并退栈
case ')':
{ while(Getstop(S)!='(')
post[j++]=Pop(S); //输出栈顶
Pop(S); //退掉‘(’
}break;
case '+': //x为运算符
case '-':
case '*':
case '/':
case '(':
{ while(Precede(Getstop(S), x)) //栈顶运算符(Q1)与x比较
{
post[j++]=Pop(S); // (Q1>=x时,输出栈顶符并栈
}
Push(S,x); //(Q1)<x时x进栈
}break;
default:post[j++]=x; //操作数直接输出
}
}
while(x!='#');
post[j]='#'; //置表达式结束符
}
int postcount(char post[]) //后缀表达式求值
{
int i=0; char x;
float z,a,b;
slink S;
S = (slink)malloc(sizeof(snode));
while(post[i]!='#')
{
x=post[i];
switch(x)
{
case '+':b=Pop(S); a=Pop(S);z=a+b; Push(S,z); break;
case '-':b=Pop(S); a=Pop(S);z=a-b; Push(S,z); break;
case '*':b=Pop(S); a=Pop(S);z=a*b; Push(S,z); break;
case '/':b=Pop(S); a=Pop(S);z=a/b; Push(S,z); break;
default:x=post[i]-'0';Push(S,x); /*Push()是字符型的,压入的是数字的ASCII码的值吗?*/
}
i++;
}
if(!Emptystack(S))return(Getstop(S));
else return 0;
}
void main()
{
char mid[255]=" ",post[255];
printf("请输入要处理的中缀表达式\n");
scanf("%s",mid);
while(getchar() != '\n') //清空缓冲区
;
printf("相应的后缀表达式为\n");
mid_post(post,mid);
printf("%s\n",post);
//printf("表达式的值为:%d",postcount(post));
getch();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fun1(char *p);
void fun2(char **p);
int main(void)
{
char *p;
fun1(p);
printf("%s\n", p);
fun2(&p);
printf("%s\n", p);
free(p);
return 0;
}
void fun1(char *p)
{
p = malloc(5 *sizeof(char));
memcpy(p, "test", 5);
}
void fun2(char **p)
{
*p = malloc(5 * sizeof(char));
memcpy(*p, "test", 5);
}
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *next;
}snode,*slink;
int Emptystack(slink *S)
{
if(*S == NULL)
return 1;
else
return 0;
}
char Pop(slink *top)
{
char e;
slink p;
if(Emptystack(top))
return -1;
else
{
e = (*top)->data;
p = *top;
*top = (*top)->next;
free(p);
return e;
}
}
void Push(slink *top, char e)
{
slink p;
p = (slink)malloc(sizeof(snode));
p->data = e;
p->next = *top;
*top = p;
}
void Clearstack(slink *top)
{
slink p;
while(*top != NULL)
{
p=(*top)->next;
Pop(top);
*top=p;
}
*top=NULL;
}
char Getstop(slink *S)
{
if(*S != NULL)
return((*S)->data);
return 0;
}
int Precede(char x,char y) /*比较x是否大于y*/
{
int a,b;
switch(x)
{
case '(':a=0;break;
case '+':
case '-':a=1;break;
case '*':
case '/':a=2;break;
case '#':a=-1;break;
default:printf("字符错误");
}
switch(y)
{
case '(': b=0; break;
case '+':
case '-': b=1; break;
case '*':
case '/': b=2; break;
case '#': b=-1;break;
default : printf("字符错误");
}
if (a >= b)
return 1;
else
return 0;
}
void mid_post(char post[],char mid[]) //中(mid)、后(post)缀表达式转换
{
int i=0,j=0;
char x;
slink S = NULL;
Push(&S, '#'); //‘#’入栈
do {
x = mid[i++] ; //扫描当前表达式分量
switch(x) {
case '\0': while(!Emptystack(&S))
post[j++] = Pop(&S);
break; //输出栈顶运算符,并退栈
case ')': while(Getstop(&S) != '(')
post[j++] = Pop(&S); //输出栈顶
Pop(&S); //退掉‘(’
break;
case '+': //x为运算符
case '-':
case '*':
case '/': while(Precede(Getstop(&S), x)) //栈顶运算符(Q1)与x比较
post[j++] = Pop(&S); // (Q1>=x时,输出栈顶符并栈
Push(&S,x); //(Q1)<x时x进栈
break;
case '(': Push(&S,x); break;
default: post[j++] = x; //操作数直接输出
}
} while(x != '\0');
post[j]='\0'; //置表达式结束符
}
int postcount(char post[]) //后缀表达式求值
{
int i=0; char x;
float z,a,b;
slink S;
S = (slink)malloc(sizeof(snode));
while(post[i]!='#')
{
x=post[i];
switch(x)
{
case '+':b=Pop(&S); a=Pop(&S);z=a+b; Push(&S,z); break;
case '-':b=Pop(&S); a=Pop(&S);z=a-b; Push(&S,z); break;
case '*':b=Pop(&S); a=Pop(&S);z=a*b; Push(&S,z); break;
case '/':b=Pop(&S); a=Pop(&S);z=a/b; Push(&S,z); break;
default:x=post[i]-'0';Push(&S,x); /*Push()是字符型的,压入的是数字的ASCII码的值吗?*/
}
i++;
}
if(!Emptystack(&S))
return(Getstop(&S));
else
return 0;
}
int main(void)
{
char mid[255]=" ";
char post[255];
printf("请输入要处理的中缀表达式\n");
scanf("%s",mid);
while(getchar() != '\n') //清空缓冲区
;
printf("相应的后缀表达式为\n");
mid_post(post, mid);
printf("%s\n", post);
//printf("表达式的值为:%d",postcount(post));
return 0;
}
//表达式求值
#include <stdio.h>
#include <malloc.h>
#include <string.h>
/*
*功能:根据运算符计算
*参数:a, b参与运算的数, ch运算符
*返回值:计算结果,操作符错误则返回0
*/
int cal(int a, char ch, int b)
{
switch(ch)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a/b;
break;
}
return 0;
}
/*
*功能:计算表达式的值(用数组模拟栈)
*参数:表达式字符串
*返回值:计算结果
*/
int evaluateExpression(char *str)
{
int i = 0, result, numSub = 0, operSub = 0;
int tmp, len = strlen(str);
int *operand = (int*)malloc(sizeof(int)*len);
char *operat = (char*)malloc(sizeof(char)*len);
while(str[i] != '\0')
{
switch(str[i])
{
case '+':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '+';
break;
case '-':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '-';
break;
case '*':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d * %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '*', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '*';
break;
case '/':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d / %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '/', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '/';
break;
case '(':
operat[operSub++] = '(';
break;
case ')':
while(operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
--operSub;
break;
default:
tmp = 0;
while(str[i] >= '0' && str[i] <= '9')
{
tmp = tmp * 10 + str[i] - '0';
++i;
}
--i;
operand[numSub++] = tmp;
break;
}
++i;
}
while(numSub > 1 && operSub >= 1)
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
result = operand[numSub-1];
free(operand);
free(operat);
return result;
}
int main()
{
char *str = "225/15-20+(4-3)*2";
int result;
printf("计算过程:\n");
result = evaluateExpression(str);
printf("计算结果:result = %d\n", result);
return 0;
}
计算过程:
225 / 15 = 15
15 - 20 = -5
4 - 3 = 1
1 * 2 = 2
-5 + 2 = -3
计算结果:result = -3