笔记show 首页    笔记show·问卷    笔记show·资料库    注册    登录
首页  ›  /滑稽

用C#.Net编写一个计算器

1   0
  gaein_nidb · 1月前 · 286 次点击  

假装这是其他科目吧...

这是我某网友弄得网站w闲来无事发个帖子水一发

  1. 编程语言:C#

  2. 架构:Microsoft .NET Framework  4.6        

  3. 大小:20,480 字节        

  4. UI框架:WPF(Windows Presentation Foundation)        

理论上支持任何安装了.NET Framework 4.6及以上版本的操作系统

XAML:

<Window x:Name="calculator" 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="计算器" Height="326" Width="290" Background="Transparent" OpacityMask="White" AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown" ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True">
    <Grid Background="Transparent">
        <Border BorderThickness="5" BorderBrush="DarkGreen"  CornerRadius="10,10,10,10"  Background="Black"  Name="top" Opacity="0.85">
            <Grid Margin="0,0,0,2">
                <Button x:Name="_1" Content="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="10,209,0,0" Height="50" FontSize="36" Click="_1_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_2" Content="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="60,209,0,0" Height="50" FontSize="36" Click="_2_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_3" Content="3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="110,209,0,0" Height="50" FontSize="36" Click="_3_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_4" Content="4" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="10,159,0,0" Height="50" FontSize="36" Click="_4_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_5" Content="5" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="60,159,0,0" Height="50" FontSize="36" Click="_5_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_6" Content="6" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="110,159,0,0" Height="50" FontSize="36" Click="_6_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_7" Content="7" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="10,109,0,0" Height="50" FontSize="36" Click="_7_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_8" Content="8" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="60,109,0,0" Height="50" FontSize="36" Click="_8_Click" Foreground="White" Background="Black" BorderBrush="Red"/>
                <Button x:Name="_9" Content="9" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="110,109,0,0" Height="50" FontSize="36" Click="_9_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="_0" Content="0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="60,259,0,0" Height="50" FontSize="36" Click="_0_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="__" Content="." HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="10,259,0,0" Height="50" FontSize="36" Click="___Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <Button x:Name="C" Content="清空" HorizontalAlignment="Left" VerticalAlignment="Top" Width="105" Height="42" Margin="165,266,0,0" FontSize="24" FontWeight="Bold" Click="C_Click" Background="Black" Foreground="White" BorderBrush="Red"/>
                <TextBox x:Name="input" HorizontalAlignment="Left" Height="50" TextWrapping="Wrap" VerticalAlignment="Top" Width="260" Margin="10,54,0,0" FontSize="36" BorderBrush="Red" Background="{x:Null}" Foreground="White" SelectionBrush="White" HorizontalContentAlignment="Right"/>
                <Button x:Name="plus" Content="+" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="165,109,0,0" FontSize="36" Height="50" FontWeight="Bold" Click="plus_Click" Foreground="White" Background="Black" BorderBrush="Red"/>
                <Button x:Name="reduce" Content="-" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="50" FontSize="36" FontWeight="Bold" Margin="220,109,0,0" Click="reduce_Click" Foreground="White" Background="Black" BorderBrush="Red"/>
                <Button x:Name="multiply" Content="×" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="50" FontSize="36" FontWeight="Bold" Margin="165,164,0,0" Click="multiply_Click" Foreground="White" Background="Black" BorderBrush="Red"/>
                <Button x:Name="division" Content="÷" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="50" FontSize="36" Margin="220,164,0,0" FontWeight="Bold" Click="division_Click" Foreground="White" Background="Black" BorderBrush="Red"/>
                <Button x:Name="result" Content="=" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="42" Margin="220,219,0,0" FontSize="36" FontWeight="Bold" Click="result_Click" VerticalContentAlignment="Top" HorizontalContentAlignment="Center" Foreground="White" Background="Black" BorderBrush="Red" Padding="1,-5,1,1"/>
                <Label x:Name="_operator" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="220,10,0,0" Height="39" Width="50" FontSize="24" FontWeight="Bold" Background="{x:Null}" Foreground="White" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                <TextBlock x:Name="output" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="10,10,0,0" Height="39" Width="205" FontSize="24" FontWeight="Bold" Foreground="White"/>
                <Button x:Name="exit" Content="关闭" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="110,259,0,0" Height="50" FontSize="24" FontWeight="Bold" Click="exit_Click" Foreground="White" Background="Black" BorderBrush="Red"/>
                <Button x:Name="opposite" Content="±" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Margin="165,219,0,0" Height="42" Background="Black" BorderBrush="Red" Foreground="White" FontWeight="Bold" FontSize="24" Click="opposite_Click"/>
            </Grid>
        </Border>
    </Grid>
