第二次作业+105032014036

1.测试帖链接

http://www.cnblogs.com/yuyaoc/p/6600970.html

 

2.提出的问题、发现的缺陷

  1)在测试字母数字混合输入的情况下不能很好的进行合法性判断。

  2)if与else嵌套语句太多,代码读起来较复杂。数量边界没有设定清楚

  3)代码建议必要的注释

 3.修改后的代码

#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;
}

  4.心得体会

  提议没有理解清楚,未对其进行正确的判断,同时应该增加对特殊字符的判断。

posted @ 2017-03-26 21:23  吴心怡  阅读(122)  评论(0)    收藏  举报