请选择 进入手机版 | 继续访问电脑版
楼主: Mutually_Sincer
1463 7

课后题《The C Programming Language》(Second Edition) [推广有奖]

  • 4关注
  • 10粉丝

博士生

89%

还不是VIP/贵宾

-

威望
0
论坛币
6969 个
通用积分
13.9148
学术水平
11 点
热心指数
15 点
信用等级
4 点
经验
4658 点
帖子
204
精华
0
在线时间
357 小时
注册时间
2014-11-4
最后登录
2023-2-9

Mutually_Sincer 学生认证  发表于 2017-11-18 20:08:21 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
练习1.8
  1. #include <stdio.h>
  2. main()
  3. {
  4.         /*
  5.         统计输入句子中空格,制表符以及换行符的个数
  6.         EOF为终止判断符,输入中为“Ctrl+Z”
  7.         Args:
  8.         n_space:空格数;
  9.         n_tab:制表符数;
  10.         n_enter:换行符数.
  11.         */
  12.         long n_space = 0;
  13.         long n_tab = 0;
  14.         long n_enter = 0;
  15.         long c;
  16.        
  17.         printf("Please input your sentences(End with 'Ctrl + z' in a new line):\n");
  18.         while((c = getchar()) != EOF){
  19.                 if(c == ' ')
  20.                         ++n_space;
  21.                 if(c == '        ')
  22.                         ++n_tab;
  23.                 if(c == '\n')
  24.                         ++n_enter;
  25.         }
  26.        
  27.         printf("Number of space = %ld\n", n_space);
  28.         printf("Number of tab = %ld\n", n_tab);
  29.         printf("Number of enter = %ld\n",n_enter);

  30.        
  31. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Language Edition Program dition

本帖被以下文库推荐

wanna be a stargazer
Mutually_Sincer 学生认证  发表于 2017-11-18 20:11:58 |显示全部楼层 |坛友微信交流群
练习1.9
  1. #include <stdio.h>
  2. main(){
  3.         /*
  4.         合并多个空格
  5.         */
  6.         printf("Input your sentence:\n");
  7.         long c;
  8.         while((c = getchar()) != EOF){
  9.                 if(c == ' '){
  10.                         putchar(c);
  11.                         while((c = getchar()) == ' ' && c!= EOF)
  12.                         ;
  13.                        
  14.                 }
  15.                 putchar(c);
  16.         }
  17. }
  18.        
复制代码

使用道具

Mutually_Sincer 学生认证  发表于 2017-11-18 21:16:58 |显示全部楼层 |坛友微信交流群
练习1.10
  1. #include<stdio.h>
  2. main(){
  3.         /*
  4.         将制表符、\以及回退符显式打印
  5.         Imperfect Version,无法非强制停止运行。
  6.         */
  7.         int c;
  8.         while((c = getch()) != EOF){
  9.                 if(c == '\t')
  10.                         printf("\\t");
  11.                 else if(c == '\\')
  12.                         printf("\\\\");
  13.                 else if(c == '\b')
  14.                         printf("\\b");
  15.                 else                        
  16.                         putchar(c);
  17.         }
  18. }
复制代码

使用道具

Mutually_Sincer 学生认证  发表于 2017-11-18 21:18:14 |显示全部楼层 |坛友微信交流群
练习1.12
  1. #include <stdio.h>
  2. main(){
  3.         /*
  4.          将输入的句子以每行一个单词形式打印
  5.          */
  6.         int c;
  7.         printf("Input your sentence(End of 'Ctrl + Z' in a new line):\n");
  8.         while((c = getchar()) != EOF){
  9.                 if(c != ' ' && c !='\t')
  10.                         putchar(c);
  11.                 else
  12.                         printf("\n");
  13.                
  14.         }
  15. }
复制代码

使用道具

Mutually_Sincer 学生认证  发表于 2017-11-18 21:19:41 |显示全部楼层 |坛友微信交流群
练习1.13
  1. #include <stdio.h>
  2. main(){
  3.         /*
  4.          输入一个句子,输出句子中每个单词的长度的直方图。
  5.          
  6.          Args:
  7.          c:存储输入的句子
  8.          t:控制输出一次显示结果的提示性语句
  9.          count_c:对每个单词进行长度统计然后在for_loop中输出图形
  10.          */
  11.         int c, t = 1, count_c = 0;
  12.         int i;
  13.        
  14.         printf("Input your sentence(End with 'Ctrl + Z' in a new line):\n");
  15.         while((c = getchar()) != EOF){
  16.                 if(t == 1){
  17.                         printf("-----OUTPUT-----\n");
  18.                         printf("The horizontally histogram of each word in your sentence is:\n");
  19.                         t = 0;
  20.                 }
  21.        
  22.                 if(c != ' ' && c != '\t' && c != '\n' && c !=',' && c != '.')
  23.                         ++count_c;
  24.                 else if(count_c != 0){
  25.                         printf("%2d ", count_c);
  26.                         for(i = 0; i < count_c; i++){
  27.                                 printf("#");
  28.                         }
  29.                         printf("\n");
  30.                         count_c = 0;

  31.                 }
  32.         }
  33.        

  34. }
复制代码

使用道具

Mutually_Sincer 学生认证  发表于 2017-11-18 21:20:47 |显示全部楼层 |坛友微信交流群
练习1.13
  1. #include<stdio.h>
  2. int max(int num[], int howmany){
  3.         /*
  4.         求取数组中的最大值
  5.        
  6.         Args:
  7.         num:待求最值的数组;
  8.         howmany:数组中元素的个数。
  9.         */
  10.         int i;
  11.         int large = num[0];
  12.         for(i = 0; i < howmany; i++){
  13.                 if(num[i] > large)
  14.                         large = num[i];
  15.         }
  16.         return large;
  17. }

  18. main(){
  19.         /*
  20.         (Imperfect version)
  21.         对于给定单词个数的输入,统计每个单词的长度并绘制竖条直方图
  22.        
  23.         Args:
  24.         count_c:待操作单词的总个数,可由用户输入 ;
  25.         result:数组,存放每个单词的长度;
  26.         temp:中间变量,求每个单词的长度并存入result中 ;
  27.         large:result数组中的最大值,即句子最长单词的长度。
  28.         */

  29.         printf("Input the total number of the words in your sentence(End with 'Enter'): ");
  30.         int c, temp = 0, count_c;
  31.         scanf("%d\n", &count_c);
  32.        
  33.         int result[count_c];
  34.         int j = 0;
  35.        
  36.         while((c = getchar()) != EOF){
  37.                 //从键盘读取含用户指定数量单词的句子。
  38.                 if(c != ' ' && c != '\n')
  39.                         {
  40.                                 ++temp;
  41.                         }
  42.                 else{
  43.                         result[j] = temp;
  44.                         j += 1;
  45.                         temp = 0;
  46.                 }
  47.         }
  48.        
  49.         int k;
  50.         int large = max(result, count_c);
  51.         printf("------------OUTPUT------------\n");
  52.         printf("The bar histogram of each word in your sentence is: \n");
  53.         //两个for_loop,绘制直方图的“横纵坐标”。
  54.         for(k = 0; k < large; k++){
  55.                 int i;
  56.                 for(i = 0; i < count_c; i++){
  57.                         if(k < (large - result[i]))
  58.                                         printf("  ");
  59.                                
  60.                         else
  61.                                 printf(" #");
  62.                        
  63.                 }
  64.                 printf("\n");       
  65.         }
  66.         int l;
  67.         for(l = 0; l < count_c; l++)
  68.         {
  69.                 printf("%2d", result[l]);
  70.         }
  71.          
  72. }
复制代码

使用道具

Mutually_Sincer 学生认证  发表于 2017-11-19 22:35:56 |显示全部楼层 |坛友微信交流群
练习1.14
  1. #include<stdio.h>
  2. #define INF 32
  3. #define SUP 127
  4. main(){
  5.         /*
  6.         取ASCII码从32(空格)开始,到127(del)结束
  7.         对输入的字符统计其个数后打印横向条形图
  8.         */
  9.         int result[SUP - INF + 1], j;
  10.         char c;
  11.         int temp;
  12.        
  13.         for(j = 0; j < (SUP - INF + 1); j++)
  14.                 result[j] = 0;
  15.         printf("Input your sentence:\n");               
  16.         while((c = getchar()) != EOF){
  17.                 temp = c;
  18.                 ++result[temp - 32];
  19.                 int temp;

  20.         }
  21.         int i;
  22.         printf("-----OUTPUT-----\n");
  23.         for(i = 0; i < 96; i++){
  24.                 if(result[i] != 0){
  25.                         printf("%c:", i+32);
  26.                         int k;
  27.                         for(k = 0; k < result[i]; k++)
  28.                                 printf("#");
  29.                         printf("\n");
  30.                 }
  31.         }
  32. }
复制代码

使用道具

Mutually_Sincer 学生认证  发表于 2017-11-20 10:47:39 |显示全部楼层 |坛友微信交流群
练习1.5
  1. #include <stdio.h>

  2. float C2F(float C){
  3.         float F;
  4.         F = 9.0 * C / 5.0 + 32.0;
  5.         return F;
  6. }

  7. float F2C(float F){
  8.         float C;
  9.         C = (F - 32.0) * 5.0 / 9.0;
  10.         return C;
  11. }

  12. main(){
  13.         /*
  14.         摄氏温度与华氏温度的相互转换
  15.        
  16.         Args:
  17.         Cel:摄氏温度;
  18.         Fah:华氏温度 .
  19.         */
  20.         printf("Input F/C for program to converse: ");
  21.         char i;
  22.         scanf("%c", &i);
  23.        
  24.         if(i == 'C'){
  25.                 float Cel;
  26.                 scanf("%f", &Cel);
  27.                 printf("The Celsius temperature you input is: %.3f\n", Cel);
  28.                 printf("The corresponding Fahrenheit temperature is: %.3f\n", C2F(Cel));
  29.         }
  30.         else{
  31.                 float Fah;
  32.                 scanf("%f", &Fah);
  33.                 printf("The Fahrenheit temperature you input is: %.3f\n", Fah);
  34.                 printf("The corresponding Celsius temperature is: %.3f\n", F2C(Fah));
  35.         }
  36. }
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-16 13:04