• 程序清单5.1,shoes1.c:

/* shoes1.c -- 把鞋码转换成英寸 */
#include <stdio.h>#define ADJUST 7.31 // 字符常量 int main(void)
{const double SCALE = 0.333; // const 变量 double shoe, foot;shoe = 9.0;foot = SCALE * shoe + ADJUST;printf("Shoe  size (men's)    foot length\n");printf("%10.1f %15.2f inches\n", shoe, foot);return 0;
}

输出结果:

  • 程序清单5.2,shoes2.c:

/* shoes2.c -- 计算多个不同鞋码对应的脚长 */
#include <stdio.h>
#define ADJUST 7.31 // 字符常量int main(void)
{const double SCALE = 0.333;double shoe, foot;printf("Shoe size  (men's)   foot length\n");shoe = 3.0;while (shoe < 18.5){foot = SCALE * shoe + ADJUST;printf("%10.1f %15.2f inches\n", shoe, foot);shoe = shoe + 1.0; }printf("If the shoe fits, wear it.\n");return 0;
} 

输出结果:

  • 程序清单5.3,golf.c:

/* golf.c -- 高尔夫锦标赛记分卡 */
#include <stdio.h>int main(void)
{int jane, tarzan, cheeta;cheeta = tarzan = jane = 68;printf("                  cheeta  tarzan    jane\n");printf("First round score %4d %8d %8d\n", cheeta, tarzan, jane);return 0;
}

输出结果:

  • 程序清单5.4,squares.c:

/* squares.c -- 计算 1~20 的平方 */
#include <stdio.h>int main(void)
{int num = 1;while (num < 21){printf("%4d %6d\n", num, num * num);num = num + 1;}return 0;
}

输出结果:

  • 程序清单5.5,wheat.c:

/* wheat.c -- 指数增长 */
#include <stdio.h>
#define SQUARES 64 // 棋盘中的方格数int main(void)
{const double CROP = 2E16; // 世界小麦年产谷粒数 double current, total;int count = 1;printf("square   grains       total        ");printf("fraction of \n");printf("         added        grains       ");printf("world total\n");total = current = 1.0; /* 从 1 颗谷粒开始 */ printf("%4d %13.2e %12.2e %12.2e\n", count, current,total, total / CROP);while (count < SQUARES){count = count + 1;current = 2.0 * current; /* 下一个方格谷粒翻倍 */ total = total + current; /* 更新总数 */ printf("%4d %13.2e %12.2e %12.2e\n", count, current,total, total / CROP);}printf("That's all.\n");return 0;
} 

输出结果:

  • 程序清单5.6,divide.c:

/* divide.c -- 演示除法 */
#include <stdio.h>int main(void)
{printf("integer division: 5/4 is %d \n", 5 / 4);printf("integer division: 6/3 is %d \n", 6 / 3);printf("integer division: 7/4 is %d \n", 7 / 4);printf("floating dividion: 7./4. is %1.2f \n", 7. / 4.);printf("mixed division:   7./4 is %1.2f \n", 7. / 4);return 0;
}

输出结果:

  • 程序清单5.7,rules.c:

/* rules.c -- 优先级测试*/
#include <stdio.h>int main(void)
{int top, score;top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3));printf("top = %d, score = %d\n", top, score);return 0;
}

输出结果:

  • 程序清单5.8,sizeof.c:

// sizeof.c -- 使用 sizeof 运算符
// 使用 C99 新增的 %zd 转换说明 -- 如果编译器不支持 %zd,请将其改成%u 或 %lu
#include <stdio.h>int main(void)
{int n = 0;size_t intsize;intsize = sizeof(int);printf("n = %d, n has %zd bytes; all ints have %zd bytes.\n", n, sizeof n, intsize);return 0;
}

输出结果:

  • 程序清单5.9,min_sec.c:

// min_sec.c -- 把秒数转换成分和秒
#include <stdio.h>
#define SEC_PER_MIN 60 //一分钟 60 秒int main(void)
{int sec, min, left;printf("Covert seconds to minutes and seconds!\n");printf("Enter the number of seconds (<=0 to quit):\n");scanf("%d", &sec); // 读取秒数 while (sec > 0){min = sec / SEC_PER_MIN; // 截断分钟数 left = sec % SEC_PER_MIN; // 剩下的秒数 printf("%d seconds is %d minutes, %d seconds.\n", sec, min, left);printf("Enter next value (<=0 to quit):\n");scanf("%d", &sec);}printf("Done!\n");return 0;
}

输出结果:

  • 程序清单5.10,add_one.c:

/* add_one.c -- 递增:前缀和后缀 */
#include <stdio.h>int main(void)
{int ultra = 0, super = 0;while (super < 5){super++;++ultra;printf("super = %d, ultra = %d \n", super, ultra);}return 0;
} 

输出结果:

  • 程序清单5.11,post_pre.c:

/* post_pre.c -- 前缀和后缀 */
#include <stdio.h>int main(void)
{int a = 1, b = 1;int a_post, pre_b;a_post = a++; // 后缀递增 pre_b = ++b; // 前缀递增 printf("a  a_post   b   pre_b \n");printf("%1d %5d %5d %5d\n", a, a_post, b, pre_b);return 0;
}

输出结果:

  • 程序清单5.12,bottles.c:

#include <stdio.h>
#define MAX 100int main(void)
{int count = MAX + 1;while (--count > 0){printf("%d bottles of spring water on the wall, ""%d bottles of spring water!\n", count, count);printf("Take one down and pass it around,\n");printf("%d bottles of spring water!\n\n", count - 1);}return 0;
}

输出结果:

  • 程序清单5.13,addemup.c:

/* addemup.c -- 几种常见的语句 */
#include <stdio.h>int main(void)                 /* 计算前 20 个整数的和 */
{int count, sum;            /* 声明                 */ count = 0;                 /* 表达式语句           */ sum = 0;                   /* 表达式语句           */ while (count++ < 20)       /* 迭代语句             */ sum = sum + count;printf("sum = %d\n", sum); /* 表达式语句           */ return 0;                  /* 跳转语句             */
}

输出结果:

  • 程序清单5.14,convert.c:

/* convert.c -- 自动类型转换 */
#include <stdio.h>int main(void)
{char ch;int i;float f1;f1 = i = ch = 'C';printf("ch = %c, i = %d, f1 = %2.2f\n", ch, i, f1);ch = ch + 1;i = f1 + 2 * ch;f1 = 2.0 * ch + i;printf("ch = %c, i = %d, f1 = %2.2f\n", ch, i, f1);ch = 1107;printf("Now ch = %c\n", ch);ch = 80.89;printf("Now ch = %c\n", ch);return 0;
}

输出结果:

  • 程序清单5.15,pound.c:

/* pound.c -- 定义一个带一个参数的函数 */
#include <stdio.h>void pound(int n); // ANSI 函数原型声明 int main(void)
{int times = 5;char ch = '!'; // ASCII 码是 53 float f = 6.0f;pound(times);  // int 类型的参数 pound(ch);     // 和 pound((int)ch); 相同 pound(f);      // 和 pound((int)f); 相同 return 0;
}void pound(int n) // ANSI 风格函数头
{                  // 表明该函数接受一个 int 类型的参数 while (n-- > 0)printf("#");printf("\n");
}

输出结果:

  • 程序清单5.16,running.c:

// running.c -- A useful program for runners
#include <stdio.h>
const int S_PER_M = 60;          // 1 分钟的秒数
const int S_PER_H = 3600;        // 1 小时的分钟数
const double M_PER_K = 0.62137;  // 1 公里的英里数 int main(void)
{double distk, distm; // 跳过的距离(分别以公里和英里为单位) double rate;         // 平均速度(以英里/小时为单位) int min, sec;         // 跑步用时(以分钟和秒为单位) int time;             // 跑步用时(以秒为单位) double mtime;         // 跑 1 英里需要的时间,以秒为单位 int mmin, msec;         // 跑 1 英里需要的时间,以分钟和秒为单位 printf("This program converts your time for a metric race\n");printf("to a time for running a mile and to your average\n");printf("speed in miles per hour.\n");printf("Please enter in kilometers, the distance run.\n");scanf("%lf", &distk); // %lf 表示读取一个 double 类型的值 printf("Next enter the time in minutes and seconds.\n");printf("Begin by entering the minutes.\n");scanf("%d", &min);printf("Now enter the seconds.\n");scanf("%d", &sec);time = S_PER_M * min + sec;     // 把时间转换成秒 distm = M_PER_K * distk;        // 把公里转换成英里 rate = distm / time * S_PER_H;  // 英里/秒 * 秒/小时 = 英里/小时 mtime = (double) time / distm;  // 时间/距离 = 跑 1 英里所用的时间 mmin = (int) mtime / S_PER_M;   // 求出分钟数 msec = (int) mtime % S_PER_M;   // 求出剩余的秒数 printf("You ran %1.2f km (%1.2f miles) in %d min, %d sec.\n", distk, distm, min, sec);printf("That pace corresponds to running a mile in %d min, ", mmin);printf("%d sec.\n Your average speed was %1.2f mph.\n", msec, rate);return 0;
}

