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

C Primer Plus(4.8)編程練習

时间:2022-12-21 14:55:39浏览次数:46  
标签:float name enter Please 編程 Plus printf Primer your

/*C Primer Plus (4.7) 5*/

 1 include<stdio.h>
 2 #define BOOK "War and Peace"
 3 int main(void)
 4 {
 5     float cost=12.99;
 6     float percent=80.0;
 7     
 8     printf("This copy of \"%s\" sells for $%.2f.\n",BOOK,cost);
 9     printf("That is %.0f%% of list.",percent);/*打印%要這樣“%%”*/
10     
11     return 0;
12 }
13 /*
14 輸出樣例:
15 
16 This copy of "War and Peace" sells for $12.99.
17 That is 80% of list.
18  
19 */

/*C Primer Plus (4.8) 1*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char firstname[30];
 5     char lastname[30];
 6 
 7     printf("Please enter your first name.\n");
 8     printf("First name:");
 9     scanf("%s",firstname);
10     printf("Please enter your last name.\n");
11     printf("Last name:");
12     scanf("%s",lastname);
13     printf("Hello! %s %s Welcome~!",firstname,lastname);
14     
15     return 0;
16 }
17 /*
18 輸出樣例
19 
20 Please enter your first name.
21 First name:Tomoko
22 Please enter your last name.
23 Last name:Kuroki
24 Hello! Tomoko Kuroki Welcome~!
25 
26 */

/*C Primer Plus (4.8) 2*/

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char firstname[30];
 6     char lastname[30];
 7     int string1=0,string2=0;
 8 
 9     printf("Please enter your first name.\n");
10     printf("First name:");
11     scanf("%s",firstname);
12     printf("Please enter your last name.\n");
13     printf("Last name:");
14     scanf("%s",lastname);
15     string1=strlen(firstname);
16     string2=strlen(lastname);
17     printf("Request 1:/*\"%s %s\"*/\n",firstname,lastname);
18     printf("Request 2:/*\"%20s %20s\"*/\n",firstname,lastname);
19     printf("Request 3:/*\"%-20s %-20s\"*/\n",firstname,lastname);
20     printf("Request 4:/*%*s %*s*/",string1+3,firstname,string2+3,lastname);
21     
22     return 0;
23 }
24 /*
25 輸出樣例 
26 
27 //Please enter your first name.
28 //First name:Tomoko
29 //Please enter your last name.
30 //Last name:Kuroki
31 // Request 1:/*"Tomoko Kuroki"*/
32 // Request 2:/*"              Tomoko               Kuroki"*/
33 // Request 3:/*"Tomoko               Kuroki              "*/
34 // Request 4:/*   Tomoko    Kuroki*/

/*C Primer Plus (4.8) 3*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     float num=0;
 5     
 6     printf("Please enter a floating-point number:");
 7     scanf("%f",&num);
 8     printf("a:The input is %.1f or %.1e.\n",num,num);
 9     printf("b:The input is %+.3f or %.3E.",num,num);
10     
11     return 0;
12 }
13 /*
14 輸出樣例
15 
16 Please enter a floating-point number:26.87
17 a:The input is 26.9 or 2.7e+001.
18 b:The input is +26.870 or 2.687E+001. 
19 
20 */

/*C Primer Plus (4.8) 4*/

 1 #include<stdio.h>
 2 #define I_TO_F 0.08333
 3 int main()
 4 {
 5     char name[30];
 6     float height=0,realheight=0;
 7     
 8     printf("Please enter your name and your height(inches).\n");
 9     printf("Your name:");
10     scanf("%s",name);
11     printf("Your height:");
12     scanf("%f",&height);
13     realheight=height*I_TO_F;
14     printf("%s, you are %.3f feet tall.",name,realheight);
15     
16     return 0;
17 }
18 /*
19 輸出樣例
20  
21 Please enter your name and your height(inches).
22 Your name:Dabney
23 Your height:74.496
24 Dabney, you are 6.208 feet tall.
25  
26 */

/*C Primer Plus (4.8) 5*/

 1 #include<stdio.h>
 2 #define BIT 8
 3 int main()
 4 {
 5     float file=0,speed=0,time=0;
 6     printf("Please enter the net speed\n");
 7     printf("The net speed:");
 8     scanf("%f",&speed);
 9     printf("Please enter the size of the file:\n");
10     printf("The size of flie:");
11     scanf("%f",&file);
12     time=file*BIT/speed;
13     printf("At %.2f megabits per seconds, a file of %.2f megabytes\n"
14            "downloads in %.2f seconds.",speed,file,time);
15     
16     return 0;
17  } 
18  /*
19  輸出樣例
20  
21 Please enter the net speed
22 The net speed:18.123
23 Please enter the size of the file:
24 The size of flie:2.203
25 At 18.12 megabits per seconds, a file of 2.20 megabytes
26 downloads in 0.97 seconds.
27   
28 */

