用go语言写一个简单的加减乘除的代码,急求 在线等

Python018

用go语言写一个简单的加减乘除的代码,急求 在线等,第1张

/*Note:YourchoiceisCIDE*/#include"stdio.h"#include"stdlib.h"#include"ctype.h"intn=0charrecord[20]floatproduct()floatchange()floatmuli(){floatsummsumm=product()while(record[n]=='-'||record[n]=='+'){switch(record[n]){case'+':n++summ+=product()breakcase'-':n++summ-=product()break}}returnsumm}floatproduct(){floatsumpsump=change()while(record[n]=='*'||record[n]=='/'){switch(record[n]){case'*':n++sump*=change()breakcase'/':n++sump/=change()break}}returnsump}floatchange(){floatsumccharrec[20],i=0if(record[n]=='('){n++sumc=muli()}if(record[n]==')')n++while(isdigit(record[n])||record[n]=='.'){while(isdigit(record[n])||record[n]=='.')rec[i++]=record[n++]rec[i]='\0'sumc=atof(rec)}returnsumc}voidmain(){while(1){n=0scanf("%s",record)printf("%s=%g\n",record,muli())}}这个是比较简单的。。。但是有点难理解。。。不过运行绝对正确。。还支持括号。。。但是在这个程序里面我加没有出错处理。。。另一种方法是用栈写。。。这个好理解。。但是麻烦。。。

#include <stdio.h>

void main()

{

//定义变量储存数据

double a,b

char c

//提示并接收数据

printf("请任意输入两个浮点数(以逗号隔开):")

scanf("%lf,%lf",&a,&b)

//提示输入运算符

printf("请输入+(加)或-(减)进行运算:")

scanf("%c",&c)

//进行判断

if(c=="+")

{

printf("%lf%c%lf=%lf/n",a,c,b,a+b)

}

else if(c=="-")

{

printf("%lf%c%lf=%lf/n",a,c,b,a-b)

}

eles if(c!="+" || c!="-")

{

printf("输入错误!/n")

}

}

用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:

1、打开visual C++ 6.0-文件-新建-文件-C++ Source File;

2、输入预处理命令和主函数

#include<stdio.h>/*函数头:输入输出头文件*/

void main()/*空类型:主函数*/

3、定义变量:

int a,b,d; /*定义变量的数据类型为整型*/

char c;/*定义变量的数据类型为字符型*/

4、输入四则运算式:

printf("输入如“3*4”或“5+2”的四则运算式:");/*输出文字提示*/

scanf("%d%c%d",&a,&c,&b);/*输入四则运算式*/

5、判断运算符号:

switch(c) /*判断运算符号*/

{

case'+':d=a+bbreak;/*进行加法运算*/

case'-':d=a-bbreak;/*进行减法运算*/

case'*':d=a*bbreak;/*进行乘法运算*/

case'/':d=a/bbreak; /*进行除法运算*/

}

6、输出结果:

printf("%d%c%d=%d\n",a,c,b,d);/*输出结果*/

完整的源代码:

#include<stdio.h>/*函数头:输入输出头文件*/

void main()/*空类型:主函数*/

{

int a,b,d;/*定义变量的数据类型为整型*/

char c;/*定义变量的数据类型为字符型*/

printf("输入如“3*4”或“5+2”的四则运算式:");/*输出文字提示*/

scanf("%d%c%d",&a,&c,&b);/*输入四则运算式*/

switch(c)/*判断运算符号*/

{

case'+':d=a+bbreak;/*进行加法运算*/

case'-':d=a-bbreak;/*进行减法运算*/

case'*':d=a*bbreak;/*进行乘法运算*/

case'/':d=a/bbreak;/*进行除法运算*/

}

printf("%d%c%d=%d\n",a,c,b,d);/*输出结果*/

}