标题:为什么没有这两个break结果会出错,出自C程序设计辅导(第四版谭浩强),书 ...
只看楼主
brotherand2
Rank: 2
等 级:论坛游民
帖 子:15
专家分:24
注 册:2013-3-16
结帖率:66.67%
已结贴  问题点数:20 回复次数:3 
为什么没有这两个break结果会出错,出自C程序设计辅导(第四版谭浩强),书上也没有break,我也觉得没有这2个break没问题,但却出问题。
# include<stdio.h>
# include<stdlib.h>
# define D "%d"
# define LF "lf"
# define PR printf
# define SC scanf
typedef struct Talist
{
    int less;
    double tax;
    double deduct;
}taxlist ;
   
   
taxlist tx[20];
double decrease(taxlist tx[20],int i,int n)
{
    if(1==i)
    {
        return(tx[i-1].less*(tx[n].tax-tx[i-1].tax));

    }
    else
    {
        return((tx[i-1].less-tx[i-2].less)*(tx[n].tax-tx[i-1].tax)+decrease(tx,i-1,n));
    }

}

int main()
{
    void input();
    void output();
    int flag=1,i,flag1;
    char c;
    PR("根据最新的规定:\n全月应纳税所得额=月收入-1600\n应纳个人所得税税额=应纳税所得额x适用税率-速算扣除数\n例如:某人月收入8000,减去1600元,全月应纳税所得额为6400元,应纳个人所得额为6400元,应纳个人所得税税额=6400x0.02-375=905元。\n");
    PR("选择1:本程序税率表已制好可直接使用,若税率及级别多处改变后选择1修改税率数据;\n选择2:用于输入个人收入及税前扣除数,用于求个人所得税额。\n当出现  继续与否(y/n)?输入y或Y继续先前操作,输入n y或Y结束程序\n");
    while(flag)
    {
        flag1=1;
            while(flag1)
            {
                PR("请输入1进行输入税率表或更改税率表,输入2进行求个人所得税:");
                SC(D,&i);
                if(i<=2&&i>=1)
                    flag1=0;
                else
                    PR("\n选择错误!重新输入!\n");

            }
            if(1==i)
                input();
            else
                output();
            PR("\n继续选择与否(y/n)?");
            getchar();
            c=getchar();
            if(c=='n'||c=='N')
                flag=0;
    }
   
   
    PR(NL);
    return 0;
}
void input()
{
double decrease(taxlist tx[20],int i,int n);

   
    int i,n;
    FILE *fp;
    if((fp=fopen("tax.txt","wb"))==NULL)
             {
        PR("the file can't open !\n");
                exit(0);
             }
    PR("税率表有多少个级别?n=");
    SC(D,&n);
    for(i=0;i<n-1;i++)
    {
        PR("第%d级别\n",i+1);
        PR("全月应纳税所得额不起过多少元:");
        SC(D,&tx[i].less);
        PR("税率:");
        SC(LF,&tx[i].tax);
        if(0==i)
        {
            tx[i].deduct=0;
        }
        else
            tx[i].deduct=decrease(tx,i,i);
     if(fwrite(&tx[i],sizeof(taxlist),1,fp)!=1)
         PR("the file write error!\n");
    }     

    PR("第%d级别\n超过%d元的税率:",n,tx[n-2].less);
    SC(LF,&tx[n-1].tax);
    tx[n-1].deduct=decrease(tx,n-1,n-1);
     if(fwrite(&tx[n-1],sizeof(taxlist),1,fp)!=1)
         PR("the file write error!\n");
fclose(fp);
}
void output()
{
    FILE *fp;
    int i,n;
    double tax_,salary,kouchu,s;
    if((fp=fopen("tax.txt","rb"))==NULL)
    {
        PR("the file can't read!there has no data!plese choose 1 to input data.\n");
        exit(0);
    }
    for(i=0;fread(&tx[i],sizeof(taxlist),1,fp)!=0;i++)
        ;
    fclose(fp);
    n=i;
    PR("                    我国现行的个人所得税税率表\n");
    PR("********************************************************************************\n");
    PR("--------------------------------------------------------------------------------\n");
    PR("级数                全月应纳税所得额(元)税率                速算扣除数(元)\n");
    PR("                    上限      下限\n");
    for(i=0;i<n-1;i++)
    {
        if(0==i)
        {
                    PR("--------------------------------------------------------------------------------\n");
                    PR("%-20d%-10d%-10d%-20.2lf%-20.2lf\n",i+1,i,tx[i].less,tx[i].tax,tx[i].deduct);

        }
        else
        {
                    PR("--------------------------------------------------------------------------------\n");
                    PR("%-20d%-10d%-10d%-20.2lf%-20.2lf\n",i+1,tx[i-1].less,tx[i].less,tx[i].tax,tx[i].deduct);
        }
    }

        PR("--------------------------------------------------------------------------------\n");

        PR("%-20d%-20d%-20.2lf%-20.2lf\n",i+1,tx[i-1].less,tx[i].tax,tx[i].deduct);
        PR("--------------------------------------------------------------------------------\n");

        PR("********************************************************************************\n");

        fclose(fp);
        PR("输入你的工资:");
        SC("%lf",&salary);
        PR("输入税前扣除数:");
        SC("%lf",&kouchu);
        s=salary-kouchu;
        
        if(s<=0)
            tax_=0;
        
        else
        {
               
             for(i=0;i<n-1;i++)
             {
                 if(0==i)
                 {
                     if(s<=tx[i].less)
                     {
                         tax_=s*tx[i].tax;
                         break;
                        
                     }
                 }
                 else
                 {
               
                     if((s<=tx[i].less)&&(s>=tx[i-1].less))
                     {
                        
                         tax_=s*tx[i].tax-tx[i].deduct;
                         break;
                        
                        
                     }
                 }
                 
             }
             if(s>tx[i].less)
                 tax_=s*tx[i].tax-tx[i].deduct;
                       

        }
        PR("应纳个人所得税额:%.2lf",tax_);




}
此程序这样没有问题,但是去掉个2break后结果却错了,这是什么问题,你若学完了C程序设计辅导,应该感兴趣吧!
搜索更多相关主题的帖子: void 辅导 C程序设计 decrease include 
2013-03-20 11:15
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
得分:20 
哦,不感兴趣

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-03-20 11:27
brotherand2
Rank: 2
等 级:论坛游民
帖 子:15
专家分:24
注 册:2013-3-16
得分:0 
此题在204面案例1:个人所得税计算
2013-03-20 11:29
brotherand2
Rank: 2
等 级:论坛游民
帖 子:15
专家分:24
注 册:2013-3-16
得分:0 
已解决,两个break可以去掉,if(s>tx[i].less)
                             tax_=s*tx[i].tax-tx[i].deduct;改为
               if(s>tx[i-1].less)
                 tax_=s*tx[i].tax-tx[i].deduct;就行了。
                       


                       

2013-03-20 12:35



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-401353-1-1.html




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

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