Results 1 to 7 of 7

Thread: Stacks: Help!!!

  1. #1

    Default Stacks: Help!!!


    Mabuang ko sa amo exercise run...2 weeks na gyud ko nag trabaho ani di gyud na ko ma imagine unsaon...palihug kug tabang guyz...c# console application ra ni siya..not asking for spoon feed help pero pls guide me lang on the right track..hehehe

    ang problem is ang user mo input ug mathematical expression like 78-93*62-87..dayun isud kuno tanang elements sa stack dayun iyang i compute ang result...mabuang gyud ko..unsaon man kuno nag tell sa program kung unsa ang number and unsa ang operations...ahhh kalibug...patabang lang ko guyz hap

  2. #2

  3. #3
    stacks pud amo lesson run...kana man nang FILO noh? suwayan nya nko solve bai
    Last edited by Deathnote; 08-31-2009 at 11:45 PM.

  4. #4
    waaa nag nose bleed ko anang reverse polish notation..dili man guro na magamit dri na program sir....or wala gyud ko kasabot sa ako gibasa...hehehe...walay mas simple dra na explanation? pero tnx bitaw sir for d info

  5. #5
    Quote Originally Posted by Thelastairbender View Post
    Mabuang ko sa amo exercise run...2 weeks na gyud ko nag trabaho ani di gyud na ko ma imagine unsaon...palihug kug tabang guyz...c# console application ra ni siya..not asking for spoon feed help pero pls guide me lang on the right track..hehehe

    ang problem is ang user mo input ug mathematical expression like 78-93*62-87..dayun isud kuno tanang elements sa stack dayun iyang i compute ang result...mabuang gyud ko..unsaon man kuno nag tell sa program kung unsa ang number and unsa ang operations...ahhh kalibug...patabang lang ko guyz hap
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    
    namespace ConsoleCalc
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("Input Expression:");
                string input = Console.ReadLine();
                Calculate(input);
                Console.ReadLine();
            }
    
            private static void Calculate(string input)
            {
                Decimal answer, currNum = 0;
                string operand;
                const string operators = "+-*/";
                Stack revStack = new Stack();
                Stack stack = new Stack();
    
                if (input.Length == 0) // ignore if no input
                    return;
    
                char[] temp = input.ToCharArray();
                string str = "";
                int i = 0;
                int len = temp.Length;
                foreach (char ch in temp)
                {
                    if (Char.IsDigit(ch)) //is digit?
                    {
                        str += ch.ToString(); // store the digits temporarily, for multi-digits
                        if (i == len - 1) // is this the last?
                        {
                            stack.Push(str); // yes, push it.
                        }
                            
                    }
                    else
                    {
                        // if the next char is not digit, probably operator, push your temp digits.
                        stack.Push(str); 
                        str = ""; // clear temp
                        if (operators.Contains(ch.ToString()))  // is token an operator?
                            stack.Push(ch);          // yes, push it
                    }
                    i++;
                }
    
                // reverse the stack! it's upside down!!!
                while (stack.Count > 0)
                    revStack.Push(stack.Pop());
    
                // revStack is now in left to right order,
                // top of stack to bottom. assign it to stack!
                stack = revStack;
    
                // calculate
    
                // get the top of stack number. proper data validation
                // would check each of the stack pops for expected token
                // Use TryParse().
                answer = Decimal.Parse(stack.Pop().ToString());
    
                while (stack.Count > 0)
                {
                    operand = stack.Pop().ToString();
                    currNum = Decimal.Parse(stack.Pop().ToString());
    
                    //1+2
                    answer = Arithmetic(answer, operand, currNum);
                }
    
                Console.WriteLine("The answer is " + answer.ToString() + ".");
            }
    
            private static Decimal Arithmetic(Decimal a, string oper, Decimal b)
            {
                Decimal result = 0;
    
                switch (oper)
                {
                    case "+":
                        result = a + b;
                        break;
    
                    case "-":
                        result = a - b;
                        break;
    
                    case "*":
                        result = a * b;
                        break;
    
                    case "/":
                        result = a / b;
                        break;
    
                    default:
                        break;
                }
    
                return result;
            }
        }
    }

  6. #6
    waaa...sir gi suwayan nko it works pero it doesnt follow the mdas rule...like if the expression is 3+5*4 32 man iya i answer T.T...need more help guyz but thank you sir null

  7. #7
    that code evaluates expressions the same as the typical calculator like the windows calculator...you can rewrite your expression to 5*4+3...

  8.    Advertisement

Similar Threads

 
  1. need help about stacks!
    By mediaskare in forum Programming
    Replies: 7
    Last Post: 08-01-2012, 12:22 PM
  2. help!!! ebay item...
    By ipog in forum Business, Finance & Economics Discussions
    Replies: 23
    Last Post: 08-25-2008, 01:30 PM
  3. HELP ABOUT AVATAR
    By sab0y in forum Support Center
    Replies: 59
    Last Post: 10-09-2007, 12:49 PM
  4. need help with p900
    By kloos in forum Gizmos & Gadgets (Old)
    Replies: 8
    Last Post: 09-04-2006, 01:55 PM
  5. Help Me I Cant Log In
    By mark eugene in forum Support Center
    Replies: 12
    Last Post: 06-06-2005, 02:35 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