Error[8]: Undefined offset: 652, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

1.题目:

中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为后缀:

分析过程如图所示:

具体代码如下:

#include
#include
#include
char *s="a+b-a*((c+d)/e-f)+g";//表示中缀的字符串 
char m[30];//表示后缀的字符串 
char stack[10];
int n=-1;
int DM(char s){//判断运算符的优先级函数 
	if(s=='+'||s=='-'){
		return 1;
	}
	if(s=='*'||s=='/'){
		return 2;
	}
	if(s=='('){
		return 0;
	}
}

int  send(char *s,int *top){//中缀变后缀功能函数 
	
	if('a'<=*s&&*s<='z'){
		m[++n]=*s; 
	}else if(*top==-1){
		stack[++(*top)]=*s;
	} else if(*s=='+'||*s=='-'||*s=='*'||*s=='/'){
		if(DM(*s)<=DM(stack[*top])){
			m[++n]=stack[(*top)--];
			send(s,top);
		}
		if(DM(*s)>DM(stack[*top])){
		stack[++(*top)]=*s;}
	} else if(*s=='('){
		stack[++(*top)]=*s;
	}else if(*s==')'){
			m[++n]=stack[(*top)--];
			
			if(stack[*top]=='('){
				(*top)--;
				
				return 0;
			}else{
			send(s,top);
			}
			
			
	}

	return 0;
}


int main(){
	int w=-1;
	char *a=m;
	
	while(*s!=')'send{
		(,s&)w;++
	    s;}
	while
	(!=w-1)[{
		m++]n=[stack--w];}
	printf
    ("%s",)m;return
	0 ;}
	
# 2.题目:

中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为前缀:

分析如下:
在中缀->后缀中的基础上分析,思想倒置。



1.从右往左扫描中缀字符串,
2.新建立的字符串也从右往左写入


具体代码如下:

include#
include#
includechar
* =s"?a+b-a*((c+d)/e-f)+g";//表示中缀的字符串 char
[ m20];//表示后缀的字符串 char
[ stack10];int
= n19;int
DM (char) s//判断运算符的优先级函数 {if
	(==s'+'||==s'-')return{
		1 ;}
	if
	(==s'*'||==s'/')return{
		2 ;}
	if
	(==s')')return{
		0 ;//此处可以写到star()函数中  }
	}
int

star  (char* ,sint* )topif{
	('a'<=*&&s*<=s'z')//数字(字母)直接写入字符串 {[
		m--n]=*;s}
	 elseif (*==s'+'||*==s'-'||*==s'*'||*==s'/')//运算符进项栈 *** 作后在进字符串 {if
	    (*==top-1)[{
		   stack++(*)top]=*;s//当前扫描到的元素是运算符,且栈为空直接入栈 //return 0;
		   }
		elseif (DM(*)s<DM([stack*]top))//当前运算符的优先级小于栈顶运算符的优先级 {[
			m--n]=[stack(*)top--];//将栈顶元素输出到字符串中 star
			(,s)top;}
		elseif (DM(*)sDM>=([stack*]top))//当前运算符的优先级大于等于栈顶运算符的优先级 {[
			stack++(*)top]=*;s//进栈 }
		}
		elseif (*==s')')//诺当前元素为右括号, {[
			stack++(*)top]=*;s//直接进栈 }
		elseif (*==s'(')//诺当前元素为左括号,{[
			m--n]=[stack(*)top--];//栈顶出栈进字符串 if
			([stack*]top==')')//当栈顶为右括号时 {(
			  *)top--;//出栈一次 //(*top)--;
			  return
			  0 ;}
		else//否则递归思想,直到右括号为止 {star
			(,s)top;}
		}
	return
	
	0 ;}

