C++基础星形打印方法

来源:互联网 发布:java 编程题 编辑:程序博客网 时间:2024/06/10 14:58
  1. #include <iostream.h>   
  2.   
  3. void rectangle(int heigth, int width);   
  4.   
  5. void isosceles(int heigth);   
  6.   
  7. void diamond(int heigth);   
  8.   
  9. int main()    
  10. {   
  11.     int heigth;   
  12.     int width;   
  13.     char c;   
  14.     cout<<"please choose the shapes that you would like using,if you want to quit please enter Q(uit)"<<endl;   
  15.     cout<<"(R/I/D)"<<endl;   
  16.     cin>>c;   
  17.     if(c == 'R' || c == 'r')    
  18.     {   
  19.         cin>>heigth>>width;   
  20.         rectangle(heigth, width);   
  21.     }   
  22.   
  23.     if(c=='I' || c=='i')    
  24.     {   
  25.         cin>>heigth;   
  26.         isosceles(heigth);   
  27.     }   
  28.   
  29.     if(c=='D' || c=='d')    
  30.     {   
  31.         cout<<"Please enter an odd number!"<<endl;   
  32.         cin>>heigth;   
  33.         if((heigth%2) != 0)   
  34.         {   
  35.             diamond(heigth);   
  36.         }   
  37.         else    
  38.         {   
  39.             cout<<"This is an IllegalAgrument!"<<endl;   
  40.         }   
  41.     }   
  42.     return 0;   
  43. }   
  44.   
  45. void rectangle(int heigth, int width)    
  46. {   
  47.     int i = 0;   
  48.     int j = 0;   
  49.     char c = '*';   
  50.     for(i=0; i<heigth; i++)    
  51.     {   
  52.         for(j=0; j<width; j++)   
  53.         {   
  54.             cout<<c;   
  55.         }   
  56.         cout<<endl;   
  57.     }   
  58. }   
  59.   
  60. void isosceles(int heigth)   
  61. {   
  62.     int i;   
  63.     int j = 0;   
  64.     int k = 0;   
  65.     int row = heigth;   
  66.     int spacenum;   
  67.     int starnum;   
  68.     for(k=0; k<heigth; k++)   
  69.     {   
  70.         spacenum = row--;   
  71.         if(k==0)    
  72.         {   
  73.             starnum = 1;   
  74.         } else    
  75.         {   
  76.             starnum = k*2+1;   
  77.         }   
  78.         for(i=spacenum; i>0; i--)    
  79.         {   
  80.             cout<<" ";   
  81.         }   
  82.         for(j=1; j<=starnum; j++)    
  83.         {   
  84.             cout<<"*";   
  85.         }   
  86.         cout<<endl;   
  87.     }   
  88. }   
  89.   
  90. void diamond(int heigth)    
  91. {   
  92.     int rownum = heigth;   
  93.     int spacenum;   
  94.     int starnum;   
  95.     int i=0;   
  96.     int j=1;   
  97.     int k;   
  98.     int a = rownum/2;   
  99.     for(i=0; i<heigth; i++)    
  100.     {   
  101.         if(i<rownum/2)   
  102.         {   
  103.             spacenum = a--;   
  104.         }   
  105.         else  
  106.         {   
  107.             spacenum = a++;   
  108.         }   
  109.         //cout<<spacenum<<"sapcenum"<<endl;   
  110.         starnum = rownum -spacenum*2;   
  111.         //cout<<starnum<<"starnum"<<endl;   
  112.         for(j=0; j<spacenum; j++)    
  113.         {   
  114.             cout<<" ";   
  115.         }   
  116.         for(k=0; k<starnum; k++)    
  117.         {   
  118.             cout<<"*";   
  119.         }   
  120.         cout<<endl;   
  121.     }   
  122. }  
原创粉丝点击