对此声明感到困惑

时间:2015-05-14 01:18:56

标签: c loops while-loop user-input

我为课程编写了这个程序,我似乎无法让while循环工作..我不知道我错在哪里,这可能是一个小错误,我知道我做了很多...谢谢! PS我对这些东西很陌生,所以如果它很小的话,对我来说很容易:P

#include <stdio.h>

#define STACK_SIZE 10
#define TRUE 1
#define FALSE 0
#define MAXNUM 5

/***************
prototypes
***************/
void make_empty(int *top);
int is_full(int *top);
int push(int content[], int maxnum, int newnum, int *top);
int pop(int contents[], int maxmun, int *top);
void printStack(int contents[], int maxnum, int *top);
int is_empty(int *top);
int search(const int content[], int maxnum, int num);

// main function
int main(void)
{
    int popNum = 0;
    int foundIndex = -1;
    int i;
    int contents[STACK_SIZE] = { 0 };
    int top = 0;
    int input = 0;


    while (! == 0)
    {
        printf("/n/nPick a number from 1 to 5, type the number then press enter: ");
        switch (input)
        {
        case 1:
            push(contents, MAXNUM, popNum, &top);
            break;
        case 2:
            if (pop(contents, MAXNUM, &top) <= 0)
                printf("Error popping stack \n");
            break;
        case 3:
            make_empty(&top);
            break;
        case 4:
            printStack(contents, MAXNUM, &top);
        }//End Loop Switch

    }

} // end main()

/******************************************
**
** make_empty function **
**
******************************************/

void make_empty(int *top)
{
    *top = 0;
}

/******************************************
**
** is_empty function **
**
******************************************/

int is_empty(int *top)
{
    if (*top == 0)
        return TRUE;
    else
        return FALSE;
}

/******************************************
**
** is_full function **
**
******************************************/

int is_full(int *top)
{
    static int nCalls = 0; // static variable

    nCalls++;
    if (*top == STACK_SIZE)
        return TRUE;
    else
        return FALSE;
}

/******************************************
**
** push function **
**
******************************************/

int push(int content[], int maxnum, int newnum, int *top)
{
    int nCalls = 0;
    nCalls++;
    if (is_full(top))
        return FALSE;
    else
        content[(*top)++] = newnum;
}

/******************************************
**
** pop function **
**
******************************************/

int pop(int contents[], int maxmun, int *top)
{
    if (is_empty(top))
        return FALSE;
    else
        return contents[--(*top)];
}

/*******************************
**
** printStack fuction **
**
******************************/
void printStack(int contents[], int maxnum, int *top)
{
    int i;
    if (!is_empty(top))
    {
        for (i = 0; i < *top; i++)
            printf("Number %d = %d\n", i, contents[i]);
    }
    else
        printf("Stack is empty");
}

/*******************************
**
** search function **
**
******************************/
int search(const int content[], int maxnum, int num)
{

    int fIndex = -1;

    for (int i = 0; i < maxnum; i++)
    {
        if (content[i] == num)
        {
            fIndex = i;
            break;
        }
    }

    return (fIndex);

}

/*




*/

2 个答案:

答案 0 :(得分:1)

第1期&gt;&gt; while (! == 0)

如果要无条件循环,请使用while (1)switch内部情况,添加一个默认(或编号)大小写,以保持循环的break语句。< / p>

第2期&gt;&gt; switch (input)

似乎计划要求用户输入,但您没有为此编码。在基于输入值切换之前,您需要从用户获取值并存储到input变量中。利用scanf()功能。有关详细信息,请阅读man page

答案 1 :(得分:0)

这个while循环是完全错误的......你正在使用switch case进行输入...... 输入 = 0总是......你没有做scanf到获取输入值...如果 输入 没有改变,将如何调用其他函数?....

相关问题