2012-08-19 32 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