第二次作业+105032014018

1.测试帖链接

http://www.cnblogs.com/destinyandfate/p/6606681.html

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

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

  2)代码编写要规范,比如要缩进

  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("请分别输入三种手机配件的销售情况:
");
while(pass==0)
{
printf("请输入耳机的销售数量:(输入exit退出)
");
cin>>headphone;
if(headphone=="exit")
{
return 0;
}
else if(tu(headphone)==-1)
{
printf("输入的数据不合法,请重新输入!
");
continue;
}
else
{
headphone_i=tu(headphone);break;
}
}
while(pass==0)
{
printf("请输入手机外壳的销售数量:
");
cin>>shell;
if(tu(shell)==-1)
{
printf("输入的数据不合法,请重新输入!
");
continue;
}
else
{
shell_i=tu(shell);break;
}
}
while(pass==0)
{
printf("请输入手机护膜的销售数量:
");
cin>>protector;
if(tu(protector)==-1)
{
printf("输入的数据不合法,请重新输入!
");
continue;
}
else
{
protector_i=tu(protector);break;
}
}
back=commission(headphone_i,shell_i,protector_i);
printf("佣金为:%.2lf
",back);
}
return 0;
}

4.心得体会

未对特殊字符进行判断,在程序设计的时候应该要好好考虑特殊字符的判断,对变量进行合适的定义。

原文地址:https://www.cnblogs.com/leezoey/p/6624063.html