输出复杂的菱形(续)

根据上次的帖子改了下,让其更智能

如能更改边框、内填充、外填充、高度、个数

效果如下图:

代码如下所示

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 void DrawRhombus(int totalRhombusHeight, int totalRhombusNum, int maxSpaceNum, int rhombusMiddleLine,char rhombusBorder, char rhombusFill, char rhombusOutFill);
  6 
  7 int main(void){
  8     int totalRhombusHeight;
  9     int totalRhombusNum;
 10     int maxSpaceNum;
 11     int rhombusMiddleLine;
 12     char rhombusBorder;
 13     char rhombusFill;
 14     char rhombusOutFill;
 15     
 16     cout << "Please type the height of rhombus:";
 17     cin >> totalRhombusHeight;
 18     cout << "Please type the numbers of rhombus:";
 19     cin >> totalRhombusNum;
 20     cout << "Please type the border of rhombus:";
 21     cin >> rhombusBorder;
 22     cout << "Please type the fill of rhombus:";
 23     cin >> rhombusFill;
 24     cout << "Please type the out fill of rhombus:";
 25     cin >> rhombusOutFill;
 26     
 27     if(0 == totalRhombusHeight % 2){
 28         cout << "The height of rhombus is wrong, the program will exit.";
 29         return -1;
 30     }
 31     
 32     rhombusMiddleLine = (totalRhombusHeight + 1) / 2;
 33     maxSpaceNum = (rhombusMiddleLine - 1) * 2 - 1;
 34     
 35     if(79 < (maxSpaceNum + 2) * totalRhombusNum){
 36         cout << "Not enough width of line. the program will exit.";
 37         return -1;
 38     }
 39     
 40     
 41     DrawRhombus(totalRhombusHeight, totalRhombusNum, maxSpaceNum, rhombusMiddleLine, rhombusBorder, rhombusFill, rhombusOutFill);
 42     
 43     return 0;
 44 }
 45 
 46 void DrawRhombus(int totalRhombusHeight, int totalRhombusNum, int maxSpaceNum, int rhombusMiddleLine,char rhombusBorder, char rhombusFill, char rhombusOutFill){
 47     int lineNum;
 48     int firstSpace;
 49     int secondSpace;
 50     int maxFirstSpace = rhombusMiddleLine - 1;
 51     int spaceNum = 0;
 52     
 53     for(lineNum = 1; lineNum <= totalRhombusHeight; ++lineNum){
 54         if(lineNum <= rhombusMiddleLine){
 55             //first step of rhombus
 56             for(int nowRhombusNum = 1; nowRhombusNum <= totalRhombusNum; ++nowRhombusNum){
 57                 if(1 == nowRhombusNum){
 58                     for(firstSpace = 1; firstSpace <= rhombusMiddleLine - lineNum; ++firstSpace){
 59                         cout << rhombusOutFill;
 60                         spaceNum++;
 61                     }
 62                     cout << rhombusBorder;
 63                     spaceNum++;
 64                 } else {
 65                     for(firstSpace = 1; firstSpace <= (rhombusMiddleLine - lineNum) * 2; ++firstSpace){
 66                         cout << rhombusOutFill;
 67                         spaceNum++;
 68                     }
 69                     cout << rhombusBorder;
 70                     spaceNum++;
 71                 }
 72                 
 73                 if(lineNum > 1){
 74                     for(secondSpace = 1; secondSpace <= (lineNum - 1) * 2 - 1; ++secondSpace){
 75                         cout << rhombusFill;
 76                         spaceNum++;
 77                     }
 78                     cout << rhombusBorder;
 79                     spaceNum++;
 80                 }
 81             }
 82         } else {
 83             //second step of rhombus
 84             for(int nowRhombusNum = 1; nowRhombusNum <= totalRhombusNum; ++nowRhombusNum){
 85                 if(1 == nowRhombusNum){
 86                     for(firstSpace = 1; firstSpace <= lineNum - rhombusMiddleLine; ++firstSpace){
 87                         cout << rhombusOutFill;
 88                         spaceNum++;
 89                     }
 90                     cout << rhombusBorder;
 91                     spaceNum++;
 92                 } else {
 93                     for(firstSpace = 1; firstSpace <= (lineNum - rhombusMiddleLine) * 2; ++firstSpace){
 94                         cout << rhombusOutFill;
 95                         spaceNum++;
 96                     }
 97                     cout << rhombusBorder;
 98                     spaceNum++;
 99                 }
100                 
101                 if(lineNum != totalRhombusHeight){
102                     for(secondSpace = 1; secondSpace <= maxSpaceNum - (lineNum - rhombusMiddleLine) * 2; ++secondSpace){
103                         cout << rhombusFill;
104                         spaceNum++;
105                     }
106                     cout << rhombusBorder;
107                     spaceNum++;
108                 }
109             }
110         }
111         
112         for(int nowSpaceNum = spaceNum; nowSpaceNum < 79; ++nowSpaceNum){
113             cout << rhombusOutFill;
114         }
115         spaceNum = 0;
116         
117         cout << endl;
118     }
119 }

 

posted @ 2012-05-08 22:50  xiangsoft  阅读(140)  评论(0编辑  收藏  举报