c语言循环 数据统计问题

问题描述:

c语言循环 数据统计问题
I can't type Chinese ,very sorry!
example:input something that appears like M38,M means this person's sex,and 38 means his/her age.
Sex:M(male) or F(female)
Age:P or M or F
< 18 (P)unk
18~65 (M)iddle-aged
≥ 65 (F)ossil
------------------------------------Following is the input of an example.
Sex & age:M38
Sex & age:F41
Sex & age:M72
Sex & age:F65
Sex & age:M10
Sex & age:F12
Sex & age:M14
Sex & age:00
------------------------------------This is the result we get.
Total:7 (numbers of persons)
Female:PMF
Male:PPMF (classify their age according to sex)
------------------------------------
I will give credits for who that has helped me!
1个回答 分类:综合 2014-12-03

问题解答:

我来补答
why did you say you coundn't type chinese but used chinese title?

you can do like following:

char get_age_char(int age)
{
if(age < 18) return 'P';
if(age < 65) return 'M';
return 'F';
}
main()
{
#define MAX_INPUT 1000
int total_famale = 0, total_male=0;
int age;
char sex;
char male_age[MAX_INPUT], famale_age[MAX_INPUT];

while(1)
{
printf("Sex & age:");
scanf("%c%d", &sex, &age);
if(sex == 'F')
famale_age[total_famale ++] = get_age_char(age);
else if(sex == 'M')
male_age[total_male ++] = get_age_char(age);
else if(sex == '0' && age == 0)
break;
}
famale_age[total_famale] = male_age[total_male] = 0;

printf("Total: %d\n", total_famale + total_male);
printf("Female: %s\nMale: %s\n", female_age, male_age);
}
 
 
展开全文阅读
剩余:2000
上一页:不是奥数题