當前位置:首頁 » 編程語言 » c語言計算器提供求倒數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言計算器提供求倒數

發布時間: 2022-06-23 16:42:53

❶ 用c#編寫計算器怎麼實現求倒數功能

定義2個輸入float float1接收第一個數字 float2接收第二個數字
然後根據點擊的運算符號 假設四個符號的text為 「+」 ,「-」, 「*」,「/」( 這個根據你喜歡自己定義,但是給用戶看到的就是加減乘除) 用一個string接收好了 str
然後定義輸出為sting putOut
if (str=="+")
{putOut=convert.tosting(float1+float2)}
else if(str=="-")
{putOut=convert.tosting(float1-float2)}
else if(str=="*")
{putOut=convert.tosting(float1*float2)}
else if(str=="/")
{putOut=convert.tosting(float1/float2)}
最後把putOut顯示到頁面上即可

❷ 求 c語言計算器 只要 加 減 乘 除 開平方根 還有倒數(不是導數)

根號用sqrt(a+b)
這個代表根號下a+b;要加上頭文件#include
<math.h>。
1/a,這個a表示的倒數,
還有什麼不懂可以問偶。

❸ C語言怎麼求倒數啊 我是菜鳥...

你的數據類型有問題,定義double的話輸入時應該用%lf,用%f的話在存儲數據時會出問題導致a中不是輸入的數

❹ 新手求助,C語言,求一個數的倒數..

1/2=0;
#include<stdio.h>
void main(){
float a;
scanf("%f",&a);//%d整形,要用%f
if(a==0)
printf("error\n");
else
printf("%f\n",1.0/a);}//要用%f

❺ c語言求一個數的倒數

#include<stdio.h>
int main()
{
double a;
scanf("%lf",&a);
printf("%lf",1/a);
}

❻ c語言求倒數的函數哪裡錯了

先問一下,你想求的是相乘等於一的倒數,還是將排列順序反過來的倒數

❼ c語言通過函數調用求n個自然數的倒數和

思路:函數傳入參數n,接著for循環從1到n依次就倒數累加和。

參考代碼:

#include"stdio.h"
doublefun(intn){
doublesum=0;
inti;
for(i=1;i<=n;i++)
sum+=1.0/i;
returnsum;
}
intmain()
{
intn;
scanf("%d",&n);
printf("%.2lf",fun(n));
return0;
}
/*
運行結果:
5
2.28
*/

❽ C語言怎麼求一個數的倒數顯示出來不是小數形式而是分數形式!

intn=5;doublex=3.141592654;while(n>0)//取出小數後n位{x=x*10;//更新x值,把小數點後第一位放在個位上printf("%d\n",(int)x%10);//int強制把double轉換成int,因為求余必須為int類型,//而%10求余,是求個位上的數字i--;}

❾ 如何用c語言求函數導數

1、首先要有函數,設置成double類型的參數和返回值。

2、然後根據導數的定義求出導數,參數差值要達到精度極限,這是最關鍵的一步。

3、假如函數是double fun(doube x),那麼導數的輸出應該是(fun(x)-fun(x-e))/e,這里e是設置的無窮小的變數。

4、C由於精度有限,因此需要循環反復測試,並判斷無窮小e等於0之前,求出上述導數的值。二級導數也是一樣,所不同的是要把上述導數公式按定義再一次求導。這是演算法,具體的實現自己嘗試編程。

C語言的數據長度和精度都有限,因此用C語言編程求的導數並不精確,換句話說C語言編程不適合求導和極限。

(9)c語言計算器提供求倒數擴展閱讀:

舉例說明:

一階導數,寫一個函數 y = f(x):

float f(float x){ ...}

設 dx 初值

計算 dy

dy = f(x0) - f(x0+dx);

導數 初值

dd1=dy/dx;

Lab:;

dx = 0.5 * dx; // 減小步長

dy = f(x0) - f(x0+dx);

dd2=dy/dx; // 導數 新值

判斷新舊導數值之差是否滿足精度,滿足則得結果,不滿足則返回

if ( fabs(dd1-dd2) < 1e-06 ) { 得結果dd2...}

else { dd1=dd2;goto Lab;}。

❿ 用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);