资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

MySQL里乘法怎么算,mysql相乘函数

c语言编写计算器程序

C语言编写计算器

创新互联公司-专业网站定制、快速模板网站建设、高性价比坪山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式坪山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖坪山地区。费用合理售后完善,十载实体公司更值得信赖。

我们可以用printf和scanf函数输出结果和获取用户的输入。需要stdio.h头文件。scanf函数在读取数据的时候不需要再一行上输入每个数据,只要数据和数据之间留出空白就可以了。先声明两个变量number1和number2,operation变量用来存储运算符。用scanf函数获取这两个数字和运算符。分别用%lf %c %lf

请点击输入图片描述

然后需要检测输入是否是正确的,检查是不是+ - * / %,在这里要用到switch函数,用来看operation变量是否别传入了正确的值。

switch(operation)

{

case '+':

printf........

}

具体的运算我们只需要再case之后的printf语句中设定和输出就可以了。

请点击输入图片描述

由于除法和取余运算比较特殊,我们单独说明。除法的除数不能为零,所以除法需要检测除数是否为零,只需要用if else语句就可以,if(number2 == ),取余运算符对于浮点数没有意义,所以将浮点数转换为long类型,强制类型转换,if((long)number2 == 0)   else ,这样整个代码就完成了。

请点击输入图片描述

简单计算器的编辑并不难,但是要注意一些细节,除法的处理要注意除数不能为零的情况,而且取模运算要将两个操作数转化为整型,当然,作为真正的计算器,只实现这些功能是不够的,还需要更多的功能,不过有一个好的开始也不错。

下面我们就运行一下这个程序吧。25*13的值和8%5的值。可以看到是我们期望的值。

请点击输入图片描述

请点击输入图片描述

使用c语言编程,用函数实现一个计算器,在主函数中调用函数,包括加减乘除,乘方,绝对值和sin函数。

#includestdio.h

#includestdlib.h

double jia(double a,double b)

{

return a+b;

}

double jian(double a,double b)

{

return a-b;

}

double cheng(double a,double b)

{

return a*b;

}

double chu(double a,double b)

{

return a/b;

}

double juedui(double a)

{

return a0 ? a : -a;

}

double chengfang(double a,double b)

{

return pow(a,b);

}

double sinx(double a)

{

return sin(a);

}

int main()

{

int m;

double a,b;

while(1)

{

printf("请输入第一个操作数:");

scanf("%lf",a);

printf("0、退出\n1、加\n2、减\n3、乘\n4、除\n5、绝对值\n6、乘方\n7sin、\n请选择一个:");

scanf("%d",m);

if(1==m || 2==m || 3==m || 4==m || 6==m)

{

printf("请输入第二个操作数:");

scanf("%lf",b);

}

switch(m)

{

case 0:

exit(0);

break;

case 1:

printf("%lf+%lf=%lf\n",a,b,jia(a,b));

break;

case 2:

printf("%lf-%lf=%lf\n",a,b,jian(a,b));

break;

case 3:

printf("%lf*%lf=%lf\n",a,b,cheng(a,b));

break;

case 4:

if(0.0==b)

{

printf("除数不能为0。\n");

}

else

{

printf("%lf/%lf=%lf\n",a,b,chu(a,b));

}

break;

case 5:

printf("|%lf|=%lf\n",a,juedui(a));

break;

case 6:

printf("%lf的%lf方=%lf\n",a,b,chengfang(a,b));

break;

case 7:

printf("sin(%lf)=%lf\n",a,sinx(a));

break;

default:

printf("无法处理的命令。\n");

break;

}

}

system("PAUSE");

return EXIT_SUCCESS;

}

用c语言编写计算器

#include stdio.h

struct s_node

{

int data;

struct s_node *next;

};

typedef struct s_node s_list;

typedef s_list *link;

link operator=NULL;

link operand=NULL;

link push(link stack,int value)

{

link newnode;

newnode=(link) malloc(sizeof(s_list));

if(!newnode)

{

printf("\nMemory allocation failure!!!");

return NULL;

}

newnode-data=value;

newnode-next=stack;

stack=newnode;

return stack;

}

link pop(link stack,int *value)

