Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. OK, as I've explained to you what a stack is and how it works on a conceptual level, let's get started.
  2.  
  3. This will be quite detailed and I'm more or less explaining each line of the code. So don't freak out; just take it line by line and barring any syntax errors the program will work fine. In total this will be about 44 lines of code.
  4.  
  5.  
  6. We will use this to learn about string parsing and error checking eventually. However we will start very simply.
  7.  
  8. Let's call it rpncalc.c. You will almost certainly only need stdio.h.
  9. Create a global variable named stack, which is an array of 5 int, and another global named stkptr (short for stack pointer) which is an int.
  10. Prototype two functions:
  11. void push(int p);
  12. int pop();
  13.  
  14. In main:
  15. - define an input buffer of 256 char;
  16. - define an int named quit, initialised to zero;
  17. - initialise stkptr to zero;
  18. - then enter a while or for loop with !quit as the test.
  19.  
  20. In that loop, display the prompt "RPNCALC> " without a newline, then read a line from the user into the input buffer using:
  21. fgets(buffer name, length of buffer, stdin);
  22.  
  23. We won't use scanf, for two reasons: (1) scanf is a piece of shit that should be avoided at all costs; (2) we don't actually know what type of data the user is going to enter, so we'll have to get something in, then parse it to find out what it is.
  24.  
  25. (I think people like talking about scanf because it's the logical converse of printf, and probably we would need to get input from the user before tackling string parsing. However, users rarely enter formatted data correctly; they like to enter loads of errors, and if they do that to scanf it'll feck up your program. So we use fgets instead.)
  26.  
  27. We'll keep this very simple to start with. Use a switch statement to check the first character of the buffer. (Don't forget we're still inside the !quit loop.)
  28.  
  29. If it's 'q', set quit to 1.
  30.  
  31. If it's '.', display the return value of pop().
  32.  
  33. Otherwise, assume it's an integer, so call push() with the return value of atoi() which itself takes the input buffer as the parameter.
  34. Even though we don't technically need a break statement on this last one, put one in anyway because it's good practice.
  35. Close the switch statement, the !quit loop and main with a } each, giving you a nice cascading:
  36. ........}
  37. ....}
  38. }
  39. (dots only in there because leading spaces would probably be removed)
  40.  
  41. As I've already given you quite a lot to do here, I'll just get you started with push() and pop():
  42.  
  43. void push(int p)
  44. {
  45. stack[stkptr++]=p;
  46. }
  47.  
  48. int pop()
  49. {
  50. return stack[--stkptr];
  51. }
  52.  
  53.  
  54. That's all for this step. All being well it will compile and run. If it doesn't, keep a cool head and check the errors one by one. If you get hundreds of errors, don't panic because you always only ever need to look at the first few. If you're stuck, post me the code and the error messages. By the way, with gcc it's good practice to use -Wall to get full error checking, and to make sure the program compiles without any warnings. So these are two very good habits to get into from the start. I might set up a Linux VM for giggles and check your code with gcc -Wall.
  55.  
  56. Note that currently there is NO error checking. So don't try to "." on an empty stack, or push more than 5 integers onto the stack. Well, you can, it might be interesting. It won't break your computer. You might get a core dump. We'll add error checking later but this is enough for now.
  57.  
  58. (If you've heard of "buffer overflow" bugs, usually in a security context, popping off an empty stack or pushing onto a full stack are both examples of that. Popping off an empty stack is also called "underflow" as a logical opposite of overflow).
  59.  
  60. When it runs, try adding some numbers to the stack and displaying them. When you've had enough, "q" to exit and send me the code.
  61.  
  62. Dave
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement