一个计算机运算的代码,用C语言实现
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/14 15:15:32
一个计算机运算的代码,用C语言实现
一个计算机运算的代码,用C语言实现
一个计算机运算的代码,用C语言实现
能够实现基本的加减乘除
2.能够实现乘方计算
3.能够实现求余#include <stdio.h> double add(double a,double b) { double s; s=a+b; return s; } double sub(double a,double b) { double s; s=a-b; return s; } double multi(double a,double b) { double s; s=a*b; return s; } double div(double a,double b) { double s; s=a/b; return s; } double power(double a,double b) { int i=0; double s=1.0; if(b==0) return s=1; else { for (i=1;i<=b;i++) s*=a; return s; } } double mod(double a,double b) { int s; s=(int)a%(int)b; return s; } void main(void) { double a,b; char c; double sum; scanf("%lf,%c,%lf",&a,&c,&b); printf("%lf,%c,%lf\n",a,c,b); switch(c) { case '+':sum=add(a,b); break; case '-':sum=sub(a,b); break; case '*':sum=multi(a,b); break; case '/':sum=div(a,b); break; case '^':sum=power(a,b); break; case '%':sum=mod(a,b); break; default:printf("input is error!"); } if(c=='%') printf("sum=%d",sum); else printf("sum=%lf",sum); getch(); }