/*C Primer Plus (4.8) 6*/

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char firstname[30];
 6     char lastname[30];
 7     int string1=0,string2=0;
 8     
 9     printf("Please enter your first name.\n");
10     printf("First name:");
11     scanf("%s",firstname);
12     string1=strlen(firstname);
13     printf("Please enter your last name.\n");
14     printf("Last name:");
15     scanf("%s",lastname);
16     string2=strlen(lastname);
17     printf("%s %s\n",firstname,lastname);
18     printf("%*d %*d\n",string1,string1,string2,string2);
19     printf("%s %s\n",firstname,lastname);
20     printf("%-*d %-*d\n",string1,string1,string2,string2);
21     
22     return 0;
23 }
24 /*
25 輸出樣例
26 
27 Please enter your first name.
28 First name:Tomoko
29 Please enter your last name.
30 Last name:Kuroki
31 Tomoko Kuroki
32      6      6
33 Tomoko Kuroki
34 6      6
35  
36 */

/*C Primer Plus (4.8) 7*/

 1 #include<stdio.h>
 2 #include<float.h>
 3 int main()
 4 {
 5     double double_value=1.0/3.0;
 6     float float_value=1.0/3.0;
 7     
 8     printf("Request1:float_value=%8f double_value=%8.6lf\n",float_value,double_value);
 9     printf("\nRequest2:float_value=%8.12f double_value=%8.12lf\n",float_value,double_value);
10     printf("\nRequest3:float_value=%8.16f double_value=%8.16lf\n",float_value,double_value);
11     printf("\nfloat and double maximum significant digits:\n");
12     printf("FLT_DIG = %d, DBL_DIG = %d\n", FLT_DIG, DBL_DIG);
13     //FLT_DIG代表float有效十进制数字位数
14     //DBL_DIG代表double有效十进制数字位数
15     
16     return 0;
17 }
18 /*
19 輸出樣例 
20 
21 Request1:num1=0.333333 num2=0.333333
22 
23 Request2:num1=0.333333333333 num2=0.333333343267
24 
25 Request3:num1=0.3333333333333333 num2=0.3333333432674408
26 
27 float and double maximum significant digits:
28 FLT_DIG = 6, DBL_DIG = 15
29 
30 */

/*C Primer Plus (4.8) 8*/

 1 #include<stdio.h>
 2 #define G_TO_L 3.785f 
 3 #define M_TO_KM 1.609f
 4 int main()
 5 {
 6     float miles=0,gallons=0;
 7     float KM=0,L=0;
 8     float Europe=0,USA=0;
 9     
10     printf("Please enter the travel journey(miles) and gas consumption(gallons).\n");
11     printf("Travel journey(in miles):");
12     scanf("%f",&miles);
13     printf("Gas consumption(in gallons):");
14     scanf("%f",&gallons);
15     KM=miles*M_TO_KM;
16     L=gallons*G_TO_L;
17     Europe=L/KM;
18     USA=miles/gallons;
19     printf("The USA program measures travel per unit of fuel consumed (higher values, better):"
20            "%.1f Miles/Gallons\n",USA);
21     printf("The European scheme measures the stroke per unit of fuel consumed (the smaller the value, the better):"
22            "%.1f L/KM",Europe);
23     
24     return 0;
25 }
26 /*
27 輸出樣例
28 
29 Please enter the travel journey(miles) and gas consumption(gallons).
30 Travel journey(in miles):62.137
31 Gas consumption(in gallons):2.1134
32 The USA program measures travel per unit of fuel consumed (higher values, better):29.4 Miles/Gallons
33 The European scheme measures the stroke per unit of fuel consumed (the smaller the value, the better):0.1 L/KM
34  
35 */

标签:float,name,enter,Please,編程,Plus,printf,Primer,your
From: https://www.cnblogs.com/NoldorFromMiddleEarth/p/16996266.html

相关文章

  • mybatis-plus的select指定字段
    使用mapper的select相关方法时,我们来观察一下其生成的语句:我们注意到,生成的sql将表的全字段都查询出来了,相当于select*。众所周知,在实际的使用中是不推荐使用select*的......
  • mybatis-plus的自定义xml
    mybatis-plus中xml的使用方法和mybatis是一样,需要一些简单的配置就可以定义xml了。配置sql日志和mapper文件路径在application.properties中指定如下配置:其中mapper-......
  • Cpp Primer:补充
    目录侯捷C++面向对象高级编程(上)(下)1.conversionfunction2.non-explicit-one-argumentctor3.pointer-likeclass4.function-likeclass5.关于namespace6.classtempl......
  • mybatis-plus的分页
    mybatis-plus包含内置的分页插件,我们要做的就是配置拦截器,然后使用内置的分页类就可以了。配置Interceptor分页的使用QueryWrapper指定查询条件Page指定获取特定......
  • 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移动构造函数......