输出结果:

  • 编程练习

题目1,方法:计算并输出数值。示例代码5_1.c:

#include <stdio.h>
#define TIME_CHANGE 60int main(void)
{int minutes, mhour, mminutes;printf("Please input the minutes you want to" "change or input a number less than 0 to quit:___\b\b\b");scanf("%d", &minutes);while (minutes > 0){mhour = minutes    / TIME_CHANGE;mminutes = minutes % TIME_CHANGE;printf("%d minutes convert to %d hours and %d mimutes.\n", minutes, mhour, mminutes);printf("Please input the minutes you want to" "change or input a number less than 0 to quit:___\b\b\b");scanf("%d", &minutes);}return 0;
}

输出结果:

题目2,方法:打印指定的数字。示例代码5_2.c:

#include <stdio.h>
#define LEN 10int main(void)
{int num;int i = 0;printf("Please input a integer: ");scanf("%d", &num);printf("%d ", num);while (++i < LEN + 1)printf("%d ", num + i);return 0 ;
}

输出结果:

题目3,方法:计算并输出数值。示例代码5_3.c:

#include <stdio.h>
#define WEEK_DAY 7int main(void)
{int days, dweeks, ddays;printf("Please input the days you want to convert or input a number less than 0 to quit: ");scanf("%d", &days);while ( days > 0){dweeks = days / WEEK_DAY;ddays = days % WEEK_DAY;printf("%d days equal to %d weeks and %d days.\n", days, dweeks, ddays);printf("Please input the days you want to convert or input a number less than 0 to quit: ");scanf("%d", &days);}printf("Done!\n");return 0;
}

输出结果:

题目4,方法:转换并输出指定数值。示例代码5_4.c:

#include <stdio.h>
#define CM_PER_INCH 2.54f
#define CM_PER_FEET 30.48fint main(void)
{float height, inch;int feet;printf("Please input your height with centimetres or input a number ""less than or equals to 0 to quit: ");while ((scanf("%f", &height) == 1) && height > 0){feet = height / CM_PER_FEET;inch = (height - feet * CM_PER_FEET) / CM_PER_INCH;printf("%.1f cm = %d feet, %.1f inches.\n", height, feet, inch);printf("Please input your height with centimetres or input a number ""less than or equals to 0 to quit: ");}printf("Done!\n");return 0;
}

输出结果:

题目5,输出数值并完成指定计算。示例代码5_5.c:

#include <stdio.h>int main(void)
{int count, sum, num;count = 0;sum = 0;printf("Please input a integer: ");scanf("%d", &num);while (count++ < num)sum = sum + count;printf("sum = %d\n", sum);return 0;
}

输出结果:

题目6,方法:计算并输出。示例代码5_6.c:

#include <stdio.h>int main(void)
{int count, sum, num;count = 0;sum = 0;printf("Please input a integer: ");scanf("%d", &num);while (count++ < num)sum = sum + count * count;printf("sum = %d\n", sum);return 0;
}

输出结果:

题目7,方法:输入并计算立方。示例代码:5_7.c:

#include <stdio.h>void cube(double num);int main(void)
{double d_num;printf("Please input a double number: ");scanf("%lf", &d_num);cube(d_num);return 0;
}void cube(double num)
{double d_cube;d_cube = num * num * num;printf("The cube of %lf is %lf\n", num, d_cube);
}

输出结果:

题目8,方法:求模运算。示例代码5_8.c:

#include <stdio.h>int main(void)
{int num, division;printf("This program computes moduli.\n") ;printf("Enter an integer to serve as the second opernd: ");scanf("%d", &division);printf("Now enter the first operand: ");while ((scanf("%d", &num) == 1) && num > 0){printf("%d %% %d is %d\n", num, division, num % division);printf("Enter next number for first operand (<= 0 to quit): ");}printf("Done!");return 0;
}

输出结果:

题目9,方法:温度转换。示例代码5_9.c:

#include <stdio.h>void Temperatures(double temp);int main(void)
{double d_f;printf("Please input a fahrenheit degree(q to quit): ");while (scanf("%lf", &d_f) == 1){Temperatures(d_f);printf("Please input a fahrenheit degree again(q to quit): ");}printf("Done!");return 0;
}void Temperatures(double temp)
{const double f_value = 32.0;const double k_value = 273.16;double c_t, k_t;c_t = 5.0 / 9.0 * (temp - f_value);k_t = c_t + k_value;printf("Fahrenheit degree is %.2lf.\n", temp);printf("Centigrade degree is %.2lf.\n", c_t);printf("Kelvin degree is %.2lf.\n", k_t);
}

