Advertisement
Guest User

Untitled

a guest
May 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. void push(char inputData[]);
  7. void pop();
  8. void printStack();
  9.  
  10.  char data[300][100];
  11.   int current = 0;
  12.  
  13. /*
  14.  *
  15.  */
  16. int main(int argc, char** argv) {    
  17.    
  18.     FILE *file;
  19.     if (argc > 1) {
  20.         file = fopen(argv[1], "r");
  21.         char line[256];
  22.  
  23.         while (fgets(line, sizeof (line), file)) {
  24.  
  25.             if (line[0] == '+') {
  26.                 memmove(line, line + 1, strlen(line));                                                  
  27.                 if (line[strlen(line) - 1] != NULL && line[strlen(line) - 1] == '\n') {                
  28.                     line[strlen(line) - 1] = '\0';                                                      
  29.                 }                          
  30.                 push(line);
  31.                 printf("Added %s to the stack\n", line);
  32.  
  33.  
  34.             } else if (line[0] == '-') {
  35.                 pop();
  36.             }
  37.         }
  38.     }
  39.          printStack();
  40.     return (EXIT_SUCCESS);
  41. }
  42.  
  43. void push(char inputData[]) {
  44.     int i = 0;
  45.     for(i = 0; i < strlen(inputData); i++) {
  46.         data[current][i] = inputData[i];
  47.     }
  48.     current++;
  49.     printf("\n Current Place: %d",current);
  50. }
  51.  
  52. void pop() {
  53.     printf("Pop: %s",data[current]);    
  54.     current--;
  55. }
  56.  
  57. void printStack() {
  58.     int i;
  59.     printf("\n[");
  60.     for(i = 0; i < current;i++) {
  61.         printf("(%s)",data[i]);
  62.     }
  63.     printf("]");
  64.    
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement