如何标记电影中的座位。 C编程

时间:2014-03-29 15:41:48

标签: c console

首先,我想说我是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));
}

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

尝试使用第二张表taken[][]。对于座位(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;
}

修改

我已对您的代码进行了一些更改,但我认为您明白了:)如果您不理解某些内容,请告诉我......