簡體   English   中英

我的代碼看起來不錯,但某些輸入會導致不需要的輸出

[英]My code seems fine, but certain inputs result in undesired outputs

該程序的目標是接收用戶輸入的數字列表,以逗號分隔。 這些數字可以包括范圍。 在輸入數字之前,用戶必須輸入問題集的標題,但這部分代碼似乎完全可以正常工作。 例如,如果用戶輸入:

"Lesson 1"1-10,12,13,15,18,19-23,25

代碼將 output:

"For Lesson 1, you will have to complete the following problems: 1, 2, 
3, 4,..." 

直到列表完成。

如果用戶輸入的數字包含在一個范圍內、相互重疊的范圍或亂序輸入(不包括向后的范圍[您不能輸入 16-12 作為范圍]),代碼將自動排序(按升序)和 output 每個數字不重復任何元素。 除了逗號和連字符之外,您也不能輸入任何字符(盡管我相信我設法讓 cin 繼續閱讀,直到它找到逗號或連字符之后的第一個 int 值),並且必須按 Enter 提交輸入。

有關分配的完整信息,如果需要,請查看此處: http://craie-programming.org/122/labs/seqlist.html

這個問題很難精確定位,因為它只有在用戶輸入一個極其復雜但有效的輸入時才會發生。 包括重疊范圍、重復數字和亂序輸入似乎會導致列表在這里和那里重復一些數字,我根本找不到為什么會這樣!

任何關於可能導致不一致問題的輸入將不勝感激。 謝謝!

它在某些情況下有效,但它必須在所有情況下都有效。 如果用戶對代碼玩得很好,它就可以正常工作……但是無論如何,這段代碼必須能夠被濫用並通過它!

我曾嘗試擺弄 binarySearch function,在 if-else 語句中更改 ub(上限)和 lb(下限)的計算,但問題似乎仍然存在。 也許我在嘗試時錯過了一些東西,但是,我找不到它。

我還嘗試在 do-while 循環中擺弄 switch 語句,那里的一切似乎都很好,盡管默認情況可能會導致錯誤……如果是這樣,我當然無法找出原因。

我目前正在使用 C4Droid IDE 開發 Chromebook——盡管 Chromebook 很糟糕,但我認為這不是問題所在。

如果需要,這是迄今為止我所有代碼的鏈接: https://docs.google.com/document/d/1umbIlfxRniBb9ANcbwcsv2K4hjAHEjH3itGFVB6p83U/edit?usp=sharing

我相信以下函數可能是問題的原因(全部封裝到problemList類中):

bool binarySearch(int num, int lb, int ub){
        int mid = (lb + ub) / 2;
        if(ub - lb > 1){
            if(num == numbers[mid]){
                return true;
            }
            else if(num > numbers[mid]){ 
                lb = mid + 1;
                binarySearch(num, lb, ub);
            }
            else{
                ub = mid;
                binarySearch(num, lb, ub);
            }
    }
        else return false;
}

和/或

void addNumber(int n){ /* adds a number to the numbers vector while sorting it at the same time via a backwards bubble sort.*/  
    if(!binarySearch(n, 0, numbers.size() - 1)){
        vector<int>::size_type currentIndex = 
                numbers.size();
        while(currentIndex > 0 && numbers[currentIndex - 1] > n){
            currentIndex--;
        }
            numbers.insert(currentIndex + numbers.begin(), 
                n);
    }
}

和/或

void setVector(){
    bool going = true;
    unsigned int firstNum, secondNum;
    cin >> firstNum;
    numbers.push_back(firstNum);
    do{
        switch(cin.peek()){
            case ',':
                cin.ignore();
                cin >> firstNum;
                addNumber(firstNum);
                break;

            case '-':
                cin.ignore();
                cin >> secondNum;
                for(int x = firstNum + 1; x <= 
                                secondNum; x++)
                    addNumber(x);       
                         break;

            case '\n':
                going = false;
                break;

            default:
                cin.ignore();
                break;
        }
    }while(going);  
}   

如果我輸入一組隨機數字(如前所述),包括重疊范圍和預先存在和亂序的數字,我經常會收到無效的 output。 通常,output 會重復數字、跳過一些數字,甚至以錯誤的順序放置數字。

例如,如果用戶輸入:

"Lesson 1"1-6,3,6,2-7,4,5,1-7,9-12,10-13

程序應為 output:

For Lesson 1 you need to do the following problems:
1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13

請注意它是如何跳過 8 的,因為 8 不包括在任何數字或任何范圍內。 然而,我會得到的 output 將是(測試時):

For Lesson 1 you need to do the following problems:
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 9, 10, 11, 11, 12, 12, 13

您的問題可能在於 binarySearch 條件:

    if(ub - lb > 1)

我無權訪問您的完整代碼,因此目前無法對此進行測試,但我認為這會導致您的 binarySearch 無法檢查向量中的第一個和最后一個元素。 也許您可以嘗試將其切換為:

    if(ub - lb >= 0)

希望這可以幫助。

使用forward_list的替代實現:

forward_list<unsigned> numbers;

void addNumber(unsigned num)
{
  auto last_visited = numbers.end();
  for (auto iter = numbers.begin(); iter != numbers.end(); ++iter)
  {
    if (*iter == num)
      return;
    if (*iter > num)
      break;
    last_visited = iter;
  }
  if (last_visited == numbers.end())
    numbers.push_front(num);
  else
    numbers.insert_after(last_visited, num);
}

使用list實現更簡潔,但它使用每個元素的額外指針。

暫無
暫無

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

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