-5

我在 C++ 实验室介绍中遇到了一个问题。我已经在下面发布了这个实验室的说明和我的代码。我想指出,模式 B 必须显示在模式 A 旁边(如图所示)而不是在其下方。

C++ 实验室作业

当我尝试构建此代码时,我在 b = by 行上收到错误 no match for 'operator-'。有没有一种简单的方法来减少字符串中的符号?说明声明要为模式 B 使用第二个循环,但我不确定从哪里开始。我非常感谢任何能引导我朝着正确方向完成这项任务的建议。谢谢你。

#include <iostream>
#include <string>

using namespace std;

int main()
{

    string a = "*";
    string b = "**********";
    string y = "*";


    for (int i=0; i <= 9; i++) {
        cout << a << "       " << b << "\n";
        a = a + y;
        b = b - y;
    }


    return 0;
}
4

3 回答 3

0

我理解的方式:

#include <iostream>   
using namespace std;

int main()
{
    int aCount = 1;
    int bCount = 10;
    while(bCount > 0) 
    {
        // Loop over A
        for (int i = 0; i < aCount; i++)
        {
            cout << '+';
        }

        // Output a few tabs to separate the samples
        cout << "\t\t\t\t";

        // Loop over B
        for (int i = 0; i < bCount; i++)
        {
            cout << '+';
        }

        // Go to next line
        cout << endl;

        aCount++;
        bCount--;
    }

    return 0;
}
于 2017-03-01T11:46:08.237 回答
0

您不能在 C++ 中“减去”字符串。字符串没有“-”运算符。

一种方法是使用 string.substring() 来获取原始字符串的“缩短”版本:

b=b.substr(0, b.size()-1); // copy all but last character of b to b
于 2017-03-01T11:26:46.910 回答
0

operator -类中没有运算符std::string。您应该改用该方法erase

但是,如果您要为每一行输出字符串,则不会有两个循环一个接一个。

似乎分配的含义类似于以下内容

#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        const char c1 = '+';
        const char c2 = ' ';

        std::cout << "Enter a non-negative number (0 - exit): ";

        unsigned int n;

        if ( not ( std::cin >> n ) or ( n == 0 ) ) break;

        std::cout << '\n';

        for ( unsigned int i = 1; i <= n; i++ )
        {
            for ( unsigned int j = 1; j <= i; j++ )
            {
                std::cout << c1;
            }

            std::cout << std::setw( 2 * n - i ) << std::setfill( c2 ) 
                      << std::right << c2;

            for ( unsigned int j = n - i + 1; j != 0; j-- )
            {
                std::cout << c1;
            }

            std::cout << '\n';
        }

        std::cout << std::endl;
    }

    return 0;
}

程序输出可能看起来像

Enter a non-negative number (0 - exit): 10

+                   ++++++++++
++                  +++++++++
+++                 ++++++++
++++                +++++++
+++++               ++++++
++++++              +++++
+++++++             ++++
++++++++            +++
+++++++++           ++
++++++++++          +

Enter a non-negative number (0 - exit): 9

+                 +++++++++
++                ++++++++
+++               +++++++
++++              ++++++
+++++             +++++
++++++            ++++
+++++++           +++
++++++++          ++
+++++++++         +

Enter a non-negative number (0 - exit): 8

+               ++++++++
++              +++++++
+++             ++++++
++++            +++++
+++++           ++++
++++++          +++
+++++++         ++
++++++++        +

Enter a non-negative number (0 - exit): 7

+             +++++++
++            ++++++
+++           +++++
++++          ++++
+++++         +++
++++++        ++
+++++++       +

Enter a non-negative number (0 - exit): 6

+           ++++++
++          +++++
+++         ++++
++++        +++
+++++       ++
++++++      +

Enter a non-negative number (0 - exit): 5

+         +++++
++        ++++
+++       +++
++++      ++
+++++     +

Enter a non-negative number (0 - exit): 4

+       ++++
++      +++
+++     ++
++++    +

Enter a non-negative number (0 - exit): 3

+     +++
++    ++
+++   +

Enter a non-negative number (0 - exit): 2

+   ++
++  +

Enter a non-negative number (0 - exit): 1

+ +

Enter a non-negative number (0 - exit): 0
于 2017-03-01T12:04:19.510 回答