當前位置:首頁 » 編程語言 » 用c語言編寫四則運算

用c語言編寫四則運算

發布時間: 2022-04-23 10:52:25

c語言編寫程序四則運演算法

1234567891011121314151617# include <stdio.h>int main(void){ int a,b,s; char c; scanf("%d%c%d",&a,&c,&b); switch(c) { case '+':s=a+b;break; case '-':s=a-b;break; case '*':s=a*b;break; case '/':s=a/b;break; default:return -1; } printf("%d",s); return 0;}

如果還要判斷除數為0的情況 再添個if即可

Ⅱ 用c語言編四則運算

用C++寫的,用C的話,函數方面很繁瑣...
實現不止是整數,小數也可以,但算式中不要有負數,結果中可以有.
2000字不夠用,分兩部分,前一部分一些全局變數和類的申明:
#include
<iostream.h>
#include
<string.h>
#include
<stdlib.h>
#include
<math.h>
#define
MAX
100
//定義運算優先順序數組,1表示優先,0表示相等,-1表示非優先,2表示表達式有誤
int
com_value[9][9]=
{
1,1,-1,-1,-1,1,1,1,2,
1,1,-1,-1,-1,1,1,1,2,
1,1,1,1,-1,-1,1,1,2,
1,1,1,1,-1,-1,1,1,2,
1,1,1,1,2,-1,1,1,2,
-1,-1,-1,-1,-1,-1,0,2,2,
1,1,1,1,1,2,1,1,2,
-1,-1,-1,-1,-1,-1,2,0,2,
2,2,2,2,2,2,2,2,2
};
//堆棧類模板
template
<class
type>
class
stack
{
public:
stack(){top=new
type[MAX];};
~stack(){};
type
*top;
public:
void
push(type
e){*top=e;top++;}
type
pop(){top--;return*top;}
type
GetTop(){return
*(top-1);}
int
GetTopValue(type
&e)
{
if(e=='+')return
0;else
if(e=='-')return
1;
else
if(e=='*')return
2;else
if(e=='/')return
3;
else
if(e=='^')return
4;else
if(e=='(')return
5;
else
if(e==')')return
6;else
if(e=='=')return
7;
else
return
8;
}
int
GetTopValue()
{
type
temp=GetTop();
if(temp=='+')return
0;else
if(temp=='-')return
1;
else
if(temp=='*')return
2;else
if(temp=='/')return
3;
else
if(temp=='^')return
4;else
if(temp=='(')return
5;
else
if(temp==')')return
6;else
if(temp=='=')return
7;
else
return
8;
}
type
calculate(char
s)
{
type
b=pop(),a=pop();
if(s=='+')return
a+b;
if(s=='-')return
a-b;
if(s=='*')return
a*b;
if(s=='/')return
a/b;
if(s=='^')return
pow(a,b);
}
};

Ⅲ 用簡單的C語言實現帶括弧的四則運算

#include<stdio.h> /*庫文件包含*/

#include<string.h> /*用於字元串操作*/

#include<stdlib.h> /*用於exit函數*/

/**************************************************************************

int check(char *c)

輸入參數:

char *c: 輸入的字元串

返回參數:

0:字元串中有不符合規定的字元

1: 字元串字元符合規定,沒有不符合規定的字元.

功能:

檢查字元串中有否除了 0-9, +,-,*,/,(,),之外的其他字元,

如果有,則返回0, 表示出現錯誤。

若沒有,則返回1,表式字元串符合規定。

**************************************************************************/

int check(char *c)

{

int k=0;

while(*c!='')

{

if((*c>='0' && *c<='9') || *c=='+' ||

*c=='-' || *c=='*' || *c=='/' ||

*c=='.' || *c=='(' || *c==')' )

{

}

else

{

printf("input error, there have the char not the math expression char! ");

return 0;

}

if(*c=='(')

k++;

else if(*c==')')

k--;

c++;

}

if(k!=0)

{

printf("input error, there is not have correct bracket '()'! ");

return 0;

}

return 1;

}

Ⅳ 請問怎麼用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語言編寫四則運算

