全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2400
推到 Plurk!
推到 Facebook!

C++Builder 寫簡易計算機的一些問題

 
Breath_Dying
一般會員


發表:1
回覆:2
積分:0
註冊:2007-01-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-01-08 19:45:37 IP:125.228.xxx.xxx 訂閱
 
有一些問題請教大大
要怎麼寫才有辦法先乘除後加減
還有ln這個鍵該怎麼寫
下面是我的程式碼
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
#include <math.h><br />#include
#define PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
double sun[100]={0};
int c=0, d=0, e=0;
char g[100]={'0'};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button0Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button0->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button0->Caption;
}
else
{
Edit1->Text=Button0->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton4Click(TObject *Sender)
{
Button0->Enabled=true;
Button1->Enabled=true;
Button2->Enabled=false;
Button3->Enabled=false;
Button4->Enabled=false;
Button5->Enabled=false;
Button6->Enabled=false;
Button7->Enabled=false;
Button8->Enabled=false;
Button9->Enabled=false;
Button10->Enabled=false;
Button11->Enabled=false;
Button12->Enabled=false;
Button13->Enabled=false;
Button14->Enabled=false;
Button15->Enabled=false;
Button17->Enabled=false;
Button19->Enabled=false;
Button22->Enabled=false;
Button28->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click(TObject *Sender)
{
Button0->Enabled=true;
Button1->Enabled=true;
Button2->Enabled=true;
Button3->Enabled=true;
Button4->Enabled=true;
Button5->Enabled=true;
Button6->Enabled=true;
Button7->Enabled=true;
Button8->Enabled=false;
Button9->Enabled=false;
Button10->Enabled=false;
Button11->Enabled=false;
Button12->Enabled=false;
Button13->Enabled=false;
Button14->Enabled=false;
Button15->Enabled=false;
Button17->Enabled=false;
Button19->Enabled=false;
Button22->Enabled=false;
Button28->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
Button0->Enabled=true;
Button1->Enabled=true;
Button2->Enabled=true;
Button3->Enabled=true;
Button4->Enabled=true;
Button5->Enabled=true;
Button6->Enabled=true;
Button7->Enabled=true;
Button8->Enabled=true;
Button9->Enabled=true;
Button10->Enabled=false;
Button11->Enabled=false;
Button12->Enabled=false;
Button13->Enabled=false;
Button14->Enabled=false;
Button15->Enabled=false;
Button17->Enabled=true;
Button19->Enabled=true;
Button22->Enabled=true;
Button28->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton3Click(TObject *Sender)
{
Button0->Enabled=true;
Button1->Enabled=true;
Button2->Enabled=true;
Button3->Enabled=true;
Button4->Enabled=true;
Button5->Enabled=true;
Button6->Enabled=true;
Button7->Enabled=true;
Button8->Enabled=true;
Button9->Enabled=true;
Button10->Enabled=true;
Button11->Enabled=true;
Button12->Enabled=true;
Button13->Enabled=true;
Button14->Enabled=true;
Button15->Enabled=true;
Button17->Enabled=false;
Button19->Enabled=false;
Button22->Enabled=false;
Button28->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button1->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button1->Caption;
}
else
{
Edit1->Text=Button1->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button2->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button2->Caption;
}
else
{
Edit1->Text=Button2->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button3->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button3->Caption;
}
else
{
Edit1->Text=Button3->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button4->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button4->Caption;
}
else
{
Edit1->Text=Button4->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button5->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button5->Caption;
}
else
{
Edit1->Text=Button5->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button6->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button6->Caption;
}
else
{
Edit1->Text=Button6->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button7->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button7->Caption;
}
else
{
Edit1->Text=Button7->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button8->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button8->Caption;
}
else
{
Edit1->Text=Button8->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click(TObject *Sender)
{
if (e==1)
{
Edit1->Text=Button9->Caption;
e=0;
}
else if (e==0)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button9->Caption;
}
else
{
Edit1->Text=Button9->Caption;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button16Click(TObject *Sender)
{
int i, j=1, k=1;
j=Edit1->Text.ToInt();
for (i=1; i<=j; i )
{
k=k*i;
}
Edit1->Text=k;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button17Click(TObject *Sender)
{
double d=Edit1->Text.ToDouble(),r;
r=PI*d/180;
Edit1->Text=sin(r);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button18Click(TObject *Sender)
{
Edit1->Text=0;
c=0;
d=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button19Click(TObject *Sender)
{
double d=Edit1->Text.ToDouble(),r;
r=PI*d/180;
Edit1->Text=cos(r);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button28Click(TObject *Sender)
{
double d=Edit1->Text.ToDouble(),r;
r=PI*d/180;
Edit1->Text=tan(r);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button20Click(TObject *Sender)
{
double i=Edit1->Text.ToDouble();
i=1/i;
Edit1->Text=i;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button34Click(TObject *Sender)
{
double i=Edit1->Text.ToDouble();
i=log10(i);
Edit1->Text=i;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button35Click(TObject *Sender)
{
double i=Edit1->Text.ToDouble();
double e=2.7182818281828;
i=log(i);
Edit1->Text=i;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button29Click(TObject *Sender)
{
double i=Edit1->Text.ToDouble();
i=i*i;
Edit1->Text=i;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button30Click(TObject *Sender)
{
double i=Edit1->Text.ToDouble();
i=i*i*i;
Edit1->Text=i;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button33Click(TObject *Sender)
{
int Len=0;
String tmp;
Len=Edit1->Text.Length();
tmp=Edit1->Text.SubString(1,Len-1);
if (Edit1->Text.Length()==1)
{
Edit1->Text=0;
}
else
{
Edit1->Text=tmp;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button22Click(TObject *Sender)
{
Edit1->Text=Edit1->Text Button22->Caption;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button10->Caption;
}
else
{
Edit1->Text=Button10->Caption;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button11Click(TObject *Sender)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button11->Caption;
}
else
{
Edit1->Text=Button11->Caption;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button12Click(TObject *Sender)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button12->Caption;
}
else
{
Edit1->Text=Button12->Caption;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button13Click(TObject *Sender)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button13->Caption;
}
else
{
Edit1->Text=Button13->Caption;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button14Click(TObject *Sender)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button14->Caption;
}
else
{
Edit1->Text=Button14->Caption;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button15Click(TObject *Sender)
{
if (Edit1->Text!=0)
{
Edit1->Text=Edit1->Text Button15->Caption;
}
else
{
Edit1->Text=Button15->Caption;
}
}
//---------------------------------------------------------------------------



void __fastcall TForm1::Button26Click(TObject *Sender)
{
sun[c]=Edit1->Text.ToDouble();
g[d]=' ';
c ;
d ;
e=1;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button27Click(TObject *Sender)
{
sun[c]=Edit1->Text.ToDouble();
d--;
int j=0, k=0;
for (int i=0; i {
if (g[j]==' ')
{
sun[i 1]=sun[i] sun[i 1];
Edit1->Text=sun[i 1];
k=sun[i 1];
}
else if (g[j]=='-')
{
sun[i 1]=sun[i]-sun[i 1];
Edit1->Text=sun[i 1];
k=sun[i 1];
}
else if (g[j]=='*')
{
sun[i 1]=sun[i]*sun[i 1];
Edit1->Text=sun[i 1];
k=sun[i 1];
}
else if (g[j]=='/')
{
sun[i 1]=sun[i]/sun[i 1];
Edit1->Text=sun[i 1];
k=sun[i 1];
}
j ;
}
c=0;
d=0;
sun[c]=k;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button25Click(TObject *Sender)
{
sun[c]=Edit1->Text.ToDouble();
g[d]='-';
c ;
d ;
e=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button24Click(TObject *Sender)
{
sun[c]=Edit1->Text.ToDouble();
g[d]='*';
c ;
d ;
e=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button23Click(TObject *Sender)
{
sun[c]=Edit1->Text.ToDouble();
g[d]='/';
c ;
d ;
e=1;
}
//---------------------------------------------------------------------------


Coffee
版主


發表:31
回覆:878
積分:561
註冊:2006-11-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-01-08 23:45:12 IP:203.73.xxx.xxx 訂閱
你有學過資料結構嗎?中序轉後序?infix to postfix
------
不論是否我發的文,在能力範圍皆很樂意為大家回答問題。
為了補我的能力不足之處,以及讓答案可以被重複的使用,希望大家能儘量以公開的方式問問題。
在引述到我的文時自然會儘量替各位想辦法,謝謝大家!
Breath_Dying
一般會員


發表:1
回覆:2
積分:0
註冊:2007-01-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-01-09 18:17:05 IP:125.228.xxx.xxx 訂閱

===================引 用 文 章===================

有聽過 不太清楚內容

Coffee
版主


發表:31
回覆:878
積分:561
註冊:2006-11-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-01-09 18:20:53 IP:220.130.xxx.xxx 訂閱
一般來說我們使用的計算式是中序,這會有優先權的問題,如a + b * c實際上是將a存起來,計算b*c,再算出a+計算值
後序表示法解決了優先權的問題,使用後序只需要由左往右計算,而不需要考慮優先權
以一個中序表示法 a b來說,後序就是將其運算子放到最後面,變成a b
這就是一個最簡單的後序表示,它意謂著先將a, b儲存起來,讀到 需要運算,再將a與b的值取出來計算
那麼,一個a b * c的後序表示法應該會被看成"a (b * c) " => "a b c * "
要怎麼去知道後面的運算子優先權比較大?就是先去偷看下一個運算子,跟現在使用的運算子哪一個優先權比較大
這就算是拆解字串的部份了,當然,你可以再視情況加入括號以及你需要的ln計算,只要把ln當成是一個運算子即可

假設我們己經取得了a b c * 這個字串,請注意這裡,我是完全使用字串來存,所以我把所有的運算子運算元都用空白隔開
那麼開始計算這個算式,首先讀進a b c三個變數,因為我們沒有遇到任何的運算子,
讀到*號時,要讀入b, c兩個值,為什麼是b, c,看起來很像是我已經先知道的,實際上後序依賴堆疊(Stack)的方式來取得所需要的資料
Stack的原理是FILO,先放進去的,就最後取出,相對的,最後放進去的,就最先取出,
所以實際上取得的順序是c, b
再來計算好b * c的結果,再放到Stack中,原先的Stack剩下a跟這個計算出來的值,再讀到 號,於是重覆上面的步驟,
將計算值與a拿出來作 的計算,就可以得到原先我們希望的a b * c的結果

同理可證
a * b c => a b * c
a * b c / d => a b * c d /
(a * b c) / d => a b * c d /
a * (b c) / d => a b c * d /

「資料結構:使用C 」 (Fundamenta of Data Structure in C ) by Horowitz 算是一般科班比較常見的資料結構用書,你可以參考一下

//又替自己複習一遍XD
------
不論是否我發的文,在能力範圍皆很樂意為大家回答問題。
為了補我的能力不足之處,以及讓答案可以被重複的使用,希望大家能儘量以公開的方式問問題。
在引述到我的文時自然會儘量替各位想辦法,謝謝大家!
Breath_Dying
一般會員


發表:1
回覆:2
積分:0
註冊:2007-01-08

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-01-09 19:29:46 IP:125.228.xxx.xxx 訂閱

===================引 用 文 章===================
一般來說我們使用的計算式是中序,這會有優先權的問題,如a b * c實際上是將a存起來,計算b*c,再算出a 計算值
後序表示法解決了優先權的問題,使用後序只需要由左往右計算,而不需要考慮優先權
以一個中序表示法 a b來說,後序就是將其運算子放到最後面,變成a b
這就是一個最簡單的後序表示,它意謂著先將a, b儲存起來,讀到 需要運算,再將a與b的值取出來計算
那麼,一個a b * c的後序表示法應該會被看成"a (b * c) " => "a b c * "
要怎麼去知道後面的運算子優先權比較大?就是先去偷看下一個運算子,跟現在使用的運算子哪一個優先權比較大
這就算是拆解字串的部份了,當然,你可以再視情況加入括號以及你需要的ln計算,只要把ln當成是一個運算子即可

假設我們己經取得了a b c * 這個字串,請注意這裡,我是完全使用字串來存,所以我把所有的運算子運算元都用空白隔開
那麼開始計算這個算式,首先讀進a b c三個變數,因為我們沒有遇到任何的運算子,
讀到*號時,要讀入b, c兩個值,為什麼是b, c,看起來很像是我已經先知道的,實際上後序依賴堆疊(Stack)的方式來取得所需要的資料
Stack的原理是FILO,先放進去的,就最後取出,相對的,最後放進去的,就最先取出,
所以實際上取得的順序是c, b
再來計算好b * c的結果,再放到Stack中,原先的Stack剩下a跟這個計算出來的值,再讀到 號,於是重覆上面的步驟,
將計算值與a拿出來作 的計算,就可以得到原先我們希望的a b * c的結果

同理可證
a * b c????????? =>?? a b * c
a * b c / d????? =>?? a b * c d /
(a * b c) / d???? =>?? a b * c d /
a * (b c) / d???? =>?? a b c * d /

「資料結構:使用C 」 (Fundamenta of Data Structure in C ) by Horowitz 算是一般科班比較常見的資料結構用書,你可以參考一下

//又替自己複習一遍XD
謝謝大大 我會試試看的
系統時間:2024-05-06 21:04:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!