C # Learning 8

Source: Internet
Author: User

C # Learning 8: Create a WPF file and evaluate the expression. The interface is simple. Add a background image. The code for adding a background image is

[Csharp]
<Grid. Background>
<ImageBrush ImageSource = "/expression evaluate; component/Images/1.jpg"/>
</Grid. Background>
Modify the attributes of the background.

The input expression is as follows: (1 + 2) * 5 + 1)/4 =


[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
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;
Evaluate the namespace expression
{
/// <Summary>
/// Interaction logic of MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Int [,] map = new int [7, 7] {,-1,-1,-, 1}, // priority relationship between operators
{1, 1,-1,-1,-1, 1 },
{1, 1, 1,-1, 1 },
{1, 1, 1,-1, 1 },
{-1,-1,-1,-1,-100 },
{, 1 },
{-1,-1,-1,-1,-1,100, 0}
};
Char [] str = new char [1005];
Char [] optr = new char [1005];
Double [] opnd = new double [1005];
Int t1, t2, k, len;
Char ch, zz;
Int temp1, temp2;
Double a, B;
Public MainWindow ()
{
InitializeComponent ();
}
Public int cam (char c)
{
Switch (c)
{
Case '+': return 0;
Case '-': return 1;
Case '*': return 2;
Case '/': return 3;
Case '(': return 4;
Case ')': return 5;
Case '#': return 6;
Default: return-1;
}
}
Public double sol (double x, char c, double y)
{
Switch (c)
{
Case '+': return x + y;
Case '-': return x-y;
Case '*': return x * y;
Case '/': return x/y;
Default: return-1;
}
}
Public int z (char c)
{
If ('0' <= c & c <= '9' | c = '.')
Return 1;
If (c = '')
Return-1;
Return 0;
}
Private void button2_Click (object sender, RoutedEventArgs e)
{
This. Close ();
}
Private void button#click (object sender, RoutedEventArgs e)
{
String s = resource. Text;
Str = s. ToCharArray ();
Len = s. Length;
Str [len-1] = '#'; // process the equals sign
T1 = t2 = k = 0;
Optr [t1 ++] = '#';
Ch = str [k ++];
While (ch! = '#' | Optr [t1-1]! = '#')
{
If (z (ch) = 1) // the operand is added to the stack.
{
Double num1, num2, sm;
Int t, flagg;
Num1 = num2 = 0;
T = K-1;
Flagg = 0;
Sm = 0.1;
While (z (str [t]) = 1)
{
If (str [t] = '.')
{
Flagg = 1;
T ++;
Continue;
}
If (flagg = 0)
Num1 = num1 * 10 + (str [t]-'0 ');
Else
{
Num2 + = sm * (str [t]-'0 ');
Sm * = 0.1;
}
T ++;
}
Opnd [t2 ++] = num1 + num2; // converts a string to a floating point number.
K = t;
Ch = str [k ++];
}
Else if (z (ch) =-1)
Ch = str [k ++];
Else
{
Temp1 = cam (optr [t1-1]);
Temp2 = cam (ch );
If (map [temp1, temp2] =-1) // The top element of the stack has a low priority.
{
Optr [t1 ++] = ch;
Ch = str [k ++];
}
Else if (map [temp1, temp2] = 0) // parentheses and accept the next character
{
T1 --;
Ch = str [k ++];
}
Else // unstack and calculate the result
{
Zz = optr [-- t1];
A = opnd [-- t2];
B = opnd [-- t2];
Opnd [t2 ++] = sol (B, zz, );
}
}
}
Result. Text = opnd [0]. ToString ();
}
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.