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

c語言簡單計算器

發布時間: 2023-02-01 02:02:08

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

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

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

⑶ 如何用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語言 (c++) 編寫計算器程序

我們平時進行數學運算都是用計算器完成的,那麼如何用C語言編寫一個計算器呢?下面我給大家分享一下。

工具/材料

Dev C++

  • 01

    首先我們需要在Dev C++軟體中創建一個C語言項目,項目類型選擇控制台程序,如下圖所示

  • 02

    接下來我們在項目下面新建C語言文件,如下圖所示

  • 03

    然後我們在C文件中寫入計算器邏輯代碼,主要是讓用戶輸入計算方式,然後程序自動計算,如下圖所示

  • 04

    接下來我們點擊運行菜單,選擇下拉菜單中的運行選項,如下圖所示

  • 05

    最後在彈出的界面中我們輸入要計算的公式,程序就會自動計算,如下圖所示

⑸ 用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語言設計一個簡單的加減乘除計算器 具體需要這樣做

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

2、輸入預處理命令和主函數:#include /*函數頭:輸入輸出頭文件*/,void main()/*空類型:主函數*/。

3、定義變數:int a,b,d; /*定義變數的數據類型為整型*/,char c;/*定義變數的數據類型為字元型*/。

4、輸入四則運算式:printf(輸入如「3*4」或「5+2」的四則運算式:);/*輸出文字提示*/scanf(%d%c%d,&a,&c,&b);/*輸入四則運算式*/。

5、判斷運算符號:switch(c) /*判斷運算符號*/{case'+':d=a+b;break;/*進行加法6、運算*/case'-':d=a-b;break;/*進行減法運算*/case'*':d=a*b;break;/*進行乘法運算*/case'/':d=a/b;break; /*進行除法運算*/}。

7、輸出結果:printf(%d%c%d=%d\n,a,c,b,d);/*輸出結果*/。

熱點內容
怎樣防止sql注入 發布:2024-04-27 06:11:25 瀏覽:235
安卓為什麼不能登蘋果系統的游戲 發布:2024-04-27 06:11:23 瀏覽:600
編程日課 發布:2024-04-27 05:56:54 瀏覽:619
漏洞上傳工具 發布:2024-04-27 05:50:58 瀏覽:716
手機如何選擇存儲 發布:2024-04-27 05:40:25 瀏覽:799
機架式伺服器怎麼操作 發布:2024-04-27 05:19:02 瀏覽:815
我的世界minez網易伺服器 發布:2024-04-27 05:09:26 瀏覽:384
易網頁源碼 發布:2024-04-27 04:51:06 瀏覽:864
攜程伺服器是什麼牌子 發布:2024-04-27 04:31:50 瀏覽:745
醫院新冠肺炎疫情防控演練腳本 發布:2024-04-27 04:04:45 瀏覽:652