《C Primer Plus》第4章 复习题与编程练习
复习题
1~12
编程练习
1. 打印姓名
/*4.8.1 -- 打印姓名*/
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main(void)
{
char name[40];//名
char surname[40];//姓
cout<<"Please enter your name: ";
cin>>name;
cout<<"and enter your surname: ";
cin>>surname;
cout<<name<<","<<surname<<endl;
system("pause");
return 0;
}
2. 多格式打印名字
/*4.8.2 -- 多格式打印名字*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
char name[40];
cout << "Please enter your name: ";
cin >> name;
printf("\"%s\"\n", name);
printf("\"%20s\"\n", name);
printf("\"%-20s\"\n", name);
printf("%*s\n", strlen(name) + 3, name);
system("pause");
return 0;
}
3. 多格式打印浮点数
/*4.8.3 -- 多格式打印浮点数*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
float number;
cout << "Please input a number: ";
cin >> number;
printf("The input is %.1f or %.1e.\n", number, number);
printf("The input is %+.3f or %.3E.\n", number, number);
system("pause");
return 0;
}
4. 打印身高和姓名
/*4.8.4 -- 打印身高和姓名*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
float height; //单位:英寸
char name[40]; //姓名
cout << "Please input your height: ";
cin >> height;
cout << "Please input your name: ";
cin >> name;
printf("%s, you are %.3f feet tall\n", name, height);
system("pause");
return 0;
}
5. 打印文件下载时间
/*4.8.5 -- 打印文件下载时间*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
float speed;
float file;
cout << "Please input the download speed and the file size: ";
cin >> speed >> file;
printf("At %.2f megabits per second, a file of %.2f megabytes\ndownloads in %.2f seconds.\n", speed, file, file * 8 / speed);
system("pause");
return 0;
}
6. 打印姓名和字符长度
/*4.8.6 -- 打印姓名和字符长度*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
char name[40]; //名
char surname[40]; //姓
cout << "Please enter your name: ";
cin >> name;
cout << "and enter your surname: ";
cin >> surname;
printf("%s %s\n", name, surname);
printf("%*d %*d\n", strlen(name), strlen(name), strlen(surname), strlen(surname));
printf("%s %s\n", name, surname);
printf("%-*d %-*d\n", strlen(name), strlen(name), strlen(surname), strlen(surname));
system("pause");
return 0;
}
7. 打印浮点数
/*4.8.7 -- 打印浮点数*/
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <iostream>
using namespace std;
int main(void)
{
float n1 = 1.0 / 3.0;
double n2 = 1.0 / 3.0;
printf("n1: %.6f %.12f %.16f\n", n1, n1, n1);
printf("n2: %.6f %.12f %.16f\n", n2, n2, n2);
printf("FLT_DIG: %f\n",FLT_DIG);
printf("DBL_DIG: %f\n",DBL_DIG);
system("pause");
return 0;
}
运行结果:
8. 打印里程
/*4.8.8 -- 打印里程*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define gallonToLitre 3.785
#define mileToKm 1.609
int main(void)
{
float mile; //英里
float gallon; //加仑
cout << "Please input the miles and gallons: ";
cin >> mile >> gallon;
printf("%f gallon/mile = %.1f L/100km.\n", gallon / mile, gallon * gallonToLitre / (mile * mileToKm * 100));
system("pause");
return 0;
}