Practice

1.输入名字和姓氏,以“名字,姓氏”的格式输出打印。

#include
int main(void)
{
char name[20];
char family[20];
printf("My Handsome Master, please enter your name:
\n");
scanf("%s %s", &name, &family);
//名字和姓氏一起读的话,需要先输入名字再输入姓氏,中间要有空白字符(空格、制表、换行)
printf("\"%s,%s\"!\n", name, family);//注意双引号的转义字符
return 0;
}

2.输入名字,以一定格式(略)输出。
#include
#include

int main(void)
{
char name[20];
int length = 0;

printf("please enter your name: \n");
scanf("%s", name);
printf("1: \"%s\"\n", name);
printf("2: \" s\"\n", name);
printf("3: \"%-20s\"\n", name);
length = strlen(name) + 3;
printf("4: \"%*s\"\n", length, name);//注意*代表输出宽度length  ! !
!

return 0;
}

3. 题略
#include
int main(void)
{
float f;
printf("please enter a float number: \n");
scanf("%f", &f);
printf("a. The input is %0.1f or %0.1e\n", f, f);
printf("b. The input is +%5.3f or %4.3e\n", f, f);

return 0;
}

4.题略
#include
int main(void)
{
char name[30];
float height;

printf("My owner, please enter your name and height (eg: tom
180): \n");
scanf("%s %f", &name, &height);
printf("Dear %s, you are %.3f cm tall\n", name, height);

return 0;
}

5.题略
#include
#include

int main (void)
{
char name[10];
char fame[10];
int num1 = 0;
int num2 = 0;

printf("Please enter your name:\n");
scanf("%s",name);
num1 = strlen(name);
printf("Please enter your famliy name:\n");
scanf("%s",fame);
num2 = strlen(fame);
printf("%s %s\n%*d
%*d\n",name,fame,num1,num1,num2,num2);
printf("%s %s\n%-*d
%-*d\n",name,fame,num1,num1,num2,num2);
//注意*的使用

return 0;
}
结果如下图
6.题略

#include
#include

int main(void)
{
float num_f = 0;
double num_d = 0;
num_f = 1 / 3;
num_d = 1 / 3;
 
     
%num_f = 1.0 / 3.0;
%num_d = 1.0
/ 3.0;

printf("%.4f\n%.12f\n%.16f\n\n", num_f, num_f, num_f);
printf("%.4lf\n%.12lf\n%.16lf\n\n", num_d, num_d,
num_d);
printf("%d\n%d\n",FLT_DIG,DBL_DIG);
return 0;
}
结果显示
上网一查得到说法如下:
语法没有错,可以这样写,不过f的值是0.0

因为数字在C/C++中默认的类型是int,两个int的四则运算结果仍然是int

所以1/3=0.33333……,强制转化成int后结果为0,再将int的0赋值给float类型的f,强制转化后就是0.0,所以不是你想要的结果0.3333……,

如果想得到0.3333……,应该这样写:

float f = (float) 1 / 3 或 f = 1 / (float) 3

或者

float f = 1.0 / 3 或 f = 1 / 3.0 或 f = 1.0 / 3.0

对于double型数据,小数点后超过15位后的数据不能保证精度。

7.题略
最后一题了,好像不是很难,懒得做了,饿死了要吃晚饭了,直接复制一份网上的答案到vs2010环境下,尼玛,居然检测出来是复制到网页上的答案,还报错了。尼玛,真是吊啊
解决办法,先复制到TXT文档里,结果还是不行,再复制到word里,这才行了

#include
#define GALLON 3.785  //1 gallon = 2.785
litre
#define MILE 1.609   //1 mile = 1.609
kilometer

int main(void)
{
float mile,gallon;
printf("Please input miles and gallons:");
scanf("%f %f",&mile,&gallon);
printf("Miles per
gallon:%.1f\n",mile/gallon); 
printf("Litres per 100
kilometre:%.1f\n",gallon*GALLON/(100*mile*MILE));
%getchar();
%getchar();
return(0);
}
好像最后的结果有点问题,懒得改了,饿了,要吃饭了,不然胃疼又不值得了(这题不难关键)

C Primer Plus_第四章_字符串和格式化输入输出_编程练习的更多相关文章

  1. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  2. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  3. [Python学习笔记][第四章Python字符串]

    2016/1/28学习内容 第四章 Python字符串与正则表达式之字符串 编码规则 UTF-8 以1个字节表示英语字符(兼容ASCII),以3个字节表示中文及其他语言,UTF-8对全世界所有国家需要 ...

  4. c语言之字符串和格式化输入输出

    字符串和格式化输入输出 #include<stdio.h> #include<string.h> #define DENSITY 62.4 int main(void) { f ...

  5. C++ Primer Plus 第四章 复合类型 学习笔记

    第四章 复合类型 1. 数组概述 1.1 数组的定义 数组(array)是一种数据格式,能够存储多个同类型的值.每个值都存储在一个独立的数组元素中,计算机在内存中依次存储数组的各个元素. 数组声明的三 ...

  6. 【C++】《C++ Primer 》第四章

    第四章 表达式 一.基础 重载运算符:当运算符作用在类类型的运算对象时,用户可以自行定义其含义. 左值和右值: C中:左值可以在表达式左边,右值不能. C++中:当一个对象被用作右值的时候,用的是对象 ...

  7. C Primer Plus_第8章_字符输入输出和输入确认_编程练习

    1.题略 #include <stdio.h> int main(void) { ; printf("Please enter text here(end with Ctrl + ...

  8. C Primer Plus_第10章_数组和指针_编程练习

    1. /*rain.c 针对若干年的降水量数据,计算年降水总量.年降水平均量,以及月降水平均量*/ #include <stdio.h> #define MONTHS 12 #define ...

  9. C Primer Plus_第9章_函数_编程练习

    1.题略 /*返回较小值,设计驱动程序测试该函数*/ #include <stdio.h> double min (double a, double b); int main (void) ...

随机推荐

  1. [译]A Beginner’s Guide to npm — the Node Package Manager

    原文: http://www.sitepoint.com/beginners-guide-node-package-manager/ Installing Node.js 验证你的安装是否成功. $ ...

  2. 【AngularJS】—— 7 模块化

    AngularJS有几大特性,比如: 1 MVC 2 模块化 3 指令系统 4 双向数据绑定 那么本篇就来看看AngularJS的模块化. 首先先说一下为什么要实现模块化: 1 增加了模块的可重用性 ...

  3. 【AngularJS】—— 13 服务Service

    在AngularJS中有很多的服务,常用的比如$http,$location等等. 本篇文章会介绍一下的内容: 1 $http这种Angular提供的服务的使用 2 如何自定义服务,并总结服务需要注意 ...

  4. IEnumerable<> ICollection <> IList<> 区别

    IEnumerable< ICollection < IList区别 public interface IEnumerable { IEnumerator GetEnumerator(); ...

  5. 特殊字符导致用正则表达式进行字符串替换失败,Java replaceAll()方法报错Illegal group reference

    String str = "给商品||?>\\n阳澄湖大闸蟹!@#$%^&*()_+-=?:\",.]\\|~.,\/??\\\\|\\br点赞" Stri ...

  6. SpringBoot使用的心得记录

    security配置 import com.yineng.corpsysland.security.*; import com.yineng.corpsysland.web.filter.Author ...

  7. 开着idea,死机了,关机重启。重启之后,重新打开idea报错java.lang.AssertionError:upexpected content storage modification

    开着idea,死机了,关机重启.重启之后,重新打开idea报错java.lang.AssertionError:upexpected content storage modification. goo ...

  8. 据说Linuxer都难忘的25个画面

    导读 虽然对 Linux 正式生日是哪天还有些争论,甚至 Linus Torvalds 认为在 1991 那一年有四个日子都可以算作 Linux 的生日.但是不管怎么说,Linux 已经 25 岁了, ...

  9. 37 网络相关函数(五)——live555源码阅读(四)网络

    37 网络相关函数(五)——live555源码阅读(四)网络 37 网络相关函数(五)——live555源码阅读(四)网络 简介 10)MAKE_SOCKADDR_IN构建sockaddr_in结构体 ...

  10. 新塘ARM平台交叉编译minigui界面库

    简介 MiniGUI 是一款面向嵌入式系统的高级窗口系统(Windowing System)和图形用户界面(Graphical User Interface,GUI)支持系统,由魏永明先生于 1998 ...