C语言剧程序问题

时间:2015-11-10 04:51:22

标签: c arrays eclipse multidimensional-array

我正在为我的班级创建一个程序。我必须显示一个有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;
}

您知道,我的教授指示班级在每个fflush(stdout)语句后写printf(),因为他说Eclipse(我的编译器)中有一个错误使得它成为必要。

1 个答案:

答案 0 :(得分:1)

如果您正在寻找可以帮助您解决代码问题的一件事,那么在编译时启用警告。至少-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 = '*';

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

#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) {