在vs2010中通过:
#include "stdafx.h"
#include <iostream>
double suan1_pang(char a[],double b[],char c);
double shu_pang(int z[],int y);
double kuo_pang(char a[],int b[]);
using namespace System;
int main(array<System::String ^> ^args)
{int i,j,l,x,y,z[10]={0},d[10]={0};
double k[20]={0};
char a[100]=" ",c[19]=" ";
gets(a);
for(y=x=l=-1,i=0;a[i]!='=';i++)
{
if(a[i]>='0'&&a[i]<='9')
{
for(j=0;j<=9;j++)
{
if(a[i]-48==j)
{
y++;
z[y]=j;
}
}
if(a[i+1]=='+'||a[i+1]=='-'||a[i+1]=='*'||a[i+1]=='/'||a[i+1]=='^'||a[i+1]=='=')
{
k[++l]=shu_pang(z,y);printf("%.2f\n",k[l]);y=-1;
if(a[i+1]=='=')
c[++x]=a[i+1];
}
}
else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='^'||a[i]=='{'||a[i]=='['||a[i]=='(')
{
if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='^')
c[++x]=a[i];
if(a[i]=='{'||a[i]=='['||a[i]=='(')
{
d[1]=i+1;
k[++l]=kuo_pang(a,d);printf("%d\n",k[l]);
i=d[1];
}
}
else
break;
}
printf("结果是:%.2lf",suan1_pang(c,k,a[i]));
getchar();
Console::WriteLine(L"Hello World");
return 0;
}
double kuo_pang(char a[],int b[])
{
int i,j,l,x,y,z[10]={0};
double k[20]={0};
char c[19]=" ";
for(y=x=l=-1,i=b[1];a[i]!='}'&&a[i]!=']'&&a[i]!=')';i++)
{
if(a[i]>='0'&&a[i]<='9')
{
for(j=0;j<=9;j++)
{
if(a[i]-48==j)
{
y++;
z[y]=j;
}
}
if(a[i+1]=='+'||a[i+1]=='-'||a[i+1]=='*'||a[i+1]=='/'||a[i+1]=='^'||a[i+1]=='='||a[i+1]=='}'||a[i+1]==']'||a[i+1]==')')
{
k[++l]=shu_pang(z,y);printf("%.2f\n",k[l]);y=-1;
if(a[i+1]=='}'||a[i+1]==']'||a[i+1]==')')
c[++x]=a[i+1];
}
}
else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='^'||a[i]=='{'||a[i]=='['||a[i]=='(')
{
if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='^')
c[++x]=a[i];
if(a[i]=='{'||a[i]=='['||a[i]=='(')
{
b[1]=i+1;
k[++l]=kuo_pang(a,b);printf("%d\n",k[l]);
i=b[1];
}
}
else
break;
}
b[1]=i;
return suan1_pang(c,k,a[i]);
}
double suan1_pang(char a[],double b[],char c)
{
int i,j;
double sum=1;
for(i=0;a[i]!=c;i++)
{
if(a[i]=='^')
{
for(j=1;j<=b[i+1];j++)
sum*=b[i];
b[i]=sum;
for(j=i;j<17;j++)
{
b[j+1]=b[j+2];
a[j]=a[j+1];
}
i--;
}
}
for(i=0;a[i]!=c;i++)
{
if(a[i]=='*'||a[i]=='/')
{
if(a[i]=='*')
{
b[i]=b[i]*b[i+1];
for(j=i;j<17;j++)
{
b[j+1]=b[j+2];
a[j]=a[j+1];
}
}
if(a[i]=='/')
{
b[i]=b[i]*1.0/b[i+1];
for(j=i;j<17;j++)
{
b[j+1]=b[j+2];
a[j]=a[j+1];
}
}
i--;
}
}
for(i=0;a[i]!=c;i++)
{
if(a[i]=='+'||a[i]=='-')
{
if(a[i]=='+')
{
b[i]=b[i]+b[i+1];
for(j=i;j<17;j++)
{
b[j+1]=b[j+2];
a[j]=a[j+1];
}
}
if(a[i]=='-')
{
b[i]=b[i]-b[i+1];
for(j=i;j<17;j++)
{
b[j+1]=b[j+2];
a[j]=a[j+1];
}
}
i--;
}
}
return b[0];
}
double shu_pang(int z[],int y)
{
int i,j,k;
double sum=0;
for(i=0;i<=y;i++)
{
k=1;
for(j=1;j<=y-i;j++)
{
k*=10;
}
sum+=z[i]*k;
}
return sum;
}