Hyluss

Hylus's calc

Apr 7th, 2017
298
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.77 KB | None | 0 0
  1. /*
  2. Kalkulator:
  3. http://www.mediafire.com/file/qm2w2dxl8z0ribw/Hylus%27s_calc.exe
  4. */
  5.  
  6.  
  7. //////////
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Windows.Forms;
  17.  
  18. namespace interfejs_uzytkownika_kalkulator
  19. {
  20.     public partial class Form1 : Form
  21.     {
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.         /// ////////////////////
  27.         ///
  28.         bool podwojnyznak = false;
  29.         bool kropka2 = false;
  30.         bool nowe = false;
  31.         bool nowe_po_wyniku = false;
  32.         String znak = "";
  33.         String p = "", d = "";
  34.         int indeksznaku;
  35.         double wynik;
  36.         Kalkulator calc = new Kalkulator();        
  37.  
  38.         private void button_Click(object sender, EventArgs e)
  39.         {
  40.             if (nowe.Equals(true))
  41.             {
  42.                 textBox1.Text = "";
  43.                 nowe = false;
  44.             }
  45.             int liczba = 0;
  46.             String a = (sender as Button).Text;
  47.             if (Int32.TryParse(a, out liczba))
  48.             {
  49.                 if (nowe_po_wyniku == true) textBox1.Clear();
  50.  
  51.                 if (a.Equals("00"))
  52.                 {
  53.                     String l = textBox1.Text;
  54.  
  55.                     if (kropka2 == true)
  56.                     {
  57.                         string czlon2 = l.Substring(l.IndexOf(znak) + 1);
  58.                         if (czlon2 == "")
  59.                         {
  60.                             textBox1.Text += "0";
  61.                         }
  62.                         else
  63.                         {
  64.                             int indeks = czlon2.IndexOf("0");
  65.                             int indeks2 = czlon2.IndexOf(",");
  66.                             if (indeks == 0 && indeks2 != 1)
  67.                             { }
  68.                             else
  69.                             {
  70.                                 textBox1.Text += a;
  71.                             }
  72.                         }
  73.                     }
  74.  
  75.                     else if (l.Length > 0 && kropka2 == false)
  76.                     {
  77.                         ///
  78.                         int indeks = l.IndexOf("0");
  79.                         int indeks2 = l.IndexOf(",");
  80.                         if (indeks == 0 && indeks2 != 1)
  81.                         { }
  82.                         else
  83.                         {
  84.                             textBox1.Text += a;
  85.                         }
  86.                         ////
  87.                         //  textBox1.Text += "00";
  88.                     }
  89.                     int test = l.IndexOf(",");
  90.                     if (test == -1) { }
  91.                     else if (test == -1 && kropka2 == false)
  92.                     {
  93.                         textBox1.Text += 0;
  94.                     }
  95.  
  96.                 }
  97.                 else
  98.                 {
  99.                     if (a == "0")
  100.                     {
  101.                         string l = (textBox1.Text);
  102.                         if (kropka2 == true)
  103.                         {
  104.                             string czlon2 = l.Substring(l.IndexOf(znak) + 1);
  105.                             if (czlon2 == "")
  106.                             {
  107.                                 textBox1.Text += "0";
  108.                             }
  109.                             else
  110.                             {
  111.                                 int indeks = czlon2.IndexOf("0");
  112.                                 int indeks2 = czlon2.IndexOf(",");
  113.                                 if (indeks == 0 && indeks2 != 1)
  114.                                 { }
  115.                                 else
  116.                                 {
  117.                                     textBox1.Text += a;
  118.                                 }
  119.                             }
  120.                         }
  121.                         else if (l.Length > 0 && kropka2 == false)
  122.                         {
  123.                             int indeks = l.IndexOf("0");
  124.                             int indeks2 = l.IndexOf(",");
  125.                             if (indeks == 0 && indeks2 != 1)
  126.                             { }
  127.                             else
  128.                             {
  129.                                 textBox1.Text += a;
  130.                             }
  131.                             //string sprawdz = l.Substring(0, indeks);
  132.                         }
  133.                         else
  134.                         {
  135.                             textBox1.Text += a;
  136.                         }
  137.                     }
  138.                     else
  139.                     {
  140.                         if (textBox1.Text == "0")
  141.                         {
  142.                             textBox1.Text = a;
  143.                         }
  144.                         else
  145.                         {
  146.                             textBox1.Text += a;
  147.                         }
  148.                         nowe_po_wyniku = false;
  149.                     }
  150.                 }
  151.             }
  152.             else if (a.Equals("DEL"))
  153.             {
  154.                 textBox1.Clear();
  155.                 nowe_po_wyniku = false;
  156.                 nowe = false;
  157.             }
  158.             else if (a.Equals(","))
  159.             {
  160.                 int xxx = 0;
  161.                 foreach (char x in (textBox1.Text))
  162.                 {
  163.                     if (x == ',') xxx++;
  164.                 }
  165.                 if (kropka2 == false && xxx == 1)
  166.                 {
  167.                 }
  168.  
  169.                 else if (kropka2 == true && xxx == 2)
  170.                 {
  171.                 }
  172.                 else
  173.                 {
  174.                     if (nowe_po_wyniku == true)
  175.                     {
  176.                         textBox1.Text = "0,";
  177.                         nowe_po_wyniku = false;
  178.                         //xxx += 1;
  179.                     }
  180.                     else if (textBox1.TextLength == 0)
  181.                     {
  182.                         textBox1.Text = "0,";
  183.                         // xxx += 1;
  184.                     }
  185.                     else
  186.                     {
  187.                         if (kropka2 == true)
  188.                         {
  189.                             String l = (textBox1.Text).Substring((textBox1.Text).IndexOf(znak) + 1);
  190.                             if (l.Length == 0)
  191.                             {
  192.                                 textBox1.Text += "0,";
  193.                             }
  194.                             else
  195.                             {
  196.                                 textBox1.Text += ",";
  197.                             }
  198.                             kropka2 = false;
  199.                         }
  200.                         else
  201.                         {
  202.                             String sprawdz = textBox1.Text;
  203.                             int ss = sprawdz.IndexOf(",");
  204.                             if (ss == -1)
  205.                             {
  206.                                 textBox1.Text += ",";
  207.                             }
  208.                         }
  209.                     }
  210.                 }
  211.             }
  212.             else if (a == "+" || a == "-" || a == "/" || a == "*")
  213.             {
  214.                 if (kropka2 == true)
  215.                 {
  216.                     a = "=";
  217.                     podwojnyznak = true;
  218.                 }
  219.                 else
  220.                 {
  221.                     if (textBox1.TextLength == 0)
  222.                     {
  223.                         textBox1.Text = "0" + a;
  224.                     }
  225.                     else
  226.                     {
  227.                         textBox1.Text += a;
  228.                     }
  229.                     if (znak == "") znak = a;
  230.                     indeksznaku = (textBox1.Text).LastIndexOf(znak);
  231.                     p = (textBox1.Text).Substring(0, indeksznaku);
  232.                     d = (textBox1.Text).Substring(indeksznaku + 1);
  233.                     nowe_po_wyniku = false;
  234.                     kropka2 = true;
  235.                 }
  236.             }
  237.  
  238.            // else if (a == "=")
  239.             if (a == "=" && (textBox1.Text).Length>0)
  240.             {
  241.                 p = (textBox1.Text).Substring(0, indeksznaku);
  242.                // if (indeksznaku == 1) { }
  243.                 //else
  244.                 d = (textBox1.Text).Substring(indeksznaku + 1);
  245.                 if (podwojnyznak == true) d = p;
  246.                 if (p.Length > 0 && d.Length > 0)
  247.                 {
  248.                     if (znak == "+")
  249.                     {
  250.                         wynik = Kalkulator.Dodawanie(p, d);
  251.                         textBox1.Text = wynik.ToString();
  252.                     }
  253.  
  254.                     if (znak == "-")
  255.                     {
  256.                         wynik = Kalkulator.Odejmowanie(p, d);
  257.                         textBox1.Text = wynik.ToString();
  258.                     }
  259.  
  260.                     if (znak == "*")
  261.                     {
  262.                         wynik = Kalkulator.Mnozenie(p, d);
  263.                         textBox1.Text = wynik.ToString();
  264.                     }
  265.  
  266.                     if (znak == "/")
  267.                     {
  268.                         try
  269.                         {
  270.                             wynik = Kalkulator.Dzielenie(p, d);
  271.                             textBox1.Text = wynik.ToString();
  272.                         }
  273.                         catch (DivideByZeroException)
  274.                         {
  275.                             textBox1.Text = "SYNTAX_ERROR";
  276.                         }
  277.                     }
  278.                 }
  279.                 //    textBox1.Text = wynik.ToString();
  280.                 nowe_po_wyniku = true;
  281.                 kropka2 = false;
  282.                 znak = "";
  283.                 podwojnyznak = false;
  284.                 indeksznaku = 0;
  285.                 int xxdd = (textBox1.Text).LastIndexOf("+");
  286.                 if (textBox1.Text.Length == (textBox1.Text).LastIndexOf("+") || textBox1.Text.Length == (textBox1.Text).LastIndexOf("-") || textBox1.Text.Length == (textBox1.Text).LastIndexOf("/") || textBox1.Text.Length == (textBox1.Text).LastIndexOf("*"))
  287.                 {
  288.                     textBox1.Text = textBox1.Text.Substring(0, (textBox1.Text).LastIndexOf("+"));
  289.                 }
  290.             }
  291.  
  292.             else if (a.Equals("sqrt"))
  293.             {
  294.                 double lic;
  295.                 if ((double.TryParse(textBox1.Text, out lic)))
  296.                 {
  297.                     try
  298.                     {
  299.                         wynik = Kalkulator.SQRT(lic);
  300.                         textBox1.Text = wynik.ToString();
  301.                     }
  302.                     catch (ArithmeticException)
  303.                     {
  304.                         textBox1.Text = "niepoprawne dane";
  305.                     }
  306.                 }
  307.             }
  308.             else if (a.Equals("+/-"))
  309.             {
  310.                 double lic;
  311.                 if ((double.TryParse(textBox1.Text, out lic)))
  312.                 {
  313.  
  314.                     wynik = Kalkulator.OdwrocLiczbe(lic);
  315.                     textBox1.Text = wynik.ToString();
  316.                 }
  317.             }
  318.             else if (a.Equals("1/x"))
  319.             {
  320.                 double lic;
  321.                 if ((double.TryParse(textBox1.Text, out lic)))
  322.                 {
  323.  
  324.                     wynik = Kalkulator.OdwrotnoscLiczby(lic);
  325.                     textBox1.Text = wynik.ToString();
  326.                 }
  327.             }
  328.  
  329.  
  330.             else if (a.Equals("M+"))
  331.             {
  332.                 double lic;
  333.                 if ((double.TryParse(textBox1.Text, out lic)))
  334.                 {
  335.                     calc.Wynik += lic;
  336.                     textBox1.Text = wynik.ToString();
  337.                 }
  338.             }
  339.  
  340.  
  341.             else if (a.Equals("M-"))
  342.             {
  343.                 double lic;
  344.                 if ((double.TryParse(textBox1.Text, out lic)))
  345.                 {
  346.                     calc.Wynik -= lic;
  347.                     textBox1.Text = wynik.ToString();
  348.                 }
  349.             }
  350.  
  351.             else if (a.Equals("MR"))
  352.             {
  353.                 if (kropka2 == true) textBox1.Text += (calc.Wynik).ToString();
  354.                 else textBox1.Text = (calc.Wynik).ToString();
  355.             }
  356.  
  357.             else if (a.Equals("MC"))
  358.             {
  359.                 calc.Wynik = 0;
  360.             }            
  361.         }
  362.     };
  363. }
  364.  
  365.  
  366. /////////////////////////
  367.  
  368.  
  369.  
  370. using System;
  371. using System.Collections.Generic;
  372. using System.Linq;
  373. using System.Text;
  374.  
  375. namespace interfejs_uzytkownika_kalkulator
  376. {
  377.     class Kalkulator
  378.     {
  379.         double wynik;
  380.        
  381.         public Kalkulator()
  382.         {
  383.             this.wynik = 0;
  384.         }
  385.  
  386.         public double Wynik { get { return wynik; } set { wynik = value; } }        
  387.  
  388.         public static double Dodawanie(String a, String b)
  389.         {
  390.             Double la = double.Parse(a);
  391.             Double lb = double.Parse(b);
  392.             return la + lb;
  393.         }
  394.  
  395.         public static double Odejmowanie(String a, String b)
  396.         {
  397.             Double la = double.Parse(a);
  398.             Double lb = double.Parse(b);
  399.             return la - lb;
  400.         }
  401.  
  402.         public static double Mnozenie(String a, String b)
  403.         {
  404.             Double la = double.Parse(a);
  405.             Double lb = double.Parse(b);
  406.             return la * lb;
  407.         }
  408.  
  409.         public static double Dzielenie(String a, String b)
  410.         {
  411.             Double la = double.Parse(a);
  412.             Double lb = double.Parse(b);
  413.             if (lb == 0) throw new DivideByZeroException();
  414.  
  415.             return la / lb;
  416.         }
  417.  
  418.         public static double SQRT(double a)
  419.         {
  420.             if (a < 0) throw new ArithmeticException();
  421.             double wynik = Math.Sqrt(a);
  422.             return Math.Sqrt(a);
  423.         }
  424.  
  425.         public static double OdwrocLiczbe(double a)
  426.         {
  427.             return a = (-a);
  428.         }
  429.  
  430.         public static double OdwrotnoscLiczby(double a)
  431.         {
  432.             return (1/a);
  433.         }
  434.  
  435.        
  436.  
  437.     };
  438. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment