簡體   English   中英

(C++) 我似乎在多次運行 for 循環時遇到問題。 難道我做錯了什么?

[英](C++) I seem to be having trouble running my for loop more than once. Am I doing something wrong?

我在下面發布了問題和我的代碼。 我基本上是試圖運行這個 for 循環 3 次來打印單詞的前 2 個字符。 我已經在這個問題上花了一個小時。 我需要知道如何修復它。

問題:編寫一個 C++ 程序,使用給定字符串前 2 個字符的 3 個副本創建一個新字符串。 如果給定字符串的長度小於 2,則使用整個字符串。

示例輸入:“abc”“Python”“J”

示例輸出:ababab PyPyPy JJJ

代碼:

            #include <iostream>
            #include <string>
            #include <vector>
            #include <cmath>
            #include <ctype.h>
            #include <algorithm>

            using namespace std;

            string test(string s1)
            {
                int x = s1.length();
                string y;

                if (x >= 2)
                {
                    for (int i = 0; i < 3; ++i)
                    {
                        y = s1.substr(0, 2);
                        return y;
                    }
    
    
                }
                else
                {
                    for (int j = 0; j < 3; ++i)
                    {
                        y = s1;
                        return y;
                    }
    
                }

            }

            int main()
            {
                cout << test("abc") << endl;
                cout << test("Python") << endl;
                cout << test("J") << endl;

                return 0;
            }

這是一個可能的解決方案:

// Example program
#include <iostream>
#include <string>

std::string test(const std::string& input) {
    if (input.length() <= 2) {
        return input + input + input;
    } else {
        std::string tmp(6, '0');
        for (int i = 0; i < 3; i++) {
            tmp[i*2 + 0] = input[0];
            tmp[i*2 + 1] = input[1];
        }
        return tmp;
    }
}

int main()
{
  std::string name;
  std::cout << test("abc") << "!\n";
  std::cout << test("Python") << "!\n";
  std::cout << test("J") << "\n";
}

您可以在此鏈接中看到它正在運行。

這是修復第一個循環的方法

  for (int i = 0; i < 3; ++i)
  {
       y += s1.substr(0, 2); // append two more characters to what we've got so far
  }
  return y;

還有其他方法可以做到這一點,但這是對您所擁有代碼的最小更改,並希望能說明您所犯的錯誤。

首先,你貼在這里的程序由於一些小錯誤而無法編譯:

 for (int j = 0; j < 3; ++i) { y=s1; return y; }

顯然,“++i”應該替換為“++j”:

 for (int j = 0; j < 3; ++j) { y=s1; return y; }

另外,你的程序有一些邏輯錯誤。 請記住,一旦函數返回,就無法再次恢復。 所以在這里你不能使用循環來“返回 3 次”,這絕對是錯誤的。

您可以在函數中完成一個字符串,然后將其打印到屏幕上,這將輕松解決您的問題。 這是我對您的程序的更正:

 #include <iostream> #include <string> using namespace std; string test(string s1) { int x = s1.length(); string y; if (x >= 2) { string res=s1.substr(0,2); y=res+res+res; //Given that the number 3 is very small, repeat it manually may bring a higher efficiency(though it cannot be seen exactly) return y; } else { return s1+s1+s1; } } int main() { //I do not think this program need so many "#include", just include what we need, that is convenient //for readers to understand your program quickly :) cout << test("abc") << endl; cout << test("Python") << endl; cout << test("J") << endl; /*Also, I recommend that you can always test some programs with a dynamic input, so that you can find some small mistakes more easily. Below is a simple example: cout<<"Please input a string:"; string str; cin>>str; cout<<test(str)<<endl;*/ return 0; }

希望我的回答可以幫助您解決問題:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM