输入一个表达式字符串求值

描述: 给定一个以字符串形式表示的算术表达式,计算该表达式的值。
表达式支持如下运算:“+、-、*、/”,其中“*”和“/”的优先级要高于“+”和“-”;
不需要考虑括号,且表达式之间没有空格;
例如:对于表达式"3-2+15*2",该表达式值为31.
思路:
1.找到连续的数字字符字串,并转化成整数。
2.算乘除
3.算加减

用双向链表实现


#include <iostream>
#include <string.h>
using namespace std;
typedef struct node 
{
	union intorchar
	{
		int a;
		char c;
	}IntChar;
	bool flag;
	struct node *next;
	struct node *pre;
}Node,*pNode;

int computer(char a[],int len)
{
	int sum=0;
	int k=1;
	pNode pnode =NULL;
	pNode pk=NULL;
	pNode pr=NULL;
	int i1,i2,i3;
	char *p_begin=NULL;
	char *p_end = NULL;
	for (int i=0;i<len;)//将字符串中连续的数字转换成一个字符,没有考虑
	{
		if(a[i]>='0' &&a[i]<<'9')
			if(p_begin ==NULL)
			{
				i1=0;
				
				p_begin =&a[i];
				p_end= p_begin;
				i++;
				for(int j=i;j<len;j++)
					if(a[j]>= '0'&&a[j]<= '9')
					{
						p_end++;
						i++;
					}
					else
						break;
				for(char *p=p_begin ; p<= p_end ; p++)//将字符串转变成整数
				{
					i1=i1*10+(int )((*p)-'0');
				}
				pk = new Node;
				pk->flag =true;
				pk->IntChar.a = i1;
				pk->next =NULL;
				if(NULL==pnode)
				{
					pr=pk;
					pnode =pk;
					pk->pre =NULL;
				}
				else
				{
					pr->next=pk;
					pk->pre =pr;
					pr= pk;
				}
				p_begin =NULL;
				p_end =NULL;
			}
			else
			{;}
		else
		{
			pk = new Node;
			pk->flag =false;
			pk->IntChar.c= a[i++];
			pk->next =NULL;
			if(NULL==pnode)
			{
				pr=pk;
				pnode =pk;
				pk->pre =NULL;
			}
			else
			{
				pr->next=pk;
				pk->pre = pr;
				pr= pk;
			}
		}
	}
	pk=pnode;
	while (pk)
	{
		if (true ==pk->flag )
		{
			cout<<pk->IntChar.a;
		}
		if (false ==pk->flag )
		{
			cout<<pk->IntChar.c;
		}
		pk=pk->next;
	}
	cout<<endl;

	pk=pnode;
	pNode pi=NULL;
	while(pk!=NULL)//找出* /并处理
	{
		if( false == pk->flag && '*'==pk->IntChar.c)
		{
			i1 = (int )pk->pre->IntChar.a;
			i2 = (int )pk->next->IntChar.a;
			i3 = i1*i2;
			pk->pre->IntChar.a =i3;
			if (pk->next->next != NULL)//防止越界
			{
				pk->pre->next =pk->next->next;
				pk->next->next->pre =pk->pre;
				pi= pk;
				pk=pk->next->next;
				delete pi->next;
				delete pi;
				pi= NULL;
			} 
			else
			{
				pk->pre->next= NULL;

				delete pk->next;
				delete pk;
				break;
			}
		}
		else if(false == pk->flag && '/'==pk->IntChar.c)
		{
			i1 = (int )pk->pre->IntChar.a;
			i2 = (int )pk->next->IntChar.a;
			i3 = i1/i2;
			pk->pre->IntChar.a =i3;
			if (pk->next->next != NULL)//防止越界
			{
				pk->pre->next =pk->next->next;
				pk->next->next->pre =pk->pre;
				pi= pk;
				pk=pk->next->next;
				delete pi->next;
				delete pi;
				pi= NULL;
			} 
			else
			{
				pk->pre->next= NULL;
				delete pk->next;
				delete pk;
				break;
			}
		}
		else
		{
			pk=pk->next;
		}
		
	}
	pk=pnode;
	while (pk)
	{
		if (true ==pk->flag )
		{
			cout<<pk->IntChar.a;
		}
		if (false ==pk->flag )
		{
			cout<<pk->IntChar.c;
		}
		pk=pk->next;
	}cout<<endl;
	//处理剩下 + -
	pk=pnode;
	while(pk!=NULL)//找出* /并处理
	{
		if( false == pk->flag && '+'==pk->IntChar.c)
		{
			i1 = (int )pk->pre->IntChar.a;
			i2 = (int )pk->next->IntChar.a;
			i3 = i1+i2;
			pk->pre->IntChar.a =i3;
			if (pk->next->next != NULL)//防止越界
			{
				pk->pre->next =pk->next->next;
				pk->next->next->pre =pk->pre;
				pi= pk;
				pk=pk->next->next;
				delete pi->next;
				delete pi;
				pi= NULL;
			} 
			else
			{
				pk->pre->next= NULL;
				delete pk->next;
				delete pk;
				break;
			}
		}
		else if(false == pk->flag && '-'==pk->IntChar.c)
		{
			i1 = (int )pk->pre->IntChar.a;
			i2 = (int )pk->next->IntChar.a;
			i3 = i1-i2;
			pk->pre->IntChar.a =i3;
			if (pk->next->next != NULL)//防止越界
			{
				pk->pre->next =pk->next->next;
				pk->next->next->pre =pk->pre;
				pi= pk;
				pk=pk->next->next;
				delete pi->next;
				delete pi;
				pi= NULL;
			} 
			else
			{
				pk->pre->next= NULL;
				delete pk->next;
				delete pk;
				break;
			}
		}
		else
		{
			pk=pk->next;
		}	
	}
	pk=pnode;
	while (pk)
	{
		if (true ==pk->flag )
		{
			cout<<pk->IntChar.a;
		}
		if (false ==pk->flag )
		{
			cout<<pk->IntChar.c;
		}
		pk=pk->next;
		
	}cout<<endl;
	sum= pnode->IntChar.a;
	delete pnode;
	return sum;
}
int main()
{
	int sum=0;
	char str[]="3-2+15*20/5";
	sum= computer(str,strlen(str));
	cout<<"sum = "<<sum<<endl;
	return 0;
}


