A. C语言编写简易计算器程序
C语言编写计算器
我们可以用printf和scanf函数输出结果和获取用户的输入。需要<stdio.h>头文件。scanf函数在读取数据的时候不需要再一行上输入每个数据,只要数据和数据之间留出空白就可以了。先声明两个变量number1和number2,operation变量用来存储运算符。用scanf函数获取这两个数字和运算符。分别用%lf %c %lf
B. 用简单c语言编写计算器
#include"stdio.h"
/*预处理命令*/
void
main()
/*主函数*/
{
double
a,b;
/*双精度实型变量说明*/
char
c,d;
/*变量说明*/
do
/*循环体*/
{
printf("input
a
(-*/)b\n");
/*输入提示*/
scanf("%lf%c%lf",&a,&c,&b);
/*输入算术表达式*/
if(c=='
')
/*判断
*/
printf("=%0.2f",a
b);
/*输出a
b的值*/
else
if(c=='-')
/*判断-*/
printf("=%0.2f",a-b);
/*输出a-b的值*/
else
if(c=='*')
/*判断**/
printf("=%0.2f",a*b);
/*输出a*b的值*/
else
if(c=='/')
/*判断/*/
printf("=%0.3f",a/b);
/*输出a/b*/
else
/*不满足以上条件*/
printf("error");
/*输出错误*/
printf("\n\ninput\n");
/*输入\n*/
scanf("%c",&d);
/*输入符号给d*/
}
/*循环体结束*/
while(d=='\n');
/*循环条件语句*/
}
C. 用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();
}
D. C语言编写简单的计算器
#
#include<stdio.h>
void main()
{
float a,b;
char d;
printf("请输入两个数a,b:");
scanf("%f,%f"&a,&b);
printf("请输入符号d");
scanf("%c",d);
switch(d)
{
case'+':printf("%f\n,a+b);break;
case'-':printf("%f\n,a-b);break;
case'*':printf("%f\n,a*b);break;
case'/':printf("%f\n,a/b);break;
default:printf("input error\n");
}
}
E. 如何用C程序编写一个计算器
1
首先,得从网上下载并安装c++,然后从桌面找到并打开它。
F. 怎样用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器
用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:
1、打开visual C++ 6.0-文件-新建-文件-C++ Source File;
G. 用c语言 (c++) 编写计算器程序
我们平时进行数学运算都是用计算器完成的,那么如何用C语言编写一个计算器呢?下面我给大家分享一下。
Dev C++
首先我们需要在Dev C++软件中创建一个C语言项目,项目类型选择控制台程序,如下图所示
接下来我们在项目下面新建C语言文件,如下图所示
然后我们在C文件中写入计算器逻辑代码,主要是让用户输入计算方式,然后程序自动计算,如下图所示
接下来我们点击运行菜单,选择下拉菜单中的运行选项,如下图所示
最后在弹出的界面中我们输入要计算的公式,程序就会自动计算,如下图所示
H. 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);
//最后输出结果
}
I. 如何用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;
}