int
 
 
main ()int{
    = a0;int = top-1;while
	(*!=s')'++;{
		s}while
	(
	*!='?'s)--;{
		sstar(
		,&s);top//功能函数 //printf("%d",a);  }
		printf
	(
	"%d",);topwhile(
    !=-top1)[--{
		m]n=[--stack]top;}while
	(
	<20a)printf({
	"%c",[]m)a;++;
	a}return
	0

	; }[+++]
[+++]

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 653, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

1.题目:

中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为后缀:

分析过程如图所示:

具体代码如下:

#include
#include
#include
char *s="a+b-a*((c+d)/e-f)+g";//表示中缀的字符串 
char m[30];//表示后缀的字符串 
char stack[10];
int n=-1;
int DM(char s){//判断运算符的优先级函数 
	if(s=='+'||s=='-'){
		return 1;
	}
	if(s=='*'||s=='/'){
		return 2;
	}
	if(s=='('){
		return 0;
	}
}

int  send(char *s,int *top){//中缀变后缀功能函数 
	
	if('a'<=*s&&*s<='z'){
		m[++n]=*s; 
	}else if(*top==-1){
		stack[++(*top)]=*s;
	} else if(*s=='+'||*s=='-'||*s=='*'||*s=='/'){
		if(DM(*s)<=DM(stack[*top])){
			m[++n]=stack[(*top)--];
			send(s,top);
		}
		if(DM(*s)>DM(stack[*top])){
		stack[++(*top)]=*s;}
	} else if(*s=='('){
		stack[++(*top)]=*s;
	}else if(*s==')'){
			m[++n]=stack[(*top)--];
			
			if(stack[*top]=='('){
				(*top)--;
				
				return 0;
			}else{
			send(s,top);
			}
			
			
	}

	return 0;
}


int main(){
	int w=-1;
	char *a=m;
	
	while(*s!=')'send{
		(,s&)w;++
	    s;}
	while
	(!=w-1)[{
		m++]n=[stack--w];}
	printf
    ("%s",)m;return
	0 ;}
	
# 2.题目:

中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为前缀:

分析如下:
在中缀->后缀中的基础上分析,思想倒置。



1.从右往左扫描中缀字符串,
2.新建立的字符串也从右往左写入


具体代码如下:

include#
include#
includechar
* =s"?a+b-a*((c+d)/e-f)+g";//表示中缀的字符串 char
[ m20];//表示后缀的字符串 char
[ stack10];int
= n19;int
DM (char) s//判断运算符的优先级函数 {if
	(==s'+'||==s'-')return{
		1 ;}
	if
	(==s'*'||==s'/')return{
		2 ;}
	if
	(==s')')return{
		0 ;//此处可以写到star()函数中  }
	}
int

star  (char* ,sint* )topif{
	('a'<=*&&s*<=s'z')//数字(字母)直接写入字符串 {[
		m--n]=*;s}
	 elseif (*==s'+'||*==s'-'||*==s'*'||*==s'/')//运算符进项栈 *** 作后在进字符串 {if
	    (*==top-1)[{
		   stack++(*)top]=*;s//当前扫描到的元素是运算符,且栈为空直接入栈 //return 0;
		   }
		elseif (DM(*)s<DM([stack*]top))//当前运算符的优先级小于栈顶运算符的优先级 {[
			m--n]=[stack(*)top--];//将栈顶元素输出到字符串中 star
			(,s)top;}
		elseif (DM(*)sDM>=([stack*]top))//当前运算符的优先级大于等于栈顶运算符的优先级 {[
			stack++(*)top]=*;s//进栈 }
		}
		elseif (*==s')')//诺当前元素为右括号, {[
			stack++(*)top]=*;s//直接进栈 }
		elseif (*==s'(')//诺当前元素为左括号,{[
			m--n]=[stack(*)top--];//栈顶出栈进字符串 if
			([stack*]top==')')//当栈顶为右括号时 {(
			  *)top--;//出栈一次 //(*top)--;
			  return
			  0 ;}
		else//否则递归思想,直到右括号为止 {star
			(,s)top;}
		}
	return
	
	0 ;}

int
 
 
main ()int{
    = a0;int = top-1;while
	(*!=s')'++;{
		s}while
	(
	*!='?'s)--;{
		sstar(
		,&s);top//功能函数 //printf("%d",a);  }
		printf
	(
	"%d",);topwhile(
    !=-top1)[--{
		m]n=[--stack]top;}while
	(
	<20a)printf({
	"%c",[]m)a;++;
	a}return
	0

	; }
[+++]

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
栈的应用之表达式相关问题(C语言实现)_C_内存溢出

栈的应用之表达式相关问题(C语言实现)

栈的应用之表达式相关问题(C语言实现),第1张

1.题目:

中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为后缀:

分析过程如图所示:

具体代码如下:

#include
#include
#include
char *s="a+b-a*((c+d)/e-f)+g";//表示中缀的字符串 
char m[30];//表示后缀的字符串 
char stack[10];
int n=-1;
int DM(char s){//判断运算符的优先级函数 
	if(s=='+'||s=='-'){
		return 1;
	}
	if(s=='*'||s=='/'){
		return 2;
	}
	if(s=='('){
		return 0;
	}
}

int  send(char *s,int *top){//中缀变后缀功能函数 
	
	if('a'<=*s&&*s<='z'){
		m[++n]=*s; 
	}else if(*top==-1){
		stack[++(*top)]=*s;
	} else if(*s=='+'||*s=='-'||*s=='*'||*s=='/'){
		if(DM(*s)<=DM(stack[*top])){
			m[++n]=stack[(*top)--];
			send(s,top);
		}
		if(DM(*s)>DM(stack[*top])){
		stack[++(*top)]=*s;}
	} else if(*s=='('){
		stack[++(*top)]=*s;
	}else if(*s==')'){
			m[++n]=stack[(*top)--];
			
			if(stack[*top]=='('){
				(*top)--;
				
				return 0;
			}else{
			send(s,top);
			}
			
			
	}

	return 0;
}


int main(){
	int w=-1;
	char *a=m;
	
	while(*s!=')'send{
		(,s&)w;++
	    s;}
	while
	(!=w-1)[{
		m++]n=[stack--w];}
	printf
    ("%s",)m;return
	0 ;}
	
# 2.题目:

中缀表示: a+b-a*((c+d)/e-f)+g--------将中缀变为前缀:

分析如下:
在中缀->后缀中的基础上分析,思想倒置。



1.从右往左扫描中缀字符串,
2.新建立的字符串也从右往左写入


具体代码如下:

include#
include#
includechar
* =s"?a+b-a*((c+d)/e-f)+g";//表示中缀的字符串 char
[ m20];//表示后缀的字符串 char
[ stack10];int
= n19;int
DM (char) s//判断运算符的优先级函数 {if
	(==s'+'||==s'-')return{
		1 ;}
	if
	(==s'*'||==s'/')return{
		2 ;}
	if
	(==s')')return{
		0 ;//此处可以写到star()函数中  }
	}
int

star  (char* ,sint* )topif{
	('a'<=*&&s*<=s'z')//数字(字母)直接写入字符串 {[
		m--n]=*;s}
	 elseif (*==s'+'||*==s'-'||*==s'*'||*==s'/')//运算符进项栈 *** 作后在进字符串 {if
	    (*==top-1)[{
		   stack++(*)top]=*;s//当前扫描到的元素是运算符,且栈为空直接入栈 //return 0;
		   }
		elseif (DM(*)s<DM([stack*]top))//当前运算符的优先级小于栈顶运算符的优先级 {[
			m--n]=[stack(*)top--];//将栈顶元素输出到字符串中 star
			(,s)top;}
		elseif (DM(*)sDM>=([stack*]top))//当前运算符的优先级大于等于栈顶运算符的优先级 {[
			stack++(*)top]=*;s//进栈 }
		}
		elseif (*==s')')//诺当前元素为右括号, {[
			stack++(*)top]=*;s//直接进栈 }
		elseif (*==s'(')//诺当前元素为左括号,{[
			m--n]=[stack(*)top--];//栈顶出栈进字符串 if
			([stack*]top==')')//当栈顶为右括号时 {(
			  *)top--;//出栈一次 //(*top)--;
			  return
			  0 ;}
		else//否则递归思想,直到右括号为止 {star
			(,s)top;}
		}
	return
	
	0 ;}

int
 
 
main ()int{
    = a0;int = top-1;while
	(*!=s')'++;{
		s}while
	(
	*!='?'s)--;{
		sstar(
		,&s);top//功能函数 //printf("%d",a);  }
		printf
	(
	"%d",);topwhile(
    !=-top1)[--{
		m]n=[--stack]top;}while
	(
	<20a)printf({
	"%c",[]m)a;++;
	a}return
	0

	; }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/568931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存