苏小红版第四版C语言程序设计第五章习题
//5.1
#include<stdio.h>
int main(void)
{
int x;
printf("Please input x :");
scanf("%d", &x);
if (x < 0)x = -x;
else if (x >= 0)x = x;
printf("|x|=%d", x);
return 0;
}
//5.2
#include<stdio.h>
#include<math.h>
int main(void)
{
int x;
printf("Please input x:");
scanf("%d", &x);
if (x % 2 == 0)
{
printf("为偶数");
}
else if (x % 2 != 0)
{
printf("为奇数");
}
return 0;
}
//5.3
#include<stdio.h>
#include<math.h>
int main(void)
{
double a, b, c, area, s;
printf("请输入三角形的三边a,b,c:");
scanf("%lf %lf %lf", &a, &b, &c);
if ((a + b > c) && (a + c > b) && (b + c > a))
{
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("三角形的面积为:%lf\n", area);
}
else
{
printf("输入的a,b,c,三边不能构成三角形");
}
return 0;
}
//5.4
#include<stdio.h>
#include<math.h>
int main(void)
{
int year;
double principal;
double l;
double reat;
printf("Please input principal and year:");
scanf("%lf %d", &principal, &year);
switch (year)
{
case 1 :reat = 0.0225; break;
case 2 :reat = 0.0243; break;
case 3 :reat = 0.0270; break;
case 5 :reat = 0.0288; break;
case 8 :reat = 0.0300; break;
default:printf("Please input again");
exit(0);
}
l = principal * pow(1 + reat, year);
printf("%lf", l);
return 0;
}
//5.7
#include<stdio.h>
#include<math.h>
int main(void)
{
char ch;
printf("Please a key and then press enter:");
ch = getchar();
if ((ch <= 'Z') && (ch >= 'A'))
{
ch = ch + 32;
printf("%c,%d", ch, ch);
}
else if((ch <='z')&&(ch >='a'))
{
ch = ch - 32;
printf("%c,%d", ch, ch);
}
else
{
printf("%d", ch);
}
return 0;
}
//5.8
#include<stdio.h>
int main(void)
{
char ch;
printf("Please input:");
ch = getchar();
if (((ch <= 'Z') && (ch >= 'A')) || ((ch <= 'z') && (ch >= 'a')))
{
printf("这是一个字母");
}
else if ((ch >= '0') && (ch <= '9'))
{
printf("这是一个数字");
}
else if (ch == ' ')
{
printf("这是一个空格");
}
else
{
printf("这是一个其他字符");
}
return 0;
}
//5.9
#include<stdio.h>
int main(void)
{
int score;
printf("Please input score:");
scanf("%d", &score);
if ((score < 0) || (score > 100))
{
printf("输入错误");
}
else if (score >= 90) printf("A");
else if (score >= 80) printf("B");
else if (score >= 70) printf("C");
else if (score >= 60) printf("D");
else if (score <60) printf("E");
return 0;
}
//5.10
#include<stdio.h>
int main(void)
{
int year,month;
printf("Please input year and month:");
scanf("%d %d", &year, &month);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("31days\n");
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0))
{
printf("29days\n");
}
else
{
printf("28days\n");
}
break;
case 4:
case 6:
case 9:
case 11:
printf("30days\n");
break;
default:
printf("输入错误\n");
}
return 0;
}
//5.11
#include<stdio.h>
#include<math.h>
int main(void)
{
char sex;
float faheight, moheighe, H;
char sports, diet;
printf("请输入您的性别(F代表女性,M代表男性):");
scanf(" %c", &sex);
printf("请输入您父亲的身高(CM):");
scanf("%f", &faheight);
printf("请输入您母亲的身高(CM):");
scanf("%f", &moheighe);
printf("您是否喜爱体育锻炼(Y喜欢,N不喜欢):");
scanf(" %c", &sports);
printf("您是否有良好的饮食习惯(Y是的,N不是):");
scanf(" %c", &diet);
if (sex == 'M')
{
H = (faheight + moheighe) * 0.54;
}
else
{
H = (faheight * 0.923 + moheighe) / 2.0;
}
if (sports == 'Y')
{
H = H * (1 + 0.02);
}
if (diet == 'Y')
{
H = H * (1 + 0.015);
}
printf("您的身高预测为:%f", H);
return 0;
}
//5.12
#include<stdio.h>
#include<math.h>
int main(void)
{
float t, w, h;
printf("请输入您的身高和体重:");
scanf("%f %f", &h, &w);
t = w / h * h;
if (t < 18)
{
printf("低体重");
}
else if (t >= 18 && t < 25)
{
printf("正常体重");
}
else if (t > 25 && t < 27)
{
printf("超重体重");
}
else if (t >= 27)
{
printf("肥胖");
}
return 0;
}