如何添加小数来切换?

时间:2015-10-01 14:35:41

标签: switch-statement double decimal calculator static-cast

我已经分配了一份作业,而且我似乎完成了大部分编码工作。我面临的唯一问题是,当我确定我们只能使用{{1}时,教授会对doublex使用似乎是变量y的内容使用int, long,时我们和char(我们被限制使用switch而不是if / else循环)。 我想我应该使用switchsetprecision(3),因为我们研究了这些,但我不确定如何在代码中添加它们。

这是所需的输出:

static_cast(double)< >

这是我的代码:

Welcome to my Calculator!

1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 1

Enter first number: 3.899999
Enter second number: 4.000001

3.900 + 4.000 = 7.900

1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 2

Enter first number: 2.34
Enter second number: 76.44422

2.340 * 76.444 = 178.879

1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 3

Enter first number: 2.342
Enter second number: 1.321

2.342 / 1.321 = 1.773

1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 4

Enter first number: -3411
Enter second number: 2454

-3411.000 - 2454.000 = -5865.000

1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 5

Enter first integer: 10
Enter second integer: 3

10 % 3 = 1

1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 6
Wrong input! Try again!


1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 56
Wrong input! Try again!


1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 5

Enter first integer: 6
Enter second integer: 5

6 % 5 = 1

1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: -1
Goodbye!!

如果有任何遗漏或更好的方法来总结编码,请分享!

1 个答案:

答案 0 :(得分:0)

您的代码op_num是用于切换的变量,但不是xy。因此,如果变量xy是双倍的,那么它是否重要?答案是不。您还在switch之外包含了所有等式。在要求中,当用户将mul = x * y; div = x / y; sub = x - y;mod = x % y;输入为1时,您不必计算op_num。因此,请将这些等式移动到交换机中各自的情况。这将解决你的所有错误。