請先給出明確答復:因為程序規定的是先輸入操作符,再輸入兩個操作數

解釋原因:請看下面部分的代碼

  1. 第一個scanf("%c",&oper);這一句要求輸入一個(注意是一個)字元格式的值,給oper;
  2. 如果你輸入一個數字、英文等等,假如你第一個輸入的是10,那oper就是1,而不是10,因為%c一次只能讀取一個字元;

  3. 接下來,輸入第二第三個,程序再往下就會拿oper判斷,如果oper等於-號,就會執行減法,如果等於+號就會執行加法;

  4. 所以你輸入的操作數是不會匹配任何運算符,就不能進行運算。

有無解決辦法:

調換語句順序:復制第一個printf那一行與第一個scanf那一行,注意這兩行printf在scanf的上面,把這兩句放到第二個scanf之後,第三個print之前。就能輸入 1 空格 + 空格 2 回車,這種的

Ⅵ C語言怎麼實現四則運算

我曾用c編了個計算器,支持四則運算,支持括弧改變優先順序,你看看吧:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define Esc 27
#define Tab 9

void needle(char* s,char* carriage,int start)//needle意為「用針縫」
{
int size_carriage=(int)strlen(carriage);
int i,j;
for(j=0;j<size_carriage;j++)
{
for(i=(int)strlen(s);i>=start;i--)
s[i+1]=s[i];
s[i+1]=carriage[j];
start=i+2;
}
}

double factorial(double d)//factorial意為「階乘」
{
return d==0?1:d*factorial(d-1);
}

void handle(char* carriage)//handle意為「處理」
{
void find_start(char*,int*);
char* save1,*save2,ch;
char op[111];
double d1,d2;
int start,i;
find_start(carriage,&start);
ch=carriage[start];
d1=strtod(carriage,&save1);//strtod是庫函數,功能是將字元串carriage中出現的第一個浮點數轉化為double,並將剩餘字元儲存在save1中
if(ch=='!')
{
sprintf(op,"%g",factorial(d1));//springf是庫函數,功能是將顯示在屏幕上的內容儲存在op中
strcpy(carriage,op);
return;
}
for(i=0;i<(int)strlen(save1);i++)//對於第一個不是'+'或'-'或數字的字元串,strtod無法作用。所以得去掉諸如"*22"字元串中的'*'
save1[i]=save1[i+1];
d2=strtod(save1,&save2);
switch(ch)
{
case '*':sprintf(op,"%.32g",d1*d2);break;
case '/':sprintf(op,"%.32g",d1/d2);break;
case '+':sprintf(op,"%.16g",d1+d2);break;
case '-':sprintf(op,"%.16g",d1-d2);
}
strcpy(carriage,op);
}

void del(char* s,int start,int end)//delete意為「刪除」
{
int i,j;
int size=(int)strlen(s);
for(i=end;i>=start;i--)
for(j=i;j<size;j++)
s[j]=s[j+1];
}

void (char* s,char* carriage,int start,int end)//意為「復制」
{
int i;
for(i=0;start<=end;i++,start++)
carriage[i]=s[start];
carriage[i]='\0';
}

void scissor(char* s,char* carriage,int* start)//scissor意為「用剪刀剪」
{
int sta,end;
for(sta=*start-1;(s[sta]>='0'&&s[sta]<='9')||s[sta]=='.';sta--);
if(sta!=0||s[*start]=='!') //處理式子是-2+3或-2!的特殊情況
sta++;
if(s[*start]=='!')//'!'是單目運算符
end=*start;
else
{
for(end=*start+1;(s[end]>='0'&&s[end]<='9')||s[end]=='.';end++);
if((s[end-1]=='*'||s[end-1]=='/')&&(s[end]=='+'||s[end]=='-'))//處理式子是2*-2等的特殊情況
for(end++;(s[end]>='0'&&s[end]<='9')||s[end]=='.';end++);
end--;
}
(s,carriage,sta,end);
del(s,sta,end);
*start=sta;//記住等下針線函數needle要開始縫的地方
}

