2012-08-19 37 views
-7
C++ Template: 
class MyClass 
{ 
public: 
getNiCount(...) 
    { 
    } 
replaceNiWithNI(...) 
    { 
    } 
}; 
int main() 
{ 
const char *szTestString1 = "Ni nI NI nI Ni"; 
const wchar_t *szTestString2 = L"Ni nI NI nI Ni"; 
// Invoke getNiCount(...) of class MyClass 
// Invoke replaceNiWithNI(...) of class MyClass 
// Display on screen: "Found X occurrences of Ni. New string: Y" 
} 

任务描述:编程任务要求

  1. 实现这两种功能getNiCount和类MyClassreplaceNiWithNI
    • getNiCount应在szTestString1/2返回“倪”的出现次数(区分大小写)
    • replaceNiWithNI应将szTestString1/2中所有出现的“Ni”替换为“NI”(区分大小写)
  2. 调用getNiCountreplaceNiWithNI这两个函数。
  3. 显示屏幕上最后评论中给出的字符串。应该用实际值替换XY
  4. MyClass应该能够处理szTestString1(ASCII)和szTestString2(Unicode)。

一般要求:

的代码应该是

  • 易于理解和维护(优先级1)
  • 技术上优雅(优先级2)
  • 为(CPU)高效为可能(优先级3)

你是人类低于使用基于C++语言的所有技术,工具包和框架。

我的解决方案(不完全)

的逻辑是以下... 然而,在我的系统函数2“取代”崩溃。无法修复它。

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

class MyClass 
{ 
public: 
void getNiCount(const char*,const wchar_t*); 
    //cout<<"\nCount is :"<<count; 

void replaceNiWithNI(const char*,const wchar_t*); 

}; 

void MyClass::getNiCount(const char* x,const wchar_t* y) 
{ 
    int count=0; 
    int ycount=0; 
    for(int i=0; x[i]!='\0';i++) 
    { 
     if(x[i]=='N') 
     { if(x[i+1]=='i') 
          count++; 
     } 
     } 
    for(int i=0; y[i]!='\0';i++) 
    { 
     if(y[i]=='N') 
     { if(y[i+1]=='i') 
          ycount++; 
     } 
     } 
     cout<<"\nFound "<<count<<" occurences of Ni in String 1"; 
     cout<<"\nFound "<<ycount<<" occurences of Ni in String 2"; 
} 

void MyClass:: replaceNiWithNI(const char* x,const wchar_t* y) 
{ char* a; 
    wchar_t* b; 
    strcpy(a,x); 


    for (int i=0;a[i]!='\0';i++) 
    { 
     if (a[i]=='N') 
     { if(a[i+1]=='i') 
      { 
          a[i+1]='I'; 
          } 
     } 
     } 
    for (int i=0;y[i]!='\0';i++) 
    { 
     b[i]=y[i]; 
    } 
    for (int i=0;b[i]!='\0';i++) 
    { 
     if (b[i]=='N') 
     { if(b[i+1]=='i') 
      { 
          b[i+1]='I'; 
          } 
     } 
    } 

    cout<<"\nNew String 1 is :"; 
    puts(a); 
    cout<<"\nNew String 2 is :";<<b 

} 


int main() 
{ 
const char *szTestString1 = "Ni nI NI nI Ni"; 
const wchar_t *szTestString2 = L"Ni nI NI nI Ni"; 
MyClass ob1; 
ob1.getNiCount(szTestString1,szTestString2); 
ob1.replaceNiWithNI(szTestString1,szTestString2); 
getchar(); 
return 0; 
} 
+1

这是你的功课,不是我们的。你尝试了什么? – amit 2012-08-19 10:58:34

+0

不,它是一个运动。 – user1609824 2012-08-19 11:13:44

+0

为谁练习?你的问题到底是什么? – amit 2012-08-19 11:14:42

回答

4

这里有几个问题:

  1. 你的程序无法在

    cout<<"\nNew String 2 is :";<<b 
    
  2. 你的程序崩溃在strcpy(a,x);因为错位的分号来编译,因为你”重新复制到a这是未初始化 - 它没有分配内存。你需要在a上调用new才能工作,这也意味着你需要知道所需数组的大小(可能是函数的另一个参数)。

  3. 使用std::stringstd::wstring几乎总是优于处理原始字符数组。例如,请参见this question。我看你可能已经考虑过了,因为你已经有了#include <string>

  4. 由于你需要对不同类型执行相同的操作,我怀疑练习的重点可能是使用模板。

  5. 你说

    getNiCount应该返回出现的次数......

    但你getNiCount不返回任何东西。

  6. using namespace std;通常是considered bad practice

  7. 它通常值得favouring pre-increments rather than post-increments,虽然在这种特殊情况下,没有开销。


给你举一个例子,包括上面的建议:

#include <iostream> 
#include <string> 

template<typename StrType> 
class MyClass { 
public: 
    int getNiCount(const StrType& input) const; 
    void replaceNiWithNI(StrType& input) const; 
}; 

template<typename StrType> 
int MyClass<StrType>::getNiCount(const StrType& input) const { 
    int count = 0; 
    for (int i = 0; i < input.size() - 1; ++i) { 
    if (input[i] == 'N' && input[i + 1] == 'i') 
     ++count; 
    } 
    return count; 
} 

template<typename StrType> 
void MyClass<StrType>::replaceNiWithNI(StrType& input) const { 
    for (int i = 0; i < input.size() - 1; ++i) { 
    if (input[i] == 'N' && input[i + 1] == 'i') 
     input[i + 1] = 'I'; 
    } 
} 


int main() { 
    const char* szTestString1 = "Ni nI NI nI Ni"; 
    MyClass<std::string> ob1; 
    std::string testString1(szTestString1); 
    int count1 = ob1.getNiCount(testString1); 
    ob1.replaceNiWithNI(testString1); 
    std::cout << "Found " << count1 << " occurences of Ni in String 1. " 
      << "New string: " << testString1 << '\n'; 

    const wchar_t* szTestString2 = L"Ni nI NI nI Ni"; 
    MyClass<std::wstring> ob2; 
    std::wstring testString2(szTestString2); 
    int count2 = ob2.getNiCount(testString2); 
    ob2.replaceNiWithNI(testString2); 
    std::wcout << L"Found " << count2 << L" occurences of Ni in String 2. " 
      << L"New string: " << testString2 << '\n'; 

    getchar(); 
    return 0; 
} 

我一般离开你找到方式和更换Ni字符,你有它。 member functions of std::string<algorithm> library中有更复杂的选项。

+0

Thanx a ton Fraser – user1609824 2012-08-21 07:31:29