利用格式操纵符输出九九乘法表,包括多种形式,比如左上三角,右上三角,左下三角,右下三角。
代码如下
编译环境,DevC++ 5.11 TDM-GCC 4.9.2 64-Bit Release
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout<<"格式操作符输出九九乘法表"<<endl;
int i,j;
cout<<setiosflags(ios::left);
//完整输出
for(i=1;i<=9;i++) {
for(j=1;j<=9;j++){
cout<<i<<"*"<<j<<"="<<setw(5)<<i*j;
}
cout<<endl;
}
//左下三角
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++){
if(j>i)
cout<<" ";
else
cout<<i<<"*"<<j<<"="<<setw(5)<<i*j;
}
cout<<endl;
}
//右上三角
for(i=1;i<=9;i++) {
for(j=1;j<=9;j++){
if(j<i)
cout<<" ";
else
cout<<i<<"*"<<j<<"="<<setw(5)<<i*j;
}
cout<<endl;
}
//左下三角
int n;
for(i=1;i<=9;i++){
for(n=1; n<=9-i; n++)
cout<<" ";
for(j=1;j<=i;j++)
cout<<i<<"*"<<j<<"="<<setw(5)<<i*j;
cout<<endl;
}
//左上三角
for(i=1;i<=9;i++) {
for(j=1;j<=9;j++){
if(j<i);
else
cout<<i<<"*"<<j<<"="<<setw(5)<<i*j;
}
cout<<endl;
}
return 0;
}