用switch语句编程设计一个简单的计算器程序
要求根据用户从键盘输入如下形式的表达式:操作数1(data1)运算符(op)操作数2(data2)然后,计算并输出表达式的值。指定的运算符为加(+)减(-)乘(*)除(/...
要求根据用户从键盘输入如下形式的表达式: 操作数1(data1) 运算符(op) 操作数2(data2)
然后,计算并输出表达式的值。
指定的运算符为 加(+) 减(-) 乘(*) 除(/) 展开
然后,计算并输出表达式的值。
指定的运算符为 加(+) 减(-) 乘(*) 除(/) 展开
4个回答
展开全部
#include <stdio.h>
int main( )
{
double a, b;
char op;
scanf("%lf%c%lf", &a, &op, &b);
switch (op)
{
case '+':
printf("%lf + %lf = %lf\n", a, b, a + b);
break;
case '-':
printf("%lf - %lf = %lf\n", a, b, a - b);
break;
case '*':
printf("%lf * %lf = %lf\n", a, b, a * b);
break;
case '/':
if (b != 0)
printf("%lf / %lf = %lf\n", a, b, a / b);
else
printf("除数不能是0! \n");
break;
}
}
int main( )
{
double a, b;
char op;
scanf("%lf%c%lf", &a, &op, &b);
switch (op)
{
case '+':
printf("%lf + %lf = %lf\n", a, b, a + b);
break;
case '-':
printf("%lf - %lf = %lf\n", a, b, a - b);
break;
case '*':
printf("%lf * %lf = %lf\n", a, b, a * b);
break;
case '/':
if (b != 0)
printf("%lf / %lf = %lf\n", a, b, a / b);
else
printf("除数不能是0! \n");
break;
}
}
展开全部
package Operat;
import java.util.Scanner;
public class Operat {
static int Addtion(int number1, int number2) {
int result = 0;
result = number1 + number2;
return result;
}
static int Multiplication(int number1, int number2) {
int result = 0;
result = number1 * number2;
return result;
}
static int Divsion(int number1, int number2) {
int ressult = 0;
try {
ressult = number1 / number2;
} catch (Exception e) {
// TODO: handle exception
e.getMessage();
System.out.println("除数不能为0");
}
return ressult;
}
static int Subtraction(int number1, int number2) {
int result = 0;
result = number1 - number2;
return result;
}
public static void main(String[] args) {
int number1;
int number2;
while (true) {
System.out.println("1:加法,2:乘法,3:除法,4:减法");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Addtion(number1,number2);
System.out.println(Addtion(number1, number2));
break;
case 2:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Multiplication(number1,number2);
System.out.println(Multiplication(number1, number2));
break;
case 3:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Divsion(number1,number2);
System.out.println(Divsion(number1, number2));
break;
case 4:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Subtraction( number1,number2);
System.out.println(Subtraction(number1, number2));
break;
default:
System.out.println("请重新输入选项:");
break;
}
}
}
}
自己手打的java代码,不喜勿喷,自己也是小白,求大神指点。
import java.util.Scanner;
public class Operat {
static int Addtion(int number1, int number2) {
int result = 0;
result = number1 + number2;
return result;
}
static int Multiplication(int number1, int number2) {
int result = 0;
result = number1 * number2;
return result;
}
static int Divsion(int number1, int number2) {
int ressult = 0;
try {
ressult = number1 / number2;
} catch (Exception e) {
// TODO: handle exception
e.getMessage();
System.out.println("除数不能为0");
}
return ressult;
}
static int Subtraction(int number1, int number2) {
int result = 0;
result = number1 - number2;
return result;
}
public static void main(String[] args) {
int number1;
int number2;
while (true) {
System.out.println("1:加法,2:乘法,3:除法,4:减法");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Addtion(number1,number2);
System.out.println(Addtion(number1, number2));
break;
case 2:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Multiplication(number1,number2);
System.out.println(Multiplication(number1, number2));
break;
case 3:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Divsion(number1,number2);
System.out.println(Divsion(number1, number2));
break;
case 4:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Subtraction( number1,number2);
System.out.println(Subtraction(number1, number2));
break;
default:
System.out.println("请重新输入选项:");
break;
}
}
}
}
自己手打的java代码,不喜勿喷,自己也是小白,求大神指点。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
int main(void)
{
int data1,data2;
char op;
printf("Please enter the expression:\n");
scanf("%d%c%d",&data1,&op,&data2);
switch(op)
{
case'+':printf("%d+%d=%d\n",data1,data2,data1+data2);break;
case'-':printf("%d-%d=%d\n",data1,data2,data1-data2);break;
case'*':printf("%d*%d=%d\n",data1,data2,data1*data2);break;
case'/':
if(0==data2)
printf("Division by zero£¡\n");
else
printf("%d/%d=%d\n",data1,data2,data1/data2);break;
default:printf("Unknown operator!\n");
}
return 0;
}
int main(void)
{
int data1,data2;
char op;
printf("Please enter the expression:\n");
scanf("%d%c%d",&data1,&op,&data2);
switch(op)
{
case'+':printf("%d+%d=%d\n",data1,data2,data1+data2);break;
case'-':printf("%d-%d=%d\n",data1,data2,data1-data2);break;
case'*':printf("%d*%d=%d\n",data1,data2,data1*data2);break;
case'/':
if(0==data2)
printf("Division by zero£¡\n");
else
printf("%d/%d=%d\n",data1,data2,data1/data2);break;
default:printf("Unknown operator!\n");
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要用java的话请找MIMOSAatBJ@163.com
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询