| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1801 人关注过本帖
标题:计算器?
取消只看楼主 加入收藏
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
结帖率:99.34%
收藏
已结贴  问题点数:20 回复次数:19 
计算器?
用一个纯代码写了一个很简单的计算器 各位大牛帮看看
我是边做边看书 要用什么控件就去了解他 所以没怎么有全局观
看看我做这个能否改进下 我总觉得ComboBox那个地方可以改进 但又不知道咋弄更好
程序代码
程序代码:
CLEAR ALL
CLOSE DATABASES all
frmMain = CREATEOBJECT("C_Form")
frmMain.show
READ events
CLEAR
RETURN

DEFINE CLASS C_Form as Form
    caption = "简单计算器"
    width = 600
    height = 400
    autocenter = .T.
   

    ADD OBJECT Label1 as label WITH caption = "第一个数", Width = 60, height = 25
    ADD OBJECT Label2 as label WITH caption = "第二个数", Width = 60, height = 25
    ADD OBJECT Label3 as label WITH caption = "最终结果", Width = 60, height = 25
    ADD OBJECT Text1 as TextBox WITH value = "", Width = 100, height = 25
    ADD OBJECT Text2 as TextBox WITH value = "", Width = 100, height = 25
    ADD OBJECT Text3 as TextBox WITH value = "", Width = 100, height = 25
    ADD OBJECT Button1 as CommandButton WITH caption = "计算", height = 25
    ADD OBJECT ComboBox1 as ComboBox WITH caption = "计算方式", Height = 20, Width = 50
                        

    PROCEDURE Arrange
        WITH ThisForm.Label1
           .Top = 100
           .Left = 50
        ENDWITH
        WITH ThisForm.Text1
           .Top = ThisForm.Label1.Top
           .Left = ThisForm.Label1.Left + ThisForm.Label1.Width + 5
        ENDWITH
        WITH ThisForm.Label2
           .Top = ThisForm.Label1.Top
           .Left = ThisForm.Width / 2
        ENDWITH
         WITH ThisForm.Text2
           .Top = ThisForm.Label1.Top
           .Left = ThisForm.Label2.Left + ThisForm.Label2.Width + 5
        ENDWITH
        WITH ThisForm.Label3
           .Top = ThisForm.Label1.Top + ThisForm.Label1.Height + 50
           .Left = ThisForm.Width / 2 - .Width
        ENDWITH
        WITH ThisForm.Text3
           .Top = ThisForm.Label3.Top
           .Left = ThisForm.Label3.Left + ThisForm.Label3.Width + 5
        ENDWITH
        WITH ThisForm.Button1
           .Top = ThisForm.Label3.Top + ThisForm.Label3.Height + 30
           .Left = ThisForm.Width / 2 - .Width / 2
        ENDWITH
         WITH Top = ThisForm.Label3.Top

           .Left = ThisForm.Label3.Left - .Width - 5
           .style = 0
           .inputmask = .displayvalue
           .list(1) = '+'
           .list(2) = '-'
           .list(3) = '*'
           .list(4) = '/'
           .ColumnCount = 1
           .ColumnLines = .F.
        ENDWITH
    ENDPROC
   

    PROCEDURE Activate
    ThisForm.Arrange
    ENDPROC
   

    PROCEDURE Resize
    ThisForm.Arrange
    ENDPROC
   

    PROCEDURE destroy
        CLEAR EVENTS
    ENDPROC
   

     PROCEDURE ComboBox1.Click
        LOCAL  i as Integer
        WITH FOR  i = 1 TO 4
                IF .selected(i)
                   .DisplayValue = .list(i)
                ENDIF
        NEXT
        ENDWITH       

    ENDPROC
   

    PROCEDURE Button1.CLick
    LOCAL v as Double
    WITH ThisForm
    DO CASE

       CASE  = '+'
          v = VAL(.Text1.Value) + VAL(.Text2.Value)
       CASE  = '-'
          v = VAL(.Text1.Value) - VAL(.Text2.Value)
       CASE  = '*'
          v = VAL(.Text1.Value) * VAL(.Text2.Value)
       CASE  = '/'
          IF VAL(.Text2.Value) != 0
             v = VAL(.Text1.Value) / VAL(.Text2.Value)
          ELSE
              MESSAGEBOX("零做除数是非法的", 0)
              return
          ENDIF
       OTHERWISE 

              MESSAGEBOX("错误的计算方法", 0)
              return
    ENDCASE
    ENDWITH

        WITH ThisForm
            .Text3.Value = STR(v,10, 2)
        ENDWITH
    ENDPROC
   

ENDDEFINE

附上 连编的app
(1.91 KB)

附图:
图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: 计算器 
2012-09-14 23:26
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
回复 2楼 sdta
clear 和 return 重复了吗?

梅尚程荀
马谭杨奚







                                                       
2012-09-14 23:35
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
好像clear是清屏 clear all是释放内存 回第一工作区吧
不太清楚VFP中的return

梅尚程荀
马谭杨奚







                                                       
2012-09-14 23:44
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
回复 6楼 sdta
咦 去试了下 还真是的
难道ComboBox能自动执行Click 命令?

梅尚程荀
马谭杨奚







                                                       
2012-09-14 23:48
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
以下是引用sdta在2012-9-14 23:46:18的发言:

[计算]按钮可以不要
    PROCEDURE Button1.CLick
可改为:
    PROCEDURE combobox1.interactivechange
前面相应部分可以不要

这样貌似出不了结果。。。。

梅尚程荀
马谭杨奚







                                                       
2012-09-14 23:54
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
回复 12楼 sdta
吼吼 改了下确实是这样的程序简洁多了
程序代码:
CLEAR ALL
frmMain = CREATEOBJECT("C_Form")
frmMain.show
READ events
RETURN

DEFINE CLASS C_Form as Form
    caption = "简单计算器"
    width = 600
    height = 400
    autocenter = .T.
   

    ADD OBJECT Label1 as label WITH caption = "第一个数", Width = 60, height = 25
    ADD OBJECT Label2 as label WITH caption = "第二个数", Width = 60, height = 25
    ADD OBJECT Label3 as label WITH caption = "最终结果", Width = 60, height = 25
    ADD OBJECT Text1 as TextBox WITH value = "", Width = 100, height = 25
    ADD OBJECT Text2 as TextBox WITH value = "", Width = 100, height = 25
    ADD OBJECT Text3 as TextBox WITH value = "", Width = 100, height = 25
    ADD OBJECT ComboBox1 as ComboBox WITH caption = "计算方式", Height = 20, Width = 50
                        

    PROCEDURE Arrange
        WITH ThisForm.Label1
           .Top = 100
           .Left = 50
        ENDWITH
        WITH ThisForm.Text1
           .Top = ThisForm.Label1.Top
           .Left = ThisForm.Label1.Left + ThisForm.Label1.Width + 5
        ENDWITH
        WITH ThisForm.Label2
           .Top = ThisForm.Label1.Top
           .Left = ThisForm.Width / 2
        ENDWITH
         WITH ThisForm.Text2
           .Top = ThisForm.Label1.Top
           .Left = ThisForm.Label2.Left + ThisForm.Label2.Width + 5
        ENDWITH
        WITH ThisForm.Label3
           .Top = ThisForm.Label1.Top + ThisForm.Label1.Height + 50
           .Left = ThisForm.Width / 2 - .Width
        ENDWITH
        WITH ThisForm.Text3
           .Top = ThisForm.Label3.Top
           .Left = ThisForm.Label3.Left + ThisForm.Label3.Width + 5
        ENDWITH
         WITH Top = ThisForm.Label3.Top

           .Left = ThisForm.Label3.Left - .Width - 5
           .style = 0
           .inputmask = .displayvalue
           .rowsource = [+,-,*,/]
           .rowsourcetype = 1
           .ColumnCount = 1
           .ColumnLines = .F.
        ENDWITH
    ENDPROC
   

    PROCEDURE Activate
    ThisForm.Arrange
    ENDPROC
   

    PROCEDURE Resize
    ThisForm.Arrange
    ENDPROC
   

    PROCEDURE destroy
        CLEAR EVENTS
    ENDPROC

    PROCEDURE ComboBox1.InterActiveChange
    LOCAL v as Double
    WITH ThisForm
    DO CASE

       CASE  = '+'
          v = VAL(.Text1.Value) + VAL(.Text2.Value)
       CASE  = '-'
          v = VAL(.Text1.Value) - VAL(.Text2.Value)
       CASE  = '*'
          v = VAL(.Text1.Value) * VAL(.Text2.Value)
       CASE  = '/'
          IF VAL(.Text2.Value) != 0
             v = VAL(.Text1.Value) / VAL(.Text2.Value)
          ELSE
              MESSAGEBOX("零做除数是非法的", 0)
              return
          ENDIF
       OTHERWISE 

              MESSAGEBOX("错误的计算方法", 0)
              return
    ENDCASE
            .Text3.Value = STR(v,10, 2)
        ENDWITH
    ENDPROC
     

ENDDEFINE


梅尚程荀
马谭杨奚







                                                       
2012-09-15 00:06
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
回复 15楼 sdta
厉害
PROCEDURE init 默认包含 PROCEDURE Activate 和 PROCEDURE Resize吗?

梅尚程荀
马谭杨奚







                                                       
2012-09-15 00:13
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
以下是引用sdta在2012-9-15 00:08:46的发言:


INIT中的代码可以放在各自的ADD中设置
ADD是什么东西 ?

梅尚程荀
马谭杨奚







                                                       
2012-09-15 00:15
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
以下是引用有容就大在2012-9-15 00:15:58的发言:

ADD是什么东西 ?

这个问的太傻了 哈哈 原来就是ADD OBJECT


梅尚程荀
马谭杨奚







                                                       
2012-09-15 00:20
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
S版照你指导的修改了 最终结果这样
程序代码:
frmMain = CREATEOBJECT("C_Form")
frmMain.show
READ events
RETURN

DEFINE CLASS C_Form as Form
    caption = "简单计算器"
    width = 600
    height = 400
    autocenter = .T.
   

    ADD OBJECT Label1 as label WITH caption="第一个数", Width=60,height=25,Top=100,Left=50
    ADD OBJECT Label2 as label WITH caption="第二个数", Width=60,height=25,Top=ThisForm.Label1.Top,Left=ThisForm.Width/2
    ADD OBJECT Label3 as label WITH caption= "最终结果",Width=60,height=25
    ADD OBJECT Text1 as TextBox WITH value="",Width=100,height=25,Top=ThisForm.Label1.Top,Left=ThisForm.Label1.Left+ThisForm.Label1.Width+5
    ADD OBJECT Text2 as TextBox WITH value="",Width=100,height=25,Top=ThisForm.Label1.Top,Left =ThisForm.Label2.Left+ThisForm.Label2.Width+5
    ADD OBJECT Text3 as TextBox WITH value="",Width=100,height=25
    ADD OBJECT ComboBox1 as ComboBox WITH caption = "计算方式", Height = 20, Width = 50

    PROCEDURE init
        WITH ThisForm.Label3
           .Top = ThisForm.Label1.Top + ThisForm.Label1.Height + 50
           .Left = ThisForm.Width / 2 - .Width
        ENDWITH
        WITH ThisForm.Text3
           .Top = ThisForm.Label3.Top
           .Left = ThisForm.Label3.Left + ThisForm.Label3.Width + 5
        ENDWITH
         WITH Top = ThisForm.Label3.Top
           .Left = ThisForm.Label3.Left - .Width - 5
           .style = 0
           .inputmask = .displayvalue
           .rowsource=[+,-,*,/] &&
           .rowsourcetype=1 &&
           .ColumnCount = 1
           .ColumnLines = .F.
        ENDWITH
    ENDPROC

 

    PROCEDURE destroy
        CLEAR EVENTS
    ENDPROC

    PROCEDURE combobox1.interactivechange
    LOCAL v as Double
    WITH ThisForm
       DO CASE
          CASE  = '+'
               v = VAL(.Text1.Value) + VAL(.Text2.Value)
          CASE  = '-'
               v = VAL(.Text1.Value) - VAL(.Text2.Value)
          CASE  = '*'
               v = VAL(.Text1.Value) * VAL(.Text2.Value)
          CASE  = '/'
               IF VAL(.Text2.Value) != 0
                  v = VAL(.Text1.Value) / VAL(.Text2.Value)
               ELSE
                  MESSAGEBOX("零做除数是非法的", 0)
                  return
               ENDIF
          OTHERWISE
              MESSAGEBOX("错误的计算方法", 0)
              return
       ENDCASE
         .Text3.Value = str(v,10, 2)
    ENDWITH
    ENDPROC
   

ENDDEFINE
非常感谢你的指教


梅尚程荀
马谭杨奚







                                                       
2012-09-15 00:26
快速回复:计算器?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069740 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved