问题:
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], §ionname[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], §ionname[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;
}
INPUT:
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'室内销售室,名为“Flowers” 第五个房间是第一个10.5英尺×10英尺的浴室,第二个浴室是9.5英尺x 10英尺。三个装载区域尺寸为15'x 10',10'x 5'和20'x 10'。
当主要到达/ *输入结束* /
时,将打印此数据不幸的是,这就是印刷品:
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
而不是:
装载Dock 1
装载Dock 2
装载Dock 3
____
答案 0 :(得分:0)
scanf(" %f", &maxwage);
我发现您在%f
所有scanf
函数中保留了space
之前的空格,请记住在输入时要考虑空格,如果不按{{然后输入值,然后这可能是错误的来源