0

问题:

Printf 和 Scanf 的问题

目标:

中级编程课程:建立花园商店。

此时,所需要的只是在一条线上输入有关商店的一串数据(以特定模式),例如启动成本、员工成本、平方英尺等。然后以半有意义的方式显示它们。

代码:

#include <stdio.h>
#include <string.h>

#define MAXSTRING       100 

#define MAXROOMS        20
#define MAXSTAFF        30  

#define ANNUAL          0
#define WATER_PERENNIAL 1
#define XERIC_PERENNIAL 2
#define NATIVE_SHRUB    3
#define NATIVE_TREE     4
#define OTHER           5   

#define AMOUNT          0
#define AVGPRICE        1
#define MARKUP          2   

#define MANAGER         0
#define OWNER           1
#define HORTICULTARIST  2
#define SALES_STAFF     3   

#define AVAILABLE       0
#define HRWAGES         1
#define MOHOURS         2   

#define INDOOR          0
#define LOADING         1
#define OUTDOOR         2
#define BATHROOM        3
#define OFFICE          4   

#define LENGTH          0
#define WIDTH           1

int main(void)
{

// Arrays
    float   stockinfo[6][3], staffdata[4][3], roomsize[5][18][2];
            /*
                -->stockinfo [stock type(6)] [Quantity / Cost / percent Markup]
                -->staffdata [staff type(4)] [Quantity / wage / Hrs. per Mo.]
                --> roomsize [room  type(5)] [Room # (i.e. Bathroom #2)] [Length / Width]
            */
    int     roomtype[5], stafftype[4];
    char    sectionname[36][40];
    char    Nstock[6], Nstaff[4], Nrooms[5];
            /*
                N____[ ]: Stock/Staff/Room code (single letter)
            */

// Counters
    int i=0, j=0, k=0, l=0;

// Variables
    float   CBL, CPU, CWU, CBP, CSF, maxwage, O_RSF, I_RSF;
        /* 
           CBL:Cost of Business License ~$500
           CPU:Cost of Power Utility Connection fee ~$250.00
           CWU:Cost of Water Utility Connection fee ~$69.99
           CBP:Cost of Business Permit ~$50.00
           CSF:Cost of Start-up Fund ~$15,000.00
        */
    int Qstock, Qstaff, Qrooms; 
        /*Number of different types of (Stock, Staff, or Rooms) */
    char    Z; 
        /* Character place holder */

// Code:
    printf("prompt>");
    scanf("%f %f %f %f %f", &CBL, &CPU, &CWU, &CBP, &CSF);
    scanf("%d", &Qstock);

    for(i=0; i<Qstock; i++)
        scanf(" %c", &Nstock[i]);
    for(j=0; j<3; j++)
    {
        for(i=0; i<Qstock; i++)
        {
            Z = Nstock[i];
            switch( Z )
            {
                case 'A': scanf(" %f", &stockinfo[ANNUAL][j]);
                    break;
                case 'W': scanf(" %f", &stockinfo[WATER_PERENNIAL][j]);
                    break;
                case 'X': scanf(" %f", &stockinfo[XERIC_PERENNIAL][j]);
                    break;
                case 'N': scanf(" %f", &stockinfo[NATIVE_SHRUB][j]);
                    break;
                case 'T': scanf(" %f", &stockinfo[NATIVE_TREE][j]);
                    break;
                case 'O': scanf(" %f", &stockinfo[OTHER][j]);
                    break;
                default : scanf(" %f", &stockinfo[OTHER][j]);
                    break;
            }
        }
    }

    scanf("%d", &Qstaff);

    for(i=0; i<Qstaff; i++)
        scanf(" %c", &Nstaff[i]);
    for(j=0; j<3; j++)
    {
        for(i=0; i<Qstaff; i++)
        {
            Z = Nstaff[i];
            switch( Z )
            {
                case 'M': scanf(" %f", &staffdata[MANAGER][j]);
                    break;
                case 'H': scanf(" %f", &staffdata[HORTICULTARIST][j]);
                    break;
                case 'O': scanf(" %f", &staffdata[OWNER][j]);
                    break;
                case 'S': scanf(" %f", &staffdata[SALES_STAFF][j]);
                    break;
                default : scanf(" %f", &staffdata[SALES_STAFF][j]);
                    break;
            }
        }
    }

    scanf(" %f", &maxwage);

    scanf( "%d", &Qrooms);

    for(i=0; i<Qrooms; i++)
        scanf(" %c", &Nrooms[i]);
    for(i=0; i<Qrooms; i++)
    {
        Z = Nrooms[i];
        switch( Z )
        {
            case 'I': scanf(" %d", &roomtype[INDOOR]);
                break;
            case 'L': scanf(" %d", &roomtype[LOADING]);
                break;
            case 'S': scanf(" %d", &roomtype[OUTDOOR]);
                break;
            case 'B': scanf(" %d", &roomtype[BATHROOM]);
                break;
            case 'O': scanf(" %d", &roomtype[OFFICE]);
                break;
        }
    }

    scanf(" %f %f", &I_RSF, &O_RSF);

/*********************************************************************************************

                    End of Input

*********************************************************************************************/

    for(i=0; i<Qrooms; i++)
    {
        Z = Nrooms[i];
        switch( Z )
        {
            case 'I': for(j=0; j<roomtype[INDOOR]; j++)
                {
                    scanf(" %f %f %s", &roomsize[INDOOR][j][LENGTH], &roomsize[INDOOR][j][WIDTH], &sectionname[k]);
                    k++;
                }
                    break;
            case 'L': for(j=0; j<roomtype[LOADING]; j++)
                {
                    scanf(" %f %f", &roomsize[LOADING][j][LENGTH], &roomsize[LOADING][j][WIDTH]);
                    k++;
                }
                    break;
            case 'S': for(j=0; j<roomtype[OUTDOOR]; j++)
                {
                    scanf(" %f %f %s", &roomsize[OUTDOOR][j][LENGTH], &roomsize[OUTDOOR][j][WIDTH], &sectionname[k+18]);
                    k++;
                }
                    break;
            case 'B': for(j=0; j<roomtype[BATHROOM]; j++)
                {
                    scanf(" %f %f", &roomsize[BATHROOM][j][LENGTH], &roomsize[BATHROOM][j][WIDTH]);
                    k++;
                }
                    break;
            case 'O': for(j=0; j<roomtype[OFFICE]; j++)
                {
                    scanf(" %f %f", &roomsize[OFFICE][j][LENGTH], &roomsize[OFFICE][j][WIDTH]);
                    k++;
                }
                    break;
            default :   break;
        }
    }


    printf("\n\n\n CBL: $%9.2f\n CPU: $%9.2f\n CWU: $%9.2f\n CBP: $%9.2f\n CSF: $%9.2f\n\n   %d:", CBL, CPU, CWU, CBP, CSF, Qstock);

    for(i=0; i<Qstock; i++)
        printf(" %c", Nstock[i]);
    for(j=0; j<3; j++)
    {
        printf("      \n");
        for(i=0; i<6; i++)
        {
            printf(" %9.2f", stockinfo[i][j]); 
        }
    }

    printf("\n\n   %d:", Qstaff);

    for(i=0; i<Qstaff; i++)
        printf(" %c", Nstaff[i]);
    for(j=0; j<3; j++)
    {
        printf("      \n");
        for(i=0; i<4; i++)
        {
            printf(" %9.2f", staffdata[i][j]); 
        }
    }

    printf("\n\n %9.2f\n", maxwage);

    printf("\n   %d:", Qrooms);

    for(i=0; i<Qrooms; i++)
        printf(" %c", Nrooms[i]);

    printf("\n");
    for(i=0; i<4; i++)
    {
        printf(" %9d", roomtype[i]); 
    }


    printf("\n\n %9.2f %9.2f\n\n\n\n", I_RSF, O_RSF);

    k=0;

    for(i=0; i<Qrooms; i++)
    {
        Z = Nrooms[i];
        switch( Z )
        {
            case 'I': for(j=0; j<roomtype[INDOOR]; j++)
                {
                printf(" %9.2f %9.2f     %s\n", roomsize[INDOOR][j][LENGTH], roomsize[INDOOR][j][WIDTH], sectionname[k]);
                k++;
                }
                break;
            case 'L': for(j=0; j<roomtype[LOADING]; j++)
                {
                l=j+1;
                printf(" %9.2f %9.2f     Loading Dock %d\n", &roomsize[LOADING][j][LENGTH], &roomsize[LOADING][j][WIDTH], l);
                k++;
                }
                break;
            case 'S': for(j=0; j<roomtype[OUTDOOR]; j++)
                {
                printf(" %9.2f %9.2f     %s\n", &roomsize[OUTDOOR][j][LENGTH], &roomsize[OUTDOOR][j][WIDTH], sectionname[k+18]);
                k++;
                }
                break;
            case 'B': for(j=0; j<roomtype[BATHROOM]; j++)
                {
                l=j+1;
                printf(" %9.2f %9.2f     Bathroom %d\n", &roomsize[BATHROOM][j][LENGTH], &roomsize[BATHROOM][j][WIDTH], l);
                k++;
                }
                break;
            case 'O': for(j=0; j<roomtype[OFFICE]; j++)
                {
                l=j+1;
                printf(" %9.2f %9.2f     Office %d\n", &roomsize[OFFICE][j][LENGTH], &roomsize[OFFICE][j][WIDTH], l);
                k++;
                }
                break;
            default : break;
        }
    }

    printf("There are %d rooms.\n\n", l);
return 0;
}

