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. SAMBA 共享服务器搭建

    yum install samba service smb start chkconfig smb on 1.给要共享的文件夹赋权限 777 2.修改 smb 的配置文件:/etc/samba/smb ...

  2. 同一个解决方案"引用"其他的项目出现感叹号...

    项目A是自己新建的... 但是项目B是"添加"→"现有项目"添加的... 所以项目B引用项目A的时候,引用的项目A显示感叹号... 项目A右击"属性& ...

  3. javascript 时间代理

    <button class="btn-active">按钮1</button> <button>按钮2</button> <b ...

  4. [Kerberos] How to Kerberize an Hadoop Cluster

    Overview Kerberos是一个第三方认证机制,用户和服务(known as principals)通过kerberos server (known as the Key Distributi ...

  5. python 多线程就这么简单(转)

    多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...

  6. IE浏览器版本判断

    <script type="text/javascript"> var browser=navigator.appName var b_version=navigato ...

  7. CentOS 6.5安装Apache

    1.Apache的特点 功能强大.配置简单.速度快.应用广泛.性能稳定可靠,并可做代理服务器或负载均衡来使用 2.Apache的应用场合 使用Apache运行静态HTML网页.图片(处理静态小文件能力 ...

  8. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  9. 使用SQLPlus连接Oracle实例

    使用Windows徽标+R,打开运行对话框,输入cmd并回车. Microsoft Windows Microsoft Corporation.保留所有权利. C:\Users\user> 在C ...

  10. 160809225-叶桦汀《C语言程序设计》实验报告

    #include<stdio.h> int main() { int a,b,c,t; printf("请输入三个整数"); scanf("%d%d%d&qu ...