이번 글은 계산기를 만들었던 것에 계산 과정과 히스토리 기능을 추가했습니다.
이 글을 다음으로, 여러 연산 계산과 우선순위에 따른 계산을 추가하려고 합니다.
1. XAML 소스 코드
<Window x:Class="cal.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:cal"
mc:Ignorable="d"
Title="MainWindow" Height="704.93" Width="700">
<Grid HorizontalAlignment="Left" Width="700" Margin="0,0,-5,-1">
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="390"/>
</Grid.ColumnDefinitions>
<ListView x:Name="liststate"
BorderBrush="Gray"
Grid.Column="4"
Grid.RowSpan="7" Margin="0,0,110,-8">
</ListView>
<Label x:Name="result2"
Grid.ColumnSpan="4"
Grid.Row="0"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Top"
FontSize="30"
Foreground="Gray"/>
<Label x:Name="resultLabel"
Content="0"
Grid.ColumnSpan="4"
Grid.Row="0"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Bottom"
FontSize="100"/>
<!-- 숫자 버튼 0 -->
<Button x:Name="ZeroButton"
Grid.Column="0"
Grid.Row="6"
Content="0"
Margin="5,5,5,-13"
Grid.ColumnSpan="2"
Foreground="white"
Background="Gray"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 1 -->
<Button x:Name="OneButton"
Grid.Column="0"
Grid.Row="5"
Content="1"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 2 -->
<Button x:Name="TwoButton"
Grid.Column="1"
Grid.Row="5"
Content="2"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 3 -->
<Button x:Name="ThreeButton"
Grid.Column="2"
Grid.Row="5"
Content="3"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 4 -->
<Button x:Name="FourButton"
Grid.Column="0"
Grid.Row="4"
Content="4"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 5 -->
<Button x:Name="FiveButton"
Grid.Column="1"
Grid.Row="4"
Content="5"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 6 -->
<Button x:Name="SixButton"
Grid.Column="2"
Grid.Row="4"
Content="6"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 7 -->
<Button x:Name="SevenButton"
Grid.Column="0"
Grid.Row="3"
Content="7"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 8 -->
<Button x:Name="EightButton"
Grid.Column="1"
Grid.Row="3"
Content="8"
Foreground="white"
Background="Gray"
FontSize="30"
Margin="5,5,5,5"
Click="numberButton_Click"/>
<!-- 숫자 버튼 9 -->
<Button x:Name="NineButton"
Grid.Column="2"
Grid.Row="3"
Content="9"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!--연산버튼 AC-->
<Button x:Name="AcButton"
Grid.Column="0"
Grid.Row="2"
Content="AC"
FontSize="30"
Margin="5,5,5,5"
Click="AcButton_Click"/>
<!-- 연산버튼 Plus/Minus -->
<Button x:Name="PlusMinusButton"
Grid.Column="1"
Grid.Row="2"
Content="+/-"
FontSize="30"
Margin="5,5,5,5"
Click="PlusMinusButton_Click"/>
<!-- 연산버튼 Percent -->
<Button x:Name="PercentButton"
Grid.Column="2"
Grid.Row="2"
Content="%"
FontSize="30"
Margin="5,5,5,5"
Click="PercentButton_Click"/>
<!-- 연산버튼 Divide -->
<Button x:Name="DivideButton"
Grid.Column="3"
Grid.Row="2"
Content="/"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Multiple -->
<Button x:Name="MultipleButton"
Grid.Column="3"
Grid.Row="3"
Content="*"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Minus -->
<Button x:Name="MinusButton"
Grid.Column="3"
Grid.Row="4"
Content="-"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Plus -->
<Button x:Name="PlusButton"
Grid.Column="3"
Grid.Row="5"
Content="+"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Dot -->
<Button x:Name="DotButton"
Grid.Column="2"
Grid.Row="6"
Content="."
Foreground="white"
Background="Gray"
FontSize="30"
Margin="5,5,5,-13"
Click="DotButton_Click"/>
<!-- 연산버튼 Equl -->
<Button x:Name="EqulButton"
Grid.Column="3"
Grid.Row="6"
Content="="
Background="Green"
Margin="5,5,5,-13"
FontSize="30"
Click="EqulButton_Click"/>
<!-- 연산버튼 Delete -->
<Button x:Name="DelButton"
Grid.Column="0"
Grid.Row="1"
Content="Del"
Margin="5,5,5,5"
FontSize="30"
Click="ExtraFunction_Click"/>
<!-- 연산버튼 Sqrt -->
<Button x:Name="SqrtButton"
Grid.Column="1"
Grid.Row="1"
Content="Sqrt"
Margin="5,5,5,5"
FontSize="30"
Click="ExtraFunction_Click"/>
<!-- 연산버튼 X^2 -->
<Button x:Name="XXButton"
Grid.Column="2"
Grid.Row="1"
Content="X^2"
Margin="5,5,5,5"
FontSize="30"
Click="ExtraFunction_Click"/>
<!-- 연산버튼 1/x -->
<Button x:Name="reverseXButton"
Grid.Column="3"
Grid.Row="1"
Content="1/x"
Margin="5,5,5,5"
FontSize="30"
Click="ExtraFunction_Click"/>
</Grid>
</Window>
2. 이벤트 함수 소스 코드
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 cal
{
///
/// MainWindow.xaml에 대한 상호 작용 논리
///
public partial class MainWindow : Window
{
double lastNumber;
double temp = 0;
SelectedOperator selectedOperator;
double result;
int flag = 0;
int flag2 = 0;
public MainWindow()
{
InitializeComponent();
//각 버튼에 대한 이벤트 함수를 안만들고 아래와 같이 해도 상관 없음.
//AcButton.Click += AcButton_Click;
}
public enum SelectedOperator
{
Addition,
Subtraction,
Multiplication,
Division
}
public class SimpleMath
{
public static double Add(double n1, double n2)
{
return n1 + n2;
}
public static double Subtract(double n1, double n2)
{
return n1 - n2;
}
public static double Multiple(double n1, double n2)
{
return n1 * n2;
}
public static double Divide(double n1, double n2)
{
if(n2==0)
{
MessageBox.Show("0으로 나눌 수 없습니다.", "Wrong Operation",
MessageBoxButton.OK, MessageBoxImage.Error);
}
return n1 / n2;
}
}
// 숫자 버튼 이벤트 함수
private void numberButton_Click(object sender, RoutedEventArgs e)
{
int selectedValue = 0;
if (sender == ZeroButton)
{
selectedValue = 0;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == OneButton)
{
selectedValue = 1;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == TwoButton)
{
selectedValue = 2;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == ThreeButton)
{
selectedValue = 3;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == FourButton)
{
selectedValue = 4;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == FiveButton)
{
selectedValue = 5;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == SixButton)
{
selectedValue = 6;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == SevenButton)
{
selectedValue = 7;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == EightButton)
{
selectedValue = 8;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == NineButton)
{
selectedValue = 9;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
}
// Plus, Minus, Multiple, Divide 4개가 있는 이벤트 함수
private void operationButton_Click(object sender, RoutedEventArgs e)
{
lastNumber = double.Parse(resultLabel.Content.ToString());
resultLabel.Content = "0";
if (sender == PlusButton)
{
selectedOperator = SelectedOperator.Addition;
result2.Content += lastNumber + " + ";
temp += lastNumber;
flag2 = 1;
}
if (sender == MinusButton)
{
selectedOperator = SelectedOperator.Subtraction;
result2.Content += lastNumber + " - ";
if(flag2==1)
{
temp += lastNumber;
flag = 0;
return;
}
}
if (sender == MultipleButton)
{
selectedOperator = SelectedOperator.Multiplication;
result2.Content += lastNumber + " * ";
}
if (sender == DivideButton)
{
selectedOperator = SelectedOperator.Division;
result2.Content += lastNumber + " / ";
}
}
private void ExtraFunction_Click(object sender, RoutedEventArgs e)
{
double lastNumber;
if (sender == DelButton)
{
if(resultLabel.Content.ToString().Length != 1)
{
resultLabel.Content = resultLabel.Content.ToString().Substring(0, resultLabel.Content.ToString().Length - 1);
}
else
{
resultLabel.Content = "0";
}
}
if (sender == SqrtButton)
{
if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
if (lastNumber < 0) return;
else
{
lastNumber = Math.Sqrt(lastNumber);
resultLabel.Content = lastNumber.ToString();
}
}
}
if (sender==XXButton)
{
if(double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber = lastNumber * lastNumber;
resultLabel.Content = lastNumber.ToString();
}
}
if (sender==reverseXButton)
{
if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber = 1 / lastNumber;
resultLabel.Content = lastNumber.ToString();
}
}
}
// AC 버튼 클릭
private void AcButton_Click(object sender, RoutedEventArgs e)
{
resultLabel.Content = "0";
flag = 0;
flag2 = 0;
temp = 0;
}
// PlusMinus 버튼 클릭
private void PlusMinusButton_Click(object sender, RoutedEventArgs e)
{
double lastNumber;
if(double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber = lastNumber * -1;
resultLabel.Content = lastNumber.ToString();
}
}
// Percent 버튼 클릭
private void PercentButton_Click(object sender, RoutedEventArgs e)
{
double lastNumber;
if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber = lastNumber * 0.01;
resultLabel.Content = lastNumber.ToString();
}
}
// Dot 버튼 클릭
private void DotButton_Click(object sender, RoutedEventArgs e)
{
Boolean a;
a = resultLabel.Content.ToString().Contains(".");
if (resultLabel.Content.ToString() == "0")
{
return;
}
else
{
if(a==false)
{
resultLabel.Content += ".";
}
}
}
// Equl 버튼 클릭
private void EqulButton_Click(object sender, RoutedEventArgs e)
{
double newNumber;
newNumber = double.Parse(resultLabel.Content.ToString()); // 연산자 후 숫자 저장
switch (selectedOperator)
{
case SelectedOperator.Addition:
if (flag == 0)
{
result = SimpleMath.Add(temp, newNumber);
flag = 1;
}
liststate.Items.Add(result2.Content.ToString() + newNumber + " = " + result.ToString());
result2.Content = "";
break;
case SelectedOperator.Subtraction:
if (flag == 0)
{
result = SimpleMath.Subtract(temp, newNumber);
flag = 1;
}
liststate.Items.Add(result2.Content.ToString() + newNumber + " = " + result.ToString());
result2.Content = "";
break;
case SelectedOperator.Multiplication:
if (flag == 0)
{
result = SimpleMath.Multiple(temp, newNumber);
flag = 1;
}
liststate.Items.Add(result2.Content.ToString() + newNumber + " = " + result.ToString());
result2.Content = "";
break;
case SelectedOperator.Division:
if (flag == 0)
{
result = SimpleMath.Divide(temp, newNumber);
flag = 1;
}
liststate.Items.Add(result2.Content.ToString() + newNumber + " = " + result.ToString());
result2.Content = "";
break;
}
resultLabel.Content = result.ToString();
temp = 0;
flag2 = 0;
}
}
}
3. 결과
'Language > C# WPF Programming' 카테고리의 다른 글
C# WPF 프로그래밍 계산기(Calculator) 프로그램 만들기 - 연산자 다중 계산 (2) | 2019.05.20 |
---|---|
C# WPF 프로그래밍 Dependency Property 다루기 (1) | 2019.05.19 |
C# WPF 프로그래밍 Basic Property 다루기 (0) | 2019.05.14 |
C# WPF 프로그래밍 속성(Property) 다루기 (0) | 2019.05.13 |
C# WPF 프로그래밍 계산기(Calculator) 프로그램 만들기 - 기능 추가(X^2, 1/X, Delete, Sqrt 버튼 추가) (0) | 2019.05.06 |
[C# WPF 프로그래밍] 네이버 단축 URL API 사용 방법 및 코드 (1) | 2019.05.05 |
C# WPF 프로그래밍 상속 및 접근자 (0) | 2019.04.27 |
C# WPF 프로그래밍 2개의 배열의 곱 구하기 (0) | 2019.04.26 |
최근댓글