用函数写c语言运算器,如何用C语言写计算器

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

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

创新互联于2013年开始,是专业互联网技术服务公司,拥有项目网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元怀宁做网站,已为上家服务,为怀宁各地企业和个人服务,联系电话:18982081108

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语言编写计算器

#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语言)

#include stdio.h

void main()

{

float a,b;

char d;

do

{

printf("Please enter the two Numbers, separated by Spaces:\n");

scanf("%f %f",a,b);

printf("Please select operation way: (-,*,/,^,s,!)\n");

scanf("%s",d);

switch(d)

{

case'+':

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

break;

case'-':

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

break;

case'*':

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

break;

case'/':

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

break;

default:

printf("input error\n");

}

printf("Do you want to continue(Y/N or y/n)");

fflush(stdin);

}

while(toupper(getchar())=='Y');

}

可以运行,不知道满不满足你的要求,你自己可以试试


名称栏目:用函数写c语言运算器,如何用C语言写计算器
本文来源:http://scyanting.com/article/dsgidee.html