简体   繁体   中英

need help making a track of constant width in C++ using for loop

I need to make something like this using a for loop instead of just printing each line seperatly:

在此处输入图片说明

So far i have something like a right triangle which is a start.

void roadBound() {
const int ROW = 17;
const int GAP = 10;
const int NUM = -17;

for (int i=ROW, g=GAP, n = NUM ; i>=0; i--, g+=2)
{
    for (int j=n; j<i; j++) 
        cout << '*';
    for (int j=0; j<g; j++) 
        cout << ' ';
    for (int j=n; j<i; j++) 
        cout << '*';
    cout << endl;
}

}

The output of that looks like : 在此处输入图片说明

不要增加gap ,而要保持不变。

Hope this helps. You can simplify it a lot but since OP is a newbie he will maybe understand it better like this.

int iteration = 0;
const int rowMaxSize = 25;
const int gapSize = 10;
const int leftSizeMin = 3;
const int leftSizeMax = 10;
const int iterationMax = 40;

int currentLeftSize = leftSizeMin;
bool increasing = true;

do {
    for(int i = 0; i < currentLeftSize; i++) {
        cout << "*"; 
    }
    for(int i = 0; i < gapSize; i++) {
        cout << " "; 
    }
    for(int i = 0; i < rowMaxSize - gapSize - currentLeftSize; i++) {
        cout << "*"; 
    }
    if(currentLeftSize >= leftSizeMax) {
        increasing = false;
    } else if (currentLeftSize == leftSizeMin) {
        increasing = true;
    }
    if(increasing) {
        currentLeftSize++;
    } else {
        currentLeftSize--;
    }
    cout << endl;
} while( iteration++ < iterationMax );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM