導航:首頁 > 編程語言 > c語言編程簡易計算器

c語言編程簡易計算器

發布時間:2022-07-24 10:32:01

A. 編寫函數實現簡易計算器的功能(C語言)

#include
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');
}
可以運行,不知道滿不滿足你的要求,你自己可以試試

B. 用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();
}

C. c語言設計一個簡單的計算器程序

#include<stdio.h>//計算器

voidmenu()//自定義的菜單界面

printf("--------------------\n");

printf("請輸入你的選擇\n");

printf("1.+\n");

printf("2.-\n");

printf("3.*\n");

printf("4./\n");

printf("--------------------\n");

intmain()

inti=0;

intj=0;

intnum=0;//計算結果存放在nun

intselect=0;//選擇的選項存放在select

do//do-while先執行再判斷循環條件,即可實現重復計算功能

menu();//列印出菜單界面

scanf("%d",&select);//輸入你的選項

printf("請輸入計算值:");

scanf("%d%d",&i,&j);//輸入要計算的數值

switch(select)

case1:

printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能

break;

case2:

printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能

break;

case3:

printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能

break;

case4:

printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能

break;

default:

printf("輸入有誤重新選擇");

break;

}while(select);

return0;

運行結果:

(3)c語言編程簡易計算器擴展閱讀:

return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。

return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。

D. C語言編寫簡易計算器程序

C語言編寫計算器

E. 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);
//最後輸出結果
}

F. C語言程序設計簡易計算器

1、首先,打開Vs 2010,如圖。

2、找到左上角的新建並點擊,給文件為簡單計算器,單擊確定。

3、點擊下一步,注意勾選空項目,點擊下一步,點擊完成。

4、點擊左側的源文件,右擊選擇「添加—>項目」,選擇C++文件,命名為簡單計算器,因為是C程序,注意後綴名要加上.c,點擊確定完成文件新建工作。

5、輸入以下代碼,好了,一個簡單的計算器便做好了

G. 用c語言編寫一個簡單計算器程序

#include<stdio.h>//計算器

voidmenu()//自定義的菜單界面

printf("--------------------\n");

printf("請輸入你的選擇\n");

printf("1.+\n");

printf("2.-\n");

printf("3.*\n");

printf("4./\n");

printf("--------------------\n");

intmain()

inti=0;

intj=0;

intnum=0;//計算結果存放在nun

intselect=0;//選擇的選項存放在select

do//do-while先執行再判斷循環條件,即可實現重復計算功能

menu();//列印出菜單界面

scanf("%d",&select);//輸入你的選項

printf("請輸入計算值:");

scanf("%d%d",&i,&j);//輸入要計算的數值

switch(select)

case1:

printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能

break;

case2:

printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能

break;

case3:

printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能

break;

case4:

printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能

break;

default:

printf("輸入有誤重新選擇");

break;

}while(select);

return0;

運行結果:

(7)c語言編程簡易計算器擴展閱讀:

return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。

return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。

H. 用簡單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');
/*循環條件語句*/
}

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;
}

閱讀全文

與c語言編程簡易計算器相關的資料

熱點內容
ios多選文件夾 瀏覽:901
加強行車調度命令管理 瀏覽:241
伺服器已禁用什麼意思 瀏覽:148
部隊命令回復 瀏覽:753
神奇寶貝伺服器地圖怎麼設置 瀏覽:380
加密演算法輸出固定長度 瀏覽:862
程序員去重慶還是武漢 瀏覽:121
伺服器如何撤銷網頁登錄限制 瀏覽:980
微信公眾平台php開發視頻教程 瀏覽:628
怎麼看蘋果授權綁定的app 瀏覽:255
壓縮機單級壓縮比 瀏覽:380
linux測試php 瀏覽:971
什麼時候梁旁邊需要加密箍筋 瀏覽:40
微信清粉軟體源碼 瀏覽:717
matlabdoc命令 瀏覽:550
如何去ping伺服器 瀏覽:75
ecshop安裝php55 瀏覽:817
javaword庫 瀏覽:958
php圖片路徑資料庫中 瀏覽:488
什麼是大擂台演算法 瀏覽:329