用c语言 (c++) 编写计算器程序

Python017

用c语言 (c++) 编写计算器程序,第1张

我们平时进行数学运算都是用计算器完成的,那么如何用C语言编写一个计算器呢?下面我给大家分享一下。

工具/材料

Dev C++

01

首先我们需要在Dev C++软件中创建一个C语言项目,项目类型选择控制台程序,如下图所示

02

接下来我们在项目下面新建C语言文件,如下图所示

03

然后我们在C文件中写入计算器逻辑代码,主要是让用户输入计算方式,然后程序自动计算,如下图所示

04

接下来我们点击运行菜单,选择下拉菜单中的运行选项,如下图所示

05

最后在弹出的界面中我们输入要计算的公式,程序就会自动计算,如下图所示

方法一:

#include <stdio.h>

#include<string.h>

#include<math.h>

#include<stdlib.h>

struct complex multiply(struct complex x, struct complex y)

struct complex{

int real

int imag

}

int main()

{

struct complex a,b,s

scanf("%d%d%d%d",&a.real,&a.imag,&b.real,&b.imag)

s=multiply(a,b)

printf("(%d+%di)*(%d+%di)=%d+%di\n",a.real,a.imag,b.real,b.imag,s.real,s.imag)

return 0

}

struct complex multiply(struct complex x, struct complex y)

{

struct complex m

m.real=x.real*y.real-x.imag*y.imag

m.imag=x.imag*y.real+x.real*y.imag

return m

}

方法二:

#include<stdio.h>

int main()

{

int a,b,c,d,e,f

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

e = a * c - b * d

f = a * d + b * c

printf("(%d+%di)*(%d+%di)=%d+%di\n",a,b,c,d,e,f)

}

#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()

}