图书在版编目(CIP)数据
C++编程入门指南/明日科技编著.—北京:电子工业出版社,2020.5
ISBN 978-7-121-38368-7
出版发行:电子工业出版社
摘要:由第二章C++语言基础课后例题展开介绍
(1)试试看输出字符"A"和”a"的ASCII码值
#include<iostream>
void main()
{
printf("%d\n%d", 'A', 'a');
}
//输出 65 97,%d输出十进制ASCII码
(2)程序中不出现字符“B”,试着输出字符“B”
#include<iostream>
void main()
{
printf("%c", 66);
}
//输出B,%c代码输出ASCII码对应的字符
(3)分别以整形(%d)和字符型(%c)输出"'A'+10"的结果
#include<iostream>
void main()
{
char ch1;
ch1 = 'A';
printf("ch1+10=%d\n", ch1+10);
printf("ch1+10=%c\n", ch1+10);
}
//输出75 K
(4)以字符型(%c)输出"'A'+32"的结果
#include<iostream>
void main()
{
printf("%c", 'A' + 32);
}
//输出 a
(5)使用count向控制台输出汉字”你好,明天“
#include<iostream>
using namespace std;
void main()
{
cout<<"你好,明天"<<endl;
}
(6)使用cout 向控制台输出唐朝诗人王之涣的《登鹤雀楼》。
#include<iostream>
using namespace std;
void main()
{
cout << " 登鹳雀楼" << endl;
cout << " 王之涣" << endl;
cout << "白日依山尽,黄河入海流。" << endl;
cout << "欲穷千里目,更上一层楼。" << endl;
}
(7) 银行的年利率为2.95%,如果在银行中存入10000元,则1年后可以取出多少钱?(小数点后保留2位)
本金+利息 =0.0295x10000 +10000
#include<iostream>
void main()
{
float a = 2.95e-2;
int n = 10000;
printf("After a year, you'll get %.2f", n + a * n);
}
(8) 向控制台输出圆周率,保留4位小数并四舍五入 (3.1416)
#include<iostream>
void main()
{
float pi = 3.1415926;
printf("%.4f",pi);
}
(9) 光在真空中的速度为299 792 458米/秒,用科学计数法输出光的速度。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double speed_of_light = 299792458.0; // 光在真空中的速度,单位:米/秒
// 使用 std::scientific 格式输出科学计数法
cout << setiosflags(ios::scientific) << setprecision(3) << speed_of_light << endl;
printf("%5.3e", speed_of_light);
return 0;
} //output:2.998e+08 2.998e+08
总结:
介绍一个实例,一段代码看懂C++流输入输出格式
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
double adouble = 123.456789012345;
cout << adouble << endl;
//123.457 保留6到7位
cout << setprecision(9) << adouble << endl;
//123.456789 有效数字9
cout << setprecision(6);
//设定有效数字6
cout << setiosflags(ios::fixed);
//小数位数固定
cout << adouble << endl;
//小数位数固定为6,123.456789
cout << setiosflags(ios::fixed) << setprecision(8) << adouble << endl;
//固定小数位数为8,123.45678901
cout << setiosflags(ios::scientific) << adouble << endl;
//1.234568e+02
cout << setiosflags(ios::scientific) << setprecision(4) << adouble << endl;
//1.2346e+02
int aint = 123456;
cout << aint << endl;//123456
cout << hex << aint << endl;//1e240
cout << setiosflags(ios::uppercase) << aint << endl;//1E240
cout << setfill('*') << setw(10) << dec<<aint << endl;//****123456
cout << setiosflags(ios::showpos) <<dec<< aint << endl;//+123456
int aint_i = 0x2F, aint_j = 255;
cout << aint_i << endl;//+47
cout << hex << aint_i << endl;//2F
cout << hex << aint_j << endl;//FF
cout << hex << setiosflags(ios::uppercase) << aint_j << endl;//FF
int aint_x = 123;
double adouble_y = -3.1415;
cout << "aint_x=";
cout.width(10);
cout << aint_x;// 123
cout << "adouble_y=";
cout.width(10);
cout << adouble_y << endl; // -3.1415
cout.setf(ios::left);
cout << "aint_x=";
cout.width(10);
cout << aint_x;//123 '
cout << "adouble_y=";
cout << adouble_y << endl;//-3.1415
cout.fill('*');
cout.precision(4);
cout.setf(ios::showpos);
cout << "aint_x=";
cout.width(10);
cout << aint_x;//+123******
cout << "adouble_y=";
cout.width(10);
cout << adouble_y << endl;//-3.142**** float afloat_x = 20, afloat_y = -400.00;
cout << afloat_x << ' ' << afloat_y << endl;//20 -400
cout.setf(ios::showpoint);//强制显示小数点和无效0
cout << afloat_x << ' ' << afloat_y << endl;// 20.0000 - 400.000
cout.unsetf(ios::showpoint);
cout.setf(ios::scientific);
cout << afloat_x << ' ' << afloat_y << endl;//2.000000e+01 -4.000000e+02
cout.setf(ios::fixed);
cout<< afloat_x << ' ' << afloat_y << endl;//20.000000 -400.000000
}
printf()函数输出格式控制,同样用一个实例代码来介绍
#include<iostream>
void main()
{
printf("%4d\n", 1);//' 1'
printf("%04d\n", 1);//'0001'
int aint_a = 10, aint_b = 20;
printf("%d%d\n", aint_a, aint_b);//'1020'
const char* str = "helloworld";
printf("%s\n%10.5s\n%-10.2s\n%.3s", str, str, str, str);
//helloworld,' hello','he','hel'
float afloat = 2998.453257845;
double adouble = 2998.453257845;
printf("%f\n%15.2f\n%-10.3f\n%f", afloat, afloat, afloat, adouble);
//2998.453369,' 2998.45','2998.453 ',2998.453258
printf("%e\n%15.2e\n%-10.3e\n%e", afloat, afloat, afloat, adouble);
//2.998453e+03,' 3.00e+03','2.998e+03 ','2.998453e+03'
}