《C Primer Plus》学习笔记——第五章编程练习

5.11.1
  1 #include<stdio.h>
  2 #define sec 60
  3 int main(void)
  4 {
  5
  6     int second;
  7     float hour,moment;
  8
  9     printf("请输入秒代表的时间:");
 10     scanf("%d", &second);
 11     while(second >= 0){
 12
 13         moment = (float)second / sec;
 14         hour = moment / sec;
 15         printf("Hours is %f\n", hour);
 16         printf("Moments is %f\n", moment);
 17         scanf("%d", &second);
 18     };
 19
 20     return 0;
 21 }

简单的循环语句练习,这里要注意两个scanf()函数的位置。

5.11.2
  1 #include<stdio.h>
  2 int main(void)
  3 {
  4     int a, s;
  5
  6     printf("请输入一个整数:");
  7     scanf("%d", &a);
  8     s = a + 10;
  9     while(a <= s){
 10         printf("%d ", a);
 11         a++;
 12     };
 13     return 0;
 14 }
5.11.3
  1 #include<stdio.h>
  2 #define week 7
  3 int main(void)
  4 {
  5     int days, weeks, day;
  6
  7     printf("请输入天数:");
  8     scanf("%d", &days);
  9     weeks = days / week;
 10     day = days % week;
 11     printf("总共:%d周,%d天", weeks, day);
 12
 13     return 0;
 14 }

利用C语言的整数除法的特性,即抹去小数点而非四舍五入,计算周数。

5.11.4
  1 #include<stdio.h>
  2 const float inch = 2.54;
  3 const float change = 12;
  4 int main(void)
  5 {
  6     double cm, r_inch, inches;
  7     int r_feet;
  8
  9     printf("Enter a hight in centimeters: ");
 10     scanf("%lf", &cm);
 11     while(cm > 0){
 12         inches = cm / inch;
 13         r_feet = (int)inches / change;
 14         r_inch = inches - (float)r_feet*change;
 15         printf("%.1f = %4dfeet, %4.1finches\n", cm, r_feet, r_inch);
 16         printf("Enter a hight in centimeters(<= 0 to quit): ");
 17         scanf("%lf", &cm);
 18     };
 19     return 0;
 20
 21 }
5.11.5
  1 #include<stdio.h>
  2 int main(void)
  3 {
  4     int count = 0, sum = 0, days;
  5
  6     printf("输入天数: ");
  7     scanf("%d", &days);
  8     while(count++ < days){
  9         sum = sum + count;
 10     };
 11     printf("sum = %d\n", sum);
 12
 13     return 0;
 14 }
5.11.6
  1 #include<stdio.h>
  2 int main(void)
  3 {
  4     int count = 0, sum = 0, days;
  5
  6     printf("输入天数: ");
  7     scanf("%d", &days);
  8     while(count++ < days){
  9         sum = sum + count*count;
 10     };
 11     printf("sum = %d\n", sum);
 12
 13     return 0;
 14 }
5.11.7
  1 #include<stdio.h>
  2 void Function(double n);
  3 int main(void)
  4 {
  5     double n;
  6     printf("请输入一个浮点数:");
  7     scanf("%lf", &n);
  8     Function(n);
  9
 10     return 0;
 11 }
 12
 13 void Function(double n)
 14 {
 15     n = n*n*n;
 16     printf("立方根是:%lf", n);
 17
 18 }
5.11.8
  1 #include<stdio.h>
  2 int main(void)
  3 {
  4     int seconde_num, first_num, resalt;
  5
  6     printf("This program computes moduli.\n");
  7     printf("Enter an integer to serve as the second operand: ");
  8     scanf("%d", &seconde_num);
  9     printf("Enter an integer to serve as the first operand: ");
 10     scanf("%d", &first_num);
 11     while(first_num > 0){
 12         resalt = first_num % seconde_num;
 13         printf("%d %% %d is %d", first_num, seconde_num, resalt);
 14         printf("\nEnter next number for first operand(<= 0 to quit): ");
 15         scanf("%d", &first_num);
 16     };
 17     printf("Done");
 18
 19     return 0;
 20 }

求模运算符的使用:

5.11.9
  1 #include<stdio.h>
  2 void Temperatures(double temp);
  3 int main(void)
  4 {
  5     int n;
  6     double t;
  7
  8     printf("请输入华氏温度: ");
  9     n = scanf("%lf", &t);		\\用于储存函数返回值
 10     while(n == 1){
 11         Temperatures(t);
 12         printf("请输入华氏温度(enter \"q\" to quit): ");
 13         n = scanf("%lf", &t);
 14     };
 15
 16     return 0;
 17 }
 18
 19 void Temperatures(double temp)
 20 {
 21     double c, k;
 22     c = 5.0 / 9.0 * (temp - 32.0);		\\常数最好用const或者define来固定,但是考虑这里不用重复使用,方便起见就不设置了
 23     k = c + 273.16;
 24     printf("摄氏温度 = %.2lf\n", c);
 25     printf("开氏温度 = %.2lf\n", k);
 26
 27 }

对于scanf()副作用是读取输入并赋值,如果读取的输入为数组则返回1;如果读取其它非数字时,则不返回1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值