2012-10-07 123 views
0

嗨,我有程序在這裏接受int作爲價值。我想將它翻譯成接受數組中的字符串。我已閱讀關於使用結構,但我無法進入它。我希望有人可以幫助我進入,而不使用結構我不知道從哪裏開始我想保留這行代碼。堆棧字符串

#include <stdio.h> 
#include <ctype.h> 
#include <conio.h> 
#include <string.h> 
#include <stdlib.h> 

int top = 0; 
int *stack = NULL; 
int size = 0; 

main() 
{ 
    int opt, num; 
    char cont[] = { 'y' }; 
    clrscr(); 

    /* <start Declaring Stack Size { */ 
       printf("Stacking Program"); 
       printf("\n\nData Size: "); 
       scanf("%d", &size); 
       printf("\n"); 
    /* } end> */ 

       /* <start Allocates size of stack { */ 
       if(size > 0) 
       { 
       stack = malloc(size * sizeof(int)); 
       if(stack == NULL) 
       { 
        printf("ERROR: malloc() failed\n"); 
        exit(2); 
       } 
       } 
       else 
       { 
        printf("ERROR: size should be positive integer\n"); 
        exit(1); 
       } 
       /* } end> */ 

    while((cont[0] == 'y') || (cont[0] == 'Y')) 
    { 
     clrscr(); 

     /* <start Main Menu { */ 
      printf("Stacking Program"); 
      printf("\n\nData Size: %d\n\n", size); 
      printf("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: "); 
      scanf("%d", &opt); 
      printf("\n"); 

     switch(opt) { 
      case 1: 
       pop(); 
       break; 
      case 2: 
       if(top==size) 
       { 
        printf("You can't push more data"); 
       } 
       else 
       { 
        printf("Enter data for Stack[%d]: ", top+1); 
        scanf("%d", &num); 
        push(num); 
       } 
       break; 
      case 3: 
       pick(); 
       break; 
      case 4: 
       view(); 
       break; 
      default: 
       printf("Your choice is not on the list."); 
       break; 
     } 
     /* } end> */ 

     printf("\n\nDo you want continue\(Y\/N\)?"); 
     scanf("%s", &cont[0]); 
    } 
    free(stack); 
} 

pop() 
{ 
    int a; 
    loading(); 
    if(top <= 0) 
    { 
     printf("Stack empty."); 
     return 0; 
    } 
    else 
    { 
     top--; 
     a=stack[top]; 
     printf("\(Stack[%d] = %d\) removed.", top+1, a); 
    } 
} 
push(int a) 
{ 
     stack[top]=a; 
     top++; 
     loading(); 
} 
pick() 
{ 
    loading(); 
    if(top <= 0) 
    { 
     printf("Nothing to display."); 
     return 0; 
    } 
    else 
    { 
     printf("\(Stack[%d] = %d\) is the last data.", top, stack[top-1]); 
    } 
} 
view() 
{ 
    int i; 
    loading(); 
    if(top <= 0) 
    { 
     printf("Nothing to display."); 
     return 0; 
    } 
    else 
    { 
     for(i=0;i<top;i++) 
     { 
      printf("Stack[%d] = %d\n", i+1, stack[i]); 
     } 
    } 
} 

loading() 
{ 
    float i, x; 
    float load; 
    int loadarea[] = { 5000, 10000, 15000, 20000, 25000, 30000 }; 
    int percentLoad; 
    x=0; 
    load=0; 
    percentLoad = loadarea[random(5)]; 
    gotoxy(26,11); 
    printf("["); 
    for(i=0;i<25;i++) 
    { 
     x = i+27; 
     gotoxy(x, 11); 
     printf("="); 
     delay(percentLoad); 
     gotoxy(51,11); 
     printf("]"); 
     gotoxy(53,11); 
     load=(i/25)*104.5; 
     if(load>100) 
      load = 100.00; 
     printf("%.2f\%",load); 
    } 
    delay(60000); 
    for(i=0;i<60;i++) { 
     printf("\b \b"); 
    } 
    printf("\n"); 
} 
+2

第一件事情之一是將'int * stack = NULL'改爲'char ** stack = NULL'。 –

回答

2

最簡單方法就是你的籌碼存儲char*,而不是int轉換。現在

char **stack; 
stack = malloc(size * sizeof(char*)); 

,你push操作將接受來自一些緩衝區char*被存儲,這是剛剛輸入的字符串,strdup複製它,而新指針存放在堆棧中。

typedef enum { 
    STACK_MEM_ERROR = -1, 
    STACK_FULL = 0, 
    STACK_OK = 1 
} StackStatus; 

StackStatus push(const char *str) 
{ 
    char *newstr; 
    if(top >= size) return STACK_FULL; 
    newstr = strdup(str); 
    if(newstr == NULL) return STACK_MEM_ERROR; 
    stack[top++] = newstr; 
    return STACK_OK; 
} 

當你pop一個字符串,你只是得到一個指針。

char *pop() 
{ 
    if(top == 0) return NULL; 
    return stack[--top]; 
} 

您負責,當你與指針完成(通過調用free)釋放內存。

char * val; 
while(NULL != (val = pop())) 
{ 
    printf("I popped: %s\n", val); 
    free(val); 
}