以前大學時期視窗程式課的一個小作業,如果是一般計算機,不能先乘除後加減的話挺簡單的,就沒有寫這一篇的意義了,這是老師特別出的題目,希望可以先乘除後加減,這一個版本是寫給同學的範例版,程式碼也盡量精簡的,下面是程式碼和主要演算法程式碼部分。
//第一次時將數字存到sum變數,之後輸入的數字都是存到temp if (status = 0) then begin sum := StrToFloat(edit1.Text); status := 1; OP1 := OP; end else begin //判斷數字存到哪個變數~ if (signflag = 1) then temp2 := StrToFloat(edit1.Text) else temp1 := StrToFloat(edit1.Text); //做運算 if (signflag = 1) then begin //temp2此時做運算 case OP2 of 1: temp1 := temp1 + temp2; 2: temp1 := temp1 - temp2; 3: temp1 := temp1 * temp2; 4: temp1 := temp1 / temp2; end; temp2 := 0; //變數歸零 OP2 := 0; signflag := 0; end; case OP1 of 1,2: begin case OP of 1,2: //++情形 begin if (OP1 = 1) then sum := sum + temp1 else sum := sum - temp1; OP1 := OP; edit1.Text := FloatToStr(sum);//顯示 end; 3,4: //啟用temp2,+×情形 begin signflag := 1; //啟用temp2 OP2 := OP; //記錄運算子 end; end; end; 3,4: //×+情形,××情形 begin if (OP1 = 3) then sum := sum * temp1 else sum := sum / temp1; OP1 := OP; edit1.Text := FloatToStr(sum);//顯示 end; end; end; //等於運算 if (OP = 5) then begin case OP1 of 1:sum := sum + temp1; 2:sum := sum - temp1; 3:sum := sum * temp1; 4:sum := sum / temp1; end; edit1.Text := FloatToStr(sum); OP1 := 0; status := 0; end; clean := 0; //設定清除edit啟動

敗家誌°
研究生2.0
0 留言▼