式文字列評価の入力

6975 ワード

説明:文字列で表される算術式を指定し、その式の値を計算します.
式は、「+、-、*、/」をサポートし、「*」と「/」の優先度は「+」、「-」より高い.
カッコを考慮する必要はなく、式の間にスペースがありません.
例えば、式「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);
}