當前位置:首頁 » 編程語言 » c語言寫計算器

c語言寫計算器

發布時間: 2022-05-09 22:13:32

① 用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;

運行結果:

(1)c語言寫計算器擴展閱讀:

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

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

② 如何用c語言寫計算器程序

計算器要求如下:1)
設計一面向對象的類,含有加、減、乘、除運算及開平方等功能的函數。
2)
設計加、減、乘、除運算及求平方根的函數。
3)
設計實現四則混合運算的函數。
4)
設計測試函數。
要求是
完善計算器程序,改為可對實數操作。
完善程序,改為多個操作數基本四則運算,遇到0為止。
增加函數,完成四則混合運算,增加相應的主菜單選項。
添加語句,使四則運算具有測試功能。
可擴充其功能。

③ 用C語言代碼編寫計算器

簡單計算器實現:
#include<stdio.h>
int
main()
{
double
num1
=
0;
//輸入1
double
num2
=
0;
//輸入2
char
ch;
//操作
double
ret
=
0;
//結果
printf(
"輸入第一個數:"
);
scanf(
"%lf",
&num1
);
printf(
"輸入第二個數:"
);
scanf(
"%lf",
&num2
);

④ c語言編寫 編寫一個簡單的計算器,實現兩個整型數的四則運算。

1、打開CodeBlocks,新建一個空白文件,先定義頭文件和主函數,接著寫程序多大的主體:

⑤ 如何用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語言編寫計算器

#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語言編寫計算器

  • 我們可以用printf和scanf函數輸出結果和獲取用戶的輸入。需要<stdio.h>頭文件。scanf函數在讀取數據的時候不需要再一行上輸入每個數據,只要數據和數據之間留出空白就可以了。先聲明兩個變數number1和number2,operation變數用來存儲運算符。用scanf函數獲取這兩個數字和運算符。分別用%lf %c %lf

⑧ 用C語言做一個計算器,能實現加減乘除混合運算

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

1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;

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

熱點內容
寫shell腳本 發布:2025-05-11 04:37:41 瀏覽:935
電腦伺服器打開有什麼用 發布:2025-05-11 04:36:49 瀏覽:98
sqlserver2008查詢時間 發布:2025-05-11 04:15:28 瀏覽:386
安卓孤膽車神被封號怎麼解封 發布:2025-05-11 04:05:22 瀏覽:940
高壓洗車泡沫怎麼配置 發布:2025-05-11 04:00:47 瀏覽:547
騰訊輕量伺服器怎麼使用 發布:2025-05-11 03:52:46 瀏覽:174
4位密碼組合有多少種至少有一個0 發布:2025-05-11 03:44:03 瀏覽:338
八卦手指演算法 發布:2025-05-11 03:23:32 瀏覽:281
編譯成exe是什麼意思 發布:2025-05-11 03:23:28 瀏覽:470
javaweb技術內幕 發布:2025-05-11 03:20:14 瀏覽:803