gpt4 book ai didi

c - C 戏剧节目问题

转载 作者:行者123 更新时间:2023-11-30 19:14:44 25 4
gpt4 key购买 nike

我正在为我的类(class)创建一个程序。我必须展示一个有 15 排、每排 30 个座位的剧院的布局。该程序应该要求用户输入每行的价格,我已经这样做了,并且似乎没有问题。然后用户需要输入正在出售的座位的行数和列数,然后显示已占用座位的剧院。空座位用 # 表示,已占用座位用 * 表示。由于某种原因,我无法让程序正确显示剧院布局。它不使用英镑符号来表示空座位,并且在座位出售时不会显示布局差异。这是我的代码:

#include <stdio.h>
#include <stdlib.h>

void mapSeats(int i, int j, char sts[][30]){
char TAKEN='*';
char EMPTY='#';
int rw=15;
int col=30;
printf(" Seats\n");
fflush(stdout);
printf(" 123456789012345678901234567890");
fflush(stdout);
for(i=0; i<rw; i=i+1){
printf("\nRow %d ", i);
fflush(stdout);
for(j=0; j<col; j=j+1){
printf("%c", sts);
}
}
}

void theatre(void){
int row=15;
int column=30;
float prices[15];
char sts[row][column];
char TAKEN='*';
char EMPTY='#';
int reserve;
int i=0;
int j=0;
//float cost;
//int static total;

printf("Enter the price for each row of seats.\n");
fflush(stdout);
for(row=0; row<15; row=row+1){
printf("Row %d:\n", row);
fflush(stdout);
scanf("%f", &prices[row]);
}

/*printf("What would you like to do? Select a number:\n");
printf("1: Reserve seats");
printf("2: View total ticket sales");
printf("3: View sold and available seats");*/

for(i=0; i<row; i=i+1){
for(j=0; j<column; j=j+1){
sts[i][j]=EMPTY;
}
}

mapSeats(i, j, sts);


printf("\nHow many seats would you like to reserve?\n");
fflush(stdout);
scanf("%d", &reserve);

printf("Enter the row and column number for the desired seat(s).\n");
fflush(stdout);
for(int k=1; k<=reserve; k=k+1){
scanf("%d %d", &row, &column);
printf("\nYou have selected Row %d, Column %d\n", row, column);
fflush(stdout);
for(i=0; i<15; i=i+1){
for(j=0; j<=30; j=j+1){
if(row==i && column==j){
sts[i][j]=TAKEN;
}
}
}
}
mapSeats(i, j, sts);





}

int main(void) {
theatre();
return 0;
}

正如你所知,我的教授指示全类在每个 printf() 语句后编写 fflush(stdout) 因为他说 Eclipse 中有一个错误(我的编译器)这使得它成为必要。

最佳答案

如果您正在寻找一件事可以真正帮助您解决代码问题,请在编译时启用警告。至少-Wall -Wextra。 (您可以添加 -pedantic 以获得其他警告)。

警告会准确告诉您在哪里寻找解决代码问题的方法。例如:

$ gcc -Wall -Wextra -Ofast -o bin/theater theater.c

theater.c: In function ‘mapSeats’:
theater.c:18:13: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char (*)[30]’ [-Wformat=]
printf ("%c", sts);
^
theater.c:7:10: warning: unused variable ‘EMPTY’ [-Wunused-variable]
char EMPTY = '#';
^
theater.c:6:10: warning: unused variable ‘TAKEN’ [-Wunused-variable]
char TAKEN = '*';

修复这些问题将使您的代码能够显示您正在查找的 map :

#include <stdio.h>
#include <stdlib.h>

void mapSeats (int i, int j, char sts[][30])
{
// char TAKEN = '*';
// char EMPTY = '#';
int rw = 15;
int col = 30;
printf (" Seats\n");
fflush (stdout);
printf (" 123456789012345678901234567890");
fflush (stdout);
for (i = 0; i < rw; i = i + 1) {
printf ("\nRow %2d ", i);
fflush (stdout);
for (j = 0; j < col; j = j + 1) {
printf ("%c", sts[i][j]);
fflush (stdout);
}
}
putchar ('\n');
}

void theatre (void)
{
int row = 15;
int column = 30;
float prices[15];
char sts[row][column];
char TAKEN = '*';
char EMPTY = '#';
int reserve;
int i = 0;
int j = 0;
//float cost;
//int static total;

printf ("Enter the price for each row of seats.\n");
fflush (stdout);
for (row = 0; row < 15; row = row + 1) {
printf ("Row %2d:\n", row);
fflush (stdout);
scanf ("%f", &prices[row]);
}

/*printf("What would you like to do? Select a number:\n");
printf("1: Reserve seats");
printf("2: View total ticket sales");
printf("3: View sold and available seats"); */

for (i = 0; i < row; i = i + 1) {
for (j = 0; j < column; j = j + 1) {
sts[i][j] = EMPTY;
}
}

mapSeats (i, j, sts);

printf ("\nHow many seats would you like to reserve?\n");
fflush (stdout);
scanf ("%d", &reserve);

printf ("Enter the row and column number for the desired seat(s).\n");
fflush (stdout);
for (int k = 1; k <= reserve; k = k + 1) {
scanf ("%d %d", &row, &column);
printf ("\nYou have selected Row %d, Column %d\n", row, column);
fflush (stdout);
for (i = 0; i < 15; i = i + 1) {
for (j = 0; j <= 30; j = j + 1) {
if (row == i && column == j) {
sts[i][j] = TAKEN;
}
}
}
}
mapSeats (i, j, sts);
}

int main (void)
{
theatre ();
return 0;
}

示例输出

$ ./bin/theater <seatprice.txt

<snip>

How many seats would you like to reserve?
Enter the row and column number for the desired seat(s).

You have selected Row 1, Column 5

You have selected Row 1, Column 6

You have selected Row 2, Column 6

You have selected Row 2, Column 7
Seats
123456789012345678901234567890
Row 0 ##############################
Row 1 #####**#######################
Row 2 ######**######################
Row 3 ##############################
Row 4 ##############################
Row 5 ##############################
Row 6 ##############################
Row 7 ##############################
Row 8 ##############################
Row 9 ##############################
Row 10 ##############################
Row 11 ##############################
Row 12 ##############################
Row 13 ##############################
Row 14 ##############################

为了完整起见,以下是使用的输入文件:

$ cat seatprice.txt
500
450
400
350
300
250
200
150
125
120
115
110
105
100
90
4
1 5
1 6
2 6
2 7

注意:要更正打印 '*' 的列,请在此处调整索引:

if (row == i && column == j+1) {

关于c - C 戏剧节目问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623107/

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