Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace NewCalculator
  17. {
  18. public partial class MainWindow : Window
  19. {
  20. static int[] numbersArray = new int[10];
  21. static string[] operatorsArray = new string[9];
  22.  
  23. static string storageVariable;
  24. static int numbersCounter = 0;
  25. static int operatorsCounter = 0;
  26. static int total = 0;
  27. static bool totalled = false;
  28.  
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. }
  33.  
  34. private void One_Click(object sender, RoutedEventArgs e)
  35. {
  36. if (totalled == true)
  37. {
  38. Display.Content = "";
  39. totalled = false;
  40. }
  41. Display.Content += "1";
  42. storageVariable += "1";
  43. }
  44. private void Two_Click(object sender, RoutedEventArgs e)
  45. {
  46. if (totalled == true)
  47. {
  48. Display.Content = "";
  49. totalled = false;
  50. }
  51. Display.Content += "2";
  52. storageVariable += "2";
  53. }
  54. private void Three_Click(object sender, RoutedEventArgs e)
  55. {
  56. if (totalled == true)
  57. {
  58. Display.Content = "";
  59. totalled = false;
  60. }
  61. Display.Content += "3";
  62. storageVariable += "3";
  63. }
  64. private void Four_Click(object sender, RoutedEventArgs e)
  65. {
  66. if (totalled == true)
  67. {
  68. Display.Content = "";
  69. totalled = false;
  70. }
  71. Display.Content += "4";
  72. storageVariable += "4";
  73. }
  74. private void Five_Click(object sender, RoutedEventArgs e)
  75. {
  76. if (totalled == true)
  77. {
  78. Display.Content = "";
  79. totalled = false;
  80. }
  81. Display.Content += "5";
  82. storageVariable += "5";
  83. }
  84. private void Six_Click(object sender, RoutedEventArgs e)
  85. {
  86. if (totalled == true)
  87. {
  88. Display.Content = "";
  89. totalled = false;
  90. }
  91. Display.Content += "6";
  92. storageVariable += "6";
  93. }
  94. private void Seven_Click(object sender, RoutedEventArgs e)
  95. {
  96. if (totalled == true)
  97. {
  98. Display.Content = "";
  99. totalled = false;
  100. }
  101. Display.Content += "7";
  102. storageVariable += "7";
  103. }
  104. private void Eight_Click(object sender, RoutedEventArgs e)
  105. {
  106. if (totalled == true)
  107. {
  108. Display.Content = "";
  109. totalled = false;
  110. }
  111. Display.Content += "8";
  112. storageVariable += "8";
  113. }
  114. private void Nine_Click(object sender, RoutedEventArgs e)
  115. {
  116. if (totalled == true)
  117. {
  118. Display.Content = "";
  119. totalled = false;
  120. }
  121. Display.Content += "9";
  122. storageVariable += "9";
  123. }
  124. private void Zero_Click(object sender, RoutedEventArgs e)
  125. {
  126. if (totalled == true)
  127. {
  128. Display.Content = "";
  129. totalled = false;
  130. }
  131. Display.Content += "0";
  132. storageVariable += "0";
  133. }
  134. private void Add_Click(object sender, RoutedEventArgs e)
  135. {
  136. setNumber(storageVariable);
  137. setOperator("+");
  138. Display.Content += "+";
  139. }
  140. private void Subtract_Click(object sender, RoutedEventArgs e)
  141. {
  142. setNumber(storageVariable);
  143. setOperator("-");
  144. Display.Content += "-";
  145. }
  146. private void Multiply_Click(object sender, RoutedEventArgs e)
  147. {
  148. setNumber(storageVariable);
  149. setOperator("*");
  150. Display.Content += "x";
  151. }
  152. private void Divide_Click(object sender, RoutedEventArgs e)
  153. {
  154. setNumber(storageVariable);
  155. setOperator("/");
  156. Display.Content += "/";
  157. }
  158. private void Equal_Click(object sender, RoutedEventArgs e)
  159. {
  160. setNumber(storageVariable);
  161. for (int i = 0; i < operatorsCounter; i++)
  162. {
  163. if (operatorsArray[i] == "+" && i == 0)
  164. {
  165. total = numbersArray[i] + numbersArray[i + 1];
  166. }
  167. else if (operatorsArray[i] == "+")
  168. {
  169. total = total + numbersArray[i + 1];
  170. }
  171. else if (operatorsArray[i] == "-" && i == 0)
  172. {
  173. total = numbersArray[i] - numbersArray[i + 1];
  174. }
  175. else if (operatorsArray[i] == "-")
  176. {
  177. total = total - numbersArray[i + 1];
  178. }
  179. else if (operatorsArray[i] == "*" && i == 0)
  180. {
  181. total = numbersArray[i] * numbersArray[i + 1];
  182. }
  183. else if (operatorsArray[i] == "*")
  184. {
  185. total = total * numbersArray[i + 1];
  186. }
  187. else if (operatorsArray[i] == "/" && i == 0)
  188. {
  189. total = numbersArray[i] / numbersArray[i + 1];
  190. }
  191. else if (operatorsArray[i] == "/")
  192. {
  193. total = total / numbersArray[i + 1];
  194. }
  195. }
  196. Display.Content = total;
  197. numbersArray = null;
  198. operatorsArray = null;
  199. storageVariable = null;
  200. numbersCounter = 0;
  201. operatorsCounter = 0;
  202. total = 0;
  203. totalled = true;
  204. }
  205. static void setNumber(String Number)
  206. {
  207. numbersArray[numbersCounter] = Convert.ToInt16(Number);
  208. storageVariable = null;
  209. numbersCounter++;
  210. }
  211. static void setOperator(String Op)
  212. {
  213. operatorsArray[operatorsCounter] = Op;
  214. operatorsCounter++;
  215. }
  216. private void AC_Click(object sender, RoutedEventArgs e)
  217. {
  218. Display.Content = "";
  219. numbersArray = null;
  220. operatorsArray = null;
  221. storageVariable = null;
  222. numbersCounter = 0;
  223. operatorsCounter = 0;
  224. total = 0;
  225. }
  226. }
  227. }
  228.  
  229. <Window x:Class="NewCalculator.MainWindow"
  230. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  231. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  232. Title="Calculator" Height="200" Width="195" ResizeMode="NoResize">
  233. <Grid>
  234. <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" />
  235. <Button x:Name="One" Content="1" HorizontalAlignment="Left" Height="20" Margin="10,40,0,0" VerticalAlignment="Top" Width="20" Click="One_Click"/>
  236. <Button x:Name="Two" Content="2" HorizontalAlignment="Left" Height="20" Margin="40,40,0,0" VerticalAlignment="Top" Width="20" Click="Two_Click"/>
  237. <Button x:Name="Three" Content="3" HorizontalAlignment="Left" Height="20" Margin="70,40,0,0" VerticalAlignment="Top" Width="20" Click="Three_Click"/>
  238. <Button x:Name="Four" Content="4" HorizontalAlignment="Left" Height="20" Margin="10,70,0,0" VerticalAlignment="Top" Width="20" Click="Four_Click"/>
  239. <Button x:Name="Five" Content="5" HorizontalAlignment="Left" Height="20" Margin="40,70,0,0" VerticalAlignment="Top" Width="20" Click="Five_Click"/>
  240. <Button x:Name="Six" Content="6" HorizontalAlignment="Left" Height="20" Margin="70,70,0,0" VerticalAlignment="Top" Width="20" Click="Six_Click"/>
  241. <Button x:Name="Seven" Content="7" HorizontalAlignment="Left" Height="20" Margin="10,100,0,0" VerticalAlignment="Top" Width="20" Click="Seven_Click"/>
  242. <Button x:Name="Eight" Content="8" HorizontalAlignment="Left" Height="20" Margin="40,100,0,0" VerticalAlignment="Top" Width="20" Click="Eight_Click"/>
  243. <Button x:Name="Nine" Content="9" HorizontalAlignment="Left" Height="20" Margin="70,100,0,0" VerticalAlignment="Top" Width="20" Click="Nine_Click"/>
  244. <Button x:Name="Zero" Content="0" HorizontalAlignment="Left" Height="20" Margin="40,130,0,0" VerticalAlignment="Top" Width="20" Click="Zero_Click"/>
  245. <Button x:Name="Add" Content="+" HorizontalAlignment="Left" Height="20" Margin="100,40,0,0" VerticalAlignment="Top" Width="20" Click="Add_Click"/>
  246. <Button x:Name="Subtract" Content="-" HorizontalAlignment="Left" Height="20" Margin="130,40,0,0" VerticalAlignment="Top" Width="20" Click="Subtract_Click"/>
  247. <Button x:Name="Multiply" Content="x" HorizontalAlignment="Left" Height="20" Margin="100,70,0,0" VerticalAlignment="Top" Width="20" Click="Multiply_Click"/>
  248. <Button x:Name="Divide" Content="/" HorizontalAlignment="Left" Height="20" Margin="130,70,0,0" VerticalAlignment="Top" Width="20" Click="Divide_Click"/>
  249. <Button x:Name="Equal" Content="=" HorizontalAlignment="Left" Height="20" Margin="100,100,0,0" VerticalAlignment="Top" Width="20" Click="Equal_Click"/>
  250. <Button x:Name="AC" Content="AC" Height="20" Margin="130,100,17,0" VerticalAlignment="Top" Width="30" Click="AC_Click"/>
  251. </Grid>
  252. </Window>
  253.  
  254. if (totalled == true)
  255. {
  256. Display.Content = "";
  257. totalled = false;
  258. }
  259. Display.Content += "1";
  260. storageVariable += "1";
  261.  
  262. if (totalled == true)
  263. {
  264. Display.Content = "";
  265. totalled = false;
  266. }
  267. Display.Content += "2";
  268. storageVariable += "2";
  269.  
  270. private void AC_Click(object sender, RoutedEventArgs e)
  271. {
  272. Display.Content = "";
  273. numbersArray = null;//null pointer exception on pressing any of the arithmetic buttons
  274. operatorsArray = null;//null pointer exception on pressing any of the arithmetic buttons
  275. storageVariable = null;//null pointer exception on pressing any of the numeric buttons
  276. numbersCounter = 0;
  277. operatorsCounter = 0;
  278. total = 0;
  279. }
  280.  
  281. static void setNumber(String Number)
  282. {
  283. numbersArray[numbersCounter] = Convert.ToInt16(Number);//no bounds check
  284. storageVariable = null;//possible null pointer when clicking arithmetic button
  285. numbersCounter++;
  286. }
  287. static void setOperator(String Op)
  288. {
  289. operatorsArray[operatorsCounter] = Op;//no bounds check
  290. operatorsCounter++;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement