Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 一叠串_C_String_Stack - Fatal编程技术网

C 一叠串

C 一叠串,c,string,stack,C,String,Stack,嗨,我这里有一个接受int作为值的程序。我想将它转换为接受数组中的字符串。我读过关于使用struct的书,但我没能深入了解它。我希望有人能帮助我不使用struct,我不知道从哪里开始,我想保留这行代码 #include <stdio.h> #include <ctype.h> #include <conio.h> #include <string.h> #include <stdlib.h> int top = 0; int *sta

嗨,我这里有一个接受int作为值的程序。我想将它转换为接受数组中的字符串。我读过关于使用struct的书,但我没能深入了解它。我希望有人能帮助我不使用struct,我不知道从哪里开始,我想保留这行代码

#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");
}
#包括
#包括
#包括
#包括
#包括
int-top=0;
int*stack=NULL;
int size=0;
main()
{
int opt,num;
char cont[]={'y'};
clrsc();
/*  */
/*  0)
{
堆栈=malloc(大小*sizeof(int));
if(stack==NULL)
{
printf(“错误:malloc()失败\n”);
出口(2);
}
}
其他的
{
printf(“错误:大小应为正整数\n”);
出口(1);
}
/*}end>*/
while((续[0]='y')| |(续[0]='y'))
{
clrsc();
/*  */
printf(“\n\n是否继续\(Y\/n\)?”;
scanf(“%s”和&cont[0]);
}
自由(堆叠);
}
流行音乐()
{
INTA;
加载();

if(top最简单的方法是将堆栈转换为存储
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;
}
当你弹出一个字符串时,你只会得到一个指针

char *pop()
{
    if( top == 0 ) return NULL;
    return stack[--top];
}
完成指针操作后,您负责释放内存(通过调用
free


首先要做的事情之一是将
int*stack=NULL
更改为
char**stack=NULL
char * val;
while( NULL != (val = pop()) )
{
    printf( "I popped: %s\n", val );
    free(val);
}