5.1
//编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间
//使用#define或const创建一个表示60的富豪常量或const常量
//通过while循环让用户重复输入值,直到用户输入小于或等于0的值才停止循环
#include
int main(void)
{
const int CHANGENUM = 60;
int time, hours, minutes; //time:输入的时间 hours:转换后的小时 minutes:转换后的分
printf("请输入时间(以分钟为单位): ");
scanf("%d", &time);
while (time > 0)
{
hours = time / CHANGENUM;
minutes = time % CHANGENUM;
printf("%d分钟是%d小时%d分钟\n", time, hours, minutes);
printf("请再次输入时间(以分钟为单位): ");
scanf("%d", &time);
}
printf("结束输入!");
return 0;
5.2
/*编写一个程序,提示用户输入一个整数,然后打印从该数到比该
数大10的所有整数。要求打印的各值之间用一个空格、制表符分开*/
#include
int main(void)
{
int num;
printf("Please enter a number: ");
scanf("%d", &num);
for (int i = 0; i < 10; ++i)
printf("%d\t", ++num);
return 0;
}
5.3
//提示用户输入天数
//将其转换成周数和天数
//while
#include
int main(void)
{
int days; //输入的天数
int week, day; //转换后的周数和天数
printf("Please enter the days number.(enter a number < 0 to quit): ");
scanf("%d", &days);
while (days > 0)
{
week = days / 7;
day = days % 7;
printf("%d days are %d weeks, %d days.\n", days, week, day);
printf("Please enter the days number again.(enter a number < 0 to quit):");
scanf("%d", &days);
}
printf("Job Done!");
return 0;
}
5.4
//编写一个程序,提示用户输入一个身高(cm),
//分别以里面和英寸为单位显示该值,允许有小数不分
//可以让用户重复无输入身高,知道用户输入一个非正值
#include
#define CMTOFEET 0.032808 //1厘米=0.032808英尺feet
#define CMTOINCH 0.3937008 //1厘米=0.3937008英尺feet
#define FEETOINCH 12 //1英尺feet = 12 英寸inch
int main(void)
{
float height; //输入身高
int feet; //转换英尺
float inch; //转换英寸
printf("Please enter your height in cm: ");
scanf("%f", &height);
while (height > 0)
{
feet = height * CMTOFEET;
inch = (height * CMTOINCH) - (FEETOINCH*feet);
printf("%.1f cm = %d feet, %.1f inches\n", height, feet, inch);
printf("Enter a height in centimeters (<=0 to quit): ");
scanf("%f", &height);
}
printf("Job Done!");
return 0;
}
5.5
/*计算n天内赚多少钱的程序,第n天赚$n*/
#include
int main(void) //
{
int count, sum;
//count = 0;
//sum = 0;
int n;
printf("Please enter the day: ");
scanf("%d", &n);
while (n > 0)
{
count = 0;
sum = 0;
while (count++ < n)
sum = sum + count;
printf("sum = %d\n", sum);
printf("Please enter the day again(enter <=0 to quit:");
scanf("%d", &n);
}
printf("Job Down!");
return 0;
}
5.6
/*计算n天内赚多少钱的程序,第n天赚$n*/
//修改为第n天赚$n*n
#include
int main(void) //
{
int count, sum;
//count = 0;
//sum = 0;
int n;
printf("Please enter the day: ");
scanf("%d", &n);
while (n > 0)
{
count = 0;
sum = 0;
while (count++ < n)
sum = sum + count * count;
printf("sum = %d\n", sum);
printf("Please enter the day again(enter <=0 to quit:");
scanf("%d", &n);
}
printf("Job Down!");
return 0;
}
5.7
//提示用户输入一个double类型的数,并打印该数的立方值
//自己设计一个函数计算
#include
double cube(double a);//函数原型
int main(void)
{
double num;
printf("Please enter a number: ");
scanf("%lf", &num);
double cube_num = cube(num);
printf("The cube of %.2lf is %.2lf.", num, cube_num);
return 0;
}
double cube(double a)
{
return a * a * a; //计算立方
}
5.8
//显示求模运算的结果,把用户输入的第一个数作为求模运算的第二个运算对象
//该值保持不变,用户后面输入的是第一个运算对象。
//输入非正值时,程序结束
#include
int main(void)
{
int one, two, moduli;
printf("This program computers moduli.\n");
printf("Enter an integer to serve as the second operand: ");
scanf("%d", &two);
printf("Now enter the first operand: ");
scanf("%d", &one);
while (one > 0)
{
moduli = one % two;
printf("%d %% %d is %d.\n", one, two, moduli);
printf("Enter next number for first operand (<= 0 to quit): ");
scanf("%d", &one);
}
printf("Job Done!");
return 0;
}
5.9
//用户输入华氏温度,读取double类型作为温度值,并将其作为参数传递给一个函数
//函数计算摄氏温度和开氏温度,精度为小数点后两位
#include
void Temperatures(double a);
int main(void)
{
double ft; //华氏温度
printf("Please enter a temperature number(华氏): ");
while (scanf("%lf", &ft) == 1)
{
Temperatures(ft);
printf("Please enter the next number: ( q to quit ) : ");
}
printf("Job Done!");
return 0;
}
void Temperatures(double a)
{
double ct; //摄氏温度
double kt; //开氏温度
ct = 5.0 / 9.0 * (a - 32.0);
kt = ct + 273.16;
printf("%.2f华氏温度 = %.2f 摄氏温度 = %.2f 开氏温度\n", a, ct, kt);
}