Advertisement
Guest User

Untitled

a guest
May 2nd, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.01 KB | None | 0 0
  1. open System
  2. open System.Drawing
  3. open System.Windows.Forms
  4.  
  5. let mutable num1 = "NULL"
  6. let mutable num2 = "NULL"
  7. let mutable oper = "NULL"
  8. let mutable flag="-"
  9.  
  10. let Math(op:string, box:TextBox) =
  11.  flag<-"+"
  12.  if (num1="NULL") then num1<-box.Text
  13.  else num2<-box.Text
  14.  if (oper="NULL") then oper<-op else do
  15.  match oper with
  16.  | "+" when (num1<>"NULL" && num2<>"NULL") -> num1 <- (Double.Parse(num1)+Double.Parse(num2)).ToString()  
  17.  | "-" when (num1<>"NULL" && num2<>"NULL") -> num1 <- (Double.Parse(num1)-Double.Parse(num2)).ToString()
  18.  | "*" when (num1<>"NULL" && num2<>"NULL") -> num1 <- (Double.Parse(num1)*Double.Parse(num2)).ToString()
  19.  | "/" when (num1<>"NULL" && num2<>"NULL") -> if (num2<>"0") then do
  20.    num1 <- (Double.Parse(num1) / Double.Parse(num2)).ToString() else num1<-"ERROR"
  21.  | "=" -> num2 <-"NULL"
  22.  box.Text<-num1
  23.  oper<-op
  24. 0
  25.  
  26. //Создание кнопки с нужными параметрами, обработка событий
  27. let createButton (text:string,form:Form, box:TextBox, pos1, pos2) =
  28.  let btn = new Button()
  29.  btn.Text <- text
  30.  btn.Size <- new Size(40,40)
  31.  btn.Location <- new System.Drawing.Point(pos1, pos2)
  32.  btn.Click.Add(fun _ -> if (flag="+") then do
  33.                            flag<-"-"
  34.                            box.Text<-""
  35.                            box.AppendText(text)
  36.                          else if (box.Text="0") then do
  37.                          box.Text<-""
  38.                          box.AppendText(text)
  39.                         else if (box.Text.Length<18)
  40.                          then
  41.                          box.AppendText(text))
  42.                                      
  43.  form.Controls.Add(btn)
  44.  btn
  45.  
  46. //Cоздаёv кнопки с цифрами и операциями
  47. let Buttons (form:Form, box:TextBox) =
  48.  let mutable multiplier = 2
  49.  for x=7 to 9 do
  50.       if (x<>7) then
  51.        createButton(x.ToString(),form, box, 20+20*multiplier, 50)
  52.        multiplier<-multiplier*2
  53.       else do createButton(x.ToString(),form, box, 20, 50)
  54.  multiplier <- 2
  55.  for x=4 to 6 do
  56.      if (x<>4) then
  57.        createButton(x.ToString(),form, box, 20+20*multiplier, 100)
  58.        multiplier<-multiplier*2
  59.      else do createButton(x.ToString(),form, box, 20, 100)
  60.  multiplier <- 2
  61.  for x=1 to 3 do
  62.     if (x<>1) then
  63.       createButton(x.ToString(),form, box, 20+20*multiplier, 150)
  64.       multiplier<-multiplier*2
  65.     else do createButton(x.ToString(),form, box, 20, 150)
  66.  
  67.  let zeroBtn = new Button(Text="0", Size = new Size (40,45), Location = new Point(20,190))
  68.  zeroBtn.Click.Add(fun _ -> if (flag="+") then do
  69.                                           flag="-"
  70.                                           box.Text <- "0" else
  71.   if (box.Text<>"0" && box.Text.Length<18) then box.AppendText("0"))
  72.  form.Controls.Add(zeroBtn)
  73.  
  74.  let eqBtn = new Button(Text="=", Size = new Size (40,45), Location = new Point (100, 190))
  75.  eqBtn.Click.Add(fun _ -> Math("=", box))
  76.  form.Controls.Add(eqBtn)
  77.  
  78.  let clearBtn = new Button(Text="CE", Size = new Size (40,45), Location = new Point(170,230))
  79.  clearBtn.Click.Add(fun _ ->
  80.  num1 <- "NULL"
  81.  num2 <- "NULL"
  82.  oper <- "NULL"
  83.  box.Text <-"0"
  84.  flag<-"+"
  85.   )
  86.  form.Controls.Add(clearBtn)
  87.  
  88.  let dotBtn = new Button(Text=",", Size = new Size(40,45), Location = new Point (60,190))
  89.  dotBtn.Click.Add(fun _ -> box.AppendText(","))
  90.  form.Controls.Add(dotBtn)
  91.  
  92.  let sinBtn = new Button(Text="Sin", Size = new Size(40, 45), Location = new Point (210, 50))
  93.  sinBtn.Click.Add(fun _ ->
  94.  box.Text <- System.Math.Sin(Double.Parse(box.Text)).ToString()
  95.  )
  96.  form.Controls.Add(sinBtn)
  97.  
  98.  let cosBtn = new Button(Text="Cos", Size = new Size(40, 45), Location = new Point (210, 95))
  99.  cosBtn.Click.Add(fun _ ->
  100.  box.Text <- System.Math.Cos(Double.Parse(box.Text)).ToString()
  101.  )
  102.  form.Controls.Add(cosBtn)
  103.  
  104.  let tgBtn = new Button(Text="Tg", Size = new Size(40, 45), Location = new Point (210, 140))
  105.  tgBtn.Click.Add(fun _ ->
  106.  box.Text <- System.Math.Tan(Double.Parse(box.Text)).ToString()
  107.  )
  108.  form.Controls.Add(tgBtn)
  109.  
  110.  let divideBtn = new Button(Text="/", Size = new Size (40,45), Location = new Point (170,185))
  111.  divideBtn.Click.Add(fun _ -> Math("/", box))
  112.  form.Controls.Add(divideBtn)
  113.  let plusBtn = new Button(Text="+", Size = new Size (40,45), Location = new Point (170,50))
  114.  plusBtn.Click.Add(fun _ -> Math("+", box))
  115.  form.Controls.Add(plusBtn)
  116.  let minusBtn = new Button(Text="-", Size = new Size (40,45), Location = new Point (170,95))
  117.  minusBtn.Click.Add(fun _ -> Math("-", box))
  118.  form.Controls.Add(minusBtn)
  119.  let multiBtn = new Button(Text="*", Size = new Size (40,45), Location = new Point (170,140))
  120.  multiBtn.Click.Add(fun _ -> Math("*", box))
  121.  form.Controls.Add(multiBtn)
  122.  0
  123.  
  124. let Main =
  125.     let form = new Form()
  126.     form.Height <- 320
  127.     form.Width <-280
  128.     form.FormBorderStyle <- FormBorderStyle.FixedDialog
  129.     let box = new TextBox(Size=new Size(210,50))
  130.     box.Location = new System.Drawing.Point(100,80)
  131.     box.AppendText("0")
  132.     form.Controls.Add(box)
  133.     Buttons(form, box)
  134.    
  135.    
  136.     Application.Run(form)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement