第8课 竖式计算--设置域宽setw()


第8课 竖式计算--设置域宽setw() 头文件iomanip,用来声明一些“流操作符”,需要一定格式输入输出时,就需要用到它, 比较常用的有设置域宽、设置左右对齐、设置实数的精确度等。   例程:数学老师请你帮忙、在屏幕上输出18+870的竖式计算,试编一程序,实现这个功能。
 1 #include 
 2 /*头文件iomanip,用来声明一些“流操作符”,
 3 需要一定格式输入输出时,就需要用到它,
 4 比较常用的有设置域宽、设置左右对齐、
 5 设置实数的精确度等。*/ 
 6 #include  
 7 using namespace std;
 8 int main()
 9 {
10     int a,b,s;
11     a=18;
12     b=870;
13     s=a+b;
14     cout<10)<endl;
15     cout<4)<<'+'<6)<endl;
16     cout<<"-------------"<<endl;
17     cout<10)<endl;
18     return 0;
19 }
  英汉小词典: iomanip:io是输入输出的缩写; manip:是manipulator(操纵器)的缩写; setw:set width的缩写,设置域宽;   动动脑: 2.阅读程序写结果。
 1 #include 
 2 /*头文件iomanip,用来声明一些“流操作符”,
 3 需要一定格式输入输出时,就需要用到它,
 4 比较常用的有设置域宽、设置左右对齐、
 5 设置实数的精确度等。*/ 
 6 #include  
 7 using namespace std;
 8 int main()
 9 {
10     int a,b,c;
11     a=3;
12     b=4;
13     c=a*a+b*b;
14     cout<'*'<'+'<'*'<'='<2)<endl;
15     return 0;
16 }
  3.完善程序。 已知a为15,b为3,输出a-b的竖式计算。
 1 #include 
 2 /*头文件iomanip,用来声明一些“流操作符”,
 3 需要一定格式输入输出时,就需要用到它,
 4 比较常用的有设置域宽、设置左右对齐、
 5 设置实数的精确度等。*/ 
 6 #include  
 7 using namespace std;
 8 int main()
 9 {
10     int a,b,c;
11     a=15;
12     b=3;
13     c=a-b;
14     cout<5)<endl;
15     cout<2)<<'-'<3)<endl;
16     cout<<"------"<<endl;
17     cout<5)<endl;
18     return 0;
19 }
 
C++