UT源码_105032014126(改)

设计佣金问题的程序

commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone)、手机壳(Mobile phone shell)、手机贴膜(Cellphone screen protector)三个部件,每个部件单价为:耳机80元,手机壳10元,手机贴膜8元,每月月末向制造商报告销量,制造商根据销量给销售商佣金。如果销售额不足1000元按10%提取佣金,1000-1800元部分按15%提取佣金,超过1800元部分按20%提取佣金。

 程序要求:

1)先显示“请分别输入三种手机配件的销售情况:”

2)不满足条件,返回:“输入数量不满足要求”,返回重新输入;

3)条件均满足, 则返回佣金额。返回等待输入。

源码使用C语言编写

 

#include <stdio.h>

float commission(int headphone,int shell,int protector)
{
float back;
float sum;
sum=headphone*80+shell*10+protector*8;
if(sum<1000)
{
back=sum*0.1; //小于1000时所抽取的佣金
}
else if(sum>=1000 && sum<=1800)
{
back=100+(sum-1000)*0.15;//计算1000--1800的佣金
}
else if(sum>=1800)
{
back=220+(sum-1800)*0.2;//1800以上的佣金
}

return back;
}

int main()
{
int pass=0;//设置初始判断循环是否通过的值
double headphone;
double shell;
double protector; //使用double类型便于判断非法的带小数点数字类型
double back;
while(pass==0)
{
printf("请分别输入三种手机配件的销售情况:\n");
printf("请输入耳机的销售数量:\n");
if(fflush(stdin),scanf("%lf",&headphone)!=1)
{
printf("输入数量不满足要求:\n");
continue;
} //判断是否输入了除数字以外的非法字符,fflush(stdin)防止scanf接受到上面一个回车形成无限循环·
else if(headphone!=(int)headphone)
{
printf("输入数量不满足要求:\n");
continue;
}//判断是否输入了带小数点的数字
else if(headphone==-1)
{
break;//-1退出循环
}
else if(headphone<0)
{
printf("输入数量不满足要求:\n");
continue;
}//判断是否输入小于0的数字
printf("请输入手机外壳的销售数量:\n");
if(fflush(stdin),scanf("%lf",&shell)!=1)
{
printf("输入数量不满足要求:\n");
continue;
}
else if(shell!=(int)shell)
{
printf("输入数量不满足要求:\n");
continue;
}
else if(shell<0)
{
printf("输入数量不满足要求:\n");
continue;
}
printf("请输入手机护膜的销售数量:\n");
if(fflush(stdin),scanf("%lf",&protector)!=1)
{
printf("输入数量不满足要求:\n");
continue;
}
else if(protector!=(int)protector)
{
printf("输入数量不满足要求:\n");
continue;
}
else if(protector<0)
{
printf("输入数量不满足要求:\n");
continue;
}

back=commission(headphone,shell,protector);
printf("佣金为:%.2lf\n",back);
}
return 0;
}

 

观看测试报告后发现代码许多漏洞,于是进行了修改

测试报告链接:http://www.cnblogs.com/destinyandfate/p/6606681.html

修改后的代码:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;

float commission(int headphone,int shell,int protector)
{
float back;
float sum;
sum=headphone*80+shell*10+protector*8;
if(sum<1000)
{
back=sum*0.1; //小于1000时所抽取的佣金
}
else if(sum>=1000 && sum<=1800)
{
back=100+(sum-1000)*0.15;//计算1000--1800的佣金
}
else if(sum>=1800)
{
back=220+(sum-1800)*0.2;//1800以上的佣金
}

return back;
}

int tu(string a)
{
int back;
const char* a_c=a.data();
int len=strlen(a_c);

for(int i=0;i<=len-1;i++) //逐个检查是否为数字,小数点同样不通过
{
if( a_c[i]>'9' || a_c[i]<'0')
{
back=-1;
return back;
}
}

back=atoi(a_c);
return back;

}


int main()
{
int pass=0;//设置初始判断循环是否通过的值
string headphone;
string shell;
string protector;
int headphone_i;
int shell_i;
int protector_i;
double back;
while(pass==0)
{
printf("请分别输入三种手机配件的销售情况:\n");
while(pass==0)
{
printf("请输入耳机的销售数量:(输入exit退出)\n");
cin>>headphone;
if(headphone=="exit")
{
return 0;
}
else if(tu(headphone)==-1)
{
printf("输入的数据不合法,请重新输入!\n");
continue;
}
else
{
headphone_i=tu(headphone);break;
}
}
while(pass==0)
{
printf("请输入手机外壳的销售数量:\n");
cin>>shell;
if(tu(shell)==-1)
{
printf("输入的数据不合法,请重新输入!\n");
continue;
}
else
{
shell_i=tu(shell);break;
}
}
while(pass==0)
{
printf("请输入手机护膜的销售数量:\n");
cin>>protector;
if(tu(protector)==-1)
{
printf("输入的数据不合法,请重新输入!\n");
continue;
}
else
{
protector_i=tu(protector);break;
}
}
back=commission(headphone_i,shell_i,protector_i);
printf("佣金为:%.2lf\n",back);
}
return 0;
}

经过个人粗略测试,已经克服报告所查出的不能判断数字字母混合的情况

转载于:https://www.cnblogs.com/HSing1225/p/6532521.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值