A. C語言編寫簡易計算器程序
C語言編寫計算器
我們可以用printf和scanf函數輸出結果和獲取用戶的輸入。需要<stdio.h>頭文件。scanf函數在讀取數據的時候不需要再一行上輸入每個數據,只要數據和數據之間留出空白就可以了。先聲明兩個變數number1和number2,operation變數用來存儲運算符。用scanf函數獲取這兩個數字和運算符。分別用%lf %c %lf
B. 用簡單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');
/*循環條件語句*/
}
C. 用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();
}
D. C語言編寫簡單的計算器
#
#include<stdio.h>
void main()
{
float a,b;
char d;
printf("請輸入兩個數a,b:");
scanf("%f,%f"&a,&b);
printf("請輸入符號d");
scanf("%c",d);
switch(d)
{
case'+':printf("%f\n,a+b);break;
case'-':printf("%f\n,a-b);break;
case'*':printf("%f\n,a*b);break;
case'/':printf("%f\n,a/b);break;
default:printf("input error\n");
}
}
E. 如何用C程序編寫一個計算器
1
首先,得從網上下載並安裝c++,然後從桌面找到並打開它。
F. 怎樣用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器
用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器的方法:
1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;
G. 用c語言 (c++) 編寫計算器程序
我們平時進行數學運算都是用計算器完成的,那麼如何用C語言編寫一個計算器呢?下面我給大家分享一下。
Dev C++
首先我們需要在Dev C++軟體中創建一個C語言項目,項目類型選擇控制台程序,如下圖所示
接下來我們在項目下面新建C語言文件,如下圖所示
然後我們在C文件中寫入計算器邏輯代碼,主要是讓用戶輸入計算方式,然後程序自動計算,如下圖所示
接下來我們點擊運行菜單,選擇下拉菜單中的運行選項,如下圖所示
最後在彈出的界面中我們輸入要計算的公式,程序就會自動計算,如下圖所示
H. 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);
//最後輸出結果
}
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;
}