首页 > 其他分享 >C Primer Plus 5.11 編程練習

C Primer Plus 5.11 編程練習

时间:2022-12-26 01:22:07浏览次数:45  
标签:int printf Please number 編程 5.11 Plus enter Primer

/*C Primer Plus (5.10) 9*/

 1 #include<stdio.h>
 2 #define G 103
 3 int main()
 4 {
 5     char ch=96;
 6     
 7     while(ch++<G)
 8     {
 9         printf("%5c",ch);
10     }
11     printf("\n");
12     
13     return 0;
14 }

/*C Primer Plus (5.11) 1*/

 1 #include<stdio.h>
 2 #define M_TO_H 60
 3 int main()
 4 {
 5     int time;
 6     int hour=0,minute=0;
 7     
 8     printf("Please enter the time(mins)\n");
 9     printf("Enter 0 or num<0 to quit.\n");
10     printf("Your time:");
11     scanf("%d",&time);
12     while(time>0)
13     {
14         hour=time/M_TO_H;
15         minute=time%M_TO_H;
16         printf("%d mintues is %d hours and %d minutes\n",time,hour,minute);
17         printf("Please enter again!\n");
18         printf("Your time:");
19         scanf("%d",&time);
20     }
21     printf("Done!");
22      
23     return 0;
24 }
25 /*
26 輸出樣例 
27 
28 Please enter the time(mins)
29 Enter 0 or num<0 to quit.
30 Your time:20
31 20 mintues is 0 hours and 20 minutes
32 Please enter again!
33 Your time:60
34 60 mintues is 1 hours and 0 minutes
35 Please enter again!
36 Your time:85
37 85 mintues is 1 hours and 25 minutes
38 Please enter again!
39 Your time:0
40 Done!
41 
42 */

/*C Primer Plus (5.11) 2*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4 int num;
 5 int i=0;
 6 
 7 printf("Please enter an integer:");
 8 scanf("%d",&num);
 9 printf("You will get %d to %d\n",num,num+10);
10 printf("%d\t",num);
11 while(i<10)
12 {
13 ++num;
14 i++;
15 printf("%d\t",num);
16 }
17 
18 return 0;
19 }
20 /*
21 輸出樣例
22 
23 Please enter an integer:5
24 You will get 5 to 15
25 5       6       7       8       9       10      11      12      13      14      15
26 
27 */

/*C Primer Plus (5.11) 3*/

 1 #include<stdio.h>
 2 #define D_TO_W 7
 3 int main()
 4 {
 5 int days;
 6 int week=0,day=0;
 7 
 8 printf("Please enter the number of days(<=0 to quit):");
 9 scanf("%d",&days);
10 while(days>0){
11 week=days/D_TO_W;
12 day=days%D_TO_W;
13 printf("%d days is %d weeks, %d days\n",days,week,day);
14 printf("Now enter the number of days again:");
15 scanf("%d",&days);
16 }
17 printf("You have quitted just now.");
18 
19 return 0;
20 }
21 /*
22 
23 輸出樣例
24 
25 Please enter the number of days(<=0 to quit):18
26 18 days is 2 weeks, 4 days
27 Now enter the number of days again:24
28 24 days is 3 weeks, 3 days
29 Now enter the number of days again:35
30 35 days is 5 weeks, 0 days
31 Now enter the number of days again:0
32 You have quitted just now.
33 
34 */

/*C Primer Plus (5.11) 4*/

 1 #include<stdio.h>
 2 #define CM_TO_IN 0.3937 //1cm=0.3937inch
 3 #define CM_TO_ML 0.0328 //1cm=0.0328inch
 4 int main()
 5 {
 6 float height;
 7 float inch=0,feet=0;
 8 
 9 printf("Enter a height in centimeters:");
10 scanf("%f",&height);
11 while(height>0)
12 {
13 feet=(int)(height*CM_TO_ML);
14 inch=(height-(feet/CM_TO_ML))*CM_TO_IN;
15 printf("%.1f cm=%d feet, %3.1f inches\n",height,(int)feet,inch);
16 printf("Enter a height in centimeters(<=0 to quit):");
17 scanf("%f",&height);
18 }
19 printf("bye.");
20 
21 return 0;
22 }
23 /*
24 Sample Output
25 
26 Enter a height in centimeters:182
27 182.0 cm=5 feet, 11.6 inches
28 Enter a height in centimeters(<=0 to quit):168.7
29 168.7 cm=5 feet, 6.4 inches
30 Enter a height in centimeters(<=0 to quit):0
31 bye.
32 
33 */

/*C Primer Plus (5.11) 5*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4 int count,sum;
 5 int i=0;
 6 
 7 count=0;
 8 sum=0;
 9 printf("Please enter the days you work: ");
10 scanf("%d",&i);
11 while(count++<i)
12 {
13 sum+=count;//sum=sum+count;
14 }
15 printf("Sum=%d\n",sum);
16 printf("That all!");
17 
18 return 0;
19 }
20 /*
21 Sample Output
22 
23 Please enter the days you work: 20
24 Sum=210
25 That all!
26 
27 */

/*C Primer Plus (5.11) 6*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4 int count,sum;
 5 int i=0;
 6 
 7 count=0;
 8 sum=0;
 9 printf("Please enter the days you work: ");
10 scanf("%d",&i);
11 while(count++<i)
12 {
13 sum+=count*count;//sum=sum+(count*count)
14 }
15 printf("Sum=%d\n",sum);
16 printf("That all!");
17 
18 return 0;
19 }
20 /*
21 Sample Output
22 
23 Please enter the days you work: 20
24 Sum=2870
25 That all!
26 
27 */

/*C Primer Plus (5.11) 7*/

 1 #include<stdio.h>
 2 double fun(double x)
 3 {
 4 printf("The cube of the number is:%g",x*x*x);
 5 return x;
 6 }
 7 int main()
 8 {
 9 double num;
10 
11 printf("Please enter a number:");
12 while(scanf("%lf",&num)==1)
13 {
14    fun(num);
15    printf("\n");
16    printf("Please enter another number:");
17 }
18 printf("You enter a wrong data,the program has been quit.");
19 return 0;
20 }
21 /*
22 Sample Output
23 
24 Please enter a number:3
25 The cube of the number is:27
26 Please enter another number:2
27 The cube of the number is:8
28 Please enter another number:5
29 The cube of the number is:125
30 Please enter another number:p
31 You enter a wrong data,the program has been quit.
32 
33 */

/*C Primer Plus (5.11) 8*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4 int num1,num2;
 5 int mod=0;
 6 
 7 printf("This program computes moduli.\n");
 8 printf("Enter an integer to serve as the second operand:");
 9 scanf("%d",&num1);
10 printf("Now enter the first operand:");
11 while(scanf("%d",&num2)==1)
12 {
13 mod=num2%num1;
14 if(num2==0)
15     break;
16 printf("%d %% %d is %d\n",num2,num1,mod);
17 printf("Enter next number for first operand (<=0 to quit):");
18 }
19 printf("Done.");
20 
21 return 0;
22 }
23 /*
24 Sample Output
25 
26 This program computes moduli.
27 Enter an integer to serve as the second operand:256
28 Now enter the first operand:438
29 438 % 256 is 182
30 Enter next number for first operand (<=0 to quit):1234567
31 1234567 % 256 is 135
32 Enter next number for first operand (<=0 to quit):0
33 Done.
34 
35 */

/*C Primer Plus (5.11) 9*/

 1 #include<stdio.h>
 2 
 3 double Temperatures(double x)
 4 {
 5 const double F_TO_C=32.0;
 6 const double C_TO_K=273.16;
 7 double c=0,k=0;
 8 c=5.0/9.0*(x-F_TO_C);
 9 k=c+C_TO_K;
10 printf("Centigrade temperature:%.3lf\n",c);
11 printf("Kelvin temperature:%.3lf\n",k);
12 }
13 
14 int main()
15 {
16 double temp=0;
17 
18 printf("Please enter temperatures in Degree Fahrenheit:");
19 while(scanf("%lf",&temp)==1)
20 {
21 printf("Fahrenheit temperature:%.3lf\n",temp);
22 Temperatures(temp);
23 printf("Please enter temperatures in Degree Fahrenheit (enter q and other non-numeric characters to quit):");
24 }
25 printf("Done.");
26 return 0;
27 }
28 /*
29 Sample Output
30 
31 Please enter temperatures in Degree Fahrenheit:77
32 Fahrenheit temperature:77.000
33 Centigrade temperature:25.000
34 Kelvin temperature:298.160
35 Please enter temperatures in Degree Fahrenheit (enter q and other non-numeric characters to quit):98
36 Fahrenheit temperature:98.000
37 Centigrade temperature:36.667
38 Kelvin temperature:309.827
39 Please enter temperatures in Degree Fahrenheit (enter q and other non-numeric characters to quit):q
40 Done.
41 
42 */

标签:int,printf,Please,number,編程,5.11,Plus,enter,Primer
From: https://www.cnblogs.com/NoldorFromMiddleEarth/p/17004903.html

相关文章

  • C++Primer
    第2章2.1基本内置类型1、表示整数、字符和布尔值的算术类型合称为**整型**2、0值算术类型代表false,任何非0的值都代表true3、在一行末尾加“\”,可将此行和下一行当......
  • C Primer Plus(4.8)編程練習
    /*CPrimerPlus(4.7)5*/1include<stdio.h>2#defineBOOK"WarandPeace"3intmain(void)4{5floatcost=12.99;6floatpercent=80.0;7......
  • Cpp Primer:补充
    目录侯捷C++面向对象高级编程(上)(下)1.conversionfunction2.non-explicit-one-argumentctor3.pointer-likeclass4.function-likeclass5.关于namespace6.classtempl......
  • CMU15-445_Project 1:C++Primer
    Project1:C++Primer先开一个坑1.Abstractabstract2.IntroductionintroPreparationtools3.ImplementationTask#1-TemplatedTrieHeadfileTrieNodeCl......
  • Cpp Primer:Sec 7
    目录Sec7类7.1定义抽象数据类型7.1.1/7.1.2关于Sales_data类的改进7.1.3定义类相关的非成员函数7.1.4构造函数7.1.5拷贝、赋值、析构7.2访问控制与封装7.2.1友元......
  • Cpp Primer:Sec 11:关联容器
    Sec11关联容器两个主要的关联容器:map:key-value对,关键字起到索引的作用,值表示与索引关联的数据例子:字典set:每个元素只包含一个关键字,set支持高效的关键字查询操作......
  • Cpp Primer:Sec 8, 9, 10
    目录Sec8IO库8.1IO类8.2文件输入输出8.3stringstreamSec9顺序容器9.4Vector对象如何增长?Sec10泛型算法10.2初识10.3定制操作10.3.3lambda捕获与返回10.4再探迭......
  • Cpp Primer:Sec 12:动态内存
    目录Sec12动态内存12.1动态内存与智能指针12.2动态数组Sec12动态内存12.1动态内存与智能指针new:在动态内存中为对象分配空间,并返回一个指向该对象的指针delete:......
  • Cpp Primer:Sec 13:拷贝控制
    目录Sec13拷贝控制13.1拷贝、赋值与销毁13.2拷贝控制和资源管理13.3交换操作13.4拷贝控制示例13.5动态内存管理类13.6对象移动13.6.1右值引用13.6.2移动构造函数......
  • Cpp Primer:Sec 14:重载与类型转换
    目录Sec14重载运算与类型转换14.1基本概念14.2输入输出运算符14.9重载、类型转换与运算符Sec14重载运算与类型转换当运算符作用于类类型的运算对象时,可以通过运算符......