C Primer Plus (6.16) 編程練習
/*C Primer Plus (6.15) 6*/
1 #include<stdio.h>
2 int main()
3 {
4 int i,j;
5 for(int i=0;i<4;i++)
6 {
7 for(int j=0;j<8;j++)
8 {
9 printf("$");
10 }
11 printf("\n");/*儅字符輸出8個“$”后出循環換行*/
12 }
13 return 0;
14 }
15 /*
16 輸出樣例
17
18 $$$$$$$$
19 $$$$$$$$
20 $$$$$$$$
21 $$$$$$$$
22
23 */
/*C Primer Plus (6.16) 1*/
1 #include<stdio.h>
2 int main()
3 {
4 char ch[26]={'a','b','c','d','f','g','h','i','j','k','l','m'
5 ,'n','o','p','q','r','s','t','u','v','w','x','y','z'};
6 int i=0;
7
8 for(i=0;i<26;i++)
9 {
10 printf("%c ",ch[i]);
11 }
12
13 return 0;
14 }
15 /*
16 輸出樣例
17
18 a b c d f g h i j k l m n o p q r s t u v w x y z
19
20 */
/*C Primer Plus (6.16) 2*/
1 #include<stdio.h>
2 int main()
3 {
4 int i,j;
5 for(int i=0;i<5;i++)
6 {
7 for(int j=0;j<(i+1);j++)
8 {
9 printf("$");
10 }
11 printf("\n");
12 }
13 return 0;
14 }
15 /*
16 輸出樣例
17
18 $
19 $$
20 $$$
21 $$$$
22 $$$$$
23
24 */
/*C Primer Plus (6.16) 3*/
1 #include<stdio.h>
2 int main()
3 {
4 int i,j;
5 int ch='F';
6
7 for(int i=0;i<6;i++)
8 {
9 for(int j=0;j<(i+1);j++)
10 {
11 printf("%c",ch-j);
12 }
13 printf("\n");
14 }
15
16 return 0;
17 }
18 /*
19 輸出樣例
20
21 F
22 FE
23 FED
24 FEDC
25 FEDCB
26 FEDCBA
27
28 */
/*C Primer Plus (6.16) 4*/
1 #include<stdio.h>
2 int main()
3 {
4 char ch='A';
5 int i=0,j=0;
6 for(i=0;i<6;i++)
7 {
8 for(j=0;j<(i+1);j++)
9 {
10 printf("%c",ch);
11 ch++;
12 }
13 printf("\n");
14 }
15 return 0;
16 }
17 /*
18 輸出樣例
19
20 A
21 BC
22 DEF
23 GHIJ
24 KLMNO
25 PQRSTU
26
27 */
/*C Primer Plus (6.16) 5*/
1 #include<stdio.h>
2 int main()
3 {
4 char ch;
5 int i,j,k;
6 /*i是計數器,j是控制字母輸出的量單位,k是計算空格使用的計數器*/
7 int length=0;
8
9 printf("Please enter a letter:");
10 scanf("%c",&ch);
11 printf("The number of lines of the triangle is "
12 "the position of the input letter %c in the alphabet.\n",ch);
13 length=ch-'A';
14
15 for(int i=0;i<=length;i++)
16 {
17 char put='A'-1;
18 for(int K=0;K<length-i;K++)
19 {
20 printf(" ");
21 }
22 for(int j=0;j<(i+1);j++)
23 {
24 ++put;
25 printf("%c",put);
26 }
27 for(int j=0;j<i;j++)
28 {
29 --put;
30 printf("%c",put);
31 }
32 printf("\n");
33 }
34
35 return 0;
36 }
37 /*
38 輸出樣例
39
40 Please enter a letter:E
41 The number of lines of the triangle is the position of the input letter E in the alphabet.
42 A
43 ABA
44 ABCBA
45 ABCDCBA
46 ABCDEDCBA
47
48 */
/*C Primer Plus (6.16) 6*/
1 #include<stdio.h>
2 int main()
3 {
4 int lolimit,uplimit;
5 int i=0,number=0;
6 int square=0,cube=0;
7
8 printf("Please enter the lower limit:");
9 scanf("%d",&lolimit);
10 printf("Please enter the upper limit:");
11 scanf("%d",&uplimit);
12 for(int i=lolimit;i<=uplimit;i++)
13 {
14 number=i;
15 square=i*i;
16 cube=i*i*i;
17 printf("number=%d\t square=%d\t cube=%d\n",number,square,cube);
18 }
19 printf("That's all.");
20 return 0;
21 }
22 /*
23 輸出樣例
24
25 Please enter the lower limit:2
26 Please enter the upper limit:20
27 number=2 square=4 cube=8
28 number=3 square=9 cube=27
29 number=4 square=16 cube=64
30 number=5 square=25 cube=125
31 number=6 square=36 cube=216
32 number=7 square=49 cube=343
33 number=8 square=64 cube=512
34 number=9 square=81 cube=729
35 number=10 square=100 cube=1000
36 number=11 square=121 cube=1331
37 number=12 square=144 cube=1728
38 number=13 square=169 cube=2197
39 number=14 square=196 cube=2744
40 number=15 square=225 cube=3375
41 number=16 square=256 cube=4096
42 number=17 square=289 cube=4913
43 number=18 square=324 cube=5832
44 number=19 square=361 cube=6859
45 number=20 square=400 cube=8000
46 That's all.
47
48 */
/*C Primer Plus (6.16) 7*/
1 #include<stdio.h>
2 #include<string.h>
3 #define WORD 40
4 int main()
5 {
6 char str[WORD];
7 int i=0;
8 int string1;
9
10 printf("Please enter a word:");
11 scanf("%s",str);
12 string1=strlen(str);
13 printf("The word is:\n");
14 printf("%s\n",str);
15
16 printf("The word in reverse order:\n");
17 for(i=string1;i>=0;i--)
18 {
19 printf("%c",str[i]);
20 }
21
22 return 0;
23 }
24 /*
25 輸出樣例
26
27 Please enter a word:Tomoko
28 The word is:
29 Tomoko
30 The word in reverse order:
31 okomoT
32
33 */
/*C Primer Plus (6.16) 8*/
1 #include<stdio.h>
2 int main()
3 {
4 float x1,x2;
5
6 printf("Please enter two floating-point digits.\n");
7 printf("num1 and num2:");
8
9 while(scanf("%g",&x1)==1&&scanf("%g",&x2)==1)
10 {
11 printf("(%g-%g) / (%g*%g) = %g\n"
12 ,x1,x2,x1,x2,(x1-x2)/(x1*x2));
13 printf("Please enter again (num1 and num2):");
14 }
15 printf("You have exited the program.");
16 return 0;
17 }
18 /*
19 輸出樣例
20
21 Please enter two floating-point digits.
22 num1 and num2:10 20
23 (10-20) / (10*20) = -0.05
24 Please enter again (num1 and num2):2 1
25 (2-1) / (2*1) = 0.5
26 Please enter again (num1 and num2):3 4
27 (3-4) / (3*4) = -0.0833333
28 Please enter again (num1 and num2):q
29 You have exited the program.
30
31 */
/*C Primer Plus (6.16) 9*/
1 #include<stdio.h>
2
3 float fun(float x1,float x2)
4 {
5 return (x1-x2)/(x1*x2);
6 }
7
8 int main()
9 {
10 float x1,x2;
11 printf("Please enter two floating-point digits.\n");
12 printf("num1 and num2:");
13
14 while(scanf("%g",&x1)==1&&scanf("%g",&x2)==1)
15 {
16 printf("(%g-%g) / (%g*%g) = %g\n"
17 ,x1,x2,x1,x2,fun(x1,x2));
18 printf("Please enter again (num1 and num2):");
19 }
20 printf("You have exited the program.");
21
22 return 0;
23 }
24 /*
25 輸出樣例
26
27 Please enter two floating-point digits.
28 num1 and num2:10 20
29 (10-20) / (10*20) = -0.05
30 Please enter again (num1 and num2):2 1
31 (2-1) / (2*1) = 0.5
32 Please enter again (num1 and num2):3 4
33 (3-4) / (3*4) = -0.0833333
34 Please enter again (num1 and num2):q
35 You have exited the program.
36
37 */
/*C Primer Plus (6.16) 10*/
1 #include<stdio.h>
2 int main()
3 {
4 int upper,lower;
5
6 printf("Enter lower and upper integer limits: ");
7
8 while(scanf("%d %d",&lower,&upper)==2&&(upper>lower))
9 {
10 int sum=0;
11 for(int i=lower;i<=upper;i++)
12 {
13 sum+=i*i;
14 }
15 printf("The sum of the squares from %d to %d is %d.\n"
16 ,lower*lower,upper*upper,sum);
17 printf("Enter next set of limits: ");
18 }
19 printf("Done.");
20
21 return 0;
22 }
23 /*
24 輸出樣例
25
26 Enter lower and upper integer limits: 5 9
27 The sum of the squares from 25 to 81 is 255.
28 Enter next set of limits: 3 25
29 The sum of the squares from 9 to 625 is 5520.
30 Enter next set of limits: 5 5
31 Done.
32
33 */
/*C Primer Plus (6.16) 11*/
1 #include<stdio.h>
2 #define NUM 8
3 int main()
4 {
5 int arr[NUM];
6 int i=0;
7
8 printf("Please enter eight integer number:");
9 for(i=0;i<8;i++)
10 {
11 scanf("%d",&arr[i]);
12 }
13
14 printf("The reserve order printing eight numbers:");
15 for(int i=7;i>=0;i--)/*在C語言中i=7,一共是八個元素的下標*/
16 /*所以一共有八個元素倒序輸出到最後一個元素的下標(arr[0])*/
17 {
18 printf("%d ",arr[i]);
19 }
20 return 0;
21 }
22 /*
23 輸出樣例
24
25 Please enter eight integer number:1 2 3 4 5 6 7 8
26 The reserve order printing eight numbers:8 7 6 5 4 3 2 1
27
28 */
/*C Primer Plus (6.16) 12*/
1 #include<stdio.h>
2 int main()
3 {
4 int count;
5
6 printf("Please enter the number of calculations (<=0 to quit):");
7
8 while(scanf("%d",&count)==1&&(count>0))
9 {
10 double sum1=0.0;
11 double sum2=0.0;
12 for(int i=1,mark=1;i<=count;i++,mark*=-1)
13 {
14 sum1+=1.0/i;
15 sum2+=(1.0/i)*mark;
16 }
17 printf("1.0+1.0/2.0+1.0/3.0+1.0/4.0+...sum are:%lf\n",sum1);
18 printf("1.0-1.0/2.0+1.0/3.0-1.0/4.0+...sum are:%lf\n",sum2);
19 printf("The sum of the two sequences is:%lf\n",sum1+sum2);
20 printf("\nNow you can enter again (<=0 to quit):");
21 }
22 printf("Done.");
23 return 0;
24 }
25 /*
26 輸出樣例
27
28 Please enter the number of calculations (<=0 to quit):100
29 1.0+1.0/2.0+1.0/3.0+1.0/4.0+...sum are:5.187378
30 1.0-1.0/2.0+1.0/3.0-1.0/4.0+...sum are:0.688172
31 The sum of the two sequences is:5.875550
32
33 Now you can enter again (<=0 to quit):1000
34 1.0+1.0/2.0+1.0/3.0+1.0/4.0+...sum are:7.485471
35 1.0-1.0/2.0+1.0/3.0-1.0/4.0+...sum are:0.692647
36 The sum of the two sequences is:8.178118
37
38 Now you can enter again (<=0 to quit):10000
39 1.0+1.0/2.0+1.0/3.0+1.0/4.0+...sum are:9.787606
40 1.0-1.0/2.0+1.0/3.0-1.0/4.0+...sum are:0.693097
41 The sum of the two sequences is:10.480703
42
43 Now you can enter again (<=0 to quit):0
44 Done.
45
46 */
/*C Primer Plus (6.16) 13*/
1 #include<stdio.h>
2 #define NUM 8
3 int main()
4 {
5 int arr[NUM],sum;
6 int i=0;
7 for(int i=0,sum=2;i<NUM;i++,sum*=2)
8 {
9 arr[i]=sum;
10 }
11
12 printf("The elements in the array are:");
13 do
14 {
15 printf("%d ",arr[i]);
16 i++;
17 }
18 while(i<NUM);
19 printf("\nDone.");
20
21 return 0;
22 }
23 /*
24 輸出樣例
25
26 The elements in the array are:2 4 8 16 32 64 128 256
27 Done.
28
29 */
/*C Primer Plus (6.16) 14*/
1 #include<stdio.h>
2 #define NUM 8
3 int main()
4 {
5 double arr1[NUM],arr2[NUM];
6 double sum=0.0;
7
8 printf("Please enter eight numbers:");
9 for(int i=0;i<NUM;i++)
10 {
11 scanf("%lf",&arr1[i]);
12 }
13 for(int i=0;i<NUM;i++)
14 {
15 arr2[i]=arr1[i]+arr2[i-1];
16 }
17 printf("The result of arr1 is:");
18 for(int i=0;i<NUM;i++)
19 {
20 printf("%g\t",arr1[i]);
21 }
22 printf("\n");
23 printf("The result of arr2 is:");
24 for(int i=0;i<NUM;i++)
25 {
26 printf("%g\t",arr2[i]);
27 }
28
29 return 0;
30 }
31 /*
32 輸出樣例
33
34 Please enter eight numbers:1 2 3 4 5 6 7 8
35 The result of arr1 is:1 2 3 4 5 6 7 8
36 The result of arr2 is:1 3 6 10 15 21 28 36
37
38 */
/*C Primer Plus (6.16) 15*/
1 #include<stdio.h>
2 #define NUM 255
3 int main()
4 {
5 char arr[NUM];
6 int i=0;
7
8 printf("Please enter what you think:");
9 while (scanf("%c",&arr[i])==1 && i<NUM )//&& arr[i]!='\n')
10 {
11 i++;
12 }
13 printf("The reverse order of the input:");
14 for (i--; (i+1)>0; i--)
15 {
16 printf("%c",arr[i]);
17 }
18 printf("\nThat's all.");
19
20 return 0;
21 }
22 /*
23 輸出樣例
24
25 Please enter what you think:apple tree
26 The reverse order of the input:eert elppa
27 That's all.
28
29 */
/*C Primer Plus (6.16) 16*/
1 #include<stdio.h>
2 int main()
3 {
4 double Daphne=100.0,Deirdre=100.0;
5 int year=0;
6 while(Deirdre<=Daphne)
7 {
8 Deirdre+=Deirdre*0.05;
9 Daphne+=100.0*0.1;
10 ++year;
11 }
12 printf("Deirdre=%.3lf\n",Deirdre);
13 printf("Daphne=%.3lf\n",Daphne);
14 printf("Deirdre>Daphne\n");
15 printf("Deirdre surpassed Daphne for %d years.",year);
16
17 return 0;
18 }
19 /*
20 輸出樣例
21
22 Deirdre=373.346
23 Daphne=370.000
24 Deirdre>Daphne
25 Deirdre surpassed Daphne for 27 years.
26
27 */
/*C Primer Plus (6.16) 17*/
1 #include<stdio.h>
2 int main()
3 {
4 double Lucky=100.0;
5 int year=0;
6
7 while(Lucky>9)
8 {
9 ++year;
10 Lucky+=Lucky*0.08;
11 Lucky-=10.0;
12 printf("After %d year, Chuckie Lucky have "
13 "%.3lf million dollars left.\n",year,Lucky);
14 }
15 printf("In the %dst year, "
16 "Chuckie Luck withdrew all the money!",year+1);
17
18 return 0;
19 }
20 /*
21 輸出樣例
22
23 After 1 year, Chuckie Lucky have 98.000 million dollars left.
24 After 2 year, Chuckie Lucky have 95.840 million dollars left.
25 After 3 year, Chuckie Lucky have 93.507 million dollars left.
26 After 4 year, Chuckie Lucky have 90.988 million dollars left.
27 After 5 year, Chuckie Lucky have 88.267 million dollars left.
28 After 6 year, Chuckie Lucky have 85.328 million dollars left.
29 After 7 year, Chuckie Lucky have 82.154 million dollars left.
30 After 8 year, Chuckie Lucky have 78.727 million dollars left.
31 After 9 year, Chuckie Lucky have 75.025 million dollars left.
32 After 10 year, Chuckie Lucky have 71.027 million dollars left.
33 After 11 year, Chuckie Lucky have 66.709 million dollars left.
34 After 12 year, Chuckie Lucky have 62.046 million dollars left.
35 After 13 year, Chuckie Lucky have 57.009 million dollars left.
36 After 14 year, Chuckie Lucky have 51.570 million dollars left.
37 After 15 year, Chuckie Lucky have 45.696 million dollars left.
38 After 16 year, Chuckie Lucky have 39.351 million dollars left.
39 After 17 year, Chuckie Lucky have 32.500 million dollars left.
40 After 18 year, Chuckie Lucky have 25.100 million dollars left.
41 After 19 year, Chuckie Lucky have 17.107 million dollars left.
42 After 20 year, Chuckie Lucky have 8.476 million dollars left.
43 In the 21st year, Chuckie Luck withdrew all the money!
44
45 */
/*C Primer Plus (6.16) 18*/
1 #include<stdio.h>
2 #define DUNBAR 150
3 int main()
4 {
5 int origin=5;
6 int Friend_sub=1;
7
8 while(origin<DUNBAR)
9 {
10 origin=2*(origin-Friend_sub++);
11 printf("After %d weeks,"
12 "Rabnud has %d friends now.\n",Friend_sub-1,origin);
13 }
14 printf("In the 9th week, "//八周后(after)才會超過Dunbar's number,所以朋友在第九周超過。
15 "Dr. Rabnum will have more friends than Dunbar's number!");
16 return 0;
17 }
18 /*
19 輸出樣例
20
21 After 1 weeks,Rabnud has 8 friends now.
22 After 2 weeks,Rabnud has 12 friends now.
23 After 3 weeks,Rabnud has 18 friends now.
24 After 4 weeks,Rabnud has 28 friends now.
25 After 5 weeks,Rabnud has 46 friends now.
26 After 6 weeks,Rabnud has 80 friends now.
27 After 7 weeks,Rabnud has 146 friends now.
28 After 8 weeks,Rabnud has 276 friends now.
29 In the 9th week, Dr. Rabnum will have more friends than Dunbar's number!
30
31 */
C Primer Plus (6.16) 編程練習的更多相关文章
- [转] 編程風格要素-The Elements of Programming Style 中文英文中英對照
转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php 下面的程序風格規則提煉自Brian Kernighan和P. J. Pl ...
- C++編程札記「基礎」
一直以為自己最擅長的編程語言是C++,那時自己的水平停留在使用C++來實現數據結構中的各種ADT和ACM算法. 創建一個類,必須實現的成員函數 explicit構造函數 對於單參數構造函數,添加exp ...
- [心得] 如何利用liquibase進行資料庫版本控制 - 實際練習
透過上一篇的基本觀念介紹,希望大家應該有一點點感覺了! 這篇我們就來做個簡單的版本演練,加深印象吧! 我使用的環境如下 System : Windows 7 Database : SQL Server ...
- [转]2010 Ruby on Rails 書單 與 練習作業
原帖:http://wp.xdite.net/?p=1754 ========= 學習 Ruby on Rails 最快的途徑無非是直接使用 Rails 撰寫產品.而這個過程中若有 mentor 指導 ...
- [Java] 練習用對戰小遊戲
繼承.介面自我練習時所建立的小遊戲,一開始輸入名稱來建立對戰腳色,之後以輸入招式號碼的方式互相打鬥,最後沒血的一方就輸了. 人物種族 abstract public class Human { int ...
- 《C++ Primer Plus》16.4 泛型编程 学习笔记
STL是一种泛型编程(generic programming).面向对象编程关注的是编成的数据方面,而泛型编程关注的是算法.它们之间的共同点是抽象和创建可重用代码,单他们的理念决然不同.泛型编程旨在编 ...
- 《C++ Primer Plus》16.3 标准模板库 学习笔记
STL提供了一组表示容器.迭代其.函数对象和算法的模板.容器是一个与数组类似的单元,可以存储若干个值.STL容器是同质的,即存储的值的类型相同:算法是完成特定任务(如对数组进行排序或在链表中查找特定值 ...
- 《C++ Primer Plus》16.2 智能指针模板类
智能指针是行为类似于指针的类对象,单这种对象还有其他功能.本节介绍三个可帮助管理动态内存分配的智能指针类.先来看看需要哪些功能以及这些功能是如何实现的.请看下面的函数:void remodel(std ...
- 《C++ Primer Plus》16.1 string类 学习笔记
16.1.1 构造字符串程序清单16.1使用了string的7个构造函数.程序清单16.1 str1.cpp---------------------------------------------- ...
- Codeforces Gym101522 D.Distribution of Days-算日期 (La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017)
D.Distribution of Days The Gregorian calendar is internationally the most widely used civil calendar ...
随机推荐
- 驱动开发:内核测试模式过DSE签名
微软在x64系统中推出了DSE保护机制,DSE全称(Driver Signature Enforcement),该保护机制的核心就是任何驱动程序或者是第三方驱动如果想要在正常模式下被加载则必须要经过微 ...
- 洛谷P5759题解
本文摘自本人洛谷博客,原文章地址:https://www.luogu.com.cn/blog/cjtb666anran/solution-p5759 \[这道题重在理解题意 \] 选手编号依次为: \ ...
- dns隧道攻击原理及常用工具流量分析
DNS协议是一种请求应答协议,也是一种可用于应用层的隧道技术.虽然DNS流量的异常变化可能会被发现,但是在基于传统socket隧道已经濒临淘汰,TCP.UDP通信大量被安全设备拦截的大背景下,DNS. ...
- Python基础部分:1、typora软件和对计算机的认识
目录 一.typora软件 1.安装 2.markdown语法 二.计算机的本质 1.进制数 三.计算机五大组成部分概要 1.控制器 2.运算器 3.存储器 4.输入设备 5.输出设备 一.typor ...
- pycharm系列---django
manage debug Python Console基本配置 DJANGO_SETTINGS_MODULE=mini_project.settings import sys import djang ...
- Seata 1.5.2 源码学习
文章有点长,我决定用半个小时来给您分享~ 基于Seata 1.5.2,项目中用 seata-spring-boot-starter 1. SeataDataSourceAutoConfiguratio ...
- Ajax基础(中)
这节主要在上节的基础上学会如何使用Ajax 源码下载: 链接:https://pan.baidu.com/s/1kG-vACFxneAZqONdo97XrQ 提取码:k21y 在WebStorm中打开 ...
- 【云原生 · Kubernetes】Kubernetes简介及基本组件
1.Kubernetes简介 Kubernetes是Google开源的容器集群管理系统,其提供应用部署.维护. 扩展机制等功能,如图1.3所示.利用Kubernetes能方便地管理跨机器运行容器化的应 ...
- Kubernetes(K8S) 配置管理-ConfigMap 介绍
作用:存储不加密数据到 etcd,让 Pod 以变量或者 Volume 挂载到容器中 场景:配置文件 创建配置文件 redis.properties redis.host=127.0.0.1 redi ...
- Day38:Lambda表达式
Lambda表达式 1.1 概述 Lambda是JDK8开始后的一种新语法形式. 作用:简化函数式匿名内部类的代码写法. 简化格式: /*部类被重写方法的参数)->{ 被重写方法的方法体代码 } ...