话说好久没有更了,起初是作业比较多想多歇些日子,结果刹不住车,导致三个月没打开过,自惭形愧。
(关于题的想法会放在题后面或代码注释里)
1.编写一个程序。将用分钟表示的时间转换成以小时和分钟表示的时间。使用 #define 或者 const 来创建一个代表60 的符号常量。使用 while 循环来允许用户重复键入值,并且当键入一个小于等于0 的时间时停止循环。
1 #include <stdio.h> 2 #define CLOCK 60 // 一小时等于 60分钟 3 4 int main(void) 5 { 6 int min, hour, time; 7 printf("please input the time: \n"); 8 scanf("%d", &time); 9 while(time>0) // 小于等于 0时退出循环 10 { 11 hour = time/CLOCK; // 转换后的小时数 12 min = time%CLOCK; // 转换后的分钟数 13 printf("%d minutes are %d h %d m.\n", time, hour, min); 14 printf("please input the time again: \n"); 15 scanf("%d", &time); 16 } 17 return 0; 18 }
2.编写一个程序,此程序要求输入一个整数,然后打印出从(包括)输入的值到(包括)比输出的值大 10的所有整数值(如果输入为5,那么输出就从5到15)。要求在各个输出值之间用空格、制表符或换行符分开。
1 #include <stdio.h> 2 3 int main(void) 4 { 5 int number, count; // 定义一个输入变量与一个计数变量 6 printf("please input a number: \n"); 7 scanf("%d", &number); 8 count=number; // 将要打印的值初始化 9 while(count<=number+10) 10 { 11 printf("%d\n", count); 12 count++; 13 } 14 return 0; 15 }
3.编写一个程序,该程序要求用户输入天数,然后将该值转换为周期和天数。例如,把 18天转换成 2周 4天。用下面的格式显示结果:
18 days are 2 weeks, 4 days.
使用一个 while 循环让用户重复输入天数;当用户输入一个非正数(如 0 或 -20)时,程序将终止循环。
(这段代码我试了下,如果输入小数没法取整,可能要用到往后的知识解决,所以就先默认只能输入整数吧)
1 #include <stdio.h> 2 3 int main(void) 4 { 5 int num, wks, dys; 6 const int cst = 7; // 一个周期为 7天 7 printf("please input the number of days: \n"); 8 scanf("%d", &num); // 输入总的天数 9 while(num>0) 10 { 11 wks=num/cst; // 转换后的周数 12 dys=num%cst; // 转换后的天数 13 printf("%d days are %d weeks, %d days.\n", num, wks, dys); 14 printf("please input the number of days: \n"); 15 scanf("%d", &num); 16 } 17 return 0; 18 }
4.编写一个程序让用户按厘米输入一个高度值,然后,程序按照厘米和英尺英寸显示这个高度值。允许厘米和英寸的值出现小数部分。程序允许用户继续输入,直到用户输入一个非正的数值。程序运行的示例如下所示:
Enter a height in centimeters : 182
182.0 cm = 5 feet, 11.7 inches
Enter a height in centimeters( <=0 to quit ): 168
168.0 cm = 5 feet, 6.1 inches
Enter a height in centimeters( <=0 to quit ): 0
bye
1 #include <stdio.h> 2 #define TRANS 30.48 // 英尺和米之间的换算倍数为 0.3048 3 4 int main(void) 5 { 6 double number, inches; 7 int feet; 8 printf("Enter a height in centimeters: "); 9 scanf("%lf", &number); 10 while(number>0) 11 { 12 feet=number/TRANS; 13 inches=(number-TRANS*feet)/TRANS*12; 14 // meter = cm/100 = (foot+inch/12)*0.3048 15 printf("%.1lf cm = %d feet, %.1lf inches\n", number, feet, inches); 16 // 根据示例小数部分保留至小数点后一位 17 printf("Enter a height in centimeters(<=0 to quit): "); 18 scanf("%lf", &number); 19 } 20 printf("bye\n"); 21 return 0; 22 }
5.改写用来找到前 20 个整数之和的程序 addemup.c(程序清单 5.13)(如果你愿意,可以把 addemup.c 程序看成是一个计算如果第一天得到 $1 ,第二天得到 $2 ,第三天得到 $3 ,以此类推,您在 20 天里会挣多少钱的程序)。修改该程序,目的是您能交互地告诉程序计算将进行到哪里。也就是说,用一个读入的变量来代替 20。
1 #include <stdio.h> 2 3 int main(void) 4 { 5 int money, sum, count=0; 6 printf("Input your number: "); 7 scanf("%d", &money); 8 while(count<=money) 9 { 10 sum+=count++; 11 } 12 printf("sum=%d\n", sum); 13 return 0; 14 }
6.现在修改编程练习 5 中的程序,使它能够计算整数平方的和(如果您喜欢,可以这样认为:如果您第一天得到 $1 ,第二天得到 $4 ,第三天得到 $9 ,以此类推您将得到多少钱)。C没有平方函数,但是您可以利用 n 的平方是 n*n 的事实。
1 #include <stdio.h> 2 3 int main(void) 4 { 5 int money, mul=1; 6 int count=1; 7 printf("Input your number of money: "); 8 scanf("%d", &money); 9 while(count<=money) 10 { 11 mul=count*count; 12 count++; 13 } 14 printf("You will get %d money.\n", mul); 15 return 0; 16 }
7.编写一个程序,该程序要求输入一个 float 型数并打印该数的立方值。使用您自己设计的函数来计算该值的立方并且将它的立方打印出来。main() 程序把输入的值传递给该函数。
(这题我本来想着返回函数后再打印,看了示例 5.15 后想到可以先在函数里打印出值再返回函数)
1 #include <stdio.h> 2 3 float cube(float n); 4 int main(void) 5 { 6 float number, sum; 7 printf("Input your number: "); 8 scanf("%f", &number); 9 cube(number); 10 printf("The cube of %f is %f.\n", number, cube(number)); 11 return 0; 12 } 13 14 float cube(float n) 15 { 16 float sum; 17 sum=n*n*n; 18 return sum; 19 }
1 #include <stdio.h> 2 3 float cube(float n); 4 int main(void) 5 { 6 float number, sum; 7 printf("Input your number: "); 8 scanf("%f", &number); 9 cube(number); 10 return 0; 11 } 12 13 float cube(float n) 14 { 15 float sum; 16 sum=n*n*n; 17 printf("The cube of %f is %f.\n", n, sum); 18 }
8.编写一个程序,该程序要求用户输入一个华氏温度。程序以 double 类型读入温度值,并将它作为一个参数传递给用户提供的函数 Temperatures() 。该函数将计算相应的摄氏温度和绝对温度,并有以小数点右边有两位数字的精度显示这三种温度。它应该用每个值所代表的温度刻度来标识这 3 个值。下面是将华氏温度转换成摄氏温度的方程:
Celsius = 1.8 * Fahrenheit + 32.0
通常用在科学上的绝对温度的刻度是 0 代表绝对零,是可能温度的下界。下面是将摄氏温度转换为绝对温度的方程:
Kelvin = Celsius + 273.16
Temperatures() 函数使用 const 来创建代表该转换里的 3 个常量的符号。main() 函数将使用一个循环来允许用户重复地输入温度,当用户输入 q 或其他非数字值时,循环结束。
(不知道为什么运行程序时数字要输入两次才能打印结果,第一次输入似乎无效?但是输入q却一次就退出循环了,是代码还有别的问题?)
1 #include <stdio.h> 2 3 void Temperatures(double n); 4 5 int main(void) 6 { 7 double temp; 8 printf("Input the temperatures: "); 9 scanf("%lf", &temp); 10 while(1) 11 { 12 int ret; 13 ret=scanf("%lf", &temp); 14 if(ret!=1)break; 15 Temperatures(temp); 16 printf("Input the temperatures again: "); 17 scanf("%lf", &temp); 18 } 19 return 0; 20 } 21 22 void Temperatures(double n) 23 { 24 double kelvin, celsius; 25 const double te_1=1.8, te_2=32.0, te_3=273.16; 26 celsius=n*te_1+te_2; 27 kelvin=celsius+te_3; 28 printf("The Fahrenheit temp is %.2lf.\n", n); 29 printf("The Celsius is %.2lf.\n", celsius); 30 printf("The Kelvin is %.2lf.\n", kelvin); 31 }