鱼C论坛

 找回密码
 立即注册
查看: 2334|回复: 1

[作品展示] 学了零基础入门delphi7的前四节课编写的一个计算器,比小甲鱼课堂展示的效果好

[复制链接]
发表于 2019-5-30 16:58:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
小甲鱼课堂上编写的计算器,有如下缺陷:
1.可以输入多个小数点;
2。实数前会出现0,例如输入7,可以输入成007,正常的应该是输入007显示7;
3.按‘=’显示计算结果后再按‘=’会不断显示更多结果(电脑自带的计算器也存在这个问题);
4.不能进行连续运算,即不能输入1+2+3-4*5÷6再按“=”求结果,只能每次计算两个数的运算;
5. 4不成立5自然也不能成立,5针对电脑自带的标准计算器而言的,连续运算时不遵守四则运算先乘除后加减的优先级。
6.输入操作数和运算符后直接按“=”会报错,电脑自带的计算器这样操作出现的结果有点怪异,我实现的效果其实也好不到哪去T_T!
7.除数为0时,没有预先处理;
8.没有删除功能和取反功能。


我的语法比较糟糕,但因为只学了前四课,做出的效果自我感觉还不错,希望大家多多指教。话不多说,我的作品在下面:
好悲催,本想直接报所有文件打包添加到附件的,好像不行,只好附上代码了:



  1. unit Unit1;

  2. interface

  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, Buttons, jpeg, ExtCtrls, StdCtrls;

  6. type
  7.   TForm1 = class(TForm)
  8.     edt1: TEdit;
  9.     img1: TImage;
  10.     btn1: TSpeedButton;
  11.     btn2: TSpeedButton;
  12.     btn3: TSpeedButton;
  13.     btn4: TSpeedButton;
  14.     btn5: TSpeedButton;
  15.     btn6: TSpeedButton;
  16.     btn7: TSpeedButton;
  17.     btn8: TSpeedButton;
  18.     btn9: TSpeedButton;
  19.     btn10: TSpeedButton;
  20.     btn11: TSpeedButton;
  21.     btn12: TSpeedButton;
  22.     btn13: TSpeedButton;
  23.     btn14: TSpeedButton;
  24.     btn15: TSpeedButton;
  25.     btn16: TSpeedButton;
  26.     btn17: TSpeedButton;
  27.     btn18: TSpeedButton;
  28.     btn19: TSpeedButton;
  29.     btn20: TSpeedButton;
  30.     btn21: TSpeedButton;
  31.     edt2: TEdit;
  32.    
  33.     procedure btn1Click(Sender: TObject);
  34.     procedure btn2Click(Sender: TObject);
  35.     procedure btn3Click(Sender: TObject);
  36.     procedure btn4Click(Sender: TObject);
  37.     procedure btn5Click(Sender: TObject);
  38.     procedure btn6Click(Sender: TObject);
  39.     procedure btn7Click(Sender: TObject);
  40.     procedure btn8Click(Sender: TObject);
  41.     procedure btn9Click(Sender: TObject);
  42.     procedure btn10Click(Sender: TObject);
  43.     procedure btn11Click(Sender: TObject);
  44.     procedure btn12Click(Sender: TObject);
  45.     procedure btn13Click(Sender: TObject);
  46.     procedure btn14Click(Sender: TObject);
  47.     procedure btn15Click(Sender: TObject);
  48.     procedure btn16Click(Sender: TObject);
  49.     procedure btn17Click(Sender: TObject);
  50.     procedure btn18Click(Sender: TObject);
  51.     procedure btn19Click(Sender: TObject);
  52.     procedure btn20Click(Sender: TObject);
  53.     procedure btn21Click(Sender: TObject);
  54.     procedure FormCreate(Sender: TObject);



  55.   private
  56.     { Private declarations }
  57.   public
  58.     { Public declarations }
  59.   end;

  60. var
  61.   Form1: TForm1;

  62. implementation

  63. var
  64.   result:Real;   //结果
  65.   mdl_result:Real;//_混合运算时加减运算的结果
  66.   temp:Real;   //中间量
  67.   opre:Char;   //运算符
  68.   mdl_opre:Char;// 混合运算时加减运算的运算符

  69. {$R *.dfm}

  70. //输入1,当1前面的数字全为0时,显示1
  71. procedure TForm1.btn1Click(Sender: TObject);
  72. Begin
  73.   If StrToFloat(edt1.Text+'1')<> 1 Then

  74.      edt1.Text:= edt1.Text + '1'

  75.   Else
  76.      edt1.Text:= '1';
  77. End;

  78. //输入2,当2前面的数字全为0时,显示2
  79. procedure TForm1.btn2Click(Sender: TObject);
  80. Begin
  81.   If StrToFloat(edt1.Text+'2')<> 2 Then

  82.      edt1.Text:= edt1.Text + '2'

  83.   Else
  84.      edt1.Text:= '2';
  85. End;

  86. //输入3,当3前面的数字全为0时,显示3
  87. procedure TForm1.btn3Click(Sender: TObject);
  88. Begin
  89.   If StrToFloat(edt1.Text+'3')<> 3 Then

  90.      edt1.Text:= edt1.Text + '3'

  91.   Else
  92.      edt1.Text:= '3';
  93. End;

  94. //输入4,当4前面的数字全为0时,显示4
  95. procedure TForm1.btn4Click(Sender: TObject);
  96. Begin
  97.   If StrToFloat(edt1.Text+'4')<> 4 Then

  98.      edt1.Text:= edt1.Text + '4'

  99.   Else
  100.      edt1.Text:= '4';
  101. End;

  102. //输入5,当5前面的数字全为0时,显示5
  103. procedure TForm1.btn5Click(Sender: TObject);
  104. Begin
  105.   If StrToFloat(edt1.Text+'5')<> 5 Then

  106.      edt1.Text:= edt1.Text + '5'

  107.   Else
  108.      edt1.Text:= '5';
  109. End;

  110. //输入6,当6前面的数字全为0时,显示6
  111. procedure TForm1.btn6Click(Sender: TObject);
  112. Begin
  113.   If StrToFloat(edt1.Text+'6')<> 6 Then

  114.      edt1.Text:= edt1.Text + '6'

  115.   Else
  116.      edt1.Text:= '6';
  117. End;

  118. //输入7,当7前面的数字全为0时,显示7
  119. procedure TForm1.btn7Click(Sender: TObject);
  120. Begin
  121.   If StrToFloat(edt1.Text+'7')<> 7 Then

  122.      edt1.Text:= edt1.Text + '7'

  123.   Else
  124.      edt1.Text:= '7';
  125. End;

  126. //输入8,当8前面的数字全为0时,显示8
  127. procedure TForm1.btn8Click(Sender: TObject);
  128. Begin
  129.   If StrToFloat(edt1.Text+'8')<> 8 Then

  130.      edt1.Text:= edt1.Text + '8'

  131.   Else
  132.      edt1.Text:= '8';
  133. End;

  134. //输入9,当9前面的数字全为0时,显示9
  135. procedure TForm1.btn9Click(Sender: TObject);
  136. Begin
  137.   If StrToFloat(edt1.Text+'9')<> 9 Then

  138.      edt1.Text:= edt1.Text + '9'

  139.   Else
  140.      edt1.Text:= '9';
  141. End;

  142. //输入0,为整数时 从高位到低位第一个非0数前不能出现0,纯小数时小数点前只能有一个0
  143. procedure TForm1.btn10Click(Sender: TObject);
  144. Begin
  145.   If (edt1.Text <>'') and(edt1.Text <>'0')or (POS('.',edt1.Text)= 1) Then

  146.      edt1.Text:= edt1.Text + '0'

  147.   Else
  148.     edt1.Text:=  '0';
  149. End;

  150. //输入00,为整数时 从高位到低位第一个非0数前不能出现0,纯小数时小数点前只能有一个0
  151. procedure TForm1.btn11Click(Sender: TObject);
  152. Begin
  153.   If (edt1.Text <>'') and (edt1.Text <>'0') and (edt1.Text <>'00') or (POS('.',edt1.Text)= 1) Then

  154.      edt1.Text:= edt1.Text + '00'

  155.   Else
  156.      edt1.Text:=  '0';
  157. End;

  158. //输入小数点,且只能输入一个小数点
  159. procedure TForm1.btn12Click(Sender: TObject);
  160. Begin
  161.   If POS('.',edt1.Text)= 0  Then

  162.      edt1.Text:= edt1.Text + '.';

  163. End;

  164. //删除功能
  165. procedure TForm1.btn13Click(Sender: TObject);
  166. Begin
  167. //edt1.Text:=Copy(edt1.Text,1,Length(Trim(edt1.Text))-1);
  168.   If (opre <>'') and (opre <> ' ')  Then     //按下过'+','-','×','÷'而未曾按下过'='键的情况下

  169.       If  Length(edt1.Text )>0 Then  //删掉edt1.Text的最末尾的一个字符

  170.           edt1.Text:=Copy(edt1.Text,1,Length(edt1.Text)-1)
  171.       Else  //Length(edt1.Text )=0  删掉前面的运算符opre
  172.           opre:=' '

  173.   Else //刚开机未曾按下过 '+','-','×','÷'或按下过'='键的情况下
  174.       If (Length(edt1.Text )>0) and (edt1.Text<>'0') Then  //当edt1.Text为不等于'0'的字符串时删掉edt1.Text的最末尾的一个字符

  175.          edt1.Text:=Copy(edt1.Text,1,Length(edt1.Text)-1)
  176.       Else  // edt1.Text ='0'  不执行删除
  177.           ;
  178. End;

  179. //浮点除
  180. procedure TForm1.btn14Click(Sender: TObject);
  181. Begin
  182.   If  Length(edt1.Text)> 0  Then

  183.       temp:= StrToFloat(edt1.Text);

  184.    Case opre of
  185.      '+': Begin      //保存前面的运算结果及加号
  186.              mdl_result:= result;

  187.              mdl_opre:= opre;

  188.              result:= temp;
  189.             End;

  190.       '-': Begin     //保存前面的运算结果及减号
  191.              mdl_result:= result;

  192.              mdl_opre:= opre;

  193.              result:= temp;
  194.             End;

  195.       '*': result:= result * temp;

  196.       '/': If  temp = 0 Then
  197.                Begin
  198.                    edt1.Text:= '除数不能为0' ;

  199.                    result:= result / temp;
  200.                 End
  201.            Else
  202.                 result:= result / temp;
  203.    End;

  204.    If  (result = 0 )and ((opre = ' ')or(opre = '')) Then
  205.        Begin
  206.           result:= temp;

  207.           temp:= 0;
  208.        End ;


  209.   opre:='/';

  210.   edt1.Text:= '' ;

  211.   edt2.Text:= FloatToStr(result) ;
  212. End;

  213. //归零 (C)
  214. procedure TForm1.btn15Click(Sender: TObject);
  215. Begin
  216.   edt1.Text:= '0';

  217.   edt2.Text:= '0';

  218.   opre:= ' ';

  219.   mdl_opre:=' ';

  220.   temp:= 0;

  221.   result:= 0;

  222.   mdl_result:= 0;
  223. End;

  224. //乘法
  225. procedure TForm1.btn16Click(Sender: TObject);
  226. Begin
  227.   If  Length(edt1.Text)> 0  Then

  228.       temp:= StrToFloat(edt1.Text);

  229.    Case opre of
  230.       '+': Begin      //保存前面的运算结果及加号
  231.              mdl_result:= result;

  232.              mdl_opre:= opre;

  233.              result:= temp;
  234.             End;

  235.       '-': Begin     //保存前面的运算结果及减号
  236.              mdl_result:= result;

  237.              mdl_opre:= opre;

  238.              result:= temp;
  239.             End;

  240.       '*': result:= result * temp;

  241.       '/': If  temp = 0 Then
  242.                Begin
  243.                    edt1.Text:= '除数不能为0' ;

  244.                    result:= result / temp;
  245.                 End
  246.            Else
  247.                 result:= result / temp;
  248.    End;

  249.    If  (result = 0 )and ((opre = ' ')or(opre = '')) Then
  250.        Begin
  251.           result:= temp;

  252.           temp:= 0;
  253.        End ;

  254.   opre:='*';

  255.   edt1.Text:= '' ;

  256.   edt2.Text:= FloatToStr(result) ;
  257. End;

  258. //输入百分数或转变为百分数
  259. procedure TForm1.btn17Click(Sender: TObject);
  260. Begin
  261.   If (Length(edt1.Text)>0) and(edt1.Text<> '0') Then    //如果edt1.Text为不等于'0' 的字符串,则将字符串转化成数字再除以100
  262.       Begin
  263.         temp:= StrToFloat(edt1.Text);

  264.         edt1.Text:=FloatToStr(temp/100);
  265.       End ;
  266.     // edt1.Text等于其他的情况忽视('',' ', '0')
  267. End;

  268. //加法
  269. procedure TForm1.btn18Click(Sender: TObject);
  270. Begin
  271.   If  Length(edt1.Text)> 0  Then

  272.       temp:= StrToFloat(edt1.Text);

  273.    Case opre of
  274.       '+': result:= result + temp;

  275.       '-': result:= result - temp;

  276.       '*': Begin
  277.              result:= result * temp;

  278.              Case  mdl_opre of

  279.                 '+' : result:= result + mdl_result ;

  280.                 '-' : result:= -result + mdl_result ;
  281.              End ;
  282.              //清空中间结果和中间运算符
  283.              mdl_result:= 0;

  284.              mdl_opre:= ' ';

  285.            End;

  286.       '/': If  temp = 0 Then

  287.                 Begin
  288.                    edt1.Text:= '除数不能为0' ;

  289.                    result:= result / temp;
  290.                 End
  291.            Else
  292.                 Begin
  293.                   result:= result / temp;

  294.                    Case  mdl_opre of

  295.                       '+' : result:= result + mdl_result ;

  296.                       '-' : result:= -result + mdl_result ;
  297.                    End ;
  298.                      //清空中间结果和中间运算符
  299.                       mdl_result:= 0;

  300.                       mdl_opre:= ' ';
  301.                 End;
  302.    End;

  303.    If  (result = 0 )and ((opre = ' ')or(opre = '')) Then
  304.        Begin
  305.           result:= temp;

  306.           temp:= 0;
  307.        End ;

  308.   opre:='+';

  309.   edt1.Text:= '' ;

  310.   edt2.Text:= FloatToStr(result) ;
  311. End;

  312. //取反键 (±)
  313. procedure TForm1.btn19Click(Sender: TObject);
  314. Begin
  315.   If (Length(edt1.Text)>0) and(edt1.Text<> '0') Then    //如果edt1.Text为不等于'0' 的字符串,则将字符串转化成数字再根据是否有运算符进取反及其他更改
  316.       Begin
  317.         temp:= StrToFloat(edt1.Text);

  318.         Case opre of

  319.             ' ' : temp:= -temp;    //按下过'='键后 ,就没有运算符了

  320.             '+' : opre:= '-';      //temp前的运算符为'+'

  321.             '-' : opre:= '+';      // temp前的运算符为'-'
  322.             //temp前的运算符为'×'
  323.             '*' : Case  mdl_opre of
  324.                       //按下过'='或'C'键后
  325.                      ' ' :  Begin
  326.                              mdl_opre:= '-';

  327.                              mdl_result:= 0 ;
  328.                             End;

  329.                      '+' : mdl_opre:= '-';

  330.                      '-' : mdl_opre:= '+';
  331.                   Else  //开机就直接进行乘法运算,此时mdl_opre的值为未知值
  332.                       Begin
  333.                          mdl_opre:= '-';

  334.                          mdl_result:= 0;
  335.                       End;
  336.                   End;

  337.             //temp前的运算符为'÷'
  338.             '/' : Case  mdl_opre of
  339.                         //按下过'='或'C'键后
  340.                      ' ' :  Begin
  341.                              mdl_opre:= '-';

  342.                              mdl_result:= 0
  343.                             End;

  344.                      '+' : mdl_opre:= '-';

  345.                      '-' : mdl_opre:= '+';
  346.                   Else  //开机就直接进行乘法运算,此时mdl_opre的值为未知值
  347.                      Begin
  348.                         mdl_opre:= '-';

  349.                         mdl_result:= 0 ;
  350.                      End;

  351.                   End;

  352.         Else
  353.             temp:= -temp;    //刚开机未输入运算符 ,则opre为的值未知

  354.          End ;


  355.       End;// edt1.Text等于其他的情况忽视('',' ', '0')

  356.   edt1.Text:=FloatToStr(temp);

  357. End;

  358. //减法
  359. procedure TForm1.btn20Click(Sender: TObject);
  360. Begin
  361.   IF  Length(edt1.Text)> 0  Then

  362.       temp:= StrToFloat(edt1.Text);

  363.    Case opre of

  364.       '+': result:= result + temp;

  365.       '-': result:= result - temp;

  366.       '*': Begin
  367.              result:= result * temp;

  368.              Case  mdl_opre of

  369.                 '+' : result:= result + mdl_result ;

  370.                 '-' : result:= -result + mdl_result ;
  371.              End ;
  372.              //清空中间结果和中间运算符
  373.              mdl_result:= 0;

  374.              mdl_opre:= ' ';

  375.            End;

  376.       '/': If  temp = 0 Then

  377.                 Begin
  378.                    edt1.Text:= '除数不能为0' ;

  379.                    result:= result / temp;
  380.                 End
  381.            Else
  382.                 Begin
  383.                   result:= result / temp;

  384.                    Case  mdl_opre of

  385.                       '+' : result:= result + mdl_result ;

  386.                       '-' : result:= -result + mdl_result ;
  387.                    End ;
  388.                      //清空中间结果和中间运算符
  389.                       mdl_result:= 0;

  390.                       mdl_opre:= ' ';
  391.                 End;
  392.    End;

  393.    
  394.    If  (result = 0 )and ((opre = ' ')or(opre = '')) Then
  395.        Begin
  396.           result:= temp;

  397.           temp:= 0;
  398.        End ;

  399.   opre:='-';

  400.   edt1.Text:= '' ;

  401.   edt2.Text:= FloatToStr(result) ;
  402. End;

  403. //'='  键
  404. procedure TForm1.btn21Click(Sender: TObject);
  405. Begin
  406.   IF  Length(edt1.Text)> 0  Then

  407.       temp:= StrToFloat(edt1.Text);

  408.   Case opre of

  409.     '+': result:= result + temp;

  410.     '-': result:= result - temp;

  411.     '*':  Begin
  412.              result:= result * temp;

  413.              Case  mdl_opre of

  414.                 '+' : result:= result + mdl_result ;

  415.                 '-' : result:= -result + mdl_result ;
  416.              End ;
  417.              //清空中间结果和中间运算符
  418.              mdl_result:= 0;

  419.              mdl_opre:= ' ';

  420.            End;

  421.      '/': If  temp = 0 Then

  422.                 Begin
  423.                    edt1.Text:= '除数不能为0' ;

  424.                    result:= result / temp;
  425.                 End
  426.            Else
  427.                 Begin
  428.                   result:= result / temp;

  429.                    Case  mdl_opre of

  430.                       '+' : result:= result + mdl_result ;

  431.                       '-' : result:= -result + mdl_result ;
  432.                    End ;
  433.                      //清空中间结果和中间运算符
  434.                       mdl_result:= 0;

  435.                       mdl_opre:= ' ';
  436.                 End;
  437.          //避免连续按”=” 后出现不良结果
  438.     ' ': If (edt2.Text <>' ')or(edt2.Text <>'')  Then

  439.              result:= StrToFloat(edt1.Text)
  440.           Else

  441.             result:=0;

  442.   Else  //开机直接按”=”的结果
  443.       If (edt1.Text ='')or (edt1.Text =' ')  Then

  444.           result:=0
  445.       Else
  446.           result:= temp;
  447.   End;
  448.    //最终结果显示
  449.     //edt1.Text:= '0';
  450.   edt2.Text:= FloatToStr(result);

  451.   edt1.Text:= FloatToStr(result);

  452.   //按下'='键后运算符号及中间值全部清除
  453.   opre:= ' ';

  454.   mdl_opre:= ' ';

  455.   mdl_result:= 0;

  456.   result:= 0;

  457.   temp:= 0;

  458. End ;

  459. //设置edt1,edt2从右边开始显示
  460. procedure TForm1.FormCreate(Sender: TObject);
  461. Begin
  462.   SetWindowLong(edt1.Handle,GWL_STYLE,GetWindowLong(edt1.Handle,GWL_STYLE)or ES_RIGHT);

  463.   edt1.Invalidate;

  464.   SetWindowLong(edt2.Handle,GWL_STYLE,GetWindowLong(edt2.Handle,GWL_STYLE)or ES_RIGHT);

  465.   edt2.Invalidate;
  466. End;

  467. End.
复制代码


计算器.JPG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-24 11:50:49 | 显示全部楼层
牛皮大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 09:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表