用C做一个简易计算器,这个后面的函数要怎么写?

#include

typedef struct {
float operand1, operand2;
char oprator;
} bexpression;

float compute1(bexpression expr);

int main(){
bexpression e;
char repeat = 'y';

do {
    printf("Enter expression (op1 op2 op) :\n");
    scanf("%f %f %c", &e.operand1, &e.operand2, &e.oprator);
    printf("compute1 = %f\n",compute1(e));
    printf("compute2 = %f\n",compute2(&e));
    getchar();
    printf("\nContinue ('y' or 'n') :\n");
    scanf("%c", &repeat);
}while (repeat == 'y');
return 0;

}

float compute1(bexpression expr){

}


2个回答

#include "stdio.h"

typedef struct {
float operand1, operand2;
char oprator;
} bexpression;

float compute1(bexpression expr);

int main(){
bexpression e;
char repeat = 'y';

do {
    printf("Enter expression (op1 op2 op) :\n");
    scanf("%f %f %c", &e.operand1, &e.operand2, &e.oprator);
    printf("compute1 = %f\n",compute1(e));
    //printf("compute2 = %f\n",compute2(&e));
    getchar();
    printf("\nContinue ('y' or 'n') :\n");
    scanf("%c", &repeat);
}while (repeat == 'y');
return 0;
}

float compute1(bexpression expr){
    switch (expr.oprator)
    {
    case '+': return expr.operand1 + expr.operand2;
    case '-': return expr.operand1 - expr.operand2;
    case '*': return expr.operand1 * expr.operand2;
    case '/': return expr.operand1 / expr.operand2;
    default: return 0.0f;
    }
}

Enter expression (op1 op2 op) :
1 2 +
compute1 = 3.000000

Continue ('y' or 'n') :
y
Enter expression (op1 op2 op) :
2 3 *
compute1 = 6.000000

Continue ('y' or 'n') :
n
Press any key to continue . . .

如果问题得到解决,请点我回答左上角的采纳,谢谢

#include<stdio.h> 

void add(int a,int b,int c)
{
c=a+b;
printf("%d\t",c);
printf("\n");
}
void minus(int a,int b,int c)
{
c=a-b;
printf("%d\t",c);
printf("\n");
}
void multiplication(int a,int b,int c)
{
c=a*b;
printf("%d\t",c);
printf("\n");
}
void div(int a,int b,int c)
{
c=(float)a/(float)b;
printf("%f\t",c);
printf("\n");
}
main()
{
int a,b,c;
char p;
puts("input A:\n");
scanf("%d",&a);
puts("input B:\n");
scanf("%d",&b);
puts("input operation:\n");
getchar();
p=getchar();
if(p=='+') add(a,b,c);else
if(p=='-') minus(a,b,c);else
if(p=='*') multiplication(a,b,c);else
if(p=='/') div(a,b,c);else
puts("没有注册这个运算符号\n");
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