求助,看了两个下午也看不出原因,
刚刚学C没多久,用VC2008写个计算器,结果出现问题了。因为刚刚学,所以写得比较复杂,希望好心人耐心看,帮帮我。#include "stdio.h"
double suan1_pang(char a[],double b[],char c);
double shu_pang(int z[],int y);
double kuo_pang(char a[],int b[]);
void main()
{
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]));
getch();
}
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;
}
问题是,用到括号,进图被调用函数kuo_pang()时的返回值返回不了;k[++l]=kuo_pang();的k[l]总等于0,希望能帮帮看看为什么。是哪里写错了,还是VC得那些功能限制了