</Window>


源代码:

using System;
using System.Windows;
using System.Windows.Input;

namespace calculator
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static string _string;

        private void Calculate(string op)//声明无返回值的函数
        {
            if (op=="=")//如果符号是等号
            {
                if (output.Text.Trim() != String.Empty)//output不为空
                {
                    if (input.Text.Trim() != String.Empty)//input不为空
                    {
                        switch (Convert.ToChar(_operator.Content))//判断符合
                        {
                            case '+':
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) + Convert.ToDouble(input.Text));//将input和label转换为double,相加,再将结果转换为string,赋值给label
                                input.Text = "";
                                break;
                            case '-':
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) - Convert.ToDouble(input.Text));
                                input.Text = "";
                                break;
                            case '×':
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) * Convert.ToDouble(input.Text));
                                input.Text = "";
                                break;
                            case '÷':
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) / Convert.ToDouble(input.Text));
                                input.Text = "";
                                break;
                        }
                    }
                }
            }
            else//符号非符号
            {
                if (output.Text.Trim() == String.Empty)//如果输出是空
                {
                    output.Text = input.Text;//将input的值给output
                    _operator.Content = op;//运算符为+
                    input.Text = "";//清空input
                }
                else
                {
                    if (input.Text.Trim() != String.Empty)//如果输入非空
                    {
                        switch (Convert.ToChar(_operator.Content))//判断符号
                        {
                            case '+'://加号
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) + Convert.ToDouble(input.Text));//将input和label转换为double,相加(进行相应运算),再将结果转换为string,赋值给label
                                input.Text = "";
                                break;
                            case '-'://减号
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) - Convert.ToDouble(input.Text));
                                input.Text = "";
                                break;
                            case '×'://乘号
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) * Convert.ToDouble(input.Text));
                                input.Text = "";
                                break;
                            case '÷'://除号
                                output.Text = Convert.ToString(Convert.ToDouble(output.Text) / Convert.ToDouble(input.Text));
                                input.Text = "";
                                break;
                        }
                        _operator.Content = op;
                    }
                }
            }
        }
        private void C_Click(object sender, RoutedEventArgs e)//清空按钮
        {
            _operator.Content = "";
            input.Text = "";
            output.Text = "";
        }

        private void _0_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "0";
        }

        private void _1_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "1";
        }

        private void _2_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "2";
        }

        private void _3_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "3";
        }

        private void _4_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "4";
        }

        private void _5_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "5";
        }

        private void _6_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "6";
        }

        private void _7_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "7";
        }

        private void _8_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "8";
        }

        private void _9_Click(object sender, RoutedEventArgs e)
        {
            input.Text = input.Text + "9";
        }

        private void ___Click(object sender, RoutedEventArgs e)//小数点按钮
        {
            if (input.Text.Trim() == String.Empty) //判断TextBox是否为空
            {
                input.Text = "0.";//如果是空设置为0.
            }
            else
            {
                if (input.Text.IndexOf('.') != -1)//判断input.Text是否为小数
                {
                }
                else
                {
                    input.Text = input.Text + ".";
                }
            }

        }

        private void plus_Click(object sender, RoutedEventArgs e)//加法按钮
        {
            Calculate("+");//调用函数,参数为“+”
        }

        private void reduce_Click(object sender, RoutedEventArgs e)//减法按钮
        {
            Calculate("-");
        }

        private void multiply_Click(object sender, RoutedEventArgs e)//乘法按钮
        {
            Calculate("×");
        }

        private void division_Click(object sender, RoutedEventArgs e)//除法按钮
        {
            Calculate("÷");
        }

        private void result_Click(object sender, RoutedEventArgs e)//等于按钮
        {
            Calculate("=");
        }

        private void exit_Click(object sender, RoutedEventArgs e)//关闭按钮
        {
            Application.Current.Shutdown();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)//窗口可移动
        {
            this.DragMove();
        }

        private void opposite_Click(object sender, RoutedEventArgs e)//正负号
        {
            if (input.Text.Length != 0)//如果input不为空
            {
                if (Convert.ToDouble(input.Text) <= 0)//如果内容小于等于0
                {
                    input.Text = Convert.ToString(-Convert.ToDouble(input.Text));//取相反数
                }
                else
                {
                    input.Text = "-" + input.Text;//前面加负号
                }
            }
        }
    }
}


打赏我,让我更有动力~

目前尚无回复
笔记show·问卷  ·  笔记show·资料库  ·  0 人在线  
You can do the best!
知道创宇云安全 sitemap
VERSION: 2.1.3 · 0.097s · UTC +08:00
♥ Powered by HYBBS Themes by Intern Design by V2EX.