c語言程序設計計算器
Ⅰ 用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語言編程一個計算器
#include
<stdio.h>
#include
<conio.h>
#include
<stdlib.h>
int
func_1(
int
n1,
int
n2
)
{
int
sum=0;/*存放最後結果*/
int
i;
for(i=n1;
i<=n2;
i++)
sum
+=
i;
return
sum;
}
long
func_2(
int
n
)
{
if(1==n
||
0==n)
return
1;
else
return
n*func_2(n-1);
}
int
func_3(
int
x,
int
y
)
{
if(0==y)
return
x;
else
return
func_3(y,x%y);
}
void
func_4(
)
{
system("cls");
puts("=======簡易計算器========");
puts("1.累加和");
puts("2.階乘");
puts("3.最大公約數");
puts("4.退出");
puts("=========================");
}
int
main()
{
int
num1,num2;
char
ch;
while(ch!='4')
{
/*讀入用戶的選擇*/
do
{
system("cls");
func_4();
printf("請選擇(1-4):");
ch=getchar();
}while(ch>'4'
||
ch<'1');
switch(ch)
{
case
'1':
printf("請輸入兩個整數:");
scanf("%d%d",&num1,&num2);
printf("%d和%d的累計和為:%d\n",num1,num2,func_1(num1,num2));
getch();
break;
case
'2':
printf("請輸入一個正整數:");
scanf("%d",&num1);
printf("%d的階乘為%d\n",num1,func_2(num1));
getch();
break;
case
'3':
printf("請輸入兩個整數:");
scanf("%d%d",&num1,&num2);
printf("%d和%d的最大公約數為:%d\n",num1,num2,func_3(num1,num2));
getch();
break;
case
'4':
puts("請按任意鍵結束程序!");
getch();
}
}
return
0;
}
Ⅳ 用C語言設計一個簡單計算器
#include<stdio.h>
voidadd(inta,intb,intc)
{
c=a+b;
printf("%d ",c);
printf(" ");
}
voidminus(inta,intb,intc)
{
c=a-b;
printf("%d ",c);
printf(" ");
}
voidmultiplication(inta,intb,intc)
{
c=a*b;
printf("%d ",c);
printf(" ");
}
voiddiv(inta,intb,intc)
{
c=(float)a/(float)b;
printf("%f ",c);
printf(" ");
}
main()
{
inta,b,c;
charp;
puts("inputA: ");
scanf("%d",&a);
puts("inputB: ");
scanf("%d",&b);
puts("inputoperation: ");
getchar();
p=getchar();
if(p=='+')add(a,b,c);else
if(p=='-')minus(a,b,c);else
if(p=='*')multiplication(a,b,c);else
if(p=='/')div(a,b,c);else
puts("沒有注冊這個運算符號 ");
}
以上是設計的一個簡易計算器。可以進行相應的加減乘除。
Ⅳ 怎麼用C語言設計一個簡單計算器
怎麼用C語言設計一個簡單計算器 #include<iostream>
#include<math.h>
using namespace std;
main ()
{
char k;
double s,g;
k='-';
cout<<"求絕對值請按A;求平方根請按B;加減乘除清分別按+-*/" <<endl;
cin>>g;
while (1)
{
cin>>k;
if (k=='=')
break;
if (k=='A')
{
g=fabs (g);
break;
}
if (k=='B')
{
g=sqrt (g);
break;
}
cin>>s;
if (k=='+')
g+=s;
if (k=='-')
g-=s;
if (k=='*')
g*=s;
if (k=='/')
{
if (s==0)
cout<<"wrong";
else
g/=s;
}
}
cout<<g;
}
#include<stdio.h> void add(int a,int b,int c) { c=a+b; printf("%d\t",c); printf("\n"); } void minus(int a,int b,int c) { c=a-b; printf("%d\t",c); printf("\n"); } void multiplication(int a,int b,int c) { c=a*b; printf("%d\t",c); printf("\n"); } void div(int a,int b,int c) { c=(float)a/(float)b; printf("%f\t",c); printf("\n"); } main() { int a,b,c; char p; puts("input A:\n"); scanf("%d",&a); puts("input B:\n"); scanf("%d",&b); puts("input operation:\n"); getchar(); p=getchar(); if(p=='+') add(a,b,c);else if(p=='-') minus(a,b,c);else if(p=='*') multiplication(a,b,c);else if(p=='/') div(a,b,c);else puts("沒有注冊這個運運算元號\n"); } 以上是設計的一個簡易計算器。可以進行相應的加減乘除。 簡介:
C語言是一種計算機程式設計語言,它既具有高階語言的特點,又具有組合語言的特點。它由美國貝爾研究所的D.M.Ritchie於1972年推出,1978年後,C語言已先後被移植到大、中、小及微型機上,它可以作為工作系統設計語言,編寫系統應用程式,也可以作為應用程式設計語言,編寫不依賴計算機硬體的應用程式。它的應用范圍廣泛,具備很強的資料處理能力,不僅僅是在軟體開發上,而且各類科研都需要用到C語言,適於編寫系統軟體,三維,二維圖形和動畫,具體應用比如微控制器以及嵌入式系統開發。
求用C語言設計一個簡單計算器
應該不難,就是按鈕的訊息傳遞處理函式等,
用C語言怎麼設計一個簡單計算器?
#include<stdio.h>
void add(int a,int b,int c)
{
c=a+b;
printf("%d\t",c);
printf("\n");
}
void minus(int a,int b,int c)
{
c=a-b;
printf("%d\t",c);
printf("\n");
}
void multiplication(int a,int b,int c)
{
c=a*b;
printf("%d\t",c);
printf("\n");
}
void div(int a,int b,int c)
{
c=(float)a/(float)b;
printf("%f\t",c);
printf("\n");
}
main()
{
int a,b,c;
char p;
puts("input A:\n");
scanf("%d",&a);
puts("input B:\n");
scanf("%d",&b);
puts("input operation:\n");
getchar();
p=getchar();
if(p=='+') add(a,b,c);else
if(p=='-') minus(a,b,c);else
if(p=='*') multiplication(a,b,c);else
if(p=='/') div(a,b,c);else
puts("沒有注冊這個運運算元號\n");
}
怎麼用C語言程式設計一個簡單計算器?
#include<<a href=":./s?wd=stdio.h&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-TLwGUv3EPH6srjc4rH61" target="_blank" class="-highlight">stdio.h</a>>
void main() { float x,y,z; char c;
scanf("%f%c%f",&x,&c,&y);
switch ( c ) {
case '+': z=x+y; break;
case '-': z=x-y; break;
case '*': z=x*y; break;
case '/': z=( y==0 )?(0):(x/y); break;
default: z=0; break;
}
printf("%f%c%f=%f\n",x,c,y,z);
}
C語言是一門通用計算機程式語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低階儲存器、產生少量的機器碼以及不需要任何執行環境支援便能執行的程式語言。
盡管C語言提供了許多低階處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程式可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(微控制器或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國家標准語法,稱為ANSI C,作為C語言最初的標准。[1] 目前2011年12月8日,國際標准化組織(ISO)和國際電工委員會(IEC)釋出的C11標準是C語言的第三個官方標准,也是C語言的最新標准,該標准更好的支援了漢字函式名和漢字識別符號,一定程度上實現了漢字程式設計。
用C語言編一個簡單計算器
已經為你實現了加減乘除了。 main.c 簡易計算器 Created by shengan on 15/6/29. Copyright (c) 2015年 shengan. All rights reserved.#include <stdio.h>double plus();double minus();double multi();double divide();int main(int argc, const char * argv[]) { insert code here... double a, b; char c; while(1 == 1) { printf("簡易計算器\n"); printf("輸入第一個數:\n"); scanf("%lf", &a); printf("輸入運運算元:\n"); scanf("%s", &c); printf("輸入第二個數:\n"); scanf("%lf", &b); switch (c) { case '+': printf("%lf %c %lf = %lf", a, c, b, plus(a, b)); break; case '-': printf("%lf %c %lf = %lf", a, c, b, minus(a, b)); break; case '*': printf("%lf %c %lf = %lf", a, c, b, minus(a, b)); break; case '/': printf("%lf %c %lf = %lf", a, c, b, minus(a, b)); break; default: printf("運運算元號輸入錯誤!\n"); break; } } return 0;}加法double plus(double a, double b){ return a + b;}double minus(double a, double b){ return a - b;}double multi(double a, double b){ return a * b;}double divide(double a, double b){ return a / b;}
設計一個簡單計算器
#include <stdio.h>
int main(void)
{
int data1, data2;
char op;
printf("please input data1 op data2:");
scanf("%d %c %d", &data1 , &op , &data2);
switch (op)
{
case '+':
printf("%d + %d = %.0f\n", data1, data2, (double)data1 + (double)data2);
break;
case '-':
printf("%d - %d = %.0f\n", data1, data2, (double)data1 - (double)data2);
break;
case '*':
printf("%d * %d = %.0f\n", data1, data2, (double)data1 * (double)data2);
break;
case '/':
if (data2 == 0)
{
printf("Error! chu shu wei 0.");
}
else
{
printf("%d / %d = %.0f\n", data1, data2, (double)data1 / (double)data2);
}
break;
case '%':
if (data2 == 0)
{
printf("Error! chu shu wei 0.");
}
else
{
printf("%d %% %d=%d3\n", data1, data2, data1 % data2);
}
break;
default:
printf("Unknown operator\n");
}
return 0;
}如果要求是浮點數的話換一下資料型別就可以了
用c語言程式設計一個簡單計算器,求其原始碼
/*
2013年12月23日 12:43:46
目的:計算器的實現
*/
# include <stdio.h>
# include <ctype.h>
# include <math.h>
char get_choice(void); 獲取使用者輸入的選項,並建立目
char get_first(void); 獲取使用者輸入的選項,並剔除錯誤輸入
float get_int(void); 獲取使用者輸入的計算值
float add(void); 定義加法函式
float subtraction(void); 定義減法函式
float multiplication(void); 定義乘法函式
float division(void); 定義除法函式
float extract(void); 定義開方函式
float square(void); 定義平方函式
float cube(void); 定義立方函式
int count = 0;
int main(void)
{
char choice;
printf("***歡迎使用由小錢製作的計算器***\n");
choice = get_choice();
while(choice != 'q')
{
switch(choice)
{
case 'a':
add(); break;
case 'b':
subtraction(); break;
case 'c':
multiplication(); break;
case 'd':
division(); break;
case 'e':
extract(); break;
case 'f':
square(); break;
case 'g':
cube(); break;
default :
printf("您輸入有誤,請重新輸入:"); break;
}
fflush(stdin);
choice = get_choice();
}
printf("bye");
return 0;
}
獲取使用者輸入的選項,並建立目錄
char get_choice(void)
{
char ch;
int a = 0;
建立目錄
printf("\n--------------------------------\n");
printf("a. 加法\t\t\tb. 減法\nc. 乘法\t\t\td. 除法\n");
printf("e. 開方\t\t\tf. 平方\ng. 立方\t\t\tq. 退出\n");
printf("--------------------------------\n");
printf("請輸入你的選項:");
ch = get_first();
while(ch == ' ' || ch == '\n' || ch == '\t')
ch = get_first();
判斷使用者輸入的選項是否有誤
while((ch<'a' || ch>'g') && ch !='q')
{
putchar(ch);
printf(" 你輸入的選項有誤,請重新輸入:");
ch = get_first();
}
return ch;
}
獲取使用者輸入的選項,並剔除錯誤輸入
char get_first(void)
{
char ch;
ch = getchar();
剔除由使用者輸入選項時產生的換行符
while(ch == '\n')
{
ch = getchar();
}
return ch;
}
獲取使用者輸入的計算值
float get_int(void)
{
float input;
char ch;
int a;
if(count == 0)
printf("親!請輸入數值:");
if(count == 1)
printf("親!請輸入第一個數值:");
if(count == 2)
printf("親!請輸入第二個數值:");
a = scanf("%f", &input);
判斷使用者的輸入是否為一個數值
while(a != 1)
{
剔除使用者輸入錯誤的字元
while((ch = getchar()) != '\n')
{
putchar(ch);
printf(" 不是一個數值,請輸入例如3、111.2、或者-1");
a = scanf("%f", &input);
}
}
return input;
}
定義加法函式
float add(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
sum = i + j;
printf("%.2f + %.2f = %.2f\n", i, j, sum);
return sum;
}
定義減法函式
float subtraction(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
sum = i - j;
printf("%.2f - %.2f = %.2f\n", i, j, sum);
return sum;
}
定義乘法函式
float multiplication(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
sum = i * j;
printf("%.2f * %.2f = %.2f\n", i, j, sum);
return sum;
}
定義除法函式
float division(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
判斷除數是否為0
while(j == 0)
{
printf("除數不能為0\n請重新輸入!!!\n");
j = get_int();
}
sum = i / j;
printf("%.2f / %.2f = %.2f\n", i, j, sum);
return sum;
}
定義開方函式
float extract(void)
{
float i, sum;
count = 0;
i = get_int();
判斷開方數是否小於0,如果小於0,則讓使用者重新輸入
while(i < 0)
{
printf("請輸入大於0的數值\n");
i = get_int();
}
sum = sqrt(i);
printf("%.2f的開方等於%.2f\n", i, sum);
return sum;
}
定義平方函式
float square(void)
{
float i, sum;
count = 0;
i = get_int();
sum = i * i;
printf("%.2f的平方等於%.2f\n", i, sum);
return sum;
}
定義立方函式
float cube(void)
{
float i, sum;
count = 0;
i = get_int();
sum = i * i * i;
printf("%f的立方等於%.3f\n", i, sum);
return sum;
}
Ⅵ C語言程序設計,做一個簡單計算器。
1、首先,打開Vs 2010,如圖。
Ⅶ 用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);/*輸出結果*/。