zoukankan      html  css  js  c++  java
  • Calculator

    MainPage.xaml

    <phone:PhoneApplicationPage
        x:Class="Calculate.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        shell:SystemTray.IsVisible="True">

        <!--LayoutRoot 是包含所有页面内容的根网格-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="220"/>
                <RowDefinition Height="548*"/>
            </Grid.RowDefinitions>

            <!--TitlePanel 包含应用程序的名称和页标题-->
            <StackPanel x:Name="TitlePanel" Margin="5,10,7,28">
                <StackPanel.Background>
                    <SolidColorBrush Color="#FF323232" Opacity="0.5"/>
                </StackPanel.Background>
                <TextBlock x:Name="Title" Text="复数计算" Margin="9,-7,0,0" Height="35" />
               
                <TextBlock x:Name="Expression" Margin="9,-7,0,0"  FontSize="28" Height="60" Foreground="#FF00FF45" />
                <TextBlock x:Name="result" Margin="9,-7,0,0" FontSize="48" TextAlignment="Right" Width="460" Height="104" Foreground="#FF00FF0B" />
            </StackPanel>

            <!--ContentPanel - 在此处放置其他内容-->
            <Grid x:Name="ContentPanel" Margin="5,0,19,0" Height="580" VerticalAlignment="Bottom" Grid.RowSpan="2">
                <Button Content="time"
                        Tap="TapTime"
                        Height="96" Margin="5,84,325,400"
                        Name="button1" Width="126" Background="#FF1F688E" BorderBrush="Black" />
                <Button Content="i"
                        Tap="TapI"
                        Height="96" HorizontalAlignment="Left"
                        Margin="2,162,0,0" Name="button2"
                        VerticalAlignment="Top" Width="126" BorderBrush="Black" >
                    <Button.Background>
                        <SolidColorBrush Color="#FF1BA1E2"/>
                    </Button.Background>
                </Button>
                <Button Content="7" FontSize="48"
                        Tap="Tap7"
                        Height="90" HorizontalAlignment="Left" Margin="2,242,0,0" Name="button3" VerticalAlignment="Top" Width="120" />
                <Button Content="4" FontSize="48"
                        Tap="Tap4"
                        Height="90" HorizontalAlignment="Left" Margin="2,322,0,0" Name="button4" VerticalAlignment="Top" Width="120" />
                <Button Content="date"
                        Tap="TapDate"
                        Height="96" Margin="115,84,215,400"
                        Name="button5" Width="126" BorderBrush="Black" >
                    <Button.Background>
                        <SolidColorBrush Color="#FF1F688E"/>
                    </Button.Background>
                </Button>
                <Button Content="C"
                        Tap="TapClean"
                        Height="96" Margin="225,84,105,400"
                        Name="button6" Width="126" BorderBrush="Black" >
                    <Button.Background>
                        <SolidColorBrush Color="#FFE22D1B"/>
                    </Button.Background>
                </Button>
                <Button Content="←"
                        Tap="TapBackspace"
                        Height="96" Margin="334,84,-4,400"
                        Name="button7" Width="126" BorderBrush="Black" >
                    <Button.Background>
                        <SolidColorBrush Color="#FFE22D1B"/>
                    </Button.Background>
                </Button>
                <Button Content="("
                        Tap="TapLeftbraket"
                        Height="96" Margin="116,162,214,322"
                        Name="button8" Width="126" BorderBrush="Black" >
                    <Button.Background>
                        <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                    </Button.Background>
                </Button>
                <Button Content=")"
                        Tap="TapRightBraket"
                        Height="96" Margin="226,162,104,322"
                        Name="button9" Width="126" BorderBrush="Black" >
                    <Button.Background>
                        <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                    </Button.Background>
                </Button>
                <Button Content="÷" Height="96" FontSize="48"
                        Margin="335,162,-5,322" Name="button10"
                        Width="126" Background="Blue"
                        Tap="TapDiv" BorderBrush="Black" />
                <Button Content="8" FontSize="48"
                        Height="90"
                        Tap="Tap8"
                        Margin="116,242,220,248" Name="button11" Width="120" />
                <Button Content="9" FontSize="48"
                        Tap="Tap9"
                        Height="90" Margin="226,242,110,248" Name="button12" Width="120" BorderBrush="White" />
                <Button Content="×"
                        Tap="TapMulti"
                        Height="96"
                        FontSize="48"
                        Margin="335,242,-5,242"
                        Name="button13" Width="126" Background="#FF0E00FF" BorderBrush="Black"  />
                <Button Content="5" FontSize="48"
                        Tap="Tap5"
                        Height="90" Margin="116,322,220,168" Name="button14" Width="120" />
                <Button Content="6" FontSize="48"
                        Tap="Tap6"
                        Height="90" Margin="226,322,110,168" Name="button15" Width="120" />
                <Button Content="-" FontSize="68"
                        Tap="TapMinus"
                        Height="96" Margin="335,322,-5,162"
                        Name="button16" Width="126" Background="#FF0E00FF" BorderBrush="Black" />
                <Button Content="1" FontSize="48"
                        Tap="Tap1"
                        Height="90" Margin="2,401,334,89" Name="button17" Width="120" />
                <Button Content="2" FontSize="48"
                        Tap="Tap2"
                        Height="90" Margin="116,401,220,89" Name="button18" Width="120" />
                <Button Content="3" FontSize="48"
                        Tap="Tap3"
                        Height="90" Margin="226,401,110,89" Name="button19" Width="120" />
                <Button Content="+" FontSize="48"
                        Tap="TapAdd"
                        Height="96" Margin="335,401,-5,83"
                        Name="button20" Width="126" Background="#FF0E00FF" BorderBrush="Black" />
                <Button Content="0"
                        Height="90" FontSize="40"
                        Tap="Tap0"
                        Margin="2,480,344,10" Name="button21" />
                <Button Content="."
                        Tap="TapDot" FontSize="48"
                        Height="90" Margin="226,480,110,10" Name="button23" Width="120" />
                <Button Content="=" FontSize="48"
                        Tap="TapEqual"
                        Height="96" Margin="335,480,-5,4"
                        Name="button24" Width="126" Background="#FF19FF00" BorderBrush="Black" />
                <Button Content="Ans"
                        FontSize="34"
                        Height="90"
                        Margin="115,480,221,10"
                        x:Name="button18_Copy"
                        Width="120" Tap="TapAns"/>
                <TextBlock Height="70" FontSize="48" Foreground="Plum" HorizontalAlignment="Left" Margin="294,0,0,0" Name="textBlock1" Text="Graphy" VerticalAlignment="Top" Width="166" />
                <TextBlock Name="textBlock2" DoubleTap="TapMatlab" Text="MATLAB" FontSize="48" Foreground="Plum" Margin="6,0,262,510" >
                   
                </TextBlock>
            </Grid>
        </Grid>
     
        <!--演示 ApplicationBar 用法的示例代码-->
        <!--<phone:PhoneApplicationPage.ApplicationBar>
            <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
                <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>
                <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>
                <shell:ApplicationBar.MenuItems>
                    <shell:ApplicationBarMenuItem Text="菜单项 1"/>
                    <shell:ApplicationBarMenuItem Text="菜单项 2"/>
                </shell:ApplicationBar.MenuItems>
            </shell:ApplicationBar>
        </phone:PhoneApplicationPage.ApplicationBar>-->

    </phone:PhoneApplicationPage>

    MainPage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;


    namespace Calculate
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // 构造函数
            public MainPage()
            {
                InitializeComponent();
            }
            public String expression = "";//存放表达式
            public Boolean expr_type;//真:计算表达式 假:其他
            public String store = null;//存放结果


            //Expression 输入

            //Ans
            private void TapAns(object sender, GestureEventArgs e)
            {

                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

               
                if (!(expression.ElementAt(expression.Length - 1) == '.' || (expression.ElementAt(expression.Length - 1) >= '0' && expression.ElementAt(expression.Length - 1) <= '9')))
                    expression += "Ans";
                }
                else expression += "Ans";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            // .
            private void TapDot(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }
                    expression += ".";
                }
                else expression += ".";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //=
            private void TapEqual(object sender, GestureEventArgs e)
            {

                expression += "=";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
                try
                {
                    store = GetResult(expression, store).ToString();
                    result.Text = store + "   ";
                }

                catch (Exception)
                {
                    result.Text = "error     ";
                }
            }

            //Time
            private void TapTime(object sender, GestureEventArgs e)
            {
                Expression.Text = DateTime.Now.TimeOfDay.Hours.ToString() + ":" + DateTime.Now.TimeOfDay.Minutes.ToString();
            }

            //Date
            private void TapDate(object sender, GestureEventArgs e)
            {
                Expression.Text = String.Format("{0}\\{1}\\{2}    {3}", DateTime.Now.Date.Year, DateTime.Now.Date.Month, DateTime.Now.Date.Day, DateTime.Now.DayOfWeek);
            }

            //0
            private void Tap0(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "0";
                }
                else expression += "0";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //1
            private void Tap1(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "1";
                }
                else expression += "1";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //2
            private void Tap2(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "2";
                }
                else expression += "2";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //3
            private void Tap3(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "3";
                }
                else expression += "3";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //4
            private void Tap4(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "4";
                }
                else expression += "4";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //5
            private void Tap5(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "5";
                }
                else expression += "5";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //6
            private void Tap6(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "6";
                }
                else expression += "6";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //7
            private void Tap7(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "7";
                }
                else expression += "7";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //8
            private void Tap8(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }

                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "8";
                }
                else expression += "8";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //9
            private void Tap9(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }
                }
                if (expression.Length > 0)
                {
                    if (!(expression.ElementAt(expression.Length - 1) == 's'))
                        expression += "9";
                }

                else expression += "9";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //+
            private void TapAdd(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = "";
                    }
                }
                expression += "+";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //-
            private void TapMinus(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = "";
                    }
                }
                expression += "-";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //*
            private void TapMulti(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = "";
                    }
                }
                expression += "×";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //  /
            private void TapDiv(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = "";
                    }
                }
                expression += "÷";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            //  i
            private void TapI(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = "";
                    }
                }
                expression += "i";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            // Clean
            private void TapClean(object sender, GestureEventArgs e)
            {
                expression = "";
                Expression.Text = expression;
            }

            //Backspace
            private void TapBackspace(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.ElementAt(expression.Length - 1) == 's')
                    {
                        expression = expression.Substring(0, expression.Length - 3);
                    }
                    else expression = expression.Substring(0, expression.Length - 1);
                }
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            // (
            private void TapLeftbraket(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }
                    if (!(expression.ElementAt(expression.Length - 1) == 's')) expression += "(";
                }
                else expression += "(";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }

            // )
            private void TapRightBraket(object sender, GestureEventArgs e)
            {
                if (expression.Length > 0)
                {
                    if (expression.IndexOf('=') >= 0)
                    {
                        expression = " ";
                    }
                }
                expression += ")";
                if (expression.Length > 20)
                {
                    Expression.Text = expression.Substring(expression.Length - 20);
                }
                else Expression.Text = expression;
            }
            //end  Expression


            //计算运算符的优先级
            public static int pp(char c)
            {
                int k = -2;
                switch (c)
                {
                    case '*':
                    case '/':
                        k = 2;
                        break;
                    case '+':
                    case '-':
                        k = 1;
                        break;
                    case '(':
                    case ')':
                        k = 0;
                        break;
                    case '=':
                        k = -1;
                        break;
                }
                return k;
            }


            //将字符串转换成实数
            public static double shishu(ref String s, ref int k)
            {
                double x, y = 1.0;
                int flag = 1;
                x = 0.0;
                char c = s.ElementAt(k);
                while (c >= '0' && c <= '9' || c == '.')
                {
                    k = k + 1;
                    if (c >= '0' && c <= '9')
                        if (flag == 0)
                        {
                            y *= 0.1;
                            x += (y * (c - 48));
                        }
                        else
                            x = 10 * x + (c - 48);
                    else flag = 0;
                    c = s.ElementAt(k);
                }
                return x;

            }

            //计算表达式的值
            double GetResult(String str, String str1 = null)
            {
                //格式化str

                int i = 0;
                str = str.Replace("Ans", str1);
                str = str.Replace(" ", null);
                while (i < str.Length)
                {
                    if (i == 0 && str.ElementAt(i) == '-')
                    {
                        str = "0" + str;
                    }
                    if (i > 0)
                    {
                        if (str.ElementAt(i - 1) == '(')
                            str = str.Insert(i, "0");
                    }
                    i++;
                }

                i = 0;
                while (str.ElementAt(i) != '=')
                {
                    switch (str.ElementAt(i))
                    {
                        case '÷':
                            str = str.Replace("÷", "/");
                            break;
                        case '×':
                            str = str.Replace("×", "*");
                            break;
                        case ' ':
                            str.Remove(i, 1);
                            break;
                    }

                    i++;
                }
                //格式化结束


                Stack<double> st_data = new Stack<double>();
                Stack<char> st_op = new Stack<char>();
                st_op.Push('=');


                int flag = 1, k;

                k = 0;
                double x, y;
                char c = str.ElementAt(k);

                while (flag != 0)
                {
                    if (c >= '0' && c <= '9' || c == '.')
                        st_data.Push(shishu(ref str, ref k));
                    else if (c == '(' || pp(c) > pp(st_op.Peek()))
                    {
                        st_op.Push(c);
                        k = k + 1;
                    }
                    else if ((c == '=') && (st_op.Peek() == '='))
                        flag = 0;
                    else if ((c == ')') && (st_op.Peek() == '('))
                    {
                        st_op.Pop();
                        k++;
                    }
                    else if (pp(c) <= pp(st_op.Peek()))
                    {
                        y = st_data.Peek();
                        st_data.Pop();
                        x = st_data.Peek();
                        st_data.Pop();
                        c = st_op.Peek();
                        st_op.Pop();
                        switch (c)
                        {
                            case '+':
                                x = x + y;
                                break;
                            case '-':
                                x = x - y;
                                break;
                            case '*':
                                x = x * y;
                                break;
                            case '/':
                                x = x / y;
                                break;
                        }
                        st_data.Push(x);
                    }
                    c = str.ElementAt(k);
                }
                return st_data.Peek();
            }

            //页面转换  main->Matlab
            private void TapMatlab(object sender, GestureEventArgs e)
            {
                NavigationService.Navigate(new Uri("/Matlab.xaml", UriKind.Relative));
            }

        }
    }

  • 相关阅读:
    Bootstrap入门(二十五)JS插件2:过渡效果
    Bootstrap入门(二十四)data属性
    Bootstrap入门(二十三)JS插件1:模态框
    Bootstrap入门(二十二)组件16:列表组
    Bootstrap入门(二十一)组件15:警告框
    Bootstrap入门(二十)组件14:警告框
    Bootstrap入门(十九)组件13:页头与缩略图
    git 上传本地代码到github 服务器
    Dropzone.js实现文件拖拽上传
    将复杂form表单序列化serialize-object.js
  • 原文地址:https://www.cnblogs.com/bacazy/p/2808366.html
Copyright © 2011-2022 走看看