gpt4 book ai didi

c - 如何在电影院中标记已占座位。 C编程

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:20 25 4
gpt4 key购买 nike

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

提前感谢您的帮助:)

最佳答案

尝试使用第二个表 taken[][]。对于一个座位 (row, column) 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;
}

编辑

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

关于c - 如何在电影院中标记已占座位。 C编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22733144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com