Friday, December 24, 2010

C# Tutorial: Operator Overloading [Add 2 Complex Numbers]

Here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Complex
    {
        int real, imaginary;

        Complex(int real, int imaginary)
        {
            this.real = real;
            this.imaginary = imaginary;
        }

        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);   
        }

        public override string ToString()
        {
            return string.Format("{0} + {1}i", real, imaginary);   
        }

        static void Main(string[] args)
        {
            Complex c1 = new Complex(4, 3);
            Complex c2 = new Complex(7, 9);
            Complex sum = c1 + c2;
            Console.WriteLine("The sum of the two complex numbers is: {0}", sum);
            Console.ReadKey(); // Pause 
        }
    }
}

Sunday, December 19, 2010

C# Tutorial: Create Shortcuts for Applications [Start Menu]

Here is the code

using IWshRuntimeLibrary;
//////////////////////////////////////////////////////////////////////////////////////////
private void button1_Click(object sender, EventArgs e)
{
    WshShell shell = new WshShell();
    IWshShortcut MyShortcut;
    // Replace [UserName] with the name of the User
    MyShortcut = (IWshShortcut)shell.CreateShortcut(@"C:\Documents and Settings\[UserName]\Start Menu\Programs\MyShortcut.lnk");
    MyShortcut.TargetPath = Application.ExecutablePath;
    MyShortcut.Description = "Launch the App!";
    // MyShortcut.IconLocation = [IconPath]; // [IconPath] - the path of the icon for the shortcut, if you have one 
    MyShortcut.Save(); 
}

Tuesday, December 14, 2010

C# Tutorial: Equation Calculator++ [Trigonometric Functions]

Here is the code

private static double trigFunctions(char[] expr, ref int index)
{
    if (((int)expr[index] >= 48 && (int)expr[index] <= 57) || expr[index] == 46 || expr[index] == 45)
        return GetDouble(expr, ref index);
    else
    {
        char op = expr[index];
        if (op != 'c' && op != 't' && op != 's')
            return GetDouble(expr, ref index);
        index += 4;
        double x = GetDouble(expr, ref index);
        if (index + 1 != expr.Length) index++;
        if (op == 'c')
            return Math.Round(Math.Cos(x), MidpointRounding.AwayFromZero);
        else if (op == 't')
            return Math.Round(Math.Tan(x), MidpointRounding.AwayFromZero);
        else
            return Math.Round(Math.Sin(x), MidpointRounding.AwayFromZero);  
    }
}

Saturday, December 11, 2010

C# Tutorial: Equation Calculator [Expression Evaluator]

Here is the code

// Math Parser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EquationParser
{
    class MathParser
    {

        public static double EvalExpression(char[] expr)
        {
            return parseSummands(expr, 0); 
        }
        
        private static double parseSummands(char[] expr, int index)
        {
            double x = parseFactors(expr, ref index);
            while (true)
            {
                char op = expr[index];
                if (op != '+' && op != '-')
                    return x;
                index++;
                double y = parseFactors(expr, ref index);
                if (op == '+')
                    x += y;
                else
                    x -= y;
            }
        }
        
        private static double parseFactors(char[] expr, ref int index)
        {
            double x = GetDouble(expr, ref index);
            while (true)
            {
                char op = expr[index];
                if (op != '/' && op != '*')
                    return x;
                index++;
                double y = GetDouble(expr, ref index);
                if (op == '/')
                    x /= y;
                else
                    x *= y;
            }
        }
        
        private static double GetDouble(char[] expr, ref int index)
        {
            string dbl = "";
            while (((int)expr[index] >= 48 && (int)expr[index] <= 57) || expr[index] == 46)
            {
                dbl = dbl + expr[index].ToString();
                index++;
                if (index == expr.Length)
                {
                    index--;
                    break;
                }
            }
            return double.Parse(dbl); 
        }
    }
}

// Button1_Click
textBox2.Text = MathParser.EvalExpression(textBox1.Text.ToCharArray()).ToString();

Saturday, December 4, 2010

C# Tutorial: ColorPicker [PAWN Color HEX Code for SAMP Scripts]

Here is the code

private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
    if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        pictureBox1.BackColor = colorDialog1.Color;
        string color;
        color = string.Format("0x{0:X8}", pictureBox1.BackColor.ToArgb());
        color = color.Remove(color.IndexOf("x") + 1, 2);
        textBox1.Text = color + "FF";
    }
}

Download ColorPicker [SA:MP]

Wednesday, December 1, 2010

SAMP Scripting Tutorial: Interiors | /enter - /exit Commands

Here is the code

// Declarations
new Float:saveX[MAX_PLAYERS],Float:saveY[MAX_PLAYERS],Float:saveZ[MAX_PLAYERS];
new cityHall;

// OnGameModeInit
cityHall = CreatePickup(1318,1,1480.9467773438,-1772.1303710938,19.966495513916);

// Uses standard player walking animation (animation of CJ)
UsePlayerPedAnims();
//-------------------------------------------------------------------------------//

// OnPlayerCommandText
if(strcmp(cmdtext,"/enter",true)==0)
{
 if(IsPlayerInRangeOfPoint(playerid,5.0,1480.9467773438,-1772.1303710938,19.966495513916))
 {
  GetPlayerPos(playerid,saveX[playerid],saveY[playerid],saveZ[playerid]);
  SetPlayerPos(playerid,388.8720,173.8050,1008.3828);
  SetPlayerInterior(playerid, 3);
 }
 return 1;
}

if(strcmp(cmdtext,"/exit",true)==0)
{
 
 SetPlayerPos(playerid,saveX[playerid],saveY[playerid],saveZ[playerid]);
 SetPlayerInterior(playerid, 0);
 
 return 1;
}


// OnPlayerPickUpPickup
if(pickupid == cityHall)
{
 GameTextForPlayer(playerid,"Type ~r~/enter",5000,5);
}

// OnPlayerUpdate
if(IsPlayerInRangeOfPoint(playerid,5.0,390.64190673828,173.77577209473,1009.553527832))
{
 GameTextForPlayer(playerid,"Type ~r~/exit",5000,5);
}