void find_start(char* s,int* start)//find_start意為「找到操作符號開始處」
{
int i;
int size=(int)strlen(s);
//由優先順序決定從上而下的順序
for(i=0;i<size;i++)
if(s[i]=='!')
{
*start=i;
return;
}
for(i=0;i<size;i++)
if(s[i]=='*'||s[i]=='/')
{
*start=i;
return;
}
if(s[0]=='+'||s[0]=='-')
{
for(*start=1;(s[*start]>='0'&&s[*start]<='9')||s[*start]=='.';(*start)++);
return;
}
for(i=0;i<size;i++)
if(s[i]=='+'||s[i]=='-')
{
*start=i;
return;
}
}

int judge(char* s)//judge意為「判斷」
{
int i;
int size=(int)strlen(s);
for(i=1;i<size;i++)
if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'||s[i]=='!')
return 1;
return 0;
}

void count(char* s)//count意為「計算」
{
int start,i;
char carriage[555];//carriage意為「運載體」
while(judge(s))//判斷式子是否還需要進行運算
{
for(i=0;i<(int)strlen(s);i++)//此循環作用是將式子中出現的'--'替換為'+'
{
for(;s[i]!='-' && i<(int)strlen(s);i++);//循環結束
if(s[i+1]=='-')
{
del(s,i,i+1);
needle(s,"+",i);
}

}
find_start(s,&start);//讓下標start指向式子應該最先開始處理的運算符
scissor(s,carriage,&start);//用剪刀scissor將start指向的運算符及相對應的操作數剪下並儲存在運載體carriage上
handle(carriage);//處理該運載體,並將運載體上運載的式子的計算結果儲存在運載體上
needle(s,carriage,start);//見函數simplify中的說明
}
}

void simplify(char* s)//simplify意為「簡化」,即將括弧去除
{
char carriage[555];//carriage意為「運載體」
int start,end,i;
int size=(int)strlen(s);
while(1)
{
for(i=0;s[i] != '(' && i<size;i++);//注意這里末尾是分號
if(i==size)
break;//說明式子已不含括弧,簡化完成
for(end=0;s[end] != ')';end++);//使end下標指向式子中第一個出現')'的地方
end--;
for(start=end;s[start] != '(';start--);//尋找與上面一個')'配對的'('
start++;
(s,carriage,start,end);//將括弧里的內容復制給運載體carriage
del(s,start-1,end+1);//刪除整個括弧及其裡面的內容
count(carriage);//計算運載體運載的表達式,運算結果儲存在運載體上
needle(s,carriage,start-1);//將運載體運載過來的結果用針線needle縫在剛才刪除式子s括弧及其內容的地方
}
}

int main()
{
int i=0;
char s[555],ch;
puts(" 本程序不對用戶輸入數據進行檢測:");
puts(" 即用戶應輸入規范的計算式,包括分母不能為0:");
puts(" 式子不能含有空格及其他非計算字元;");
puts(" 按任意一個鍵開始,按回車鍵顯示運算結果;");
while(1)
{
i++;
ch=getch();
if(Esc==ch)
break;
if(Tab==ch)
{
system("cls");//清除當前屏幕
puts("顯示運算結果後:按「Esc」鍵退出,按「Tab」鍵清除當前屏幕");
}
if(i==1)
{
system("cls");
puts("顯示運算結果後:按「Esc」鍵退出,按「Tab」鍵清除當前屏幕");
}
puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
gets(s);
simplify(s);
count(s);
puts(s);
//實驗數據:23.243*(5!-23.123/.133+(2!-13/13.23)*(2!-.235)-((4!-20)!/2!)-32*(3!+2.23/(2*3)!))
//其正確結果是:-5953.9401265774754346182917611489
}
puts("謝謝使用!\n有問題可加q問我:281754179");
getch();
return 0;
}

我記得學了數據結構後我又重新用棧寫了個,容錯能力會更好,上面的代碼可以改進很多,也只是給你一個思路罷了

Ⅶ 怎麼用C語言編寫一個大整數的四則運算器,求解(急!!)

1.打開CodeBlocks,創建一個新的空白文件,定義頭文件和主要功能,然後寫程序的主體:

Ⅷ c語言怎麼用switch語句編寫四則運算

switch 是一個開關語句,和case配套使用, 和if else 判斷語句差不多, switch 語句是用於多分支語句進行條件判斷。下例為用switch語句編寫的四則運算:

#include <stdio.h>
void main()
{
double N1,N2;
char Operation;
printf("輸入運算的兩個數");
scanf("%lf%lf",&N1,N2);
printf("請輸入運算符(+,-,*,/):");
switch(Operation)
{
case '+':printf("%lf%c%lf=%lf",N1,N2,Operation,N1+N2);
break;
case '-':printf("%lf%c%lf=%lf",N1,N2,Operation,N1-N2);
break;
case '*':printf("%lf%c%lf=%lf",N1,N2,Operation,N1*N2);
break;
case '/':printf("%lf%c%lf=%lf",N1,N2,Operation,N1/N2);
break;
}
}

Ⅸ 用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>
#include <stdlib.h>
#include <time.h>
int add(int num1,int num2)//加法
{
return num1+num2;
}
int subtraction(int num1,int num2)//減法
{
return num1-num2;
}
int mul(int num1,int num2)//乘法
{
return num1*num2;
}
float divis(int num1,int num2)//除法
{
return ((int)(((float)num1/num2)*100+0.5))/100.0;
}
int main()
{
int type = -1;
int data = -1;
int choice,num1,num2,results;
float div_result;
int right = 0;
int wrong = 0;
char ch;
printf("歡迎進入四則運算題,現在開始爆發你的小宇宙吧!\n");
srand((unsigned int)time(NULL));
while(1)
{
printf("請選擇運算類型:1.加法 2.減法 3.乘法 4.除法(保留兩位小數)\n");
scanf("%d",&choice);
printf("請選擇位數:1.一位數 2.兩位數\n");
scanf("%d",&data);
switch (choice)
{
case 1:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d + %d=__?__\n",num1,num2);
scanf("%d",&results);
if(results == add(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d + %d=__?__\n",num1,num2);
scanf("%d",&results);
if(results == add(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 2:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d - %d=__?__\n",num1,num2);
scanf("%d",&results);
if(results == subtraction(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d - %d=__?__\n",num1,num2);
scanf("%d",&results);
if(results == subtraction(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 3:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d * %d=__?__\n",num1,num2);
scanf("%d",&results);
if(results == mul(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d * %d=__?__\n",num1,num2);
scanf("%d",&results);
if(results == mul(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 4:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10+1;
printf("%d / %d=__?__\n",num1,num2);
scanf("%f",&div_result);
if(div_result == divis(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100+1;
printf("%d / %d=__?__\n",num1,num2);
scanf("%f",&div_result);
if(div_result == divis(num1,num2))
{
printf("回答正確!\n");
right++;
}
else
{
printf("回答錯誤!\n");
wrong++;
}
printf("是否繼續?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
default :
printf("輸入錯誤!\n");
break;
}
}
end:
printf("回答正確%d題,回答錯誤%d題\n",right,wrong);
printf("\n====================謝謝光臨====================!\n");
system("pause");
return 0;
}
//注意除法是採用四捨五入的,有什麼不明白的再問吧,望採納!

熱點內容
xpftp外網 發布:2025-05-17 23:58:11 瀏覽:384
如何評價一個伺服器的性能 發布:2025-05-17 23:40:53 瀏覽:270
淘寶客適合什麼伺服器 發布:2025-05-17 23:39:26 瀏覽:612
python循環文件 發布:2025-05-17 23:39:22 瀏覽:828
androidstudio更新 發布:2025-05-17 23:38:22 瀏覽:643
java項目面試 發布:2025-05-17 23:30:53 瀏覽:780
若主存儲器按位元組編址 發布:2025-05-17 23:30:46 瀏覽:24
kotlinandroid 發布:2025-05-17 23:19:09 瀏覽:974
雲編程英語 發布:2025-05-17 23:18:34 瀏覽:623
androidstudio導入類 發布:2025-05-17 23:15:36 瀏覽:237