输入:

500.00 250.00 69.99 50.00 15000.00 
4 X A N O 65 500 150 325 35.50 2.00 22.00 7.25.75 .90 1.00 .90 
3 M H S 2 1 5 12.50 18.00 10.10 120 48.8 76.4 27.50 
4 S I B L 3 1 2 3 
120.00 75.50

40.0 60.0 Hardy_Shrubs
20.0 20.0 Native_Shrubs
20.0 20.0 Hardy_Trees
45.5 35.3 Flowers
10.5 10.0
9.5 10.0
15.0 10.0
10.0 5.0
20.0 10.0

这代表:

business license cost of            $500.00
power utility connection cost of    $250
water utility connection fee of     $69.99
business permit cost of             $50
start up fund of                    $15,000

Stock Type(4):      Quantity    Cost        Markup
xeric perennials    65          $35.50      0.75
annuals             500         $ 2.00      0.90
native shrubs       150         $22.00      1.00
and other           325         $ 7.25      0.90

Staff Type(3):      Quantity    Hourly-Wage Hours-worked-per-month  
managers            2           $12.50      120
horticulturists     1           $18.00      48.58
sales staff         5           $10.10      76.4

The maximum hourly wage for any employee is $27.50.  

Room Type(4):       Quantity
Outdoor Sales       3
Indoor Sales        1
Bathrooms           2
Loading Areas       3

