如何在不使用if else
和switch case
的情况下在C中创建计算器?
以下是目前的代码:
void main()
{
int a,b,sum,sub,pro,divide; /** sum is for addition,,sub is for subtraction,,pro is for product,,divide is for division**/
char operator;
clrscr();
printf("enter a value:");
scanf("%d",&a);
printf("enter another value:");
scanf("%d",&b);
printf("enter a operator:");
scanf("%c",&operator);
operator=='+';
sum=a+b;
printf("\nAnswer=%d",sum);
operator=='-'
sub=a-b;
printf("\nAnswer=%d",sub);
operator=='*';
pro=a*b;
prinf("\nAnswer=%d",pro);
operator=='/';
printf("\nAnswer=%d",divide);
getch();
}
答案 0 :(得分:4)
以下应该可以解决问题。它通过利用将运算符与if else
,switch
,+
或-
进行比较的事实来避免使用*
和/
只返回一个比较1
,其余比例0
。因此,结果是这些比较的总和,乘以对应于每个算子的数学表达式:
#include<stdio.h>
main(){
float a,b,result;
char oper [2];
printf("enter a value:");
scanf("%f",&a);
printf("enter another value:");
scanf("%f",&b);
printf("enter an operator:");
scanf("%1s", oper);
result = (oper[0] == '+') * (a + b) +
(oper[0] == '-') * (a - b) +
(oper[0] == '*') * (a * b) +
(oper[0] == '/') * (a / b);
printf("%4.2f %s %4.2f = %4.2f\n", a, oper, b, result);
}
当保存到名为calculator.c
的文件时,以下命令将编译它:
gcc calculator.c
输出将被称为a.out
,它可以像这样运行:
./a.out
enter a value:24
enter another value:4
enter an operator:/
24.00 / 4.00 = 6.00
我一直在使用gcc (GCC) 4.4.1 20090725 (Red Hat 4.4.1-2)
,但我确信其他版本也能正常使用。
如果我通过你的作业,请告诉我; - )
答案 1 :(得分:2)
实际上,我已经想到了一个满足作业条件的解决方案,而且完全延迟。
您可以为每个操作创建一个功能;它将两个输入作为参数并返回结果。然后,您可以创建一个指向这些函数的指针表,由操作符添加索引。
然后你可以通过指针执行该功能。
int multiply(int a, int b)
{
return a * b;
}
/**
* do the same for add, subtract, and divide
*/
int main(void)
{
int a, b;
char op;
/**
* create an array of pointers to functions indexed by
* character values; we will only be using four of these entries,
* but this allows us to index the array using the operator
* character directly instead of having to do any mapping.
* We're trading some unused space for simplicity.
*/
int (*func_table[128])(int, int);
/**
* Set the table entries for each operator.
*/
func_table['*'] = multiply;
/**
* Do the same for each of '-', '+', '/'
*/
...
/**
* After reading in a, b, and the operator, execute
* the function through the lookup table
*/
result = (*func_table[op])(a, b);
的Presto;没有控制结构。
由于几个原因,我不愿意发布这个帖子。首先,它基本上放弃了商店。其次,我怀疑你是否已经在课堂上讨论过函数指针。我不知道你的教练期望什么;我怀疑是这个。
答案 2 :(得分:1)
您应该能够通过将运算符和值推入堆栈来解决它,然后只需“执行”堆栈。
由于这可能是一项功课,我告诉你实现它的任务:)
答案 3 :(得分:1)
正如评论中所说,代码存在问题。试试这个简单的switch-case
语句:
switch(op) {
case '*': printf("%d",a*b); break;
case '/': printf("%d",a/b); break;
case '+': printf("%d",a+b); break;
case '-': printf("%d",a-b); break;
default: printf("Unknown operator."); break;
}
你会处理其余的事情,对吧?
[编辑]
好的,我认为使用switch-case
是支持的。
这个怎么样:
int result = 0;
result = (op == '*') ? a * b : result;
result = (op == '/') ? a / b : result;
result = (op == '+') ? a + b : result;
result = (op == '-') ? a - b : result;
printf("%d",result);
没有if-else
或switch-case
:)如果您愿意,您也可以嵌套语句(但这是不可读的)。
答案 4 :(得分:0)
在C
比较中有一个值。如果为真则为1,如果为假则为0。也许你可以使用它而不是禁止的if
(operator == '+') /* this is either 0 or 1 */
(operator == '-') /* this is either 0 or 1 */
(operator == '*') /* this is either 0 or 1 */
(operator == '/') /* this is either 0 or 1 */
答案 5 :(得分:0)
void main()
{
int a,b,sum,sub,pro,divide; /** sum is for addition,,sub is for subtraction,,pro is for product,,divide is for division**/
char operator;
clrscr();
printf("enter a value:");
scanf("%d",&a);
printf("enter another value:");
scanf("%d",&b);
printf("enter a operator:");
scanf("%c",&operator);
operator=='+'? printf("%d,a+b"): ;
//same goes for all the other operators
getch();
}
答案 6 :(得分:0)
我认为它也会像这样工作
# include "stdio.h"
# include "conio.h"
void main()
{
float a , b;
char c;
printf("Value 1:");
scanf("%f",&a);
printf("value 2:");
scanf("%f",&b);
printf("Enter a operator[ + - * /]\n");
c=getche();
(c=='+')&&(printf("\nAnswer is:%3.2f",a+b));
(c=='-')&&(printf("\nAnswer is:%3.2f",a-b));
(c=='*')&&(printf("\nAnswer is:%3.2f",a*b));
(c=='/')&&(printf("\nAnswer is:%3.2f",a/b));
getch();
}