在while循环中运行字符(=, - ,*,/,%)的开关 [英] Run a switch for characters (=, -, *, /, %) in a while loop
问题描述
我正在尝试为类项目编写一个程序,该程序创建一个接受+, - ,*,/,%的运行总计算器。我有一个switch语句,用于评估运算符。我正在努力将切换转换为while或do while语句以在循环上运行它。
最初,我无法获得放入while语句的条件(即while(num< 0))。计算器必须能够接受所有正数和负数的整数,但不需要为浮点数工作。
我的理论是为初始用户表达式(即6 + 7)运行初始switch语句来计算表达式。然后我想将该值存储到临时整数中然后运行一个调用开关函数的while循环。然后用户可以输入类似* 8的东西,用之前的总计来计算。计算器需要连续运行,直到收到无效的输入。
我想不出另一种方法来运行这个计算器,但我也无法让它工作。
这是一个非常粗糙的代码。我真的很新C,所以我知道它不是完美。
我的尝试:
#include < stdio.h >
int calculator( int num1, char operator , int num2);
int secondCalculator( int temp, char operator , int num2);
int main( void ){
int num1 = 0 ;
int num2 = 0 ;
int temp = 0 ;
int result = 0 ;
char operator ;
scanf( %d%c%d,& num1 ,& operator,& num2);
while ( 0 < = num1 || num1< 0 ){
int calculator( int num1, char operator , int num2 );
// temp = temp;
scanf( %c%d,& operator,& num2);
int secondCalculator( int temp, char operator , int num2);
}
return 0 ;
}
int calculator( int num1, char operator , int num2){
switch ( operator ){
case ' +':
printf( %d,num1 + num2);
break ;
case ' - ' :
printf( %d,num1 - num2);
break ;
case ' *' :
printf( %d,num1 * num2);
break ;
case ' /' :
printf( %d,num1 / num2);
break ;
case ' %' :
printf( %d,num1%num2);
break ;
默认:
printf( 感谢您使用COP2220计算器。);
return num2;
}
int secondCalculator( int temp, char operator , int num2){
switch ( operator ){
case ' +':
printf( %d,temp + num2);
break ;
case ' - ' :
printf( %d,temp - num2);
break ;
case ' *' :
printf( %d,temp * num2);
break ;
case ' /' :
printf( %d,temp / num2);
break ;
case ' %' :
printf( %d,temp%num2);
break ;
默认:
printf( 感谢您使用COP2220计算器。);
return num2;
}
}
}
看看你的循环:
while ( 0 < = num1 || num1< 0 )因此,如果
num1
为负数,则测试通过。或者,如果num1
为零则传递。否则,如果它是正面的,它会通过。所以基本上就是:
while ( true )
但......代码无法编译。
secondCalculator
在所有情况下都不返回值,因为你的返回
语句在交换机内,而不是在外面。
计算器
做同样的事情,除了secondCalculator
在其中声明,这在C中是不允许的。
为什么你有两个相同的函数,不同的名字呢?
首先,注释掉所有的计算器函数,以及调用它们。
更改循环以使其有意义,并使用scanf的返回值来检查输入错误。
然后只需打印你在循环中读取的值来检查循环是否有效。
然后开始取消注释你的函数,并解决问题我提到了。并且让你缩进正确 - 这使得阅读代码变得更加容易!
你可以做类似的事情:
#include < stdio .h >
#include < stdlib.h > ;
int 计算( int num1 , char op, int num2)
{
int answer = 0 ;
switch (op)
{
case ' +':
answer = num1 + num2;
break ;
case ' - ' :
answer = num1 - num2;
break ;
// ...其他操作遵循
}
return answer;
}
int main()
{
int num1,num2;
char op;
do
{
int count = scanf ( %d%c%d,& num1,& op,& num2) ;
if (op == ' 0' )
break ;
// 调用计算器并显示结果
int result = calculate(num1,op,num2);
printf( %d%c%d的结果为%d \ n ,num1,op,num2,result);
} while ( 1 ); // 重复直到用户输入0 0 0
}
I am trying to write a program for a class project that creates a running total calculator that accepts +,-,*,/,%. I have a switch statement that evaluates for the operator. I am struggling with getting the switch into a while or do while statement to run it on a loop.
Initially, I cannot get a condition to put into the while statement (ie while (num < 0). The calculator has to be able to accept all integers both positive and negative, but does not need to work for floats.
My theory is to run an initial switch statement for the initial user expression (ie 6 + 7) that will evaluate the expression. Then I want to store that value into a temporary integer. Then run a while loop that calls a switch function. So the user could then input something like * 8 that would be evaluated with the previous total. The calculator needs to run continuously until an invalid input is received.
I cannot think of another way to run this calculator, but I also cannot get this to work.
This is a really rough code. I am really new to C so I know it isn't perfect.
What I have tried:
#include <stdio.h>
int calculator(int num1, char operator, int num2);
int secondCalculator(int temp, char operator, int num2);
int main(void) {
int num1 = 0;
int num2 = 0;
int temp = 0;
int result = 0;
char operator;
scanf("%d %c %d", &num1, &operator, &num2);
while(0 <= num1 || num1 < 0) {
int calculator(int num1, char operator, int num2);
//temp = temp;
scanf("%c %d", &operator, &num2);
int secondCalculator(int temp, char operator, int num2);
}
return 0;
}
int calculator(int num1, char operator, int num2) {
switch(operator) {
case '+':
printf("%d ", num1 + num2);
break;
case '-':
printf("%d ", num1 - num2);
break;
case '*':
printf("%d ", num1 * num2);
break;
case '/':
printf("%d ", num1 / num2);
break;
case '%':
printf("%d ", num1 % num2);
break;
default:
printf("Thank you for using the COP2220 calculator.");
return num2;
}
int secondCalculator(int temp, char operator, int num2) {
switch(operator) {
case '+':
printf("%d ", temp + num2);
break;
case '-':
printf("%d ", temp - num2);
break;
case '*':
printf("%d ", temp * num2);
break;
case '/':
printf("%d ", temp / num2);
break;
case '%':
printf("%d ", temp % num2);
break;
default:
printf("Thank you for using the COP2220 calculator.");
return num2;
}
}
}
Look at your loop:
while(0 <= num1 || num1 < 0)so if
num1
is negative, the test passes. Or, ifnum1
is zero it passes. Otherwise, if it's positive, it passes. So basically, that is:
while(true)
But ... that code won't compile.
secondCalculator
doesn't return a value in all cases, because yourreturn
statement in inside the switch, not outside.
calcuator
does the same, exceptsecondCalculator
is declared inside it, which isn't allowed in C.
And why do you have two identical functions with different names anyway?
First off, comment out the whole of both calculator functions, and the calls to them.
Change your loop so it makes some sense, and use the return value from scanf to check for input errors.
Then just print the values you read inside the loop to check the loop works.
Then start uncommenting your functions, and fix the problems I mentions. And get you indentation right - it makes reading the code much easier!
You could do something like:
#include <stdio.h> #include <stdlib.h> int calculate(int num1, char op, int num2) { int answer = 0; switch (op) { case '+': answer = num1 + num2; break; case '-': answer = num1 - num2; break; // ... other operations follow } return answer; } int main() { int num1, num2; char op; do { int count = scanf("%d %c %d", &num1, &op, &num2); if (op == '0') break; // call the calculator and display the result int result = calculate(num1, op, num2); printf("result of %d %c %d is %d\n", num1, op, num2, result); } while (1); // repeat until user types 0 0 0 }
这篇关于在while循环中运行字符(=, - ,*,/,%)的开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!