Re: Калькулятор
				Добавлено: 31 Март 2008, 14:09
				 Alexey5634888
				MEMBER('calculiator.clw')           ! This is a MEMBER module
Calculiator          PROCEDURE                    ! Declare Procedure
Number     string(@s16)         !!!Needs to be a string for proper '0' display
Operand    REAL                 !The first operand for +,-,*,/,^ operations
memory1     REAL                 !The value contained in memory1
Operation  REAL                 !The field number of the operation key
NewNumber  BYTE                 !True following = or %
Decimal    BYTE                 !True after pressing decimal point key
Digit      BYTE                 !Numeric digit represented by number key
DecimalFlg BYTE                 !!!decimal flag
Calculator WINDOW('Калькулятор'),AT(,,116,192),FONT('MS Sans Serif',8,,),CENTER,SYSTEM,GRAY, |
         DOUBLE,AUTO
       BUTTON('&0'),AT(67,125,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad0),USE(?Zero)
       BUTTON('&1'),AT(31,109,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad1),USE(?One)
       BUTTON('&2'),AT(49,109,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad2),USE(?Two)
       BUTTON('&3'),AT(67,109,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad3),USE(?Three)
       BUTTON('&4'),AT(31,93,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad4),USE(?Four)
       BUTTON('&5'),AT(49,93,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad5),USE(?Five)
       BUTTON('&6'),AT(67,93,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad6),USE(?Six)
       BUTTON('&7'),AT(31,77,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad7),USE(?Seven)
       BUTTON('&8'),AT(49,77,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad8),USE(?Eight)
       BUTTON('&9'),AT(67,77,16,14),FONT('MS Sans Serif',8,,),KEY(KeyPad9),USE(?Nine)
       BUTTON('.'),AT(49,125,16,14),FONT('MS Sans Serif',8,,),KEY(DecimalKey),USE(?Decimal)
       BUTTON('CE'),AT(9,125,16,14),FONT('MS Sans Serif',8,,),TIP('Очистить'),USE(?Clear)
       BUTTON('p'),AT(49,56,16,14),FONT('Symbol',10,,),TIP('Pi'),USE(?Pi)
       BUTTON('+/-'),AT(31,125,16,14),FONT('MS Sans Serif',8,,),TIP('Изменить знак'),USE(?Sign)
       BUTTON(' x<178>'),AT(67,41,16,14),FONT('MS Sans Serif',8,,),TIP('Возведение в квадрат'),USE(?Square)
       BUTTON('Ц'),AT(67,56,16,14),FONT('Symbol',8,,),TIP('Квадратный корень'),USE(?Root)
       BUTTON('1/x'),AT(13,56,16,14),FONT('MS Sans Serif',8,,),TIP('Обратная величина'),USE(?Reciprical)
       BUTTON('10X'),AT(31,56,16,14),FONT('MS Sans Serif',8,,),TIP('Умножить на 10'),USE(?TenTimes)
       BUTTON('%'),AT(90,41,18,16),FONT('MS Sans Serif',8,,),TIP('Процент'),USE(?Percent)
       BUTTON(' lg'),AT(31,41,16,14),FONT('MS Sans Serif',8,,),TIP('Десятичный логарифм'),USE(?Log)
       BUTTON(' ln'),AT(13,41,16,14),FONT('MS Sans Serif',8,,),TIP('Натуральный логарифм'),USE(?Ln)
       BUTTON('MR'),AT(9,77,16,14),FONT('MS Sans Serif',8,,),TIP('Вызвать из памяти'),USE(?Recall)
       BUTTON('M'),AT(9,93,16,14),FONT('MS Sans Serif',8,,),TIP('Сохранить память'),USE(?Store)
       BUTTON(' M+'),AT(9,109,16,14),FONT('MS Sans Serif',8,,),TIP('Прибавить в память'),USE(?Accumulate)
       BUTTON('+'),AT(90,116,18,46),FONT('MS Sans Serif',8,,),TIP('Сложение'),KEY(PlusKey),USE(?Add)
       BUTTON('-'),AT(90,97,18,16),FONT('MS Sans Serif',8,,),TIP('Вычитание'),KEY(MinusKey),USE(?Subtract)
       BUTTON('x'),AT(90,78,18,16),FONT('MS Sans Serif',8,,),TIP('Умножение'),KEY(AstKey),USE(?Multiply)
       BUTTON('/'),AT(90,60,18,16),FONT(,8,,),TIP('Деление'),KEY(SlashKey),USE(?Divide)
       BUTTON(' exp'),AT(49,41,16,14),FONT('MS Sans Serif',8,,),TIP('Экспонента'),USE(?Power)
       BUTTON('='),AT(10,143,77,19),FONT('MS Sans Serif',8,,),KEY(EnterKey),USE(?Equal)
       BUTTON,AT(37,169,71,20),TIP('Выход'),KEY(AltF4),USE(?Exit)
       BUTTON,AT(11,169,22,20),TIP('Копирование в КлипБоард'),KEY(AltC),USE(?COPY),ICON(ICON:Copy)
       BOX,AT(5,35,107,130),USE(?Box1),ROUND,COLOR(0FFFFH),FILL(0808080H)
       PANEL,AT(28,74,58,67),USE(?Panel1),FILL(0FFFFFFH),BEVEL(1,-1)
       STRING('Память:'),AT(4,22,30,10),LEFT
       STRING(@S20),AT(34,22,79,10),FONT(,8,,),USE(memory1),LEFT
       ENTRY(@s20),AT(6,4,104,15),FONT('MS Sans Serif',11,,FONT:bold),USE(Number),RIGHT(1),INS
     END
  CODE                                            ! Begin processed code
    OPEN(Calculator)                        !Open the calculator window
  NewNumber = True                        !Start with a new number
  Decimal = False                         !No decimal point has been entered
  Operation = ?Equal                      !No outstanding operation
  DecimalFlg = TRUE                       !!!Set flag
 
  ACCEPT                                  !Enable windows and wait for an event
    DISPLAY
    IF ACCEPTED()                         ! user has caused accepted event
      CASE ACCEPTED()                     ! Jump to the accepted field
      OF ?EXIT                            !!!Exit Button
        BREAK                             !!!
      OF ?COPY                            !!!Copy to clipboard
        NewNumber = False                 !!!
        SETCLIPBOARD(NUMBER)              !!!
      OF ?Recall                          ! For the Recall memory1 key 
        Number = memory1                   !  Set number to memory1
        cycle
      OF ?Zero TO ?Nine                   ! For a numeric key
        Digit = ACCEPTED() - ?Zero        !  The digit is the field number
         IF NewNumber                     !  For the first digit
          Number = Digit                  !   Set the number to the digit
          NewNumber = False               !   Turn NewNumber flag off
          Decimal = False                 !   Turn decimal point flag off
         ELSE                             !   For any other digit
          IF Decimal                      !    For a fractional digit
            
          IF DecimalFlg                      !!!
             Number=clip(number) &'.'& Digit !!!
             DecimalFlg = false              !!!
          ELSE                               !!!
            Number=clip(number) & digit      !!!
          END                                !!!
          ELSE                            !    For an integer digit
             DecimalFlg = true
             number=clip(number)& digit
          END                             !   End the IF
        END                               !   End the IF
        CYCLE                             !   Continue number entry
      OF ?Decimal                         ! For the decimal point key
        Decimal = True                    !  Turn decimal point flag on
        DecimalFlg=TRUE                   !!!Reset Flag
        IF NEWNUMBER = TRUE               !!!No need to press 0
           NUMBER = '0'                   !!!if new number
           NEWNUMBER = FALSE              !!!Reset Flag
        END                               !!!
        CYCLE                             !  Continue number entry
      OF ?Pi                              ! For the Pi key
        Number = 3.141592654              !  Set number to Pi
        NewNumber = TRUE                  !  Start new number
        CYCLE                             !  Continue
      OF ?Clear                           ! For the ClearEntry/Clear key
        IF NewNumber THEN                 !  completed number entry
          Operation = ?Equal              !   so Clear current calculation
        END                               !  End IF
        Number = 0                        !  Clear number
        NewNumber = 0                     !  Start new number
      END                                 ! End CASE
       
      IF Operation <> ?Equal              ! Complete outstanding operations
        IF ACCEPTED() = ?Percent          ! For the percent key
          Number = Number * Operand / 100 ! Calculate % value
          IF  (Operation <> ?Add ) |      ! Check not adding
          AND (Operation <> ?Subtract ) | !  or subtractiong percentage
          AND (Operation <> ?Multiply)  | !!! Added Multiply and Divide
          AND (Operation <> ?Divide)      !!! To the percentage
            Operation = ?Equal            !   Finished operation
          END                             !  End IF
        END                               ! End IF
        CASE Operation                    !  Jump to saved operation key
        OF ?Add                           ! For Add key
          Number += Operand               !   Add number to operand
        OF ?Subtract                      !  For Subtract key
          Number = Operand - Number       !   Subtract number from operand
        OF ?Multiply                      !  For Multiply key
          Number *= Operand               !   Multiply operand by number
        OF ?Divide                        !  For Divide key
          IF Number <> 0 THEN             !  Check for divide by zero
            Number = Operand / Number     !   Divide operand by number
          END                             !  End IF
        OF ?Power                         !  For Raise to a Power key
          Number = Operand ^ Number       !   Raise operand to number power
        END                               !  End CASE
        Operation = ?Equal                !  Operation done
      END                                 ! End IF
      CASE ACCEPTED()                     ! Jump to the accepted field
      OF ?Sign                            ! For the Change Sign key
        Number *= -1                      !  Multiply by -1
      OF ?Square                          ! For the Square key
        Number *= Number                  !  Multilpy by itself
      OF ?Root                            ! For the Square Root key
        Number = SQRT(Number)             !  Find the square root
      OF ?TenTimes                        ! For the 10X key
        Number *= 10                      !  Multiply by 10
      OF ?Reciprical                      ! For the Reciprical key
        IF Number <> 0 THEN               ! Check for divide by zero
          Number = 1/Number               !  Find the reciprical
        END                               ! End IF
      OF ?Log                             ! For the logarithm key
        Number = LOG10(Number)            !  Find the base 10 logarithm
      OF ?Ln                              ! For the natural logarithm key
        Number = LOGE(Number)             !  Find the natural logarithm
      OF ?Store                           ! For the Store memory1 key
        memory1 = Number                   !  Set memory1 to number
      OF ?Accumulate                      ! For the Add to memory1 key
        memory1 += Number                  !  Add number to memory1
      OF ?Add TO ?Power                   ! For two operand operation keys
        Operation = ACCEPTED()            !  Save the operator
        Operand = Number                  !  Save the first operand
      END                                 ! End CASE
      SELECT(?Zero)                       ! Set focus to the Zero key
      NewNumber = TRUE                    ! ready for next number
      DecimalFlg = TRUE                   !!!Reset flag
    END                                   !End CASE
  END                                     !End ACCEPT
   CLOSE(Calculator)                      !Close the calculator window
   !!RNUMBER=NUMBER                         !!!
   !!RETURN(RNUMBER)                        !!!Return value
     RETURN