2011-10-29 6 views
3

文字列1の文字列2の出現を排除し、2つの文字列の共通部分も見つけなければなりません。ここで2つの文字列の交差と結合

は、私が試したものです:私は、コンパイラのエラーを取得していますし、また、私は私がしようとしていますかわからないです

#include "stdafx.h" 
#include "stdio.h" 
#include "conio.h" 
#include "string.h" 

class operation 
{ 
public: 
    char string1[100]; 
    char string2[50]; 


    operation(){}; 
    operation(char a[100], char b[50]); 
    operation operator+(operation); 
    operation operator-(operation); 
    operation operator*(operation); 
}; 

operation::operation(char a[100], char b[50]) 
{ 
    strcpy(string1, a); 
    strcpy(string2, b); 
} 

operation operation::operator +(operation param) 
{ 
    operation temp; 
    strcpy(param.string1, temp.string1); 
    strcpy(param.string2, temp.string2); 
    strcat(temp.string1, temp.string2); 
    return (temp); 
} 

operation operation::operator -(operation param) 
{ 
    operation temp; 
    strcpy(param.string1, temp.string1); 
    strcpy(param.string2, temp.string2) ; 
    for (int i = 0; i<strlen(temp.string2); i++) 
    { 
     temp.string1.erase(i, 1); 
    } 
    return (temp); 
} 

operation operation::operator *(operation param) 
{ 
    operation temp; 
    strcpy(param.string1, temp.string1); 
    strcpy(param.string2, temp.string2); 
    char result[50]; 
    for(int i = 0; i<strlen(temp.string2); i++) 
    { 
     if(temp.string1.find(temp.string2[i]) != string::npos) 
      result = result + temp.string2[i]; 
    } 

    return (temp); 

} 

が正しいかではありません。

次のようにエラーは以下のとおりです。

C2228: left of .erase must have class/struct/union 
C2228: left of .find must have class/struct/union 
+3

実際には** C++ **のように見えますが、それは奇妙なものです。 –

+0

Cに演算子のオーバーロードがありません。あなたはC++を意味しましたか? – dan04

+0

この宿題はありますか?もしそうなら、そのようにタグ付けすることはできますか? – Suroot

答えて

0
  1. これは、C++(ないC:Cは、演算子のオーバーロードを持っていない)であるあなたは、私たちの前に私達にあなたのクラス定義を表示する必要が

  2. コンパイルエラーの特定に役立ちます。

  3. あなただけではエラーを説明すること:)

  4. あなたがC++を使用している場合は、「オペレータ」のためのクラス定義を持っていない場合、あなたはおそらく代わりに、(標準C++「string」を使用する必要がありますC "char []"配列の)。 "string"を使用すると、実装コードにも影響します。

  5. Q:これは宿題ではありませんか? もしそうなら、タグに「宿題」を追加してください。

事前にありがとう... PSM

1

strcpy(string1...がコンパイルされた場合、string1char*ないstd::stringです。文字列のCとC++の機能を混在させているようです。 (あなたがC++をやっているので、私はstd::stringを言うと思います)のいずれかを選択し、それに固執

7

は幸いなことに、C++でdifferenceintersectionを設定し、unionアルゴリズムは、すでに標準ライブラリで実装されています。これらは、任意のコンテナクラスのような文字列に適用できます。ここで

は(あなたが簡単なchar配列でこれを行うことができますが、私は明確にするためstd::stringを使用しています)のデモです:

あなたは operationクラスでこれを実装する方法
#include <string> 
#include <algorithm> 
#include <iostream> 

int main() 
{ 
    std::string string1 = "kanu"; 
    std::string string2 = "charu"; 
    std::string string_difference, string_intersection, string_union; 

    std::sort(string1.begin(), string1.end()); 
    std::sort(string2.begin(), string2.end()); 

    std::set_difference(string1.begin(), string1.end(), string2.begin(), string2.end(), std::back_inserter(string_difference)); 
    std::cout << "In string1 but not string2: " << string_difference << std::endl; 

    std::set_intersection(string1.begin(), string1.end(), string2.begin(), string2.end(), std::back_inserter(string_intersection)); 
    std::cout << "string1 intersect string2: " << string_intersection << std::endl; 

    std::set_union(string1.begin(), string1.end(), string2.begin(), string2.end(), std::back_inserter(string_union)); 
    std::cout << "string1 union string2: " << string_union << std::endl; 
} 

Run it!

が残っています運動として。

+0

なぜ文字列を交差点や共用体にする前にソートしたのですか? – Humoyun

+0

@Humoyun、リンクされた資料によると、「***両方ソート範囲*** [ 'first1'、' last1')および[ 'first2に見出される元素からなるd_first''で始まるソート範囲を構築しているため'、' last2')。 " – Johnsyweb

関連する問題