0
#include<iostream>
#include<string>
using namespace std;

main()
{
    int i, j=0, perlen, countcp=0, countsp=0, countrp=0, countcl=0, countsl=0, countrl=0;
    string str, str1;
    cout<<"Please enter string"<<endl;
    getline(cin, str);
    perlen=(str.length())/2;
    for(i=0; i<str.length(); i++)
    {
        if(str[i]=='{')
            countcp++;  
        if(str[i]=='[')
            countsp++;
        if(str[i]=='(')
            countrp++;
        if(str[i]=='}')
            countcl++;
        if(str[i]==']')
            countsl++;
        if(str[i]==')')
            countrl++;
    }
    str1=str;

    if(countcp==countcl and countsp==countsl and countrp==countrl)
    {
        cout<<"equal"<<endl;
        int countwhile=0, j=0;
        while(!str.length()==0)
        {
            if(str[j]=='{' and str[j+1]=='}')
            {
                str.erase(i, 2);
                countwhile++;
            }
            else if(str[j]=='(' and str[j+1]==')')
            {
                str.erase(i, 2);
                countwhile++;
            }
            else if(str[j]=='[' and str[j+1]==']')
            {
                str.erase(i, 2);
                countwhile++;
            }
            if(countwhile>perlen)
            {
                countwhile=1;
                cout<<"reached break"<<endl;
                break;
            }
            j++;
        }
        if(countwhile==1)
        {
            cout<<"Balanced string "<<str1<<endl;
        }
    }
}

ブラケットのバランスをとろうとしています。入力には、中括弧、丸括弧、および角括弧が含まれます。このコードで何が間違っていたのかを見つけようとしています。私はC ++が初めてで、学ぼうとしています。

説明

countcp中開き括弧用countcp四角開き括弧用countsp 丸開き括弧用countrp中括弧 または最後の開き括弧用 countcl countsl四角 閉じ括弧用countrl 丸い閉じ括弧 用 入力 {()} 出力のバランス 入力 {(}{)} 出力のバランスが取れていない 30 行目までは機能し、その後は等しいと出力され、エラーが発生します セグメンテーション違反 (コア ダンプ)








4

3 に答える 3

0

私が見る問題:

  1. に戻り型がありませんmain。使用する:

    int main() ...
    
  2. 範囲外の配列にアクセスしています。あなたが持っている:

    while ( !str.length() == 0 ) 
    

    それは十分ではありません。また、範囲外にアクセスしないようにする必要がありstrます。したがって、次のように変更します。

    while ( !str.length() == 0 && j+1 < str.length() ) 
    

    または、より良い、

    while ( !str.empty() && j+1 < str.length() ) 
    

    ループでアクセスしているため、 の使用j+1が必要です。str[j+1]

  3. 文字列から間違った要素を消去します。それ以外の

    str.erase(i, 2);
    //        ^^
    

    使用する

    str.erase(j, 2);
    //        ^^
    
  4. jの値を適切に更新していません。j 1 () str "{}"strと等しいとします。それを処理できるようにするには、 の値を に設定する必要があります。ロジックは次のようにする必要があります。"{()}". Whenis equal to, you are trying to removefrom the string. After that,is equal toj0

    j一致しない場合はインクリメントします。一致があり、一致する文字が削除されると
    減少します。j

改善提案

iおよびにunsigned 型を使用しjて、コンパイラの警告を回避します。

以下を使用できます。

std::string::size_type i = 0;
std::string::size_type j = 0;

while上記の修正を含むループの更新バージョン

coutまた、論理エラーの診断に役立つ行を追加しました。

  int countwhile=0;
  std::string::size_type j=0;
  while(!str.length()==0 && j+1 < str.length())
  {
     if(str[j]=='{' and str[j+1]=='}')
     {
        cout<<"erasing {}"<<endl;
        str.erase(j, 2);
        cout << str << endl;
        countwhile++;
        --j;
     }
     else if(str[j]=='(' and str[j+1]==')')
     {
        cout<<"erasing ()"<<endl;
        str.erase(j, 2);
        cout << str << endl;
        countwhile++;
        --j;
     }
     else if(str[j]=='[' and str[j+1]==']')
     {
        cout<<"erasing []"<<endl;
        str.erase(j, 2);
        cout << str << endl;
        countwhile++;
        --j;
     }
     else
     {
        j++;
     }
     if(countwhile>perlen)
     {
        countwhile=1;
        cout<<"reached break"<<endl;
        break;
     }
  }

入力を正しく処理できるよう"{[}{]}"にするには、コードを少し再構築する必要があります。これは、そのような入力を処理できるようにコードを精巧にリファクタリングすることを提案する適切な場所ではないようです。

于 2016-01-11T07:43:38.907 に答える