首页 > 其他分享 >C Primer Plus (6.16) 編程練習

C Primer Plus (6.16) 編程練習

时间:2023-01-01 23:33:09浏览次数:53  
标签:Chuckie int printf Please Lucky 編程 Plus year Primer

/*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 */

标签:Chuckie,int,printf,Please,Lucky,編程,Plus,year,Primer
From: https://www.cnblogs.com/NoldorFromMiddleEarth/p/17019266.html

相关文章