[cpp]
描述:思路很簡單,優先級比較完的情況下,采用兩個棧就可以完成了……
[cpp]
#include <iostream>
#include <cstdio>
#include <cstring>
#include "hanshu.h" //頭文件
using namespace std;
int flag;//下面會多次用到flag
int num_value(char *s,char *str,double *num,int len,int i,int j);//計算實數值存入num中
char get_precede(char a,char b);//比較兩個字符優先級,獲取優先級
int calculate_num(char c,double *num,int j);//優先級比較大小後計算結果
double solve_s(char *s ,char *str,double *num,int len);//處理函數,得出等式的結果
int main()
{
freopen("a.txt","r",stdin);
double num[10010];
char c,s[10010],str[10010];
memset(s,0,sizeof(s));
int left,right,len;
left=right=len=flag=0;
while(scanf("%c",&c)!=EOF)//單字符輸入
{
if((c>='0'&&c<='9')||c=='.'||c=='('||c==')'||c=='+'||c=='-'||c=='*'||c=='/')//選擇合適的字符輸入
{
s[len++]=c;
if(c=='(')
{
if(!right)flag=1;
left++;
}
else if(c==')')
{
right++;
if(!left) flag=2;
}
}
if(c=='\n')//討論幾種不符合情況的輸入
{
if(flag==2) printf("輸入錯誤,首先出現了右括號!\n");
else if(left>right) printf("左括號比右括號多!\n");
else if(left<right) printf("右括號比左括號多!\n");
else if(len<=0) printf("沒有數據!\n");
else
{
s[len++]='#';
flag=0;
memset(str,0,sizeof(str));
memset(num,0,sizeof(num));
double x=solve_s(s,str,num,len);
if(flag) printf("出現零為除數\n");
else printf("solve_s=%g\n",x);
}
memset(s,0,sizeof(s));
flag=len=left=right=0;
}
}
return 0;
}
[cpp]
//頭文件
[cpp]
<pre name="code" class="cpp">#ifndef HANSHU_H_INCLUDED
#define HANSHU_H_INCLUDED
char str_operator[]= {'+','-','*','/','(',')','#'};
char str_precede[7][7]=
{
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
extern int flag;
int num_value(char *s,char *str,double *num,int len,int i,int j)
{
int k,count(0),c(0);
for( k=i; k<len; k++)
if(s[k]>='0'&&s[k]<='9')
{
num[j]=num[j]*10+s[k]-'0';
if(c) count++;
}
else if(s[k]=='.') c=1;
else break;
for( c=0; c<count; c++)
num[j]/=10;
if((i==1&&(s[i-1]=='+'||s[i-1]=='-'))||(i>=2&&(s[i-1]=='-'||s[i-1]=='+')&&s[i-2]=='('))
{
flag=1;
if(s[i-1]=='-') num[j]=0-num[j];
}
return k;
}
char get_precede(char a,char b)
{
int i,j;
for(int k=0; k<7; k++)
{
if(a==str_operator[k]) i=k;
if(b==str_operator[k]) j=k;
}
return str_precede[i][j];
}
int calculate_num(char c,double *num,int j)
{
j--;
switch(c)
{
case '+':
num[j-1]=num[j-1]+num[j];
break;
case '-':
num[j-1]=num[j-1]-num[j];
break;
case '/':
num[j-1]=num[j-1]/num[j];
break;
case '*':
num[j-1]=num[j-1]*num[j];
break;
}
if(c=='-'||c=='+'||c=='*'||c=='/')
{
num[j]=0;
flag=1;
return j;
}
else return j+1;
}
double solve_s(char *s ,char *str,double *num,int len)
{
int i,j,k(1);
str[0]='#';
for(i=j=0; i<len; i++)
{
if(s[i]>='0'&&s[i]<='9')
{
i=num_value(s,str,num,len,i,j);
if(flag)
{
str[--k]=0;
flag=0;
}
j++;
}
char c=get_precede(str[k-1],s[i]);
if(c=='<') str[k++]=s[i];
else if(c=='=') str[--k]=0;
else if(c=='>')
{
while(c=='>')
{
if(str[k-1]=='/'&&!num[j-1])
{
flag=1;
return -1;
}
flag=0;
j=calculate_num(str[k-1],num,j);
if(flag)
{
str[--k]=0;
c=get_precede(str[k-1],s[i]);
flag=0;
continue;
}
else break;
}
if(str[k-1]=='('&&s[i]==')') str[--k]=0;
else str[k++]=s[i];
}
}
return num[0];
}
#endif // HANSHU_H_INCLUDED
</pre><br>
<br>
<pre></pre>
<br>
<br>