重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

c语言实现计算器子函数 用c语言计算函数

用C语言设计并实现一个简单计算器

额,搞定了。

成都创新互联公司制作网站网页找三站合一网站制作公司,专注于网页设计,网站设计、成都网站制作,网站设计,企业网站搭建,网站开发,建网站业务,680元做网站,已为1000+服务,成都创新互联公司网站建设将一如既往的为我们的客户提供最优质的网站建设、网络营销推广服务!

你交给老师的时候,你要告诉他for循环的功能,for()循环体里也就是for下方{}大括号里的代码要被循环执行。然后你就一行一行的解释 switch()里的语句就行了。

break表示跳出switch()。

至于int a,b,i; 这些你肯定懂了的吧。

最后那里表示在主函数 main()里调用自定义的函数

#include stdio.h

int calculator()  //定义一个函数。完成计算功能

{

int a,b, i;

char c;

for(i=0;;i++)

{

printf("请输入所要计算的两个数,以及所要执行的计算符号\n");

scanf("%d %d %c", a,b,c);

switch (c)

{

case '+':

printf("所要计算的式子:%d+%d\n",a,b);

a = a + b;printf("计算结果为:%d\n\n",a);

break;

case '-':

 printf("所要计算的式子:%d-%d\n",a,b);

a = a - b;printf("计算结果为:%d\n\n",a);

break;

case '*':

 printf("所要计算的式子:%d*%d\n",a,b);

a = a * b;printf("所要计算的式子:%d*%d\n",a,b);printf("计算结果为:%d\n\n",a);

break;

case '/':

 printf("所要计算的式子:%d/%d\n",a,b);

a = a / b;printf("所要计算的式子:%d/%d\n",a,b);printf("计算结果为:%d\n\n",a);

break;

}

}

}

int main()

{

calculator();//在main()函数里调用自定义的函数 calculator

}

如何用c语言实现一个计算器???

1、#includestdio.hint main()

2、{    int a,b,c;    scanf("%d%d%d",a,b,c);

3、 int sum = a+b+c;

4、   printf("和: %d",sum);

5、printf("平均值:%f",sum/3.0);

6、return 0

讲解:

1、先定义四个整形。

2、一个浮点型保存平均值。

3、然后在控制台等待输入。

4、将输入的三个整数加起来赋值给sum。

5、将三个整形除以3.0(为什么是3.0,是因为ave是浮点型的,隐性转换到float)。

6、然后输出。

c语言编写计算器程序

C语言编写计算器

我们可以用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语言编写计算器

#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语言计算函数
文章起源:http://cqcxhl.cn/article/doddips.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP