Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace NewCalculator
- {
- public partial class MainWindow : Window
- {
- static int[] numbersArray = new int[10];
- static string[] operatorsArray = new string[9];
- static string storageVariable;
- static int numbersCounter = 0;
- static int operatorsCounter = 0;
- static int total = 0;
- static bool totalled = false;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void One_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "1";
- storageVariable += "1";
- }
- private void Two_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "2";
- storageVariable += "2";
- }
- private void Three_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "3";
- storageVariable += "3";
- }
- private void Four_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "4";
- storageVariable += "4";
- }
- private void Five_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "5";
- storageVariable += "5";
- }
- private void Six_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "6";
- storageVariable += "6";
- }
- private void Seven_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "7";
- storageVariable += "7";
- }
- private void Eight_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "8";
- storageVariable += "8";
- }
- private void Nine_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "9";
- storageVariable += "9";
- }
- private void Zero_Click(object sender, RoutedEventArgs e)
- {
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "0";
- storageVariable += "0";
- }
- private void Add_Click(object sender, RoutedEventArgs e)
- {
- setNumber(storageVariable);
- setOperator("+");
- Display.Content += "+";
- }
- private void Subtract_Click(object sender, RoutedEventArgs e)
- {
- setNumber(storageVariable);
- setOperator("-");
- Display.Content += "-";
- }
- private void Multiply_Click(object sender, RoutedEventArgs e)
- {
- setNumber(storageVariable);
- setOperator("*");
- Display.Content += "x";
- }
- private void Divide_Click(object sender, RoutedEventArgs e)
- {
- setNumber(storageVariable);
- setOperator("/");
- Display.Content += "/";
- }
- private void Equal_Click(object sender, RoutedEventArgs e)
- {
- setNumber(storageVariable);
- for (int i = 0; i < operatorsCounter; i++)
- {
- if (operatorsArray[i] == "+" && i == 0)
- {
- total = numbersArray[i] + numbersArray[i + 1];
- }
- else if (operatorsArray[i] == "+")
- {
- total = total + numbersArray[i + 1];
- }
- else if (operatorsArray[i] == "-" && i == 0)
- {
- total = numbersArray[i] - numbersArray[i + 1];
- }
- else if (operatorsArray[i] == "-")
- {
- total = total - numbersArray[i + 1];
- }
- else if (operatorsArray[i] == "*" && i == 0)
- {
- total = numbersArray[i] * numbersArray[i + 1];
- }
- else if (operatorsArray[i] == "*")
- {
- total = total * numbersArray[i + 1];
- }
- else if (operatorsArray[i] == "/" && i == 0)
- {
- total = numbersArray[i] / numbersArray[i + 1];
- }
- else if (operatorsArray[i] == "/")
- {
- total = total / numbersArray[i + 1];
- }
- }
- Display.Content = total;
- numbersArray = null;
- operatorsArray = null;
- storageVariable = null;
- numbersCounter = 0;
- operatorsCounter = 0;
- total = 0;
- totalled = true;
- }
- static void setNumber(String Number)
- {
- numbersArray[numbersCounter] = Convert.ToInt16(Number);
- storageVariable = null;
- numbersCounter++;
- }
- static void setOperator(String Op)
- {
- operatorsArray[operatorsCounter] = Op;
- operatorsCounter++;
- }
- private void AC_Click(object sender, RoutedEventArgs e)
- {
- Display.Content = "";
- numbersArray = null;
- operatorsArray = null;
- storageVariable = null;
- numbersCounter = 0;
- operatorsCounter = 0;
- total = 0;
- }
- }
- }
- <Window x:Class="NewCalculator.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Calculator" Height="200" Width="195" ResizeMode="NoResize">
- <Grid>
- <Label x:Name="Display" Content="" HorizontalAlignment="Left" Height="32" Margin="10,3,0,0" VerticalAlignment="Top" Width="138" BorderThickness="1" RenderTransformOrigin="0.543,1.375" />
- <Button x:Name="One" Content="1" HorizontalAlignment="Left" Height="20" Margin="10,40,0,0" VerticalAlignment="Top" Width="20" Click="One_Click"/>
- <Button x:Name="Two" Content="2" HorizontalAlignment="Left" Height="20" Margin="40,40,0,0" VerticalAlignment="Top" Width="20" Click="Two_Click"/>
- <Button x:Name="Three" Content="3" HorizontalAlignment="Left" Height="20" Margin="70,40,0,0" VerticalAlignment="Top" Width="20" Click="Three_Click"/>
- <Button x:Name="Four" Content="4" HorizontalAlignment="Left" Height="20" Margin="10,70,0,0" VerticalAlignment="Top" Width="20" Click="Four_Click"/>
- <Button x:Name="Five" Content="5" HorizontalAlignment="Left" Height="20" Margin="40,70,0,0" VerticalAlignment="Top" Width="20" Click="Five_Click"/>
- <Button x:Name="Six" Content="6" HorizontalAlignment="Left" Height="20" Margin="70,70,0,0" VerticalAlignment="Top" Width="20" Click="Six_Click"/>
- <Button x:Name="Seven" Content="7" HorizontalAlignment="Left" Height="20" Margin="10,100,0,0" VerticalAlignment="Top" Width="20" Click="Seven_Click"/>
- <Button x:Name="Eight" Content="8" HorizontalAlignment="Left" Height="20" Margin="40,100,0,0" VerticalAlignment="Top" Width="20" Click="Eight_Click"/>
- <Button x:Name="Nine" Content="9" HorizontalAlignment="Left" Height="20" Margin="70,100,0,0" VerticalAlignment="Top" Width="20" Click="Nine_Click"/>
- <Button x:Name="Zero" Content="0" HorizontalAlignment="Left" Height="20" Margin="40,130,0,0" VerticalAlignment="Top" Width="20" Click="Zero_Click"/>
- <Button x:Name="Add" Content="+" HorizontalAlignment="Left" Height="20" Margin="100,40,0,0" VerticalAlignment="Top" Width="20" Click="Add_Click"/>
- <Button x:Name="Subtract" Content="-" HorizontalAlignment="Left" Height="20" Margin="130,40,0,0" VerticalAlignment="Top" Width="20" Click="Subtract_Click"/>
- <Button x:Name="Multiply" Content="x" HorizontalAlignment="Left" Height="20" Margin="100,70,0,0" VerticalAlignment="Top" Width="20" Click="Multiply_Click"/>
- <Button x:Name="Divide" Content="/" HorizontalAlignment="Left" Height="20" Margin="130,70,0,0" VerticalAlignment="Top" Width="20" Click="Divide_Click"/>
- <Button x:Name="Equal" Content="=" HorizontalAlignment="Left" Height="20" Margin="100,100,0,0" VerticalAlignment="Top" Width="20" Click="Equal_Click"/>
- <Button x:Name="AC" Content="AC" Height="20" Margin="130,100,17,0" VerticalAlignment="Top" Width="30" Click="AC_Click"/>
- </Grid>
- </Window>
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "1";
- storageVariable += "1";
- if (totalled == true)
- {
- Display.Content = "";
- totalled = false;
- }
- Display.Content += "2";
- storageVariable += "2";
- private void AC_Click(object sender, RoutedEventArgs e)
- {
- Display.Content = "";
- numbersArray = null;//null pointer exception on pressing any of the arithmetic buttons
- operatorsArray = null;//null pointer exception on pressing any of the arithmetic buttons
- storageVariable = null;//null pointer exception on pressing any of the numeric buttons
- numbersCounter = 0;
- operatorsCounter = 0;
- total = 0;
- }
- static void setNumber(String Number)
- {
- numbersArray[numbersCounter] = Convert.ToInt16(Number);//no bounds check
- storageVariable = null;//possible null pointer when clicking arithmetic button
- numbersCounter++;
- }
- static void setOperator(String Op)
- {
- operatorsArray[operatorsCounter] = Op;//no bounds check
- operatorsCounter++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement