当前位置:首页 » 编程语言 » c语言四则运算代码

c语言四则运算代码

发布时间: 2023-09-16 09:07:36

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

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

❷ 用c语言编写四则运算,急呀!越简单越好

用纯粹的C语言实现,代码如下:

#include<stdio.h>
intmain()
{
doublea,b;
scanf("%lf%lf",&a,&b);
printf("a+b=%lf,a-b=%lf,a*b=%lf",a+b,a-b,a*b);
if(b==0)
printf(",error! ");
else
printf(",a/b=%lf ",a/b);
return0;
}

❸ 输入两个整数,进行加减乘除四则运算的c语言程序怎么写啊,拜托了~

代码

#include<stdio.h>

int main()

{

int a,b;

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

printf("%d ",a + b);

printf("%d ",a - b);

printf("%d ",a * b);

printf("%d ",a / b);

return 0;

}

运行截图


分析

C语言中的加减乘除和数学中的加减乘除一样,不同在于符号的表示问题,乘号需要用“*”表示。除号需要用“/”表示。新手学习C语言,很容易把除号和取余好混淆,强调一下,取余号是“%”,百分号就是取余的意思。因此在输入两个整数以后,按照数学方法就可以直接输出结果,整数的输入用scanf()函数。

❹ 请问怎么用C语言编写四则运算的程序呢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define size 20

typedef float NUM;

typedef struct
{
NUM data[size];
int top;
}*Space,Lnode;

void begin();
void initialize(Space x,Space y);
void input(Space x,Space y);
int is_operator(char a_operator);
int pushf(Space s,float x);
int pushc(Space s,char x);
int empty(Space s);
int priority(char o);
int popf(Space s,float *x);
int popc(Space s,int *x);
float result(int a_operator,float operand1,float operand2);

main()
{
begin();
system("pause");
}

void begin()
{
Lnode operand,a_operator;//定义两个指向结构体的指针
Space s_operand=&operand,s_operator=&a_operator;
initialize(s_operand,s_operator);//初始化
input(s_operand,s_operator);//开始
}

void initialize(Space s,Space t)//初始化数据栈、运算符栈
{
s->top=0;
t->top=0;
}

void input(Space x,Space y)
{
int i,j,position=0,op=0;
float operand1=0,operand2=0,evaluate=0;//用来储存两个计算数 和 一个结果
char string[50];//所输入的表达式
char temp[50];//用来临时存放小数

printf("请输入表达式: ");
gets(string);

while(string[position]!='\0'&&string[position]!='\n')
{
i=0;
strcpy(temp,"0");

if(is_operator(string[position]))//判断是否为运算符
{
if(!empty(y))
{
while(!empty(y)&&priority(string[position])<=priority(y->data[y->top-1]))//判断优先级
{
popf(x,&operand1);
popf(x,&operand2);
popc(y,&op);
pushf(x,result(op,operand1,operand2));//计算结果
}
}
pushc(y,string[position]);//运算符入栈
position++;
}

while((string[position]!='\0'&&string[position]!='\n')&&(!is_operator(string[position])))//数据存入temp数组
{
temp[i]=string[position];

i++;
position++;
}
pushf(x,atof(temp));//将数组强制转换为浮点型 然后进行入栈操作 x为指向数据栈的指针 atof函数即使强制转换类型
}

while(!empty(y))
{
popc(y,&op);
popf(x,&operand1);
popf(x,&operand2);

pushf(x,result(op,operand1,operand2));
}
popf(x,&evaluate);

printf("结果是 : %f",evaluate);
}

int pushf(Space s,float x)//数据入栈
{
if(s->top==size)
return 0;
s->data[s->top]=x;
s->top++;
return 1;
}

int pushc(Space s,char x)//运算符入栈
{
if(s->top==size)
return 0;
s->data[s->top]=x;
s->top++;
return 1;
}

int popf(Space s,float *x)//数据出栈
{
if(s->top==0)
return 0;
else
{
s->top--;
*x=s->data[s->top];
return 1;
}
}

int popc(Space s,int *x)//运算符出栈
{
if(s->top==0)
return 0;
else
{
s->top--;
*x=s->data[s->top];
return 1;
}
}

int empty(Space s)//判断栈空
{
if(s->top==0)
return 1;
else
return 0;
}

int is_operator(char Operator) //判断是否为运算符
{
switch(Operator)
{
case '+':
case '-':
case '*':
case '/':
return 1;
default:
return 0;
}
}

int priority(char o) //判断运算符的优先级别
{
switch(o)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}

float result(int a_operator,float operand1,float operand2)//计算结果
{
switch(a_operator)
{
case '+':
return operand2+operand1;
case '-':
return operand2-operand1;
case '*':
return operand2*operand1;
case '/':
return operand2/operand1;
}
}

这是用栈写的 没有写输入错误的判断 你自己添加一下吧
我是因为刚好有一个现成的程序

❺ c语言四则运算

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int n,n1;
char fun(int op){if(op==0)return '+';else if(op==1)return '*';else if(op==2)return '-';else return '/';}
int Compute( int a, int b, int op )
{
int p;
if(op==0)p=a+b;else if(op==1)p=a*b;else if(op==2)p=a-b;else p=a/b;
if(n==p){n1++;
return 1;}
else
return 0;
}
void Print(int flag)
{
if(flag==1)printf("Right!\n");
else
printf("Not correct!\n");
}
void main()
{
srand((unsigned)time(NULL));
int a,b,op,i=0,num;
while(true)
{
if(i==10)break;
a=rand()%10+1;
b=rand()%10+1;
op=rand()%4;
if(a%b==0&&a>=b)
{
i++;
printf("%d%c%d=",a,fun(op),b);
scanf("%d",&n);
num=Compute(a,b,op);
Print(num);
}
}
printf("you grade is:%d,you falsed %d!\n",n1*10,10-n1);
}

❻ c语言用switch编写一个简单的四则运算程序

代码如下:

#include <stdio.h>

int main()

{

float fFloat1=.0,fFloat2=.0;

char cOP=NULL;

printf("请输入要进行四则运算表达式: ");

scanf("%f%c%f",&fFloat1,&cOP,&fFloat2);

switch(cOP)

{

case '+':

printf("%f+%f=%f ",fFloat1,fFloat2,fFloat1+fFloat2);

break;

case '-':

printf("%f-%f=%f ",fFloat1,fFloat2,fFloat1-fFloat2);

break;

case '*':

printf("%f*%f=%f ",fFloat1,fFloat2,fFloat1*fFloat2);

break;

case '/':

if(0!=fFloat2)

{

printf("%f/%f=%f ",fFloat1,fFloat2,fFloat1/fFloat2);

}

else

{

printf("error! ");

}

break;

default:

printf("error! ");

break;

}

return 0;

}

(6)c语言四则运算代码扩展阅读

switch语句和if语句的区别:

1、大于等于(>=)、小于等于(<=)的判断用if语句,而等于(=)的判断用switch语句。

2、switch语句中的case类似于if…else…else if…else,但是离散值的判断。(离散值的判断自认为是等于情况的判断)。

3、switch一般都可以及用if重写,但是if不一定能用switch重写。

4、不要忘了break.C#中break不写是不行的,除了合并case的情况。

5、case中的值必须是常量,不能是变量、表达式。

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:431
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:556
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:536
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:145
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:232
java驼峰 发布:2025-02-02 09:13:26 浏览:650
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:530
linuxadobe 发布:2025-02-02 09:09:43 浏览:211
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:725