{

link top;

if(stack !=NULL)

{

top=stack;

stack=stack-next;

*value=top-data;

free(top);

return stack;

}

else

*value=-1;

}

int empty(link stack)

{

if(stack==NULL)

return 1;

else

return 0;

}

int is_operator(char operator)

{

switch (operator)

{

case '+': case '-': case '*': case '/': return 1;

default:return 0;

}

}

int priority(char operator)

{

switch(operator)

{

case '+': case '-' : return 1;

case '*': case '/' : return 2;

default: return 0;

}

}

int two_result(int operator,int operand1,int operand2)

{

switch(operator)

{

case '+':return(operand2+operand1);

case '-':return(operand2-operand1);

case '*':return(operand2*operand1);

case '/':return(operand2/operand1);

}

}

void main()

{

char expression[50];

int position=0;

int op=0;

int operand1=0;

int operand2=0;

int evaluate=0;

printf("\nPlease input the inorder expression:");

gets(expression);

while(expression[position]!='\0'expression[position]!='\n')

{

if(is_operator(expression[position]))

{

if(!empty(operator))

while(priority(expression[position])= priority(operator-data)

!empty(operator))

{

operand=pop(operand,operand1);

operand=pop(operand,operand2);

operator=pop(operator,op);

operand=push(operand,two_result(op,operand1,operand2));

}

operator=push(operator,expression[position]);

}

else

operand=push(operand,expression[position]-48);

position++;

}

while(!empty(operator))

{

operator=pop(operator,op);

operand=pop(operand,operand1);

operand=pop(operand,operand2);

operand=push(operand,two_result(op,operand1,operand2));

}

operand=pop(operand,evaluate);

printf("The expression [%s] result is '%d' ",expression,evaluate);

getch();

}

用C语言做一个计算器,能实现加减乘除混合运算

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

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

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

#includestdio.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+b;break;/*进行加法运算*/

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

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

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

}

6、输出结果:

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

完整的源代码:

#includestdio.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+b;break;/*进行加法运算*/

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

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

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

}

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

}

C语言函数做计算器的问题

在jisuanqi()已经输出,在main()又一次输出jisuanqi()的返回值a+b。可以修改如下:

#include

"stdio.h"

int

jisuanqi(int

a,char

c,

int

b)

{

switch(c)

{

case

'+':

printf("%d\n",a+b);

break;

case

'-':

printf("%d\n",a-b);

break;

case

'*':

printf("%d\n",a*b);

break;

case

'/':

printf("%d\n",a/b);

break;

}

return

0;

}

int

main(int

argc,

char*

argv[])

{

int

a,b;

char

c;

scanf("%d

%c

%d",a,c,b);

jisuanqi(a,c,b);

return

0;

}

C语言编写计算器

总算看懂了,一个只能两个数相加减乘除的计算器何必写的那么复杂,竟然还用了六个函数,下面我写一个功能一样的,更精简方便的,只要一个函数。

/*

Note:Your

choice

is

C

IDE

*/

/*一个具有两个数加减乘除功能的计算器*/

#include

"stdio.h"

void

main()

{

int

iFirNum,iSecNum,iResult;

char

ch,ch1;

printf("请输入表达式如

5+6=

然后按回车键:");

scanf("%d%c%d%c",iFirNum,ch,iSecNum,ch1);

switch(ch)

{

case

'+':

iResult=iFirNum+iSecNum;

printf("%d+%d=%d\n",iFirNum,iSecNum,iResult);

break;

case

'-':

iResult=iFirNum-iSecNum;

printf("%d-%d=%d\n",iFirNum,iSecNum,iResult);

break;

case

'*':

iResult=iFirNum*iSecNum;

printf("%d*%d=%d\n",iFirNum,iSecNum,iResult);

break;

case

'/':

iResult=iFirNum/iSecNum;

printf("%d/%d=%d\n",iFirNum,iSecNum,iResult);

break;

default:

printf("输入表达式错误或该计算器不具备

%ch

功能\n",ch);

}

}


分享文章:MySQL里乘法怎么算,mysql相乘函数
文章分享:http://cdkjz.cn/article/hsihse.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220