Exemple #1
0
        static void Main(string[] args)
        {
            InputWrapper iw = new InputWrapper();
            int          a  = iw.getInt("Please input the first coordiante x: ");
            int          b  = iw.getInt("Please input the first coordiante y: ");

            Console.WriteLine
                ("Here is the section number for the first coordinate: {0}", FindSection(a, b));

            int c = iw.getInt("Please input the second coordiante x: ");
            int d = iw.getInt("Please input the second coordiante y: ");

            Console.WriteLine
                ("Here is the section number for the second coordinate: {0}", FindSection(c, d));

            int FindSection(int x, int y)
            {
                if (x > 0 && y > 0)
                {
                    return(1);
                }
                else if (x > 0 && y < 0)
                {
                    return(4);
                }
                else if (x < 0 && y > 0)
                {
                    return(2);
                }
                else
                {
                    return(3);
                }
            }
        }
Exemple #2
0
    public static void Main()
    {
        InputWrapper iw = new InputWrapper();

        // initialize and display array
        int[] primes = { 2, 3, 5, 7, 11, 13 };
        for (int i = 0; i < primes.Length; i++)
        {
            Console.Write("{0} ", primes[i]);
        }
        Console.WriteLine();
        // loop to read and search for targets
        Console.WriteLine("Enter numbers to search for, -1 when done");
        int target = iw.getInt("target number: ");

        while (target != -1)
        {
            int index = Search(primes, target);
            if (index == -1)
            {
                Console.WriteLine("{0} not found", target);
            }
            else
            {
                Console.WriteLine("{0} found at {1}", target, index);
            }
            target = iw.getInt("target number: ");
        }
    }
Exemple #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input the number:");
            InputWrapper iw = new InputWrapper();
            int          a  = iw.getInt("enter the first integer: ");
            int          b  = iw.getInt("enter the second integer: ");

            int[] c     = new int[b - a + 1];
            int   count = 0;

            for (int i = 0; i < c.Length; i++)
            {
                c[i] = a;
                a++;
            }

            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] % 6 == 0)
                {
                    Console.Write("{0}:", c[i]);
                    count++;
                }
            }
            Console.WriteLine();
            Console.WriteLine("There are {0} number divisiable by 6", count);
        }
Exemple #4
0
    public static void Main()
    {
        Bank         bank = new Bank();
        InputWrapper iw   = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("open"))
            {
                AccountType type;
                string      stype = iw.getString("account type: ");
                switch (stype)
                {
                case "checking":
                    type = AccountType.Checking;
                    break;

                case "savings":
                    type = AccountType.Savings;
                    break;

                default:
                    type = AccountType.Invalid;
                    break;
                }
                if (type == AccountType.Invalid)
                {
                    Console.WriteLine("Valid account types are checking/savings");
                    continue;
                }
                decimal bal   = iw.getDecimal("starting balance: ");
                string  owner = iw.getString("owner: ");
                int     id    = bank.AddAccount(type, bal, owner);
                Console.WriteLine("Account opened, id = {0}", id);
            }
            else if (cmd.Equals("close"))
            {
                int id = iw.getInt("account id: ");
                bank.DeleteAccount(id);
            }
            else if (cmd.Equals("show"))
            {
                ShowArray(bank.GetAccounts());
            }
            else if (cmd.Equals("account"))
            {
                int     id  = iw.getInt("account id: ");
                Account acc = bank.FindAccount(id);
                Atm.ProcessAccount(acc);
            }
            else
            {
                help();
            }
            cmd = iw.getString("> ");
        }
    }
Exemple #5
0
        static void Main(string[] args)
        {
            InputWrapper iw = new InputWrapper();
            int          a  = iw.getInt("Please input the first coordiante x: ");
            int          b  = iw.getInt("Please input the first coordiante y: ");

            Console.WriteLine
                ("The corordinate({0}, {1}) lies in the {2} quandrant", a, b, FindSection(a, b));

            int c = iw.getInt("Please input the second coordiante x: ");
            int d = iw.getInt("Please input the second coordiante y: ");

            Console.WriteLine
                ("The corordinate({0}, {1}) lies in the {2} quandrant", c, d, FindSection(c, d));;

            int FindSection(int x, int y)
            {
                if (x > 0 && y > 0)
                {
                    return(1);
                }
                else if (x > 0 && y < 0)
                {
                    return(4);
                }
                else if (x < 0 && y > 0)
                {
                    return(2);
                }
                else
                {
                    return(3);
                }
            }
        }
Exemple #6
0
    public static void Main()
    {
        // Initialize accounts and show starting state
        accounts = new ArrayList();
        Console.WriteLine("accounts.Count = {0}", accounts.Count);
        Console.WriteLine("accounts.Capacity = {0}", accounts.Capacity);
        AddAccount(100, "Bob", 1);
        AddAccount(200, "Mary", 2);
        AddAccount(300, "Charlie", 3);
        ShowAccounts(accounts);
        Console.WriteLine("accounts.Count = {0}", accounts.Count);
        Console.WriteLine("accounts.Capacity = {0}", accounts.Capacity);
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowAccounts(accounts);
                }
                else if (cmd.Equals("enum"))
                {
                    ShowEnum(accounts);
                }
                else if (cmd.Equals("remove"))
                {
                    int id = iw.getInt("id: ");
                    RemoveAccount(id);
                }
                else if (cmd.Equals("add"))
                {
                    decimal balance = iw.getDecimal("balance: ");
                    string  owner   = iw.getString("owner: ");
                    int     id      = iw.getInt("id: ");
                    AddAccount(balance, owner, id);
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }
Exemple #7
0
        static void Main(string[] args)
        {
            InputWrapper iw = new InputWrapper();
            int          a  = iw.getInt("Please input the size of an array: ");

            int[] b     = new int[a];
            int   count = 1;

            for (int i = 0; i < b.Length; i++)
            {
                Console.Write("Please input number {0}", count++);
                b[i] = iw.getInt(" : ");
            }
            Array.Sort(b);
            foreach (var key in b)
            {
                Console.WriteLine(key.ToString());
            }
        }
Exemple #8
0
    public static void Main()
    {
        Bank         bank = new Bank();
        InputWrapper iw   = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("open"))
            {
                decimal bal   = iw.getDecimal("starting balance: ");
                string  owner = iw.getString("owner: ");
                int     id    = bank.AddAccount(bal, owner);
                Console.WriteLine("Account opened, id = {0}", id);
            }
            else if (cmd.Equals("close"))
            {
                int id = iw.getInt("account id: ");
                bank.DeleteAccount(id);
            }
            else if (cmd.Equals("show"))
            {
                ShowArray(bank.GetAccounts());
            }
            else if (cmd.Equals("account"))
            {
                int     id  = iw.getInt("account id: ");
                Account acc = bank.FindAccount(id);
                Atm.ProcessAccount(acc);
            }
            else
            {
                help();
            }
            cmd = iw.getString("> ");
        }
    }
Exemple #9
0
    public static int Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter -1 to terminate the program");
        int year = iw.getInt("year: ");

        while (false /*(year != -1*/)
        {
            if ((year % 4 == 0) &&
                ((year % 400 == 0) || (year % 100 != 0)))
            {
                Console.WriteLine("{0} is a leap year", year);
            }
            else
            {
                Console.WriteLine("{0} is not a leap year", year);
            }
            year = iw.getInt("year: ");
        }
        return(0);
    }
    public static int Main(string[] args)
    {
        InputWrapper iw     = new InputWrapper();
        decimal      amount = iw.getDecimal("Amount of loan: "); // amount of loan
        double       rate   = iw.getDouble("Interest rate");     // interest rate
        int          months = iw.getInt("Number of periods");    // number of periods
        // Prompt for amount of loan, interest rate, and months
        // Calculate monthly payment using payment() method
        // Display this payment
        decimal monthlyPayment = payment(amount, rate, months);

        Console.WriteLine("Your monthly payment is {0}", monthlyPayment);
        return(0);
    }
Exemple #11
0
    public static void Main(string[] args)
    {
        InputWrapper iw      = new InputWrapper();
        int          fahr    = iw.getInt("Temperature in Fahrenheit: ");
        int          celsius = (fahr - 32) * 5 / 9;

        Console.WriteLine("fahrenheit = " + fahr);
        Console.WriteLine("celsius = " + celsius);
        // Redo calculation using double
        // Cast one of the integers to double and use a double
        // variable for celsius
        double dblCel = (fahr - (double)32) * 5 / 9;

        Console.WriteLine("Using double, celsius = " + dblCel);
    }
Exemple #12
0
    public static int Main(string[] args)
    {
        InputWrapper iw   = new InputWrapper();
        int          year = iw.getInt("year: ");

        if ((year % 4 == 0) &&
            ((year % 400 == 0) || (year % 100 != 0)))
        {
            Console.WriteLine("{0} is a leap year", year);
        }
        else
        {
            Console.WriteLine("{0} is not a leap year", year);
        }
        return(0);
    }
    public static int Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();     // get an input reader (read from stdin)
        int          i  = iw.getInt("enter integer: ");

        Console.WriteLine("i = {0}", i);
        double d = iw.getDouble("enter double: ");

        Console.WriteLine("d = {0}", d);
        decimal dec = iw.getDecimal("enter decimal: ");

        Console.WriteLine("dec = {0}", dec);
        string s = iw.getString("enter string: ");

        Console.WriteLine("s = {0}", s);
        return(0);
    }
Exemple #14
0
    public static int Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();
        double       amount;    // annual deposit amount
        double       rate;      // interest rate
        int          years;     // number of years
        double       total;     // total accumulation

        amount = iw.getDouble("amount: ");
        rate   = iw.getDouble("rate: ");
        years  = iw.getInt("years: ");
        total  = amount * (Math.Pow(1 + rate, years) - 1) / rate;
        long total_in_cents = (long)Math.Round(total * 100);

        total = total_in_cents / 100.0;
        WriteLine("total = {0}", total);
        return(0);
    }
Exemple #15
0
    public static int Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();
        decimal      amount;     // amount of loan
        double       rate;       // interest rate
        int          months;     // number of periods

        // Prompt for amount of loan, interest rate, and months
        amount = iw.getDecimal("amount: ");
        rate   = iw.getDouble("rate: ");
        months = iw.getInt("months: ");
        // Calculate monthly payment using payment() method
        decimal monthlyPayment = payment(amount, rate, months);

        // Display this payment
        Console.WriteLine("The monthly payment is: {0}", monthlyPayment);
        Console.ReadLine();
        return(0);
    }
Exemple #16
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input 10 numbers:");
            InputWrapper iw = new InputWrapper();

            int[] a     = new int[10];
            int   sum   = 0;
            int   count = 0;

            for (int i = 0; i < a.Length; i++)
            {
                Console.Write("Number {0}", ++count);
                a[i] = iw.getInt(": ");
            }
            for (int i = 0; i < a.Length; i++)
            {
                sum += a[i];
            }
            float average = sum / 10;

            Console.WriteLine("The sum of the numbers: {0}", sum);
            Console.WriteLine("The average of the numbers: {0}", average);
        }
