KiKi实现一个简单计算器,实现两个数的“加减乘除”运算,用户从键盘输入算式“操作数1运算符操作数2”,计算并输出表达式的值,如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。如果操作数2等于0.0,则输出“Wrong!Division by zero!”
数据范围:字符串长度满足 ,保证运算符是一个char类型字符。
输入一行字符串,操作数1+运算符+操作数2 (其中合法的运算符包括:+、-、*、/)。
输出为一行。
如果操作数和运算符号均合法,则输出一个表达式,操作数1运算符操作数2=运算结果,各数小数点后均保留4位,数和符号之间没有空格。
如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。
如果操作数2等于0.0,则输出“Wrong!Division by zero!”。
1.0+3.0
1.0000+3.0000=4.0000
1.0;4.0
Invalid operation!
44.0/0.0
Wrong!Division by zero!
#include<bits/stdc++.h> using namespace std; int main(){ double a, b; char c; while(cin >> a >> c >> b){ switch(c){ case '+': cout << fixed << setprecision(4) << a << c << fixed << setprecision(4) << b << '=' << fixed << setprecision(4) << (a + b) << endl; break; case '-': cout << fixed << setprecision(4) << a << c << fixed << setprecision(4) << b << '=' << fixed << setprecision(4) << (a - b) << endl; break; case '*': cout << fixed << setprecision(4) << a << c << fixed << setprecision(4) << b << '=' << fixed << setprecision(4) << (a * b) << endl; break; case '/': if(b == 0.0) cout << "Wrong!Division by zero!" << endl; else cout << fixed << setprecision(4) << a << c << fixed << setprecision(4) << b << '=' << fixed << setprecision(4) << (a / b) << endl; break; default: cout << "Invalid operation!" << endl; break; } } return 0; }
#include <iostream> using namespace std; int main() { double a, b; char op; scanf("%lf%c%lf", &a, &op, &b); if (op == '+') { printf("%.4lf%c%.4lf=%.4lf", a, op, b, a + b); } else if (op == '-') { printf("%.4lf%c%.4lf=%.4lf", a, op, b, a - b); } else if (op == '*') { printf("%.4lf%c%.4lf=%.4lf", a, op, b, a * b); } else if (op == '/') { if(b) printf("%.4lf%c%.4lf=%.4lf", a, op, b, a / b); else cout << "Wrong!Division by zero!" << endl; } else cout << "Invalid operation!" << endl; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String exp = scanner.nextLine(); String op = "+-*/"; int i; char ch = '0'; for (i = 0; i < exp.length(); i++) { ch = exp.charAt(i); if (ch != '.' && (ch < '0' || ch > '9')) { break; } } if (op.contains(ch + "")) { double num1 = Double.parseDouble(exp.substring(0, i)); double num2 = Double.parseDouble(exp.substring(i + 1)); double result; if (ch == '/' && num2 == 0.0) { System.out.println("Wrong!Division by zero!"); } else { switch (ch) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: result=0; } System.out.printf("%.4f%c%.4f=%.4f", num1, ch, num2,result); } } else { System.out.println("Invalid operation!"); } } }
#include <stdio.h> /* float精度不够 要用double数据类型 */ int main() { double a, c; char b; while(scanf("%lf%c%lf",&a,&b,&c)!=EOF) { if(b=='+') printf("%.4f+%.4f=%.4f\n",a,c,a+c); else if(b=='-') printf("%.4f-%.4f=%.4f\n",a,c,a-c); else if(b=='*') printf("%.4f*%.4f=%.4f\n",a,c,a*c*1.0); else if(b=='/') { if(c==0.0) printf("Wrong!Division by zero!\n"); else printf("%.4f/%.4f=%.4f\n",a,c,a/c); } else printf("Invalid operation!\n"); } return 0; }
我这个思路比较适合初学者。 import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int p=0; while(sc.hasNext()){ String c=sc.nextLine(); for(int i=0;i<c.length();i++) { if(c.charAt(i)=='+'){ String[] n=c.split("\\+"); double sum1=Double.parseDouble(n[0]); double sum2=Double.parseDouble(n[1]); System.out.println(String.format("%.4f",sum1)+"+"+String.format("%.4f",sum2) +"="+String.format("%.4f",(sum1+sum2))); p=p+1;#p值为真 } else if(c.charAt(i)=='-'){ String[] n=c.split("\\-"); double sum1=Double.parseDouble(n[0]); double sum2=Double.parseDouble(n[1]); System.out.println(String.format("%.4f",sum1)+"-"+String.format("%.4f",sum2) +"="+String.format("%.4f",(sum1-sum2))); p=p+1; } else if(c.charAt(i)=='*'){ String[] n=c.split("\\*"); double sum1=Double.parseDouble(n[0]); double sum2=Double.parseDouble(n[1]); System.out.println(String.format("%.4f",sum1)+"*"+String.format("%.4f",sum2) +"="+String.format("%.4f",(sum1*sum2))); p=p+1; } else if(c.charAt(i)=='/'){ String[] n=c.split("\\/"); double sum1=Double.parseDouble(n[0]); double sum2=Double.parseDouble(n[1]); if(sum2!=0){ System.out.println(String.format("%.4f",sum1)+"/"+String.format("%.4f",sum2) +"="+String.format("%.4f",(sum1/sum2))); p=p+1; } else{ System.out.println("Wrong!Division by zero!"); p=p+1; } } } if(p==0) { System.out.println("Invalid operation!");} } } }
#忘记使用except的功能输出 除零错误 while True: try: (3389)# In = [] In = input() if '+' in In: In = In.split('+') num1 = float(In[0]) num2 = float(In[1]) print("%.4f+%.4f=%.4f"%(num1,num2,num1+num2)) elif '-' in In: In = In.split('-') num1 = float(In[0]) num2 = float(In[1]) print("%.4f-%.4f=%.4f"%(num1,num2,num1-num2)) elif '*' in In: In = In.split('*') num1 = float(In[0]) num2 = float(In[1]) print("%.4f*%.4f=%.4f"%(num1,num2,num1*num2)) elif '/' in In: In = In.split('/') num1 = float(In[0]) num2 = float(In[1]) if num2 == 0.0: print("Wrong!Division by zero!") else: print("%.4f/%.4f=%.4f"%(num1,num2,num1/num2)) else: print("Invalid operation!") except: break
#include <stdio.h> #include <math.h> int main(void) { char ch; double num1, num2, sum; while (scanf("%lf%c%lf", &num1, &ch, &num2) != EOF) { if (ch == '+') { sum = num1 + num2; printf("%.4lf+%.4lf=%.4lf\n", num1, num2, sum); } else if (ch == '-') { sum = num1 - num2; printf("%.4lf-%.4lf=%.4lf\n", num1, num2, sum); } else if (ch == '*') { sum = num1 * num2; printf("%.4lf*%.4lf=%.4lf", num1, num2, sum); } else if (ch == '/') { if (fabs(num2) <= 1e-6) { puts("Wrong!Division by zero!"); } else { sum = num1 / num2; printf("%.4lf/%.4lf=%.4lf", num1, num2, sum); } } else { puts("Invalid operation!"); } } return 0; }//浮点数不能直接与0相比较,考虑浮点数的存储方式;
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); //正则切割 String[] str = s.split("[+\\-*/]"); if (str.length<2){ //题目已交代了格式是“操作数 运算符 操作数”,所以不需要判断奇葩的不合法情况,例如"1+2:333"” System.out.println("Invalid operation!"); }else { double a = Double.parseDouble(str[0]); double b = Double.parseDouble(str[1]); String opt = s.substring(str[0].length(),str[0].length()+1); switch (opt){ case "+": System.out.printf("%.4f"+opt+"%.4f=%.4f",a,b,a+b);;break; case "-": System.out.printf("%.4f"+opt+"%.4f=%.4f",a,b,a-b);;break; case "*": System.out.printf("%.4f"+opt+"%.4f=%.4f",a,b,a*b);;break; default: if (b==0){ System.out.printf("Wrong!Division by zero!"); }else { System.out.printf("%.4f"+opt+"%.4f=%.4f",a,b,a/b); }; } System.out.println(); } } } }
#include <stdio.h> int main() { double a,b,res; char ch; while(scanf("%lf",&a)!=EOF){ ch=getchar(); scanf("%lf",&b); if(ch=='+'){ res=a+b+0.0; printf("%.4f%c%.4f=%.4f\n",a,ch,b,res); }else if(ch=='-'){ res=a-b-0.0; printf("%.4f%c%.4f=%.4f\n",a,ch,b,res); }else if(ch=='*'){ res=a*b*1.0; printf("%.4f%c%.4f=%.4f\n",a,ch,b,res); }else if(ch=='/'){ if(b==0){ printf("Wrong!Division by zero!\n"); }else{ res=1.0*a/b; printf("%.4f%c%.4f=%.4f\n",a,ch,b,res); } }else{ printf("Invalid operation!\n"); } } }
while True: try: expr = input() for i in expr: if i.isnumeric() or i == '.': pass else: x, y = expr.split(i) res = eval(expr) print(f'{eval(x):.4f}{i}{eval(y):.4f}={res:.4f}') except ZeroDivisionError as e: print("Wrong!Division by zero!") except SyntaxError as e: print("Invalid operation!") finally: break
def calculator(x): for symb in x: if symb in ['+', '-', '*', '/']: a, b = map(float, x.split(symb)) if symb == '/' and b == 0: print('Wrong!Division by zero!') else: print('%.4f%s%.4f=%.4f' % (a, symb, b, eval(x))) break else: print('Invalid operation!')
#include <stdio.h> int main(){ double num1,num2,result; //double-float可能会发生精度丢失,这里的22.3在计算机内无法精确表示, //double比float尾数长,表示的更精确,转换为float,就会把多余的尾数舍入, //这也是为什么用float结果比double值偏小;但若22.3换成其他可用float精确表示的数 //则不会有精度丢失 int mark = 1; char operator; scanf("%lf%c%lf",&num1,&operator,&num2); if(operator == '+'){ result = num1 + num2; } else if(operator == '-'){ result = num1 - num2; } else if(operator == '*'){ result = num1 * num2; } else if(operator == '/'){ if(num2 != 0){ result = num1 / num2; } else { printf("Wrong!Division by zero!"); mark = 0; } } else { printf("Invalid operation!"); mark = 0; } if(mark){ printf("%.4f%c%.4f=%.4f",num1,operator,num2,result); } return 0; }
#include<stdio.h> int main(){ char oper; double num1,num2,result; scanf("%lf%c%lf",&num1,&oper,&num2); if( (oper != '+') && (oper != '-') && (oper != '*') && (oper != '/') ) printf("Invalid operation!"); else if(oper == '/' && num2 == 0) printf("Wrong!Division by zero!"); else{ if(oper == '+') result=num1+num2; if(oper == '-') result=num1-num2; if(oper == '*') result=num1*num2; if(oper == '/') result=num1/num2; printf("%.4lf%c%.4f=%.4lf",num1,oper,num2,result); } return 0; }
#include <stdio.h> int main(){ double n1, n2 = 0; char a = 0; scanf("%lf%c%lf", &n1, &a, &n2); switch(a){ case '+': printf("%.4lf+%.4f=%.4lf", n1, n2, n1 + n2); break; case '-': printf("%.4lf-%.4f=%.4lf", n1, n2, n1 - n2); break; case '*': printf("%.4lf*%.4lf=%.4lf", n1, n2, n1 * n2); break; case '/': if(n2 == 0) printf("Wrong!Division by zero!"); else printf("%.4lf/%.4lf=%.4lf", n1, n2, n1 / n2); break; default: printf("Invalid operation!"); break; } return 0; }
while True: try: lt = input() for b in lt: if b in ['+','-','*','/']: a,c = map(float,lt.split(b)) if c==0.0 and b in '/': print('Wrong!Division by zero!') break else: d = eval(lt) print('{:.4f}{}{:.4f}={:.4f}'.format(a,b,c,d)) break else: print('Invalid operation!') except: break
#include <stdio.h> int main() { double operand1 =0,operand2= 0; //操作数--operand char operator = '0'; //运算符--operator scanf("%lf%c%lf",&operand1, &operator, &operand2); if(operator =='+' || operator == '-' || operator == '*' || operator == '/') //合法操作符 { if(operator == '+') //操作符为字符‘+’ printf("%.4lf+%.4lf=%.4lf\n",operand1,operand2,operand1+operand2); if(operator == '-') printf("%.4lf-%.4lf=%.4lf\n",operand1,operand2,operand1-operand2); if(operator == '*') printf("%.4lf*%.4lf=%.4lf\n",operand1,operand2,operand1*operand2); if(operator == '/') //操作数2不为0 { if(operand2 == 0) printf("Wrong!Division by zero!\n"); else printf("%.4lf/%.4lf=%.4lf\n",operand1,operand2,operand1/operand2); } } else { printf("Invalid operation!\n"); } return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int Validity(char c, int factor) { //判断符号 if (c != '+' && c != '-' && c != '*' && c != '/') { printf("Invalid operation!\n"); return 1; } //判断除数 if (factor == 0) { printf("Wrong!Division by zero!\n"); return 1; } //合法 return 0; } double Add(double a, double b) { return a + b; } double Sub(double a, double b) { return a - b; } double Div(double a, double b) { return a / b; } double Mul(double a, double b) { return a * b; } int main() { double a = 0.0; double b = 0.0; double result = 0.0; char operator = 0; scanf("%lf%c%lf", &a, &operator, &b); //合法性判断 if (Validity(operator, b)) { return 0; } switch (operator) { case '+': result = Add(a, b); break; case '-': result = Sub(a, b); break; case '*': result = Mul(a, b); break; case '/': result = Div(a, b); break; //其实这里不需要default,因为前面已经进行过合法性判断 //但是为了养成良好习惯,还是加上了 default: break; } printf("%.4lf%c%.4lf=%.4lf\n", a, operator, b, result); return 0; }