几种图形打印基于C++IO流操作

来源:互联网 发布:为了抵御网络黑客攻击 编辑:程序博客网 时间:2024/05/18 02:42
1.
*    *
** **
*****




2.
*
**
***
****
***
**
*

3.金字塔



/*
打印
*   *
** **
*****
上面图形
*/

#include<iostream>
using namespace std;
void print(int high)
{
int i=1,j;
while(i<=high)
{  
 cout.setf(ios::left);
 cout.width(i);
 cout.fill('*');
 cout<<"";
 cout.unsetf(ios::left);
 
 cout.width(2*high-1-2*i);
 cout.fill(' ');
 cout<<"";

 cout.setf(ios::right);
 if(i!=high) j=i;
 else  j=i-1;
 cout.width(j);
 cout.fill('*');
 cout<<"";
 cout.unsetf(ios::right);
 i++;
 cout<<endl;
}

}


int main()
{
cout<<"请输入一个整形值:";
int high;
cin>>high;
print(high);
return 0;
}




=================================================

/*
*
**
***
****
***
**
*
*/


#include<iostream>
using namespace std;


void print(int width)
{
int i=1;
while(i<=width)
{

  cout.setf(ios::left);
  cout.width(i);
  cout.fill('*');
  cout<<"";
  cout<<endl;
  i++;
}
i-=2;
while(i>=1)
{

  cout.setf(ios::left);
  cout.width(i);
  cout.fill('*');
  cout<<"";
  cout<<endl;
  i--;
}
}
int main()
{
  cout<<"请输入一个整形值:";
int width;
cin>>width;
print(width);
return 0;


}

==================================================

3

#include<iostream>
using namespace std;
/*width,fill*/
void print(int high)
{
int i;
for(i=1;i<=high;i++)
{
cout.width(high-i);
cout.fill(' ');
cout<<"";//当为空时,填充字符
cout.width(2*i-1);
cout.fill('*');
cout<<"";
cout<<endl;
}

}


int main()
{
int iHeight;
cout<<"请输入高度:";
cin>>iHeight;
print(iHeight);
return 0;
}

0 0
原创粉丝点击