Kyaria

Perish all Demons!

Feb 28th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Ghettorechner
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.  
  18.             textBox1.Enabled = false;
  19.         }
  20.  
  21.         private void button7_Click(object sender, EventArgs e)
  22.         {
  23.             if (sender == button1) { textBox1.Text += "7"; } // 7
  24.             else if (sender == button2) { textBox1.Text += "8"; } // 8
  25.             else if (sender == button3) { textBox1.Text += "9"; } // 9
  26.             else if (sender == button4) { textBox1.Text += "4"; } // 4
  27.             else if (sender == button5) { textBox1.Text += "5"; } // 5
  28.             else if (sender == button6) { textBox1.Text += "6"; } // 6
  29.             else if (sender == button7) { textBox1.Text += "1"; } // 1
  30.             else if (sender == button8) { textBox1.Text += "2"; } // 2
  31.             else if (sender == button9) { textBox1.Text += "3"; } // 3
  32.             else if (sender == button10) { textBox1.Text += "0"; } // 0
  33.             else if (sender == button11) { textBox1.Text += "."; } // .
  34.             else if (sender == button12) { textBox1.Clear(); } // AC
  35.             else if (sender == button13) { textBox1.Text += "+"; } // +
  36.             else if (sender == button14) { textBox1.Text += "*"; } // *
  37.             else if (sender == button16) { textBox1.Text += "-"; } // -
  38.             else if (sender == button17) { textBox1.Text += "/"; } // /
  39.             else if (sender == button18) { textBox1.Text += "("; } // (
  40.             else if (sender == button19) { textBox1.Text += ")"; } // )
  41.         }
  42.  
  43.         private string calc_code(string calc_code_one)
  44.         {
  45.             double TheAnswer = sub_code(calc_code_one);
  46.             return TheAnswer.ToString();
  47.         }
  48.  
  49.         private void pressed_enter(object sender, EventArgs e)
  50.         {
  51.             //textBox1.Text = calc_code(textBox1.Text);
  52.             textBox1.Text = RemoveTheDemons(textBox1.Text);
  53.         }
  54.  
  55.         private string RemoveTheDemons(string calc_code_one)
  56.         {
  57.             //string[] demons = calc_code.Split('(', ')');
  58.  
  59.             while (calc_code_one.Contains('(') && calc_code_one.Contains(')'))
  60.             {
  61.                 int openindex = 0;
  62.                 int closedindex = 0;
  63.  
  64.                 for (int i = 0; i < calc_code_one.Length; i++)
  65.                 {
  66.                     if (calc_code_one[i] == '(')
  67.                     {
  68.                         openindex = i;
  69.                     }
  70.                     if (calc_code_one[i] == ')')
  71.                     {
  72.                         closedindex = i;
  73.  
  74.                         calc_code_one = calc_code_one.Remove(openindex, closedindex - openindex + 1).Insert(openindex, PerishTheDemons(openindex, closedindex, calc_code_one));
  75.  
  76.                         break;
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             if (calc_code_one[0] == '-') // Kümmert sich um ein - am Anfang
  82.             {
  83.                 calc_code_one = '0' + calc_code_one;
  84.             }
  85.  
  86.             return calc_code(calc_code_one); ;
  87.         }
  88.  
  89.         private string PerishTheDemons(int open, int close, string calc_code_one)
  90.         {
  91.             string DemonsAnswer = calc_code(calc_code_one.Substring(open + 1, close - open - 1));
  92.  
  93.             return DemonsAnswer;
  94.         }
  95.  
  96.         private double sub_code(string calc_code_one)
  97.         {
  98.             string[] calc_array = calc_code_one.Split('-');
  99.             double stage_one = add_code(calc_array[0]);
  100.  
  101.             for (int i = 1; i < calc_array.Length; i++)
  102.             {
  103.                 stage_one = stage_one - add_code(calc_array[i]);
  104.             }
  105.  
  106.             return stage_one;
  107.         }
  108.  
  109.         private double add_code(string calc_code_one)
  110.         {
  111.             string[] calc_array = calc_code_one.Split('+');
  112.             double stage_one = mult_code(calc_array[0]);
  113.  
  114.             for (int i = 1; i < calc_array.Length; i++)
  115.             {
  116.                 stage_one = stage_one + mult_code(calc_array[i]);
  117.             }
  118.  
  119.             return stage_one;
  120.         }
  121.  
  122.         private double mult_code(string calc_code_one)
  123.         {
  124.             string[] calc_array = calc_code_one.Split('*');
  125.             double stage_one = dev_code(calc_array[0]);
  126.  
  127.             for (int i = 1; i < calc_array.Length; i++)
  128.             {
  129.                 stage_one = stage_one * dev_code(calc_array[i]);
  130.             }
  131.  
  132.             return stage_one;
  133.         }
  134.  
  135.         private double dev_code(string calc_code_one)
  136.         {
  137.             string[] calc_array = calc_code_one.Split('/');
  138.             double stage_one = Convert.ToDouble(calc_array[0]);
  139.  
  140.             for (int i = 1; i < calc_array.Length; i++)
  141.             {
  142.                 stage_one = stage_one / Convert.ToDouble(calc_array[i]);
  143.             }
  144.  
  145.             return stage_one;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment