Results 1 to 3 of 3
  1. #1

    Default Advance Calculator: C#


    Guyz finally figured out how to do the mdas prob...the problem is this...the user would input mathematical equations like 4+3*5 then the program would give u the answer...simple enough but theres more it should follow the mdas rule....took me weeks before i figured the mdas thing but finally done with it...but theres another problem...it should also be able to read negative integers......my brain is about to explode...this week is the deadline to pass this exercise and no one in the class is even near the answer...pls guyz i need ur help...heres the code...it works perfectly fine but no idea how to do the negative thing

    Code:
    static void Main(string[] args)
            {
                string qans = "yes";
                do
                {
                Console.Write("Input Expression:");
                string input = Console.ReadLine();
                Calculate(input);
                Console.WriteLine("\n\n\nAnother Expression[yes/no]: ");
                qans = Console.ReadLine();
                Console.Clear();
                }while(qans== "yes");
    
                Console.ReadLine();
            }
    
    
    
    
    
            private static void Calculate(string input)
            {
                try
                {
    
                    string[] array = new string[input.Length];
                    string[] revarray = new string[input.Length];
    
    
                    const string operators = "+-*/";
    
    
                    if (input.Length == 0)
                        return;
    
                    char[] temp = input.ToCharArray();
                    string str = "";
                    int i = 0;
                    int ctr = 0;
                    int len = temp.Length;
                    foreach (char ch in temp)
                    {
                        if (Char.IsDigit(ch))
                        {
                            str += ch.ToString();
    
                            if (i == len - 1)
                            {
    
                                array[ctr] = str;
    
                            }
    
                        }
                        else
                        {
    
    
                            array[ctr] = str;
                            ctr = ctr + 1;
                            str = "";
                            if (operators.Contains(ch.ToString()))
                            {
    
                                array[ctr] = ch.ToString();
                                ctr = ctr + 1;
                            }
                        }
                        i++;
    
                    }
                    decimal tempans;
                    for (ctr = 0; ctr < input.Length; ctr++)
                    {
                        if (array[ctr] == "*")
                        {
                            tempans = decimal.Parse(array[ctr - 1]) * decimal.Parse(array[ctr + 1]);
                            array[ctr] = "+";
                            array[ctr - 1] = "0";
                            array[ctr + 1] = tempans.ToString();
    
    
    
                        }
                        if (array[ctr] == "/")
                        {
                            tempans = decimal.Parse(array[ctr - 1]) / decimal.Parse(array[ctr + 1]);
                            array[ctr] = "+";
                            array[ctr - 1] = "0";
                            array[ctr + 1] = tempans.ToString();
    
                        }
                    }
                    decimal ans = decimal.Parse(array[0]);
                    for (ctr = 1; ctr < input.Length; ctr = ctr + 2)
                    {
                        if (array[ctr] == "+")
                        {
                            ans = ans + decimal.Parse(array[ctr + 1]);
                        }
                        if (array[ctr] == "-")
                        {
                            ans = ans - decimal.Parse(array[ctr + 1]);
                        }
    
                    }
                   
            
                    Console.WriteLine("The answer is: " + ans);
                }
                catch
                {
                    Console.WriteLine("That Was An Invalid Entry!!!");
                }
    
            }

  2. #2
    hala pareha lagi ta exercise bai...exercise 2 ni noh ni maam jenny...hehehe...lisod bitaw kaayo bro...dili ba stacks ang gamitun ana?

  3. #3
    I'll try to figure this out tonight...

  4.    Advertisement

Similar Threads

 
  1. Love calculator
    By Meganda in forum "Love is..."
    Replies: 30
    Last Post: 04-20-2010, 01:20 AM
  2. Advance Java
    By gloryhunter in forum Programming
    Replies: 25
    Last Post: 11-15-2007, 10:14 AM
  3. Replies: 18
    Last Post: 10-10-2005, 11:46 AM
  4. Advanced webdevelopers and video editors
    By janspeed in forum Websites & Multimedia
    Replies: 50
    Last Post: 06-12-2005, 07:09 PM
  5. See what ADVANCED really means.
    By $dbpasswd in forum Websites & Multimedia
    Replies: 22
    Last Post: 05-04-2005, 01:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top