Well, I finished up my project... one week early too! So I figured I'll post it here and perhaps someone could blow a whistle and tell me of some things I could do to improve it!
Basically, the program reads in a file with a single equation, in postix form, and solves it.
I basically used a linked list implementation of a stack (because I wanted to, dammit!), but it would be nice if I could think of a way to pass one parameter to pop(*node, *node) rather than 2.
Any other comments (memory leaks, inefficeincy, insults, etc.) are welcomed, and the code is released for public use under the GPL.
Code:#include <iostream.h> #include <stdio.h> #include <fstream.h> #include <cstring> struct node { int key; struct node *next;}; typedef node* stack_ptr; void push(stack_ptr& a_stack, int number); void print_list(stack_ptr& a_stack, stack_ptr& b_stack); int pop(stack_ptr& a_stack,stack_ptr& b_stack); main() { char inFileName[16], str1[50]; int n, v, total=0; struct node *t, *x; ifstream read; /* initialize the list */ t = new node; t->next = NULL; x = t; /****** Ask user for input file *****/ cout <<"Enter file name to be read: "; cin >> inFileName; /***** open file **********/ read.open(inFileName); if (read.fail()) { cout << "Input file opening failed!"<<endl; exit(1); } /********************************************************* * Read file for digits and operators * (it is assumed there are no illegal chars in the file *********************************************************/ for (int i = 0; !read.eof(); i++) { read.getline(str1,50, ' '); if (isdigit(str1[0])) { v = atoi(str1); push(t, v); } else if (str1[0] == '+') { v = pop(t, x) + pop(t,x); push (t, v); } else if (str1[0] == '-') { v = pop(t, x) - pop(t,x); push (t, v); } else if (str1[0] == '*') { v = pop(t, x) * pop(t,x); push (t, v); } else if (str1[0] == '/') { v = pop(t, x) / pop(t,x); push (t, v); } } /**************************************************** * Now print all the numbers left in the linked list * which should only be one left if correct postfix * notation was used in the input file. ****************************************************/ print_list(t, x); return 0; } void push(stack_ptr& a_stack, int number) { a_stack->next = new node; a_stack->next->next = NULL; a_stack->key = number; a_stack = a_stack->next; } void print_list(stack_ptr& a_stack, stack_ptr& b_stack) { if (b_stack->next == NULL) { cout << "The stack is currently empty! "<<endl; } else { a_stack = b_stack; do { if (a_stack->next != NULL) cout << a_stack->key << " "; a_stack = a_stack->next; }while(a_stack->next != NULL); cout << endl; } } int pop(stack_ptr& a_stack,stack_ptr& b_stack) { int rtn_value = 0; if (b_stack->next == NULL) { cout << "The stack is currently empty! "<<endl; } else { a_stack = b_stack; while(a_stack->next->next != NULL) a_stack = a_stack->next; rtn_value = a_stack->key; delete a_stack->next; a_stack->next = NULL; } return rtn_value; }



LinkBack URL
About LinkBacks




I used to be an adventurer like you... then I took an arrow to the knee.