g******a 发帖数: 13 | 1 刚开始学c programming,问个浅显的问题。该怎么写这个program? 用getchar() 还是
scanf()?? 哪位能写个
program让我学习一下吗?
Let us say a Company uses codes to represent its various products
a codes includes, among other information, a character in tenth position
that represents the zone from which that product was made, and 4 digits
integer in position 4 thru 7 that represents the district in which it will
be sold. Due to some reorganization, products from zone R are banned
from being sold in districts with a designation of 2000 or higher.
Writ a complete program to count and print number of banned and valid code
entered.
Example: TRV2475A5R-14 valid
TRL2k74A5R-11 : district is not numeric
TRV2105A2 : improper code length
The user should signal end of file to stop the program | L*******g 发帖数: 913 | 2 两个都可以
#include
...
int i, n, ok, banned, valid;
char c[20];
scanf("%d", &n);
banned=valid=0;
for(i=0; i
scanf("%s", c);
if(strlen(c)>=10) {
ok=1;
for(j=3; j<7 && ok; j++) { if(c[j]<'0' || c[j]>'9') ok=0; }
if(ok) {
if(c[9]=='R' && c[3]>='2') banned++;
else valid++;
}
}
}
printf("banned=%d, valid=%d\n", banned, valid); | g******a 发帖数: 13 | 3 Thanks for your help! I compiled the program you wrote, however it always
returned banned=0, valid=0 | L*******g 发帖数: 913 | 4 我没检查我写的code。你可以在不同的地方加一点输出,debug看看 |
|