c語言編譯計算器功能
⑴ 用簡單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語言實現一個計算器
1、#include<stdio.h>intmain()
2、{inta,b,c;scanf("%d%d%d"盯拆,&a,&b,&c);
3、intsum=a+b+c;
4、printf("和:%d",sum);
5、printf("平均值:%f",sum/3.0);
6、return0
⑶ 編寫函數實現簡易計算器的功能(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');
}
可以運行,不知道滿不滿足你的要求,你自己可以試試
⑷ 用c語言 (c++) 編寫計算器程序
我們平時進行數學運算都是用計算器完成的,那麼如何用C語言編寫一個計算器呢?下面我給大家分享一下。
工具/材料
Dev C++
- 01
首先我們需要在Dev C++軟體中創建一個C語言項目,項目類型選擇控制台程序,如下圖所示
- 02
接下來我們在項目下面新建C語言文件,如下圖所示
- 03
然後我們在C文件中寫入計算器邏輯代碼,主要是讓用戶輸入計算方式,然後程序自動計算,如下圖所示
- 04
接下來我們點擊運行菜單,選擇下拉菜單中的運行選項,如下圖所示
- 05
最後在彈出的界面中我們輸入要計算的公式,程序就會自動計算,如下圖所示
⑸ c語言編寫計算器程序
C語言編寫計算器
我們可以用printf和scanf函數輸出結果和獲取用戶的輸入。需要<stdio.h>頭文件。scanf函數在讀取數據的時候不需要再一行上輸入每個數據,只要數據和數據之間留出空白就可以了。先聲明兩個變數number1和number2,operation變數用來存儲運算符。用scanf函數獲取這兩個數字和運算符。分別用%lf %c %lf
⑹ c語言如何實現一個簡單的計算器
代碼如下:
#include<stdio.h>
void main()
{
int n,a,b,c;
scanf("%d",&n);
a=n; c=a%10; a/=10; b=a%10; a/=10; a%=10;
printf("%d的個位為%d,十位為%d,百位為%d。 ",n,c,b,a);
}
⑺ 用C語言編寫一個計算器程序,實現加,減,乘,除,求平方根(正數),倒數等功能.
#include<iostream>
#include<cmath>
#include<string>
using
namespace
std;
const
double
pi
=
3.14159265;
const
double
e
=
2.718281828459;
const
int
SIZE
=
1000;
typedef
struct
node//為了處理符號而建立的鏈表(如:
1+(-2))
{
char
data;
node
*next;
}node;
typedef
struct
stack_num//存儲
數
的棧
{
double
*top;
double
*base;
}stack_num;
typedef
struct
stack_char//存儲
運算符號
的棧
{
char
*top;
char
*base;
}stack_char;
stack_num
S_num;//定義
stack_char
S_char;//定義
char
fu[18]
=
{'\n',
')',
'+',
'-',
'*',
'/',
'%',
'^',
'Q',
'L',
'C',
'S',
'T',
'c',
's',
't',
'('};
int
compare[1000];//表現出各運算符號的優先順序
double
shu[1000];//存儲
"數"
的數組
double
dai_result;//運算的結果,是為了處理
M
運算(簡介函數里有M的定義)
int
biao
=
0;//和dia_result
一樣,為了處理
M
運算
char
line[SIZE];//輸入的所要計算的表達式
void
init()//初始化
{
compare[fu[0]]
=
-2;//用數字的大小表現出符號的優先順序
compare[fu[1]]
=
-1;
compare[fu[2]]
=
2;
compare[fu[3]]
=
2;
compare[fu[4]]
=
4;
compare[fu[5]]
=
4;
compare[fu[6]]
=
4;
compare[fu[7]]
=
5;
for(int
i
=
8;
i
<=
15;
i++)
compare[fu[i]]
=
6;
compare[fu[16]]
=
7;
S_num.base
=
(double*)malloc(sizeof(double)*SIZE);//為棧開辟空間
S_char.base
=
(char*)malloc(sizeof(char)*SIZE);//同上
S_num.top
=
S_num.base;
S_char.top
=
S_char.base;
}
void
push_num(double
n)//數字進棧
{
*
++S_num.top
=
n;
}
void
push_char(char
c)//運算符號進棧
{
*
++S_char.top
=
c;
}
double
pop_num()//數字出棧
{
double
m
=
*S_num.top;
S_num.top--;
return
m;
}
char
pop_char()//運算符號出棧
{
char
cc
=
*S_char.top;
S_char.top--;
return
cc;
}
char
get_top_char()//得到運算符號的棧中最頂端的運算符號
{
return
*S_char.top;
}
double
operate(double
y,
char
c,
double
x)//
對兩個數計算
(
含是雙目運算符
:
如
*,
/
等等
)
{
double
r;
if(c
==
'-')
r
=
x
-
y;
else
if(c
==
'+')
r
=
x
+
y;
else
if(c
==
'/'
&&
y
!=
0)
r
=
x
/
y;
else
if(c
==
'*')
r
=
x
*
y;
else
if(c
==
'^')
{
r
=
1;
for(int
i
=
1;
i
<=
y;
i++)
r
*=
x;
}
else
if(c
==
'%')
{
int
r0
=
(int)x
%
(int)y;
r
=
double(r0);
}
return
r;
}
double
operate_one(double
one,
char
cc)//
對一個數運算
(
含單目運算符
:
如
log(L),
sin(S)
等等
)
{
double
r;
if(cc
==
'Q')
r
=
sqrt(one);
else
if(cc
==
'C')
r
=
cos(one);
else
if(cc
==
'S')
r
=
sin(one);
else
if(cc
==
'T')
r
=
tan(one);
else
if(cc
==
'c')
i++;
}
i++;
}
if(ge
>=
3)
return
0;
else
return
1;
}
void
output(double
result)//
打出結果
{
printf("
所得結果是
:
");
cout<<result<<endl;
}
void
check()//
檢查表達式是否合法
{
void
introce();
char
cc;//
決定計算器按哪種功能進行計算
double
result;//
結果
void
input();//
定義
if(
check_kuohao()
&&
check_char()
)//
看是否合法
,
合法則計算
{
result
=
compute();
output(result);
cout<<"
輸入一個字元
'M'
或
'D'
或
'F',
決定是否繼續
:
"<<endl;
while(cin>>cc)
{
if(cc
==
'M')
{
system("cls");
introce();
printf("
您上次所得結果為
:
");
cout<<result<<endl;
cout<<"
在上次計算結果的基礎上
,
請繼續輸入想計算的表達式
"<<endl;
dai_result
=
result;
biao
=
1;
input();//
輸入表達式
break;
}
else
if(cc
==
'D')
{
system("cls");
introce();
cout<<"
計算器已清零
,
請輸入您所要計算的表達式
"<<endl;
input();//
輸入表達式
break;
}
else
if(cc
==
'F')
{
system("cls");
cout<<"
計算器關閉
,
謝謝使用
!"<<endl;
break;
}
else
{
cout<<"
所輸入字元無效
,
請輸入一個字元
'M'
或
'D'
或
'F'!"<<endl;
continue;
}
}
}
else//
不合法,分兩種不合法
{
if(check_kuohao()
==
0
&&
check_char()
==
1)
{
cout<<"
您所輸入的表達式括弧不匹配
,
請重新輸入
:"<<endl;
input();//
輸入表達式
}
else
{
cout<<"
您所輸入的表達式不合法
,
請重新輸入
:"<<endl;
input();//
輸入表達式
}
}
}
void
tackle_fuhao()//
處理負號
{
node
*root,
*head,
*p,
*q,
*p1;
root
=
head
=
new
node;
head->next
=
NULL;
int
i;
for(i
=
0;
line[i]
!=
'\0';
i++)//
建立鏈表
{
p
=
new
node;
p->data
=
line[i];
p->next
=
head->next;
head->next
=
p;
head
=
p;
}
//
delete
p;
q
=
(node*)malloc(sizeof(node));
head
=
root;
if(root->next->data
==
'+'
||
root->next->data
==
'-')//
處理第一個字元
{
p
=
new
node;
p->data
=
'0';
p->next
=
head->next;
head->next
=
p;
}
if(root->next
!=
NULL)
{
for(q
=
root->next;
q;
q
=
q->next)
{
if(q->data
==
'('
&&
(q->next->data
==
'-'
||
q->next->data
==
'+'))
{
p
=
new
node;
p->data
=
'0';
p->next
=
q->next;
q->next
=
p;
}
}
}
//
delete
q;
p1
=
new
node;
int
qi
=
-1;
for(p1
=
root->next;
p1;
p1
=
p1->next)
{
line[++qi]
=
p1->data;
}
line[++qi]
=
'\0';
}
void
input()//
輸入
{
cin>>line;
if(biao
==
0)
tackle_fuhao();//
處理負號
check();//
檢查表達式是否合法
}
void
introce()//
對計算器的符號功能的簡要介紹
{
cout<<"
計算器簡要介紹
"<<endl;
cout<<"C(cos)
S(sin)
T(tan)
a(arccos)
c(arcsin)
"<<endl;
cout<<"7
8
9
/
on
t(arctan)
"<<endl;
cout<<"4
5
6
*
%
L(log)"<<endl;
cout<<"1
2
3
-
M(M+)
Q(sqrt)
"<<endl;
cout<<"0
.
+
^(
乘方
)
F(off)
Enter(=)
"<<endl;
cout<<"
對於對數輸入
L2_5
表示以
2
為底
5
的對數
"<<endl;
cout<<"M(
在前面結果的基礎上繼續計算,
如:
上次結果為
10
,
現輸入
+10.5*2)"<<endl;
cout<<"D(
清零並繼續輸入
)"<<endl;
cout<<"F(
計算機關閉
)"<<endl;
cout<<"
輸入
P
就代表輸入圓周率
,
輸入
E
代表輸入自然對數
"<<endl<<endl;
}
void
print()
{
system("color
2");
cout<<"
歡迎使用本計算器
"<<endl;
cout<<"
輸入一個字元串
on,
計算器開始啟動
"<<endl;
}
void
if_start()//
是否啟動計算器
{
string
start;
print();
while(cin>>start)
{
if(start
!=
"on")
{
cout<<"
您所輸入的字元無效
,
請按照介紹的繼續輸入
:"<<endl;
continue;
}
else
break;
}
if(start
==
"on")
{
system("color
5");//
顏色的處理
system("cls");//
刷屏
}
introce();//
對計算器的簡要介紹
cout<<"
現在
,
請輸入您所要計算的表達式
"<<endl;
input();//
輸入所要計算的表達式
}
int
main()
{
if_start();//
調用是否啟動計算器函數
return
0;
}
r
=
acos(one);
else
if(cc
==
's')
r
=
asin(one);
else
if(cc
==
't')
r
=
atan(one);