C# WPF 를 이용한 계산기 프로그램 만들기 최종에 이어 X^2, 1/X, Delete, Sqrt 버튼을 추가하였습니다.
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="700" Width="406.745">
<Grid HorizontalAlignment="Left" Width="404" 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"/>
</Grid.ColumnDefinitions>
<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
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
double lastNumber;
SelectedOperator selectedOperator;
double result;
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)
{
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;
}
if (sender == MinusButton)
{
selectedOperator = SelectedOperator.Subtraction;
}
if (sender == MultipleButton)
{
selectedOperator = SelectedOperator.Multiplication;
}
if (sender == DivideButton)
{
selectedOperator = SelectedOperator.Division;
}
}
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))
{
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";
}
// 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:
result = SimpleMath.Add(lastNumber, newNumber);
break;
case SelectedOperator.Subtraction:
result = SimpleMath.Subtract(lastNumber, newNumber);
break;
case SelectedOperator.Multiplication:
result = SimpleMath.Multiple(lastNumber, newNumber);
break;
case SelectedOperator.Division:
result = SimpleMath.Divide(lastNumber, newNumber);
break;
}
resultLabel.Content = result.ToString();
}
// 숫자 0 버튼 클릭
/*private void ZeroButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "0";
}
else
{
resultLabel.Content += "0";
}
}
// 숫자 1 버튼 클릭
private void OneButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "1";
}
else
{
resultLabel.Content += "1";
}
}
// 숫자 2 버튼 클릭
private void TwoButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "2";
}
else
{
resultLabel.Content += "2";
}
}
// 숫자 3 버튼 클릭
private void ThreeButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "3";
}
else
{
resultLabel.Content += "3";
}
}
// 숫자 4 버튼 클릭
private void FourButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "4";
}
else
{
resultLabel.Content += "4";
}
}
// 숫자 5 버튼 클릭
private void FiveButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "5";
}
else
{
resultLabel.Content += "5";
}
}
// 숫자 6 버튼 클릭
private void SixButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "6";
}
else
{
resultLabel.Content += "6";
}
}
// 숫자 7 버튼 클릭
private void SevenButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "7";
}
else
{
resultLabel.Content += "7";
}
}
// 숫자 8 버튼 클릭
private void EightButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "8";
}
else
{
resultLabel.Content += "8";
}
}
// 숫자 9 버튼 클릭
private void NineButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "9";
}
else
{
resultLabel.Content += "9";
}
}
*/
/*
// Divide 버튼 클릭
private void DivideButton_Click(object sender, RoutedEventArgs e)
{
}
// Multiple 버튼 클릭
private void MultipleButton_Click(object sender, RoutedEventArgs e)
{
}
// Minus 버튼 클릭
private void MinusButton_Click(object sender, RoutedEventArgs e)
{
}
// Plus 버튼 클릭
private void PlusButton_Click(object sender, RoutedEventArgs e)
{
}
*/
}
}
'Language > C# WPF Programming' 카테고리의 다른 글
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) 프로그램 만들기 - layout 추가(계산 과정, 히스토리 기능 추가) (1) | 2019.05.08 |
[C# WPF 프로그래밍] 네이버 단축 URL API 사용 방법 및 코드 (1) | 2019.05.05 |
C# WPF 프로그래밍 상속 및 접근자 (0) | 2019.04.27 |
C# WPF 프로그래밍 2개의 배열의 곱 구하기 (0) | 2019.04.26 |
C# 기초 프로그래밍 Main 함수와 Console 코드 (0) | 2019.04.25 |
최근댓글