下面是别人做的:

#include <iostream>
#include <string>
#include <stack>

using namespace std;


void cal(char * a){  
    int len = strlen(a);  
    stack<int> s1;  
    stack<char> s2;  
    int k = 0;  
    int sum =0;  
    int sum1 = 0;  
    int count =0;  
	
    while(k <= len){
		
		//把字符串中数字转化成整形存入数组
        if(a[k] <= '9' && a[k] >= '0')
		{  
			sum = 0;  
			while(a[k]<= '9' && a[k] >= '0' )
			{  
				sum = sum * 10 + a[k] - '0';  
				++k;  
			}  
			s1.push(sum);  
        }
		
		else
		{  
			if(!s2.empty())
			{  
                if(s2.top() == '*')
				{  
                    s2.pop();  
                    int number1 = s1.top();  
                    s1.pop();  
                    int number2 = s1.top();  
                    s1.pop();  
                    int tt = 0;  
                    tt = number2 * number1;  
                    s1.push(tt);  
                }
				else if(s2.top() == '/'){  
                    s2.pop();  
                    int number1 = s1.top();  
                    s1.pop();  
                    int number2 = s1.top();  
                    s1.pop();  
                    int tt = 0;  
                    tt = number2 / number1;  
                    s1.push(tt);  
                }  
            }  
            if(k != len){  
                s2.push(a[k]);  
				
            }  
            ++k;  
        }  
    }  
	
	
    sum1 = s1.top();  
    s1.pop();  
    char ch;  
    int temp;  
    while(!s2.empty()){  
        ch = s2.top();  
        temp = s1.top();  
        s1.pop();  
        s2.pop();  
        if(ch == '+'){  
            sum1 = temp + sum1;  
        }else if(ch == '-'){  
            sum1 = temp - sum1;  
        }  
    }  
    cout<<sum1<<endl;  
	
}  


int main()
{
	double sum;
	
	char *a="11+2+18/3/2";
	cal(a);
}


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值