BC77. 简单计算器
描述
输入描述
输入一行字符串,操作数1+运算符+操作数2 (其中合法的运算符包括:+、-、*、/)。输出描述
输出为一行。示例1
输入:
1.0+3.0
输出:
1.0000+3.0000=4.0000
示例2
输入:
1.0;4.0
输出:
Invalid operation!
示例3
输入:
44.0/0.0
输出:
Wrong!Division by zero!
C 解法, 执行用时: 1ms, 内存消耗: 304KB, 提交时间: 2021-05-22
#include <stdio.h> int main() { double a,b; char c; while(scanf("%lf%c%lf\n",&a,&c,&b)!=EOF) { if(c!='+'&&c!='-'&&c!='*'&&c!='/') { printf("Invalid operation!\n"); } else { if(c=='/') { if(b==0) { printf("Wrong!Division by zero!\n"); } else printf("%.4lf%c%.4lf=%.4lf\n",a,c,b,a/b); } else { if(c=='+') printf("%.4lf%c%.4lf=%.4f\n",a,c,b,a+b); else if(c=='-') printf("%.4lf%c%.4lf=%.4lf\n",a,c,b,a-b); else printf("%.4lf%c%.4lf=%.4lf\n",a,c,b,a*b); } } } return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 316KB, 提交时间: 2021-11-21
#include<stdio.h> int main() { double a,b; char ch; while(scanf("%lf %c %lf",&a,&ch,&b)!=EOF) { if(ch=='+'||ch=='-'||ch=='*'||ch=='/') { if(ch=='+') printf("%.4lf%c%.4lf=%.4lf\n",a,ch,b,a+b); else if(ch=='-') printf("%.4lf%c%.4lf=%.4lf\n",a,ch,b,a-b); else if(ch=='*') printf("%.4lf%c%.4lf=%.4lf\n",a,ch,b,a*b); else { if(b==0.0) printf("Wrong!Division by zero!\n"); else printf("%.4lf%c%.4lf=%.4lf\n",a,ch,b,a/b); } } else printf("Invalid operation!\n"); } return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 348KB, 提交时间: 2021-03-03
#include<stdio.h> int main() { double a,c,d; char b; while(scanf("%lf%c%lf",&a,&b,&c)!=EOF) { if(b=='+') { d=a+c; printf("%.4lf+%.4lf=%.4lf\n",a,c,d); } else if(b=='-') { d=a-c; printf("%.4lf-%.4lf=%.4lf\n",a,c,d); } else if(b=='*') { d=a*c; printf("%.4lf*%.4lf=%.4lf\n",a,c,d); } else if(b=='/') { d=a/c; if(c==0.0) printf("Wrong!Division by zero!\n"); else printf("%.4lf/%.4lf=%.4lf\n",a,c,d); } else printf("Invalid operation!\n"); } return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 356KB, 提交时间: 2020-12-20
#include <stdio.h> int main () { double n1 = 0.0; double n2 = 0.0; char op = 0; while (~scanf("%lf%c%lf",&n1,&op,&n2)) { switch(op) { case '+': printf("%.4lf+%.4lf=%.4lf\n",n1,n2,n1+n2); break; case '-': printf("%.4lf-%.4lf=%.4lf\n",n1,n2,n1-n2); break; case '*': printf("%.4lf*%.4lf=%.4lf\n",n1,n2,n1*n2); break; case '/': { if(n2==0.0) { printf("Wrong!Division by zero!\n"); } else { printf("%.4lf/%.4lf=%.4lf\n",n1,n2,n1/n2); } } break; default: printf("Invalid operation!\n"); } } return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 364KB, 提交时间: 2021-03-03
#include<stdio.h> int main() { double a,b;//最初用float,通过率90% char o; while(scanf("%lf%c%lf",&a,&o,&b)!=EOF) { switch(o) { case '+':printf("%.4f+%.4f=%.4f\n",a,b,a+b);break; case '-':printf("%.4f-%.4f=%.4f\n",a,b,a-b);break; case '*':printf("%.4f*%.4f=%.4f\n",a,b,a*b);break; case '/': if(b==0) { printf("Wrong!Division by zero!\n"); } else { printf("%.4f/%.4f=%.4f\n",a,b,a/b); } break; default:printf("Invalid operation!\n");break; } } return 0; }