The rental rate per indoor square foot per year is $120.00.  
The rental rate per outdoor square foot per year is $75.50. 

剩余的数据是每个房间的一行。
线的顺序与给定的房间顺序相匹配,因此 3 个室外销售室、1 个室内销售室、2 个浴室和 3 个装载区是 9 条线。

对于销售房间,有长度、宽度和房间名称。在上面的例子中:

第一个室外销售室是 40 英尺乘 60 英尺,它被命名为“Hardy_Shrubs”。
第二个室外销售室是 20' x 20',被称为“Native_Shrubs”。
对于名为“Hardy_Trees”的第三个户外销售室也是如此。第四个房间是 45.5' x 35.3' 的室内销售室,名称为“鲜花”。
第 5 个房间是第一个浴室,大小为 10.5' x 10',第二个浴室为 9.5' x 10'。三个装载区的尺寸分别为 15' x 10'、10' x 5' 和 20' x 10'。

然后在 main 到达 / * End of Input * /时打印此数据

不幸的是,这是打印出来的:

 CBL: $   500.00
 CPU: $   250.00
 CWU: $    69.99
 CBP: $    50.00
 CSF: $ 15000.00

   4: X A N O      
    500.00      0.00     65.00    150.00      0.00    325.00      
      2.00      0.00     35.50     22.00      0.00      7.25      
      0.90      0.00      0.75      1.00     -0.00      0.90

   3: M H S      
      2.00      0.00      1.00      5.00      
     12.50      0.00     18.00     10.10      
    120.00      0.00     48.80     76.40

     27.50

   4: S I B L
     1         3         3         2

    120.00     75.50   /* <-- Correct */

    /* ERROR SECTION */

    120.00     75.50                                /* wrong */  
    120.00     75.50                                /* wrong */
    120.00     75.50                                /* wrong */
     45.50     35.30     Flowers                    /* Correct! */
     45.50     35.30     Bathroom 1826556656        /* wrong */
                                                    /* Should be: 10.50     10.00   Bathroom 1 */
     45.50     35.30     Bathroom 1826556664        /* wrong */
     45.50     35.30     Loading Dock 1826556368    /* wrong */
     45.50     35.30     Loading Dock 1826556376    /* wrong */
     45.50     35.30     Loading Dock 1826556384    /* wrong */

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** /

       QUESTION

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** /

我已经盯着这个看了好几个小时了。

为什么房间尺寸从每平方英尺的价格开始,然后更改为室内花卉面积的正确值,然后其余房间保持相同的尺寸?

还有为什么打印:

装货码头 1826556368

装货码头 1826556376

装货码头 1826556384

代替:

装卸台 1

装卸台 2

装载码头 3

??????

4

1 回答 1

0

scanf(" %f", &maxwage);我看到您%f在所有scanf函数中都保留了一个空格,请记住在键入时会考虑空格,因此如果不按space然后键入值,那么这可能是错误的来源

于 2013-10-23T14:18:52.493 回答