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!
char str[50]={0};//没写完,难道输入的一行"字符串"能自动识别里面的数字? char* p1=str; int a=0; char* p2=NULL; int b=0; char oper[4]={'+','-','*','/'}; char* poper=str; int i=0; while((scanf("%s",str))!=EOF) { while(*poper!='+'||*poper!='-'||*poper!='*'||*poper!='/') { poper++; } while(p1++!=poper) { } }
#include <stdio.h> int main() { double a = 0, b = 0; char n = 0; scanf("%lf%c%lf", &a, &n, &b); if (n != '+' && n != '-' && n != '*' && n != '/' ) { printf("Invalid operation!"); } else { if ( n == '/' && b == 0.0 ) { printf("Wrong!Division by zero!"); return 0; } switch(n) { case ('+') : printf("%.4lf+%.4lf=%.4lf", a, b, a + b ); break; case ('-') : printf("%.4lf-%.4lf=%.4lf", a, b, a - b ); break; case ('*') : printf("%.4lf*%.4lf=%.4lf", a, b, a * b ); break; case ('/') : printf("%.4lf/%.4lf=%.4lf", a, b, a / b ); break; } } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> //用单引号引出的一个字符本质上代表的就是一个整数 //用双引号引出的字符串本质上代表的是一个指向无名数组起始字符的指针 int main() { double a, b; char c = 0; while (scanf("%lf%c%lf", &a, &c, &b) == 3) { switch(c){ case('+'): printf("%.4lf+%.4lf=%.4lf", a, b, a + b); break; case('-'): printf("%.4lf-%.4lf=%.4lf", a, b, a - b); break; case('*'): printf("%.4lf*%.4lf=%.4lf", a, b, a * b); break; case('/'): if (b == 0.0) printf("Wrong!Division by zero!\n"); else printf("%.4lf/%.4lf=%.4lf", a, b, a / b); break; default: printf("Invalid operation!\n"); break; } } return 0; }
#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; }
#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 (0.0 == n2) { printf("Wrong!Division by zero!\n"); } else { printf("%.4lf/%.4lf=%.4lf\n", n1, n2, n1 / n2); } break; default: printf("Invalid operation!\n"); break; } } return 0; }
//BC77 简单计算器 #include "stdio.h" int main(){ double a,b; char c; scanf("%lf%c%lf",&a,&c,&b); switch(c){ case '+':printf("%.4f%c%.4f=%.4f\n",a,c,b,(a+b));break; case '-':printf("%.4f%c%.4f=%.4f\n",a,c,b,(a-b));break; case '*':printf("%.4f%c%.4f=%.4f\n",a,c,b,(a*b));break; case '/': if(b==0){ printf("Wrong!Division by zero!\n"); break; } else{ printf("%.4f%c%.4f=%.4f\n",a,c,b,(a/b)); break; } default:printf("Invalid operation!\n");break; } return 0; }
#include <stdio.h> int main() { double a = 0.0f; double b = 0.0f; char ch = 0; scanf("%lf%c%lf", &a, &ch, &b); if(b!=0.0 && ch=='+') { printf("%.4lf%c%.4lf=%.4lf", a, ch, b, a+b); } else if(b!=0.0 && ch=='-') { printf("%.4lf%c%.4lf=%.4lf", a, ch, b, a-b); } else if(b!=0.0 && ch=='*') { printf("%.4lf%c%.4lf=%.4lf", a, ch, b, a*b); } else if(b!=0.0 && ch=='/') { printf("%.4lf%c%.4lf=%.4lf", a, ch, b, a/b); } else if(ch!='+' && ch!='-' && ch!='*' && ch!='/') { printf("Invalid operation!\n"); } else if(b==0.0 && ch=='/') { printf("Wrong!Division by zero!\n"); } return 0; }