当前位置:首页 » 编程语言 » c语言简单计算器

c语言简单计算器

发布时间: 2023-02-01 02:02:08

c语言编写 编写一个简单的计算器,实现两个整型数的四则运算。

1、打开CodeBlocks,新建一个空白文件,先定义头文件和主函数,接着写程序多大的主体:

⑵ C语言编写一个简单的计算器

我给你写一个简单的计算器程序,你可以看一下。如果需要更多的功能,那么还要更复杂一些。不是一句话可以说明白的。要用到很多函数的调用,和函数的方法。
#include
"stdio.h"
void
main()
{
int
a,b,result;
char
m;
printf("请输入需要计算的数:\n");
scanf("%d
%d",&a,&b);
printf("请输入加、减、乘或除\n");
scanf("%c",&m);
if(m=="+")
//判断是否进行加法运算,以下同理
result=a+b;
else
if(m=="-")
result=a-b;
elsee
if(m=="*")
result=a*b;
else
if(m=="/")
result=a/b;
else
printf("您输入有误\n");
//如果输入的符号非加减乘或是除,报错
printf("计算结果为:%d\n",result);
//最后输出结果
}

⑶ 如何用C语言写一个简易计算器

#include<stdio.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++) 编写计算器程序

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

工具/材料

Dev C++

  • 01

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

  • 02

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

  • 03

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

  • 04

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

  • 05

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

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

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

⑺ 用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);/*输出结果*/。

热点内容
app什么情况下找不到服务器 发布:2025-05-12 15:46:25 浏览:714
php跳过if 发布:2025-05-12 15:34:29 浏览:467
不定时算法 发布:2025-05-12 15:30:16 浏览:131
c语言延时1ms程序 发布:2025-05-12 15:01:30 浏览:166
动物园灵长类动物配置什么植物 发布:2025-05-12 14:49:59 浏览:736
wifi密码设置什么好 发布:2025-05-12 14:49:17 浏览:148
三位数乘两位数速算法 发布:2025-05-12 13:05:48 浏览:397
暴风影音缓存在哪里 发布:2025-05-12 12:42:03 浏览:542
access数据库exe 发布:2025-05-12 12:39:04 浏览:630
五开的配置是什么 发布:2025-05-12 12:36:37 浏览:365