编程任务问

programming task ask

本文关键字:任务 编程      更新时间:2023-10-16
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. 实现类MyClass的两个函数getNiCountreplaceNiWithNI
    • getNiCount应返回szTestString1/2内"Ni"的出现次数(区分大小写(
    • 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]!='';i++)
   {
       if(x[i]=='N')
       {   if(x[i+1]=='i')
                          count++;
       }
       }
   for(int i=0; y[i]!='';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]!='';i++)
     {
         if (a[i]=='N')
         {  if(a[i+1]=='i')
            {
                           a[i+1]='I';
                           }
         }
         }
     for (int i=0;y[i]!='';i++)
     {
         b[i]=y[i];
     }
     for (int i=0;b[i]!='';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. 您的程序无法编译,因为分号在

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

  3. 使用 std::stringstd::wstring 几乎总是比处理原始字符数组更可取。例如,请参阅此问题。 我看你可能已经考虑过了,因为你已经有了#include <string>

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

  5. 你说

    getNiCount应返回出现次数...

    然而,您的getNiCount不会返回任何内容。

  6. using namespace std;通常被认为是不好的做法。

  7. 通常值得支持前增量而不是后增量,尽管在这种特殊情况下,没有开销。


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

#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字符的方式。 在 std::string 的成员函数和<algorithm>库中提供了更复杂的选项。