Tein laskimen C#:lla ja WPF:llä. En katsonut esimerkkejä siitä miten laskin kannattaa tehdä, vaan koitin tehdä sen täysin tyhjästä. Koodi on sekavaa, mutta toimii.
MainWindow.xaml:
<Window x:Class="calculator.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:calculator" mc:Ignorable="d" Title="Laskin" Height="319" Width="346"> <Grid HorizontalAlignment="Left" Width="346" Background="#FFC9C9C9" Margin="0,0,-8,0"> <TextBox x:Name="calc_Table" HorizontalAlignment="Left" Height="25" Margin="40,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="255" FontFamily="Arial" FontSize="18" TextAlignment="Right" Text="0" IsReadOnly="True"/> <Button x:Name="btn_3" Content="3" HorizontalAlignment="Left" Margin="170,175,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_2" Content="2" HorizontalAlignment="Left" Margin="105,175,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick" RenderTransformOrigin="-0.4,-1.925"/> <Button x:Name="btn_1" Content="1" HorizontalAlignment="Left" Margin="40,175,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_6" Content="6" HorizontalAlignment="Left" Margin="170,130,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_5" Content="5" HorizontalAlignment="Left" Margin="105,130,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" RenderTransformOrigin="-0.45,-0.625" Click="OnClick"/> <Button x:Name="btn_4" Content="4" HorizontalAlignment="Left" Margin="40,130,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_9" Content="9" HorizontalAlignment="Left" Margin="170,85,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" RenderTransformOrigin="0.333,-1.35" Click="OnClick"/> <Button x:Name="btn_8" Content="8" HorizontalAlignment="Left" Margin="105,85,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_7" Content="7" HorizontalAlignment="Left" Margin="40,85,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_0" Content="0" HorizontalAlignment="Left" Margin="105,220,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_dot" Content="." HorizontalAlignment="Left" Margin="40,220,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FFB6B6B6" Click="OnClick"/> <Button x:Name="btn_ce" Content="CE" HorizontalAlignment="Left" Margin="235,40,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FF9B9B9B" Click="OnClick" RenderTransformOrigin="0.717,-0.575"/> <Button x:Name="btn_plus" Content="+" HorizontalAlignment="Left" Margin="235,220,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FF75A079" Click="OnClick"/> <Button x:Name="btn_minus" Content="-" HorizontalAlignment="Left" Margin="235,175,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FF75A079" Click="OnClick" RenderTransformOrigin="1.883,0.6"/> <Button x:Name="btn_kerto" Content="*" HorizontalAlignment="Left" Margin="235,130,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FF75A079" Click="OnClick"/> <Button x:Name="btn_jako" Content="/" HorizontalAlignment="Left" Margin="235,85,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FF75A079" Click="OnClick" RenderTransformOrigin="0.533,-0.525"/> <Button x:Name="btn_calculate" Content="=" HorizontalAlignment="Left" Margin="170,220,0,0" VerticalAlignment="Top" Width="60" Height="40" FontSize="20" FontFamily="Showcard Gothic" Background="#FF5EBF70" Click="OnClick" RenderTransformOrigin="0.25,0.45"/> </Grid> </Window>
MainWindow.xaml.cs:
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 calculator { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void OnClick(object sender, RoutedEventArgs e) { if(calc_Table.Text == "Epäonnistui") { calc_Table.Text = "0"; } if(sender == btn_0) { addNumber(0); } else if (sender == btn_1) { addNumber(1); } else if (sender == btn_2) { addNumber(2); } else if (sender == btn_3) { addNumber(3); } else if (sender == btn_4) { addNumber(4); } else if (sender == btn_5) { addNumber(5); } else if (sender == btn_6) { addNumber(6); } else if (sender == btn_7) { addNumber(7); } else if (sender == btn_8) { addNumber(8); } else if (sender == btn_9) { addNumber(9); } else if(sender == btn_dot) { addDot(); } else if(sender == btn_ce) { delete(); } else if(sender == btn_plus) { check(); calc_Table.Text += " + "; } else if(sender == btn_minus) { check(); calc_Table.Text += " - "; } else if (sender == btn_jako) { check(); calc_Table.Text += " / "; } else if (sender == btn_kerto) { check(); calc_Table.Text += " * "; } else if(sender == btn_calculate) { if(calc_Table.Text.Substring(calc_Table.Text.Length - 1, 1) == " ") { calc_Table.Text = "Epäonnistui"; } else { calculate(); } } } private void check() { if (calc_Table.Text.Split(' ').Length == 3) calculate(); } private void addNumber(int number) { if (calc_Table.Text == "0") calc_Table.Text = ""; calc_Table.Text += number; } private void addDot() { if (calc_Table.Text.Length >= 1 && !(calc_Table.Text.Contains('.'))) calc_Table.Text += ','; } private void delete() { if(calc_Table.Text.Substring(calc_Table.Text.Length - 1, 1) == " ") { calc_Table.Text = calc_Table.Text.Substring(0, calc_Table.Text.Length - 3); } else if (calc_Table.Text.Length <= 1) { calc_Table.Text = calc_Table.Text.Substring(0, calc_Table.Text.Length - 1); calc_Table.Text += 0; } else { calc_Table.Text = calc_Table.Text.Substring(0, calc_Table.Text.Length - 1); } } private void calculate() { StringBuilder sb = new StringBuilder(); string[] numbersAndOperators = calc_Table.Text.Split(' '); double help; for (int i = 0; i < numbersAndOperators.Length; i++) { if(numbersAndOperators[i] == "*") { help = double.Parse(numbersAndOperators[i - 1]) * double.Parse(numbersAndOperators[i + 1]); numbersAndOperators[i] = help.ToString(); numbersAndOperators[i + 1] = "s"; numbersAndOperators[i - 1] = "s"; foreach (var place in numbersAndOperators) { if (place != "s") sb.Append(place + " "); } calc_Table.Text = sb.ToString().Substring(0, sb.ToString().Length - 1); } else if(numbersAndOperators[i] == "/") { help = double.Parse(numbersAndOperators[i - 1]) / double.Parse(numbersAndOperators[i + 1]); numbersAndOperators[i] = help.ToString(); numbersAndOperators[i + 1] = "s"; numbersAndOperators[i - 1] = "s"; foreach (var place in numbersAndOperators) { if (place != "s") sb.Append(place + " "); } calc_Table.Text = sb.ToString().Substring(0, sb.ToString().Length - 1); } else if(numbersAndOperators[i] == "+") { help = double.Parse(numbersAndOperators[i - 1]) + double.Parse(numbersAndOperators[i + 1]); numbersAndOperators[i] = help.ToString(); numbersAndOperators[i + 1] = "s"; numbersAndOperators[i - 1] = "s"; foreach (var place in numbersAndOperators) { if (place != "s") sb.Append(place + " "); } calc_Table.Text = sb.ToString().Substring(0, sb.ToString().Length - 1); } else if (numbersAndOperators[i] == "-") { help = double.Parse(numbersAndOperators[i - 1]) - double.Parse(numbersAndOperators[i + 1]); numbersAndOperators[i] = help.ToString(); numbersAndOperators[i + 1] = "s"; numbersAndOperators[i - 1] = "s"; foreach (var place in numbersAndOperators) { if (place != "s") sb.Append(place + " "); } calc_Table.Text = sb.ToString().Substring(0, sb.ToString().Length - 1); } } } } }
Aihe on jo aika vanha, joten et voi enää vastata siihen.