09_竖式计算设置域宽setw
数学老师请你帮忙,在屏幕上输出18+870的竖式,试编一程序,实现这个功能。
#include<iostream>
#include<iomanip> //为了使用setw()来设置域宽
using namespace std;
int main()
{
int a,b,s;
a=18;
b=870;
s=a+b;
cout<<setw(10)<<a<<endl;
cout<<setw(4)<<'+'<<setw(6)<<b<<endl;
cout<<" -----------"<<endl;
cout<<setw(10)<<s<<endl;
return 0;
}
输出结果:
setw()的使用
默认右对齐;
setw只对直接跟在后面的输出数据起作用;
如果输出数据较小,则按设置域宽输出,前面补空格;
如果输出数据较大,则按实际位数输出。
iomanip io是输入输出的缩写,
manip是manipulator(操纵器)的缩写
setw set width的缩写,设置域宽
选择题
为了让计算机完成一个完整的任务而编写的一串指令序列称为( )。
命令
口令
程序
软件
阅读程序写结果。
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
int a,b,c;
a=3;
b=4;
c=a*a+b*b;
cout<<a<<"*"<<a<<"+"<<b<<"*"<<b<<"="<<setw(2)<<c<<endl;
return 0;
}
完善程序。
已知a为15,b为3,输出a-b的竖式计算。
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
int a,b,c;
a=15; b=3;
c=a-b;
cout<<setw(5)<<a<<endl;
cout<<setw(2)<<'-'<<setw(3)<<____<<endl;
cout<<"----------"<<endl;
cout<<________<<c<< endl; //占5个字符宽度
return 0;
}
所有程序作业,必须上机完成!
- 上一篇:08_交换两个变量的值
- 下一篇:10_cin语句_植树造林

评论已关闭