| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1454 人关注过本帖
标题:求助,看了两个下午也看不出原因,
取消只看楼主 加入收藏
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:11 
求助,看了两个下午也看不出原因,
刚刚学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
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
谢咯,希望快找出原因
2011-02-14 18:08
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
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
问题就是我找不出啊
2011-02-14 20:22
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
被调用函数kuo_pang()好像没办法带返回值回调用函数main(),我把kuo_pang()的return suan1_pang(c,k,a[i]);改成return 5;  可是在main()里面的k【l】的值依然是0;我就是这个不知道错哪了
2011-02-14 21:19
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
已经坐了两个下午了,实在没办法才来求助的
2011-02-14 21:21
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
希望能帮我快点解决,不然我这程序就算失败作了
2011-02-14 23:14
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
楼上的哥哥,我用的是VS2008,而且我是用C写的,不是C++;我把你这个复制进去之后运行不了
错误    1    fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?    e:\e_code\优美\优美\shi.c    179
出现上面的这个错误。
其实我最想知道的就是,为什么kuo_pang()无法带返回值给main()
2011-02-15 13:16
a563622106
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2011-2-14
收藏
得分:0 
我懂的不多啊,认识的函数没多少,而且我是想让你们帮我看看,是什么原因使得kuo_pang()无法带返回值给main()
2011-02-15 14:37
快速回复:求助,看了两个下午也看不出原因,
数据加载中...
 
   



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

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