C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice
1. 输入分钟输出对应的小时和分钟。
#include
#define MIN_PER_H 60
int main(void)
{
int mins, hours, minutes;
printf("Convert mins to hours and minutes\n");
printf("Please enter the mins: \n");
scanf("%d", &mins);
while (mins > 0)
{
hours = mins / MIN_PER_H;
minutes = mins % MIN_PER_H;
printf("%d mins is %d hours and %d minutes\n\n",
mins, hours, minutes);
printf("Please enter the next mins: \n");
scanf("%d", &mins);
}
printf("ok, let's stop!\n");
return 0;
}
2.题目略
#include
int main(void)
{
int num, i = 0;
printf("Please enter an int number: \n");
scanf("%d", &num);
printf("Here we go:\n");
while(i++ <= 10)
{
printf("%d ", num+i-1);
}
printf("\n");
return 0;
}
3.题略
#include
#define D_PER_W 7
int main(void)
{
int day, week, days;
printf("Convert day to weeks and days\n");
printf("Please enter the days: \n");
scanf("%d", &day);
while(day > 0)
{
week = day / D_PER_W;
days = day % D_PER_W;
printf("%d days are %d weeks and %d days\n\n",
day, week, days);
printf("Please enter the next days:\n");
scanf("%d", &day);
}
printf("Done!\n");
return 0;
}
4.题略
#include
#define C_P_F 36.2
#define C_P_I 16.5
int main(void)
{
float c, f, i;
printf("Please enter a height in centimeters: ");
scanf("%f", &c);
// printf("%d", (int)c);
while (c > 0)
{
f = c / C_P_F;
i = c / C_P_I;
printf("%1.1f cm = %1.1f feet, %1.1f inches.\n\n",
c, f, i);
printf("Enter a height in centimeters (<=0 to quit):
");
scanf("%f", &c);
// printf("%d", int(c));
}
printf("bye\n");
return 0;
}
5.题略
#include
int main (void)
{
int N, n=0;
int sum=0;
printf("计算前N个数的和(不包括0):\n");
printf("Please input the N:");
scanf("%d", &N);
printf("N = %d\n",N);
while(n++ < N)
sum = sum + n;
printf("前%d个数之和为%d\n", N, sum);
getchar();
getchar();
return 0;
}
6.题略
#include
int main(void)
{
int begn = 0, stop, sum = 0;
//printf("please enter a begin number: \n");
//scanf("%d", &begn);
printf("please enter a stop number:
");
scanf("%d", &stop);
//while (begn <= stop)
while (begn++ < stop)
sum = sum + begn * begn;
printf("前%d个数的平方和为%d\n",stop,sum);
return 0;
}
7.题略
#include
void cube (int n);
int main(void)
{
int i;
printf("Please enter a number:\n");
scanf("%d", &i);
cube (i);
return 0;
}
void cube(int n)
{
int m;
m = n * n * n;
printf("cube of the number = %d\n", m);
}
#include
int cube (int n);
int main(void)
{
int i,m;
printf("Please enter a number:\n");
scanf("%d", &i);
m = cube (i);
printf("cube of the number = %d\n", m);
return 0;
}
int cube(int n)
{
int m;
m = n * n * n;
return m;
}
8.题略
#include
void Temperatures (double f);
int main(void)
{
double tmp;
printf("Please enter a Fahrenheit temperature: ");
scanf("%lf", &tmp); //%lf是读取double型数据的意思
while(scanf("%lf", &tmp) == 1)//利用scanf函数返回值来作循环判断条件
!!!!!!吊炸天!!!!!!
{
Temperatures (tmp);
printf("Please enter another f: \n");
scanf("lf", &tmp);
}
printf("Done!");
return 0;
}
void Temperatures (double f)
{
double c, k;
const double X = 1.8;
const double Y = 32.0, Z = 273.16;
c = X * f + Y;
k = c + Z;
printf("the fahrenheit is %1.2f = %1.2f celsius = %1.2f
kelvin\n\n", f, c, k);
}
结果如下