输出结果:

第五章 运算符、表达式和语句相关推荐

  1. 第五章运算符表达式和语句

    title: 第五章 运算符.表达式和语句 author: HardyDragon tags: C Notes 第五章 运算符.表达式和语句 5.1 循环简介 5.2 基本运算符 5.2.1 赋值运算 ...

  2. 《C Primer Plus》第五章-运算符 表达式和语句(笔记、复习题、编程题,副作用和序列点,升降级)

    文章目录 运算符 表达式和语句 本章内容 循环简介 基本运算符 赋值运算符:= 几个术语:数据对象.左值.右值和运算符 加法运算符:+ 减法运算符:- 5.2.4 符号运算符:-和+ 乘法运算符:* ...

  3. 【JAVA SE】第三章 运算符、条件语句、switch结构、while循环、do…while循环、for循环、break关键字以及break和continue关键字

    第三章 运算符.条件语句.switch结构.for循环.break关键字以及break和continue关键字 文章目录 第三章 运算符.条件语句.switch结构.for循环.break关键字以及b ...

  4. 《C Primer Plus》中文第六版 编程练习答案 第五章 运算符、表达式和语句

    C Primer Plus 第5章 运算符.表达式和语句 编程练习答案 ***先说一下关于 i++和 ++i 情况.*** 1.编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#de ...

  5. C Primer Plus编程题-第五章 运算符、表达式和语句

    第一题: 编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创建一个表示60的符号常量或const变量.通过while循环让用户重复输入值,直到用户输入小于或 ...

  6. C Primer Plus第六版第五章运算符,表达式,语句源码

    源码 //5.1#include<stdio.h> #define ADJUST 7.31 int main(void) {const double SCALE = 0.333;doubl ...

  7. 零基础学Python(第五章 运算符)

    本套学习内容共计[22]个章节,每个章节都会有对应的从0-1的学习过程详细讲解,希望可以给更多的人提供帮助. 开发环境:[Win10] 开发工具:[Visual Studio 2019] 1.什么是运 ...

  8. 《C Prime Plus》(第六版) 第05章 运算符、表达式和语句 例题集和编程练习

    OS: Mac Catalina 10.15.4 Hardware: Intel Core i9/16G 2667MHz DDR4 编译器版本:Mac Xcode 11.6  第05章 运算符.表达式 ...

  9. 《C Primer Plus(第6版)中文版》 第5章 运算符、表达式和语句 学习笔记

    对标题和序号稍加修改. 5.1 循环简介 本章仍从一个程序开始. #include<stdio.h> int main(void) {int val=1;while(val<11){ ...

最新文章

  1. 《Nature》发文:好导师的16个标准
  2. CSS元素的基本应用(附加京东面试题)
  3. abb变频器acs800功率_ABB变频器如何选型(1)
  4. MyBatis Generator模板
  5. c盘users的用户名怎么改_做完这几个操作,我从C盘中清理了30G垃圾文件
  6. SAP License:标准价格修改
  7. GitHub pages + Hexo 搭建自己的个人博客
  8. 原来算法还能这样用图画出来?
  9. 15.Nginx 服务器的高级配置
  10. 国内首个SENT 信号解析软件 适配NXP KMA321, melexis MLX90372等SENT信号输出芯片 完美替代PicoScope 解析SENT
  11. Web测试中性能测试基础
  12. 2021年认证杯SPSSPRO杯数学建模D题(第一阶段)停车的策略全过程文档及程序
  13. 如何在 Python 中调用函数?九种方法任你挑选
  14. 微信小程序开发与公众号运营有什么区别
  15. 电脑如何清理重复文件,查找电脑重复文件的软件
  16. 线性代数(4):伴随矩阵、逆矩阵和矩阵的秩
  17. Excel如何在同一单元格内同时添加公式和字符?
  18. 0033__PDM,全称为 Persepolis Download Manager
  19. Windows 10 已禁用输入法
  20. 2019抖音最近火的歌曲精选1000多首无损音乐

热门文章

  1. T100的引导式作业转为客制模组注意事项
  2. New Online Judge 1014
  3. cocos creator 支持gif
  4. 大数据在物流行业的应用以及影响
  5. 前端代码规范速成 prettier - code formatter
  6. 人脸识别考勤管理系统
  7. 平稳噪声与白噪声的区别与联系?
  8. R语言与回归分析计算实例
  9. css创建一个真正的地图标点
  10. MapInfo7.0序列号和许可文件