UVa465 switch语句——愿天下再无WA
2017-01-13 15:53
363 查看
这些是可以通过测试的,但是删去 ch=='*'&& 不行 ?原因——对比正解!注意break语句位置的变化
//uva 465 #include<stdio.h> #include<limits.h> #include<string.h> #include<stdlib.h> char num1[10000]; char num2[10000]; bool fits(char* s); int main() { //freopen("input.txt", "r", stdin); char ch; while(scanf("%s %c %s", num1, &ch, num2) == 3) { printf("%s %c %s\n", num1, ch, num2); bool cal = true; if(fits(num1) == false) { printf("%s\n","first number too big"); cal = false; } if(fits(num2) == false) { printf("%s\n","second number too big"); cal = false; } if(ch == '*') { if((fits(num1)&&atoi(num1)==0)||(fits(num2)&&atoi(num2)==0)) continue; } if(cal == false) printf("result too big\n"); else { double n1 = atof(num1); double n2 = atof(num2); switch(ch) { case '+': if( n1 + n2> INT_MAX) { printf("%s\n","result too big"); continue; } case '*': if(ch=='*' && n1 *n2 > INT_MAX) //删去 ch=='*'&& 不行 { printf("%s\n","result too big"); break; } } /* //将switch替换成这个,通过测试 if(ch == '+' && n1 + n2> INT_MAX) { printf("%s\n","result too big"); } if(ch=='*' && n1 *n2 > INT_MAX) { printf("%s\n","result too big"); } */ } } } bool fits(char* ss) { char limit[15]; sprintf(limit,"%d",INT_MAX); char s[10000]; memset(s, 0, sizeof(s)); int i; for(i = 0; i < strlen(ss)&&ss[i] == '0'; i++); if(i == strlen(ss)) i--; strcpy(s,ss+i); if(strlen(s) > strlen(limit)) return false; else if(strlen(s) == strlen(limit)) { return strcmp(limit, s) >=0; } else return true; }
正解:
//uva 465
#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<stdlib.h>
char num1[10000];
char num2[10000];
bool fits(char* s);
int main()
{
//freopen("input.txt", "r", stdin);
char ch;
while(scanf("%s %c %s", num1, &ch, num2) == 3)
{
printf("%s %c %s\n", num1, ch, num2);
bool cal = true;
if(fits(num1) == false)
{
printf("%s\n","first number too big");
cal = false;
}
if(fits(num2) == false)
{
printf("%s\n","second number too big");
cal = false;
}
if(ch == '*')
{
if((fits(num1)&&atoi(num1)==0)||(fits(num2)&&atoi(num2)==0))
continue;
}
if(cal == false)
printf("result too big\n");
else
{
double n1 = atof(num1);
double n2 = atof(num2);
switch(ch)
{
case '+':
if( n1 + n2> INT_MAX)
printf("%s\n","result too big");
break;
case '*':
if(n1 *n2 > INT_MAX) //删去 ch=='*'&& 不行
printf("%s\n","result too big");
break;
}
/* //将switch替换成这个,通过测试
if(ch == '+' && n1 + n2> INT_MAX)
{
printf("%s\n","result too big");
}
if(ch=='*' && n1 *n2 > INT_MAX)
{
printf("%s\n","result too big");
}
*/
}
}
}
bool fits(char* ss)
{
char limit[15];
sprintf(limit,"%d",INT_MAX);
char s[10000];
memset(s, 0, sizeof(s));
int i;
for(i = 0; i < strlen(ss)&&ss[i] == '0'; i++);
if(i == strlen(ss)) i--;
strcpy(s,ss+i);
if(strlen(s) > strlen(limit))
return false;
else if(strlen(s) == strlen(limit))
{
return strcmp(limit, s) >=0;
}
else return true;
}
相关文章推荐
- UVa 465 Overflow——WA
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- Switch语句以及一个问题
- Java基础笔记整理---【05】switch分支语句、for循环语句
- switch语句用法
- 【php基础班】第9天 if语句、switch语句、while语句、dowhile语句
- 控制流程语句之顺序语句、if判断语句、键盘录入、switch选择判断语句
- 用Switch语句判断消费折扣问题
- goto语句 switch语句
- SWIFT——条件语句(if/switch)
- switch语句注意点
- Golang从入门到精通(七):Golang控制语句之switch
- switch-case语句用法
- java中的switch case语句
- 定义一个枚举类型以及switch语句使用
- js流程控制语句(switch语句)
- switch-case语句用法
- Switch语句的技巧
- Interview----求 1+2+...+n, 不能用乘除法、for、while if、else、switch、case 等关键字以及条件判断语句 (A?B:C)
- Javascript:基本语句(if-else/switch/for/while/for-in/try-catch)