| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1454 人关注过本帖
标题:求助,看了两个下午也看不出原因,
只看楼主 加入收藏
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:24 
求助,看了两个下午也看不出原因,
刚刚学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得那些功能限制了
搜索更多相关主题的帖子: 计算器 
2011-02-14 15:18
刘定邦
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:687
专家分:1570
注 册:2010-9-21
收藏
得分:2 
晚上回家编译一下看看....
2011-02-14 15:37
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
谢咯,希望快找出原因
2011-02-14 18:08
点线面
Rank: 8Rank: 8
来 自:NO.-1
等 级:蝙蝠侠
帖 子:525
专家分:980
注 册:2011-1-3
收藏
得分:2 
觉得你的程序可读性很差,又不加注释,估计这里没有人很有耐性帮你看。

小代码,大智慧
2011-02-14 18:14
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
第一次发,没经验
2011-02-14 19:33
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
加了注释了,希望有人能耐心的帮我看下
#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);    /*输入一个式子,如:4+(8-3^2)*7=  注:最后必须要有= */
    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;   /*k[l]:储存转换后的整形数字*/
            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;    /*把i+1的值传给kou_pang()*/
                k[++l]=kuo_pang(a,d);printf("%d\n",k[l]);
                i=d[1];     /*接受括号运算后 i 的值*/
            }
        }
        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;   /*把变化后的 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;
}
2011-02-14 19:43
pcbaichi
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:486
专家分:1185
注 册:2010-11-13
收藏
得分:2 
你的程序小错有,先改小的再说吧

免费赠送河蟹一只
2011-02-14 19:43
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
问题就是我找不出啊
2011-02-14 20:22
liangjinchao
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:376
专家分:697
注 册:2010-11-8
收藏
得分:2 
学习学习

因为有了因为,所以有了所以,既然已成既然,何必再说何必
2011-02-14 20:42
pcbaichi
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:486
专家分:1185
注 册:2010-11-13
收藏
得分:0 
回复 8楼 a563622106
其实你不说有哪些问题,我们很难回答你,毕竟每个人的意思可以有不同的表达方法,解决一道题目也可以有不同的思路

免费赠送河蟹一只
2011-02-14 20:45
快速回复:求助,看了两个下午也看不出原因,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.079548 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved