【问题标题】:How to tag taken seats in Cinema. C Programming如何标记电影院中的座位。 C 编程
【发布时间】:2014-03-29 15:41:48
【问题描述】:

首先我想说我是 C 和编程的初学者。 C 是我的第一语言,我觉得它很有趣。 我正在编写一个模拟电影软件的程序。我的意思是你选择电影、时间和座位。 我已经完成了电影和时间的选择,但座位有问题。 我想做的是当你选择行和列时,以某种方式在控制台中打印出哪个座位被占用(改变颜色,增加字体或类似的东西)

这是我的代码:

 void SeatSelection()
{
    int row = 0;
    int column = 0;
    int i, j;
    printf("\t\t\t\tSCREEN\n\n\n\n\n\n\n");
    int A[11][11] = {
        { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
        { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
    };

    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 11; j++)
            printf("%d\t", A[i][j]);

        printf("\n\n");
    }

    do
    {
        printf("Choose seat: Row and Column\n");
        scanf("%d %d", &row, &column);
        if ((row<1 || row>10) && (column<1 || column>10)) printf("Wrong choice, try again\n");
    } while ((row<1 || row>10) && (column<1 || column>10));
}

提前感谢您的帮助:)

【问题讨论】:

    标签: c console


    【解决方案1】:

    尝试使用第二张表taken[][]。对于一个座位(row, column)take[row][column],如果座位被占用,则为 1,否则为 0。代码:

    #include <stdio.h>
    
    void SeatSelection()
    {
        int row = 0;
        int column = 0;
        int i, j;
        int A[11][11] = {
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
        };
    
        int taken[11][11] = {
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        };
    
        printf("============= The Cinema ==============\n");
        for (i = 1; i <= 10; i++)
        {
            for (j = 1; j <= 10; j++)
                printf(" %d  ", A[i][j]);
    
            printf("\n");
        }
        fflush(stdout);
        do
        {
            printf("Choose seat: Row and Column\n");
            fflush(stdout);
            scanf("%d %d", &row, &column);
            if ((row<1 || row>10) && (column<1 || column>10)) printf("Wrong choice, try again\n");
            if(taken[row][column]) printf("This seat is taken, try again\n");
            else {
              taken[row][column] = 1;
              printf("======== The Cinema =========\n");
              for (i = 1; i <= 10; i++)
              {
                  for (j = 1; j <= 10; j++) {
                      if(taken[i][j] == 0)
                        printf(" %d  ", A[i][j]);
                      else
                        printf("[%d] ", A[i][j]);
                  }
                  printf("\n");
              }
            }
            fflush(stdout);
        } while (true);
    }
    
    int main() {
      SeatSelection();
      return 0;
    }
    

    编辑

    我对你的代码做了一些改动,但我想你明白了 :) 如果你不明白什么就告诉我...

    【讨论】:

    • 这正是我的想法,非常感谢!编辑:我不明白您为什么将第一列和第一行更改为 0?
    • @user3476022 我对第一行的使用以及最后一列的初始化有点困惑。从这两个来看,第二个更重要,因为编译器会在未初始化的位置放置随机整数,这可能会导致特殊的运行时错误......
    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    相关资源
    最近更新 更多