表达式问题-C

表达式转后缀表达式: 

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h> 
#include<assert.h>
#define INITSIZE  20
#define INCREMENT 10
#define MAXBUFFER 20
#define LEN  sizeof(Elemtype)
/*栈的动态分配存储结构*/ 
typedef char Elemtype;
typedef struct{
	Elemtype *base;
	Elemtype *top;
	int StackSize;
}SqStack;

/*初始化栈*/
void InitStack(SqStack *S) {
	S->base=(Elemtype*)malloc(LEN*INITSIZE);
	assert(S->base !=NULL);
	S->top=S->base;
	S->StackSize=INITSIZE;
}

/*压栈操作*/ 
void PushStack(SqStack *S,Elemtype c) {
	if(S->top - S->base >= S->StackSize) {
		S->base=(Elemtype*)realloc(S->base,LEN*(S->StackSize+INCREMENT));
		assert(S->base !=NULL);
		S->top =S->base+S->StackSize;
		S->StackSize+=INCREMENT;
	}
	*S->top++ = c;
}
/*求栈长*/
int StackLength(SqStack *S) {
	return (S->top - S->base);
}
/*弹栈操作*/
int PopStack(SqStack *S,Elemtype *c) {
	if(!StackLength(S)) {
		return 0;
	}
	*c=*--S->top;
	return 1;
}

/*中缀转后缀函数*/
void Change(SqStack *S,Elemtype str[]) {
	int i=0;
	Elemtype e;
	InitStack(S);
	while(str[i]!='\0') {
		while(isdigit(str[i])) {
			/*过滤数字字符,直接输出,直到下一位不是数字字符打印空格跳出循环 */
			printf("%c",str[i++]);
			if(!isdigit(str[i])) {
				printf(" ");
			}
		}
		if(str[i]=='+'||str[i]=='-') {
			if(!StackLength(S)) {
				PushStack(S,str[i]);
			} else {
				do {
					PopStack(S,&e);
					if(e=='(')
					{
						PushStack(S,e);
					} else {
						printf("%c ",e);
					}
				} while( StackLength(S) && e != '(' ); 
				PushStack(S,str[i]);
			}
		}
		/*当遇到右括号是,把括号里剩余的运算符弹出,直到匹配到左括号为止
		左括号只弹出不打印(右括号也不压栈)*/
		else if(str[i]==')') {
			PopStack(S,&e);
			while(e!='(') {
				printf("%c ",e);
				PopStack(S,&e);
			}
		} else if(str[i]=='*'||str[i]=='/'||str[i]=='(') {
			/*乘、除、左括号都是优先级高的,直接压栈*/
			PushStack(S,str[i]);
		} else if(str[i]=='\0') {
			break;
		} else {
			printf("\n输入格式错误!\n");
			return ;
		}
		i++;
	}
	/*最后把栈中剩余的运算符依次弹栈打印*/
	while(StackLength(S)) {
		PopStack(S,&e);
		printf("%c ",e);
	}
}
int main() {
	printf("请输入表达式,如:(2+(((2+6)*6+2)*2+6)*2):\n\n"); 
	Elemtype str[MAXBUFFER];
	SqStack S;
	gets(str);
	printf("\n转化为后缀表达式:");
	Change(&S,str);
	return 0;
}

 后缀表达式的计算:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<ctype.h>
#define INITSIZE  20
#define INCREMENT 10
#define MAXBUFFER 10
#define LEN   sizeof(Elemtype)
/*栈的动态分配顺序存储结构*/
typedef double Elemtype;

//结构体 
typedef struct{
	Elemtype *base;
	Elemtype *top;
	int StackSize; 
}SqStack;
//初始化栈 
void InitStack(SqStack *S) {
	S->base=(Elemtype*)malloc(LEN*INITSIZE);
	assert(S->base != NULL);
	S->top=S->base;
	S->StackSize=INITSIZE;
}
//压栈 
void PushStack(SqStack *S,Elemtype e) {
	if(S->top - S->base >= S->StackSize)
	{
		S->base=(Elemtype*)realloc(S->base,(S->StackSize+INCREMENT)*LEN);
		assert(S->base !=NULL);
		S->top=S->base+S->StackSize;
		S->StackSize+=INCREMENT;
	}
	*S->top =e;
	 S->top++;
} 
//出栈 
void PopStack(SqStack *S,Elemtype *e) {
	*e=*--S->top;
} 
//计算-关键函数 
void CalFunction(SqStack *S,char str[]) {
	Elemtype number,e,d;
	char arr[MAXBUFFER];
	int i=0,j=0; 
	InitStack(S);
	while(str[i]!='\0') {
		while(isdigit(str[i])||str[i]=='.') { //过滤数字
			arr[j++]=str[i++];
			arr[j]='\0';
			if( j >= MAXBUFFER ) {
				printf("输入单个数据过大!\n");
				return ;
			}
			if(str[i]==' ') {
				number=atof(arr);    //利用atof函数将数字字符转化为double型数据
				PushStack(S,number); //将转换的数进行压栈
				j=0;
				break;
			}
		}
		switch(str[i]) {
			case '+':
				PopStack(S,&e);
				PopStack(S,&d);
				PushStack(S,d+e);
				break;
			case '-':
				PopStack(S,&e);
				PopStack(S,&d);
				PushStack(S,d-e);
				break;
			case '*':
				PopStack(S,&e);
				PopStack(S,&d);
				PushStack(S,d*e);
				break;
			case '/':
				PopStack(S,&e);
				PopStack(S,&d);
				if(e == 0) {
					printf("输入出错,分母为零!\n");
					return ;
				}
				PushStack(S,d/e);
				break;
		}
		i++; 	
	}
	PopStack(S,&e);
	printf("计算结果为:%lf",e);	
}
//主函数 
int main() {
	char str[100];
	SqStack S;
	printf("请按逆波兰表达式输入数据,每个数据之间用空格隔开,如:2 2 6 + 6 * 2 + 2 * 6 + 2 * + \n");
	gets(str);
	CalFunction(&S,str);
	printf("\n\n");
	return 0;
}

 

全部评论

相关推荐

(黑话警告⚠️:hc=岗位数量,&nbsp;mt=导师,&nbsp;ld=直属领导,&nbsp;cr=代码审查)25年1月,我加入了字节某前端团队,并期望能在这里待到秋招并尝试转正。然而,就在上周,ld&nbsp;找我1v1,告诉我,我的能力和团队预期不太匹配,并和我劝退。晴天霹雳吗?肯定是有的。那一刻,脑子里嗡嗡作响,各种情绪翻涌。但冷静下来想想,这几个月,自己在能掌控的范围内,确实有不少地方做得不尽如人意。所以,我想把这段不算成功的经历复盘一下,希望能给同样在努力转正的你提个醒,避开我踩过的坑。一、ld&nbsp;的要求要注意刚进组时,ld就和我聊过转正的事。我当时发问:“咱们这儿有hc&nbsp;吗?”&nbsp;ld没直接回答,只是说:“看能力,能力到了...
牛客上的彭于晏:过来人告诉你,入职后要做的第一件事儿不是说主动找活儿做,你要先学会融入团队,摸清ld的性格,投其所好。然后才是展示你的能力,能力上可以说技术或者业务,以业务能力为主,技术能力为辅。优先保证自己对业务需求的开发保证质量效率,然后再谈技术的问题,不要你觉得啥啥啥不行就想着整体优化了(发现校招生最喜欢干这事儿),我工作快5年了发现搞这种的最后都没啥好的结果,产出没有还引入新的bug,校招或者实习的水平看到的问题别人看不到嘛?为什么别人不去搞?浪费时间还没收益的事儿不要去做,技术上的能力体现在对于一个新需求,在不符合现在业务发展的架构设计上,你能拿出好的技术方案同时能考虑到后续业务发展逐渐将技术架构引入合理的架构,这是一个漫长的过程而不是一次性的
点赞 评论 收藏
分享
05-01 22:41
中南大学 Java
点赞 评论 收藏
分享
04-17 10:16
门头沟学院 Java
小浪_coder:24届很难找了,马上25的都毕业了还有很多没找到的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务