P87
编程练习1:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
char surname[40];//打印字符串类型时尽量在定义时将其放在数组里
char name[40];//字符数组 用于存储字符串
printf("Please enter your surname:\n");
scanf("%s",surname);//将字符串读入字符数组时不需要添加&
printf("Please enter your name:\n");
scanf("%s",name);
printf("Your surname is %s and your name is %s", surname, name);
return 0;
}
编程练习2:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
int list;
char name[40];
printf("Please enter your name:\n");
scanf("%s", name);
list= strlen(name)+3;
printf("Your name is \"%s\"\n", name);
printf("Your name is \"%20s\"\n", name);
printf("Your name is \"%-20s\"\n", name);
printf("Your name is \"%*s\"\n",list,name);//*对应list
}
编程练习3:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
float Num;
printf("Please enter your wangting number:");
scanf("%f", &Num);
printf("The input is %.1f or %.1e\n",Num,Num);//.1e=2.1e+001
printf("The inout is %+.3f or %.3E", Num, Num);//.3E=2.129E+001
}
编程练习4:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
char name[40];
printf("Please enter your name:\n");
scanf("%s", name);
float height;
printf("Please enter your height in inch:");
scanf("%f", &height);
printf("%s,you are %.3f feet tall", name,height);//.*f就精确到小数点后几位输出
return 0;
}
编程练习5:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
float MB;
float Content;
float Speed;
float Seconds;
printf("Please enter your downloads Speed(MB/s):");
scanf("%.2f", &Speed);//问什么使用%.f程序就可以正常运行 使用%.2f程序就会出错?答:输入数据时不能限制数据的精度!
printf("Please enter your file Content:");
scanf("%f", &MB);
Content = MB * 8;
Seconds = Content/ Speed;
printf("At %.2f megabits per second",Speed);
printf("a file of % .2f megabutes\n",Content);
printf("downloads in %.2f Seconds", Seconds);
return 0;
}
编程练习6:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
int A;
int B;
char surname[40];
char name[40];
printf("Please enter your surname and name:\n");
scanf("%s", surname);
scanf("%s", name);
A = strlen(surname);
B = strlen(name);
printf("%2d", A);
printf("%5d\n", B);
scanf("%s", surname);
scanf("%s", name);
printf("%-2d", A);
printf("%2d", B);
return 0;
}//需要确定输出字符或者输出数字的内容时,可以使用%*d %*s来确定空出的空格数,也可以使用%-*d来使输出左对齐。
编程练习7:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <float.h>
int main()
{
double a;
float b;
a= 1.0 / 3.0;
b= 1.0 / 3.0;
printf("%f\n", a);
printf("%f\n",b);
printf("%.12f\n", a);
printf("%.12f\n", b);
printf("%.12f\n",a);
printf("%.12f\n", b);
printf("FLT_DIG = %d, DBL_DIG = %d\n", FLT_DIG, DBL_DIG);//FLT_DIG和DBL_DIT分别是float有效十进制字位数和doule有效十进制字位数
}
编程练习8:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define gallon 3.785
#define mile 1.609
int main()
{
float mileage;
float fule;
float km;
float oil;
float emm;
float emm2;
printf("Please enter your mileage and fule:");
scanf("%f", &mileage);
printf("Please enter your fule:");
scanf("%f",&fule);
km = mileage * mile;
oil = fule * gallon;
emm = fule / mileage;
emm2 = oil / km;
printf("Your km is %.1f", km);
printf("Your oil is%.1f\n", oil);
printf("In US your emm is %.1f(gallon/mile)/n", emm);
printf("In Europe your emm is %.1f(liter/km)",emm2);
}