栈的应用

//括号匹配

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef int datatype;
typedef struct
{
	datatype a[MAXSIZE];
	int top;
}sequence_stack;
void init(sequence_stack *st)
{
	st->top=0;
 }
 datatype read(sequence_stack *st)    //读取栈顶的元素
 {
 	if(st->top==0)
 	{
 		printf("栈是空的\n");
 		exit(1);
	 }
	 return st->a[st->top-1];
 }
int empty(sequence_stack *st)
{
	return st->top ? 0:1;
}
void push(sequence_stack *st,datatype x) //将元素压入栈
{
	if(st->top==MAXSIZE)
	{
		printf("栈是满的\n");
		exit(1);
	}
	st->a[st->top]=x;
	st->top++;
}
void pop(sequence_stack *st)  //弹出栈顶元素
{
	if(!st->top)
	{
		printf("栈是空的\n");
		exit(1);
	}
	st->top--;
}
int match_kuohao(char s[])
{
	int i=0;
	sequence_stack s1;
	init(&s1);
	while(s[i]!='#')
	{
	    switch(s[i])
		{
		   case '(' :
		   case '['	:
		   case '{' :
		   	    push(&s1,s[i]);
		   	    break;
		   case ')':
		   	     if(!empty(&s1)&&read(&s1)=='(') //每次匹配成功,删除匹配成功的括号
		   	     {
		   	        pop(&s1);
		   	        break;
				 }
				 else return 0;
		   case '}':
		   	     if(!empty(&s1)&&read(&s1)=='}')
		   	     {
		   	        pop(&s1);
					break;
				 }
				 else return 0;
		   case ']':
		   	      if(!empty(&s1)&&read(&s1)=='[')
		   	      {
		   	      	  pop(&s1);
		   	      	  break;
				  }
		}
		i++;
	}
	return (empty(&s1));//当栈中没有元素时括号匹配成功
 }
int main()
{
	char s[100];
	int i=0;
	int res=0;
	while(scanf("%c",&s[i])&&s[i]!='#')  //从左到右将字符串压入栈
	{
	     // getchar();
	      i++;
	}
	res=match_kuohao(s);
	if(res)
	   printf("yes\n");
	else printf("no\n");

	return 0;
}



后缀表达式

#include<stdio.h>
#include<stdlib.h>
#define maxn 11111
double readnum(char f[],int *i)
{
	double x=0;
	int k=0;  //记录小数位数 
	while(f[*i]>='0'&&f[*i]<='9')
	{
		x=x*10+(f[*i]-'0');
		(*i)++;
	}
	if(f[*i]=='.') (*i)++;
	while(f[*i]>='0'&&f[*i]<='9')
	{
		x=x*10+(f[*i]-'0');
		(*i)++;
		k++; 
	}
	while(k)
	{
		x/=10;
		k--;
	} 
	return x;
}
double evalpost(char f[])
{
	double obst[100];
	int top=0;
	int i=0;
	double x1,x2;
	while(f[i]!='#')
	{
		if(f[i]>='0'&&f[i]<='9')
			obst[top++]=readnum(f,&i);
		else if(f[i]==' ') i++;
		      else if(f[i]=='+')
		           {
		           	   x1=obst[--top];
		           	   x2=obst[--top];
		           	   obst[top++]=x1+x2;
		           	   i++;
				   }
				   else 
			       if(f[i]=='-')
			         {
			         	x1=obst[--top];
			         	x2=obst[--top];
			         	obst[top++]=x2-x1;
			         	i++;
					 }else 
					 if(f[i]=='*')
					 {
			       	  x1=obst[--top];
			       	  x2=obst[--top];
			       	  obst[top++]=x2*x1;
			       	  i++;
					 }else 
					 if(f[i]=='/')
					   { 	
				       	  x1=obst[--top];
				       	  x2=obst[--top];
				       	  obst[top++]=x2/x1;
				       	  i++;
					   }
	}
	return obst[0];
}
int main()
{
	char s[maxn];
	int i=0;
	while(scanf("%c",&s[i])&&s[i]!='#')  i++;
	printf("%lf",evalpost(s));
	
	return 0;
} 



中缀表达式

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 11111
double readnum(char s[],int *i)
{
    int k=0;
    double sum=0;
    while(s[*i]>='0'&&s[*i]<='9')
    {
        sum=sum*10+(s[*i]-'0');
        (*i)++;
    }
    if(s[*i]=='.')
	{
	    (*i)++;
		while(s[*i]>='0'&&s[*i]<='9')
	    {
	        sum=sum*10+(s[*i]-'0');
	        k++;
	        (*i)++;
	    }
	}
    while(k)
    {
        sum/=10;
        k--;
    }
    return sum;
}
double evalpost(char s[])
{
    int i=0;
    double obst[maxn];
    int top=0;
    double x,y;
    while(s[i]!='#')
    {
        if(s[i]>='0'&&s[i]<='9')
            obst[top++]=readnum(s,&i);
        else if(s[i]==' ') i++;
             else if(s[i]=='+')
             {
                 x=obst[--top];
                 y=obst[--top];
                 obst[top++]=y+x;
                 i++;
             }else if(s[i]=='-')
               {
                   x=obst[--top];
                   y=obst[--top];
                   obst[top++]=y-x;
                   i++;
               }else if(s[i]=='*')
                {
                    x=obst[--top];
                    y=obst[--top];
                    obst[top++]=y*x;
                    i++;
                }else if(s[i]=='/')
                 {
                     x=obst[--top];
                     y=obst[--top];
                     obst[top++]=y/x;
                     i++;
                 }
    }
    return obst[0];
}
int is_operator(char c)
{
    switch(c)
    {
      case '+':
      case '-':
      case '*':
      case '/':  return 1;
      default :  return 0;
    }
}
int priority(char c)
{
    switch(c)
    {
       case '#':
           return -1;
       case '(':
           return 0;
       case '+':
       case '-':
           return 1;
       case '*':
       case '/':
           return 2;
       default :
           return -1;
    }
}
void postfix(char s[],char f[])
{
    int i=0;
    int top=0;
    int j=0;
    char opst[maxn];
    opst[top++]='#';
    while(s[i]!='#')
    {
        if(s[i]>='0'&&s[i]<='9'||s[i]=='.') f[j++]=s[i];
        else if(s[i]=='(') opst[top++]=s[i];
            else if(is_operator(s[i]))
            {
                 f[j++]=' ';
                 while(priority(opst[top-1])>=priority(s[i]))
                 {
                    f[j++]=opst[--top];
                 }
                 opst[top++]=s[i];
            }else if(s[i]==')')
              {
                  int t=top-1;
                  while(opst[t]!='(')
                  {
                    f[j++]=opst[top-1];
                    top--;  //pop一个值,减一
                    t=top-1;//栈顶的下标
                  }
                  top--; //弹出’)‘
              }
        i++;
    }
    while(top) f[j++]=opst[--top];
}
int main()
{
    char s[maxn];
    char f[maxn];
    int i=0;
    double sum=0;
    while(scanf("%c",&s[i])&&s[i]!='#') i++;
    postfix(s,f);
    int len =strlen(f);
    for(i=0;i<len;i++) printf("%c",f[i]);
    sum=evalpost(f);
    printf("%lf\n",sum);
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LIN协议是一种用于低成本、低速率串行网络的通信协议,主要应用于车辆电子系统中。以下是关于LIN协议应用的一些分享。 首先,LIN协议在车辆电子系统中常用于连接各种低速率从设备,如门控制单元、窗户控制单元、座椅控制单元等。通过LIN总线,这些从设备可以与车辆主控制单元进行通信和控制。相比于其他高速率通信协议,如CAN协议,LIN协议更适用于这些低速率从设备的通信需求。 其次,LIN协议具有较低的成本优势。由于LIN协议使用的是普通的串行通信线路,不需要专用的硬件设备支持,因此整体成本相对较低。这使得LIN协议在汽车电子系统中得到广泛的应用,特别是在非关键性应用中。 此外,LIN协议还具有较低的功耗。由于LIN协议使用的是低速率通信,通信过程中的能耗相对较低。这在需要长时间运行的系统中尤为重要,例如关闭引擎后仍需要继续运行的车辆电子系统。 最后,LIN协议还支持多主从架构。这意味着在LIN总线上可以连接多个主设备和从设备,实现复杂的通信和控制功能。这种灵活性使得LIN协议在车辆电子系统中能够满足不同设备之间的通信需求。 总之,LIN协议在车辆电子系统中应用广泛,并且具有成本低、功耗低和多主从支持等优势。随着车辆电子化的发展,LIN协议在汽车行业中的应用前景将会更加广阔。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值