用C語言編寫一個簡單的可以進(jìn)行加減乘除運算混合運算的計算器的方法:

創(chuàng)新互聯(lián)于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元懷寧做網(wǎng)站,已為上家服務(wù),為懷寧各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;
2、輸入預(yù)處理命令和主函數(shù):
#includestdio.h /*函數(shù)頭:輸入輸出頭文件*/
void main()/*空類型:主函數(shù)*/
3、定義變量:
int a,b,d; /*定義變量的數(shù)據(jù)類型為整型*/
char c;/*定義變量的數(shù)據(jù)類型為字符型*/
4、輸入四則運算式:
printf("輸入如“3*4”或“5+2”的四則運算式:");/*輸出文字提示*/
scanf("%d%c%d",a,c,b);/*輸入四則運算式*/
5、判斷運算符號:
switch(c) /*判斷運算符號*/
{
case'+':d=a+b;break;/*進(jìn)行加法運算*/
case'-':d=a-b;break;/*進(jìn)行減法運算*/
case'*':d=a*b;break;/*進(jìn)行乘法運算*/
case'/':d=a/b;break; /*進(jìn)行除法運算*/
}
6、輸出結(jié)果:
printf("%d%c%d=%d\n",a,c,b,d);/*輸出結(jié)果*/
完整的源代碼:
#includestdio.h /*函數(shù)頭:輸入輸出頭文件*/
void main()/*空類型:主函數(shù)*/
{
int a,b,d;/*定義變量的數(shù)據(jù)類型為整型*/
char c;/*定義變量的數(shù)據(jù)類型為字符型*/
printf("輸入如“3*4”或“5+2”的四則運算式:");/*輸出文字提示*/
scanf("%d%c%d",a,c,b);/*輸入四則運算式*/
switch(c)/*判斷運算符號*/
{
case'+':d=a+b;break;/*進(jìn)行加法運算*/
case'-':d=a-b;break;/*進(jìn)行減法運算*/
case'*':d=a*b;break;/*進(jìn)行乘法運算*/
case'/':d=a/b;break;/*進(jìn)行除法運算*/
}
printf("%d%c%d=%d\n",a,c,b,d);/*輸出結(jié)果*/
}
#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();
}
#include stdio.h
void main()
{
float a,b;
char d;
do
{
printf("Please enter the two Numbers, separated by Spaces:\n");
scanf("%f %f",a,b);
printf("Please select operation way: (-,*,/,^,s,!)\n");
scanf("%s",d);
switch(d)
{
case'+':
printf("a+b=%f\n",a+b);
break;
case'-':
printf("a-b=%f\n",a-b);
break;
case'*':
printf("a*b=%f\n",a*b);
break;
case'/':
printf("a/b=%f\n",a/b);
break;
default:
printf("input error\n");
}
printf("Do you want to continue(Y/N or y/n)");
fflush(stdin);
}
while(toupper(getchar())=='Y');
}
可以運行,不知道滿不滿足你的要求,你自己可以試試
文章標(biāo)題:用函數(shù)寫c語言運算器,如何用C語言寫計算器
路徑分享:http://chinadenli.net/article44/dsgidee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、移動網(wǎng)站建設(shè)、網(wǎng)站制作、標(biāo)簽優(yōu)化、動態(tài)網(wǎng)站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)