基于其他变量格式化字符串

Formatting strings based on other variables

本文关键字:变量 格式化 字符串 其他 于其他      更新时间:2023-10-16

正在做一个类的作业,它要求我们输入,这样我们就可以根据输入的数字创建一个"畜栏"。我只是很难根据另一个变量得到正确的间距。栏应该看起来像这样:

|==|==|==|
:        :
-        -
:        :
|==|==|==|

我的问题是在中心的空白空间(这应该基于顶部/底部的长度)。Setw()在某种程度上是有效的,但同样,只是一个令人恼火的时间。这是我目前为止剩下的内容:

#include <iostream>
#include <iomanip>
using namespace std;
main () {
    int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
    char choice;
    theloop = 0;
    while (theloop < 1) {
        do { 
            cout << "How many North/South Fence posts? ";
            cin >> nsfp;
            if (nsfp < 2)
            cout << "Value must be at least 2, please try againn";
            else if (nsfp > 10)
                nsfp = 10;
        } while (nsfp < 2);
        do { 
            cout << "How many East/West Fence posts? ";
            cin >> ewfp;
            if (ewfp < 2)
                cout << "Value must be at least 2, please try againn";
            else if (ewfp > 10)
                nsfp = 10;
        } while (ewfp < 2);
        for (count = 1; count <= nsfp; count++) {
            cout << "|";
            nsfp2 = nsfp - 1;
            for (count = 1; count <= nsfp2; count++) {
                cout << "==|";
                nsfpw = count;
            }
        }
        for (count = 1; count < ewfp; count++) {
            cout << "n:" << " " << ":" << endl;
            ewfp2 = ewfp;
            for (count = 1; count < ewfp2; count++){
                cout << "-" << setw(nsfpw) << "-" << "n:" << ":" << endl;
            }   
        }
        cout << "n" << nsfp << endl;
        cout << ewfp;
        theloop = 1;
    }
}

也许你正在寻找的是这个。空格数为

3 * NSFP - 4;

#include <iostream>
#include <iomanip>
using namespace std;
main () {
    int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
    char choice;
    theloop = 0;
    while (theloop < 1) {
    do { 
    cout << "How many North/South Fence posts? ";
    cin >> nsfp;
    if (nsfp < 2)
        cout << "Value must be at least 2, please try againn";
    else if (nsfp > 10)
        nsfp = 10;
    } while (nsfp < 2);
    do { 
    cout << "How many East/West Fence posts? ";
    cin >> ewfp;
    if (ewfp < 2)
        cout << "Value must be at least 2, please try againn";
    else if (ewfp > 10)
        nsfp = 10;
    } while (ewfp < 2);
    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }
    cout << "n";
    int spaces = 3 * nsfp - 4;
    for (count = 1; count < ewfp; count++) {
        cout << ":";
        for (int i = 0; i < spaces; i++) {
            cout << " ";
        }
        cout << ":n-";
        ewfp2 = ewfp;
        for (int i = 0; i < spaces; i++){
            cout << " ";
        }
        cout << "-n";   
    }
    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }
    cout << "n" << nsfp << endl;
    cout << ewfp;
    theloop = 1;
    }
}

更简洁的方法:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
main () {
    int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
    char choice;
    theloop = 0;
    while (theloop < 1) {
    do { 
    cout << "How many North/South Fence posts? ";
    cin >> nsfp;
    if (nsfp < 2)
        cout << "Value must be at least 2, please try againn";
    else if (nsfp > 10)
        nsfp = 10;
    } while (nsfp < 2);
    do { 
    cout << "How many East/West Fence posts? ";
    cin >> ewfp;
    if (ewfp < 2)
        cout << "Value must be at least 2, please try againn";
    else if (ewfp > 10)
        nsfp = 10;
    } while (ewfp < 2);
    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }
    cout << "n";
    string spaces(3 * nsfp - 4, ' ');
    for (count = 1; count < ewfp; count++) {
        cout << ":" << spaces << ":n";
        cout << "-" << spaces << "-n";
    }
    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }
    cout << "n" << nsfp << endl;
    cout << ewfp;
    theloop = 1;
    }
}