github地址如下:
https://github.com/huige666666/gaijin/commit/d3925696483426c57045a99ca4d22b7121bede46
1,题目:
对上一次的四则运算题目进行升级,我选择了对功能进行升级,使得计算时能够带括号进行运算。
2,思路:
实现的功能或者难点有以下几个:
a. 连续运算
b. 识别数字和运算符以及括号
c. 乘法/除法和括号优先级的问题
d. 把带小数的字符串转换成数字
先算乘法和除法,把算完的结果储存在一个float类型的数组里面,把遇到的加号和减号放在一个字符数组里,最后再算加减法。当遇到括号时,运用递归的思想,先算出最里面的括号,依次算外面的,再进行下一组运算。
当出现多级括号时,为了能够找到最里面的括号,并且准确的识别并保存括号内的数,用count_r和count_l计算左右两边括号的数目,从左边第一个括号开始,当右边的括号大于左边的括号时,就证明已经把里面的括号遍历完毕。
3,代码说明:
此时加了一个对多级括号的处理,当s1读取括号里的字符串时,如果右面的括号多于左边的括号,则读取字符串完毕,注意在下一次读取时要把运算符数组(operat)和左右括号的计数(count_l,count_r)重新变成0。
#include <cstdio>
#include <cstring>
float aux(char*s);
int main()
{
float number[80],x=0,y=0,tem_y=1;
int len,i=0,j=0,k=0,l=0,count_r=0,count_l=0,tem_j,tem_k,tem_i;
char operat[80],s[80],s1[80];
printf("Please input the numbers which you want to calculate:");
scanf("%s",s);
len=strlen(s);
if(s[0]=='(')
{
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r||count_l==0))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=tem_j;
k=tem_k;
i=++tem_i;
}
else
{
i=0;
for(;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y*=0.1;
}
number[j]=x+y;
}
j++;
for(;i<len;)
{
if(s[i]=='*')
{
if(s[i+1]=='(')
{
i++;
l=0;
count_l=count_r=0;
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r||count_l==0))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=tem_j;
k=tem_k;
i=++tem_i;
}
else
{
x=0;y=0;tem_y=1;
for(i++;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y*=0.1;
}
number[j]=x+y;
}
number[j-1]=number[j-1]*number[j];
}
if(s[i]=='/')
{
if(s[i+1]=='(')
{
i++;
l=0;
count_l=count_r=0;
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r||count_l==0))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=tem_j;
k=tem_k;
i=++tem_i;
}
else
{
x=0;y=0;tem_y=1;
for(i++;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y*=0.1;
}
number[j]=x+y;
}
if(number[j]==0)
{
printf(" The operator is error! ");
//system("pause");
return 0;
}
number[j-1]=number[j-1]/number[j];
}
if(s[i]=='+'||s[i]=='-')
{
operat[k]=s[i];
k++;
if(s[i+1]=='(')
{
i++;
l=0;
count_l=count_r=0;
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r||count_l==0))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=++tem_j;
k=tem_k;
i=++tem_i;
}
else
{
x=0;y=0;tem_y=1;
for(i++;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y*=0.1;
}
number[j]=x+y;
j++;
}
}
}
for(i=0;i<j+k;i++)
{
if(operat[i] == '+')
{
number[0] += number[i+1];
continue;
}
if(operat[i]=='-')
{
number[0] -= number[i+1];
continue;
}
}
printf(" %s = %.2f ",&s,number[0]);
//system("pause");
return 0 ;
}
float aux(char*s)
{
float number[80],x=0,y=0,tem_y=1;
int len,i=0,j=0,k=0,l=0,count_r=0,count_l=0,tem_j,tem_k,tem_i;
char operat[80],s1[80];
len=strlen(s);
if(s[0]=='(')
{
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=tem_j;
k=tem_k;
i=++tem_i;
}
else
{
i=0;
for(;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y=0.1*tem_y;
}
number[j]=x+y;
}
j++;
for(;i<len;)
{
if(s[i]=='*')
{
if(s[i+1]=='(')
{
i++;
l=0;
count_l=count_r=0;
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r||count_l==0))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=tem_j;
k=tem_k;
i=++tem_i;
}
else
{
x=0;y=0;tem_y=1;
for(i++;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y*=0.1;
}
number[j]=x+y;
}
number[j-1]=number[j-1]*number[j];
}
if(s[i]=='/')
{
if(s[i+1]=='(')
{
i++;
l=0;
count_l=count_r=0;
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r||count_l==0))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=tem_j;
k=tem_k;
i=++tem_i;
}
else
{
x=0;y=0;tem_y=1;
for(i++;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y*=0.1;
}
number[j]=x+y;
}
if(number[j]==0)
{
printf(" The operator is error! ");
//system("pause");
return 0;
}
number[j-1]=number[j-1]/number[j];
}
if(s[i]=='+'||s[i]=='-')
{
operat[k]=s[i];
k++;
if(s[i+1]=='(')
{
i++;
l=0;
count_l=count_r=0;
while(1)
{
i++;
if(s[i]=='(')
count_l++;
s1[l]=s[i];
l++;
if(s[i]==')')
count_r++;
if(s[i]==')'&&(count_l<count_r||count_l==0))
break;
}
s1[l-1]=' ';
tem_j=j;tem_k=k;tem_i=i;
number[tem_j]=aux(s1);
j=++tem_j;
k=tem_k;
i=++tem_i;
}
else
{
x=0;y=0;tem_y=1;
for(i++;s[i]<='9'&&s[i]>='0';i++)
x=(s[i]-'0')+x*10;
if(s[i]=='.')
for(i++;s[i]<='9'&&s[i]>='0';i++)
{
y=(s[i]-'0')*0.1*tem_y+y;
tem_y*=0.1;
}
number[j]=x+y;
j++;
}
}
}
for(i=0;i<j+k;i++)
{
if(operat[i] == '+')
{
number[0] += number[i+1];
continue;
}
if(operat[i]=='-')
{
number[0] -= number[i+1];
continue;
}
}
operat[0]=0;
return number[0];
}
4,测试运行:
5,效能分析
考虑到用户输入会影响效能分析中的时间因素,去掉了主函数中接受用户输入并比较的部分,直接改成由代码随机生成算式然后计算结果。
6,PSP表格: