c语言函数计算器 c语言用函数编写计算器

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);

}

}

用c语言设计一个简单的加减乘除计算器 具体需要这样做

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

2、输入预处理命令和主函数:#include /*函数头:输入输出头文件*/,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;/*进行加法6、运算*/case'-':d=a-b;break;/*进行减法运算*/case'*':d=a*b;break;/*进行乘法运算*/case'/':d=a/b;break; /*进行除法运算*/}。

7、输出结果: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语言写一个简易计算器?

#includestdio.h

int main()

{

double num1;

double num2;

double result;

char ch;

printf("Please enter express to caculate, 'q' to exit(eg. 1+3):");

while(scanf("%lf%c%lf",num1,ch,num2) == 3)

{

switch(ch)

{

case '+':

{

result = num1 + num2;

break;

}

case '-':

{

result = num1 - num2;

break;

}

case '/':

{

if(num2 == 0)

printf("Error:div/0\n");

else

result = num1 / num2;

break;

}

case '*':

{

result = num1 * num2;

break;

}

}

printf("%g%c%g=%g\n",num1,ch,num2,result);

printf("Please enter express to caculate, 'q' to exit(eg. 1+3):");

}

return 0;

}


当前名称:c语言函数计算器 c语言用函数编写计算器
网页URL:http://pwwzsj.com/article/hgjisd.html