Exemple #17
0
    public static void Main()
    {
        // Initialize strings and show starting state
        list = new List <string>(4);
        ShowCount();
        AddString("Bob");
        AddString("Mary");
        AddString("Charlie");
        ShowList(list);
        ShowCount();
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowList(list);
                }
                else if (cmd.Equals("array"))
                {
                    ShowArray(list);
                }
                else if (cmd.Equals("enum"))
                {
                    ShowEnum(list);
                }
                else if (cmd.Equals("add"))
                {
                    string str = iw.getString("string: ");
                    AddString(str);
                }
                else if (cmd.Equals("remove"))
                {
                    string str = iw.getString("string: ");
                    RemoveString(str);
                }
                else if (cmd.Equals("removeat"))
                {
                    int index = iw.getInt("index: ");
                    RemoveAt(index);
                }
                else if (cmd.Equals("index"))
                {
                    string str = iw.getString("string: ");
                    ShowIndex(str);
                }
                else if (cmd.Equals("count"))
                {
                    ShowCount();
                }
                else if (cmd.Equals("sort"))
                {
                    Sort();
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }
Exemple #18
0
    public static void Main()
    {
        // Initialize accounts and show starting state
        Account[] accounts = new Account[3];
        accounts[0] = new CheckingAccount(100, "Bob", 0);
        accounts[1] = new SavingsAccount(200, "Mary", 1);
        accounts[2] = new CheckingAccount(300, "Charlie", 2);
        ShowAccounts(accounts);
        acc = accounts[0];
        SetInterfaces();
        Console.WriteLine(istat.GetStatement());
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowAccounts(accounts);
                }
                else if (cmd.Equals("account"))
                {
                    int id = iw.getInt("account id: ");
                    acc = accounts[id];
                    SetInterfaces();
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("deposit"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    iacc.Deposit(amount);
                    ShowBalance(istat);
                }
                else if (cmd.Equals("withdraw"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    iacc.Withdraw(amount);
                    ShowBalance(istat);
                }
                else if (cmd.Equals("statement"))
                {
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("post"))
                {
                    istat.Post();
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("month"))
                {
                    istat.MonthEnd();
                    Console.WriteLine(istat.GetStatement());
                }
                else if (cmd.Equals("fee"))
                {
                    if (ichk == null)
                    {
                        Console.WriteLine("IChecking is not supported");
                    }
                    else
                    {
                        Console.WriteLine("fee = {0:C}", ichk.Fee);
                    }
                }
                else if (cmd.Equals("interest"))
                {
                    if (isav == null)
                    {
                        Console.WriteLine("ISavings is not supported");
                    }
                    else
                    {
                        Console.WriteLine("interest = {0:C}", isav.Interest);
                    }
                }
                else if (cmd.Equals("rate"))
                {
                    if (isav == null)
                    {
                        Console.WriteLine("ISavings is not supported");
                    }
                    else
                    {
                        Console.WriteLine("rate = {0}", isav.Rate);
                    }
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }
Exemple #19
0
    public static void Main()
    {
        string       cmd;
        InputWrapper iw = new InputWrapper();

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString(": ");
        while (!cmd.Equals("quit"))
        {
            if (cmd.Equals("create"))
            {
                size  = iw.getInt("size: ");
                array = new int[size];
            }
            else if (cmd.Equals("destroy"))
            {
                array = null;
                size  = 0;
            }
            else if (cmd.Equals("squares"))
            {
                for (int i = 0; i < size; i++)
                {
                    array[i] = i * i;
                }
                show();
            }
            else if (cmd.Equals("reverse"))
            {
                int[] temp = new int[size];
                for (int i = 0; i < size; i++)
                {
                    temp[i] = array[size - 1 - i];
                }
                array = temp;
                show();
            }
            else if (cmd.Equals("show"))
            {
                show();
            }
            else if (cmd.Equals("sort"))
            {
                if (array != null)
                {
                    Array.Sort(array);
                    show();
                }
            }
            else if (cmd.Equals("random"))
            {
                Random rand = new Random();
                for (int i = 0; i < size; i++)
                {
                    array[i] = rand.Next(100);
                }
                show();
            }
            else if (cmd.Equals("foreach"))
            {
                if (array != null)
                {
                    foreach (int x in array)
                    {
                        Console.Write("{0} ", x);
                    }
                    Console.WriteLine();
                }
            }
            else if (cmd.Equals("search"))
            {
                int target = iw.getInt("target: ");
                int index  = Array.BinarySearch(array, target);
                if (index < 0)
                {
                    Console.WriteLine("{0} not found", target);
                }
                else
                {
                    Console.WriteLine("{0} found at index {1}", target, index);
                }
            }
            else
            {
                help();
            }
            cmd = iw.getString(": ");
        }
    }
    public static void Main()
    {
        Account   acc;
        IChecking ichk;
        ISavings  isav;

        // Initialize accounts and show starting state
        Account[] accounts = new Account[3];
        accounts[0] = new CheckingAccount(100, "Bob", 0);
        accounts[1] = new SavingsAccount(200, "Mary", 1);
        accounts[2] = new CheckingAccount(300, "Charlie", 2);
        ShowAccounts(accounts);
        acc = accounts[0];
        Console.WriteLine(acc.GetStatement());
        // Command processing loop
        InputWrapper iw = new InputWrapper();
        string       cmd;

        Console.WriteLine("Enter command, quit to exit");
        cmd = iw.getString("> ");
        while (!cmd.Equals("quit"))
        {
            try
            {
                if (cmd.Equals("show"))
                {
                    ShowAccounts(accounts);
                }
                else if (cmd.Equals("account"))
                {
                    int id = iw.getInt("account id: ");
                    acc = accounts[id];
                    Console.WriteLine(acc.GetStatement());
                }
                else if (cmd.Equals("deposit"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Deposit(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("withdraw"))
                {
                    decimal amount = iw.getDecimal("amount: ");
                    acc.Withdraw(amount);
                    ShowBalance(acc);
                }
                else if (cmd.Equals("statement"))
                {
                    Console.WriteLine(acc.GetStatement());
                }
                else if (cmd.Equals("fee"))
                {
                    // cast to interface and check for exception
                    try
                    {
                        ichk = (IChecking)acc;
                        Console.WriteLine("fee = {0:C}", ichk.Fee);
                    }
                    catch (InvalidCastException e)
                    {
                        Console.WriteLine("IChecking in not supported");
                        Console.WriteLine(e.Message);
                    }
                }
                else if (cmd.Equals("interest"))
                {
                    // use C# "is" operator
                    if (acc is ISavings)
                    {
                        isav = (ISavings)acc;
                        Console.WriteLine("interest = {0:C}", isav.Interest);
                    }
                    else
                    {
                        Console.WriteLine("ISavings in not supported");
                    }
                }
                else if (cmd.Equals("rate"))
                {
                    // use C# "as" operator
                    isav = acc as ISavings;
                    if (isav != null)
                    {
                        //isav = (ISavings) acc;	// cast not necessary
                        Console.WriteLine("rate = {0}", isav.Rate);
                    }
                    else
                    {
                        Console.WriteLine("ISavings in not supported");
                    }
                }
                else
                {
                    help();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
            cmd = iw.getString("> ");
        }
    }