【问题标题】:character scanning via user switch-case通过用户 switch-case 进行字符扫描
【发布时间】:2014-11-10 19:40:18
【问题描述】:

我正在尝试编写一个代码,该代码通过用户获取数字并读取字符“e”或“”(空格)以及数字。我的意思是数字“e”或空格“e”数字“e”或空格等等。但我得到的数字很荒谬。程序将显示添加的号码。如果输入 '\n' 将停止取号(scanf 将停止)。 (我很抱歉标题,因为我对标题一无所知) 我哪里错了。感谢您对所有赞赏的回答。

示例输入:

  • e 1 8 7 2 3 6
  • e 1 e 8 e 7 e 2 e 3 e 6

    #include <stdio.h>

    #define MAX 10

    void addq ( int *, int, int *, int * ) ;
    void test();

    int main( )
    {

        test();
        return 0;
    }


    void test(){

        int arr[MAX] ;
        int i, front, rear,num ;
        char ch;

        front = rear = -1 ;
        scanf("%c",&ch);
        /* initialise data member */
        switch(ch){

        case 'e':
        case ' ':
            for ( i = 0 ; i < MAX; i++ ){
            arr[i] = scanf("%d",&num);
            scanf("%c",&ch);
            addq ( arr, num, &front, &rear );
            }
            break;

        }

        printf ( "\nElements in the circular queue: " ) ;
        display ( arr ) ;

}
void display ( int * arr )
{
int i ;
printf ( "\n" ) ;
for ( i = 0 ; i < MAX ; i++ )
    printf ( "%d\t", arr[i] ) ;
printf ( "\n" ) ;
}
    /* adds an element to the queue */
    void addq ( int *arr, int item, int *pfront, int *prear )
    {
        if ( ( *prear == MAX - 1 && *pfront == 0 ) || (  *prear + 1 == *pfront ) )
        {
            printf ( "\nQueue is full." ) ;
            return ;
        }

        if ( *prear == MAX - 1 )
            *prear = 0 ;
        else
            ( *prear )++ ;

        arr[*prear] = item ;

        if ( *pfront == -1 )
            *pfront = 0 ;
    }

【问题讨论】:

    标签: c arrays function for-loop switch-statement


    【解决方案1】:

    当你使用

    scanf("%c",&ch);
    

    它会跳过空格。你应该使用

    int ch; // Use type int, not char.
    ch = fgetc(stdin);
    

    【讨论】:

    • 我不想跳过空格。因为当用户按下 enter 时,scannig 必须停止。我的意思是'\n'
    • 然后测试fgetc();的输出: if(ch == '\n'){stop scan}。
    • 我输入了e 1 e 8 e 7 e 2 e 3 e 6 e 4 e 3 e 2,但程序显示1 1 8 8 7 7 2 2 3 3
    • @Soner,听起来您的代码中还有其他逻辑错误。从您的帖子中不清楚您希望在'e'' ' 之后看到多少数字。您使用for ( i = 0 ; i &lt; MAX; i++ ){ 读取数字与示例输入不匹配。
    • @RSahu 当我按下回车键时,必须停止扫描。此外,输入可以用空格或“e”写成“e”。但是当开始输入输入时,必须以'e'开头,如e 1 8 7 2 3 6e 1 e 8 e 7 e 2 e 3 e 6,按回车后显示(arr)将在屏幕上显示数字,如@9​​87654331@
    【解决方案2】:
    #include <stdio.h>
    
    #define MAX 10
    
    typedef struct q {
        int arr[MAX];
        int front, rear;
    } Q;
    
    void init_q(Q *q);
    void add_q (Q *q, int v);
    void display_q(Q *q);
    void test();
    
    int main( ){
        test();
        return 0;
    }
    
    void test(){
        int num, end = 0;
        char ch;
        Q q;
        init_q(&q);
    
        scanf("%c", &ch);
        while(!end){
            switch(ch){
            case 'e':
            case ' ':
                if(1==scanf("%d", &num))
                    add_q(&q, num);
                scanf("%c", &ch);
                break;
            case '\n':
                end = 1;
                break;
            default:
                printf("invalid input(%c)\n", ch);
                scanf("%c", &ch);
            }
        }
    
        printf("\nElements in the circular queue: ") ;
        display_q(&q) ;
    }
    
    void init_q(Q *q){
        q->front = q->rear = -1;
    }
    int empty_q(Q *q){
        return q->front == -1;
    }
    int full_q(Q *q){
        return (q->rear == MAX - 1 && q->front == 0) || q->rear + 1 == q->front;
    }
    void add_q (Q *q, int item){
        if(full_q(q)){
            printf( "\nQueue is full.\n");
            return ;
        }
    
        if( q->rear == MAX - 1 )
            q->rear = 0;
        else
            q->rear++;
    
        q->arr[q->rear] = item;
    
        if(q->front == -1 )
            q->front = 0;
    }
    void display_q(Q *q){
        if(!empty_q(q)){
            int i = q->front;
            printf("\n");
            for(;;){
                printf("%d\t", q->arr[i]);
                if(i++ == q->rear)
                    break;
                if(i == MAX)
                    i = 0;
            }
            printf("\n");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多