❶ 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中的值必須是常量,不能是變數、表達式。