C语言的格式输出用scanf可以直接控制,C++中是通过一些库函数来实现。
setw()设定输出字段的最小宽度,setfill()设定默认填充的字符,默认是空格。
DEMO
#include <iomanip>
#include <iostream>
#include <ostream>
using namespace std;
int main()
{
int const low(1);
int const high(15);
int const colwidth(8);
cout<<right;
cout<<setw(colwidth)<<"*"<<"|";
for(int i(low);i <= high; i++)
cout<<setw(colwidth)<<i;
cout<<endl;
cout<<setfill('-')<<setw(colwidth)<<""<<'+'<<setw((high-low+1)*colwidth)<<""<<"\n";
cout<<setfill(' ');
for(int row(low); row <= high; row++)
{
cout<<setw(colwidth)<<row<<"|";
for(int col(low); col <= high; col++)
cout<<setw(colwidth)<<row*col;
cout<<endl;
}
return 0;
}
OUPT