Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编程任务询问_C++ - Fatal编程技术网

C++ 编程任务询问

C++ 编程任务询问,c++,C++,任务说明: 实现类MyClass的两个函数getNiCount和replaceNiWithNI: getNiCount应返回szTestString1/2中出现“Ni”的次数(区分大小写) replaceNiWithNI应将szTestString1/2中出现的所有“Ni”替换为“Ni”(区分大小写) 调用两个函数getNiCount和replaceNiWithNI 显示屏幕上最后一条注释中给出的字符串X和Y应替换为实际值 类MyClass应该能够处理szTestString1(ASCII)

任务说明:

  • 实现类
    MyClass
    的两个函数
    getNiCount
    replaceNiWithNI
    • getNiCount
      应返回
      szTestString1/2
      中出现“Ni”的次数(区分大小写)
    • replaceNiWithNI
      应将
      szTestString1/2
      中出现的所有“Ni”替换为“Ni”(区分大小写)
  • 调用两个函数
    getNiCount
    replaceNiWithNI
  • 显示屏幕上最后一条注释中给出的字符串
    X
    Y
    应替换为实际值
  • MyClass
    应该能够处理
    szTestString1
    (ASCII)和
    szTestString2
    (Unicode)
  • 一般要求:

    代码应该是

    • 易于理解和维护(优先级1)
    • 技术优雅(优先2)
    • 尽可能提高(CPU)效率(优先级3)
    <> P>允许使用基于C++语言的所有技术、工具包和框架。 我的解决方案(不完整)

    逻辑如下。。。 然而,在我的系统中,功能2“替换”正在崩溃。无法修复它

    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"
    }
    
    #包括
    #包括
    使用名称空间std;
    类MyClass
    {
    公众:
    void getNiCount(const char*,const wchar_t*);
    
    //cout这里有几个问题:

  • 您的程序无法编译,因为在中放错了分号

    #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;
    }
    

    我通常会保留您查找和替换
    Ni
    字符的方式。在和中有更复杂的选项。

    这是您的家庭作业,而不是我们的。您尝试了什么?不,不是,这是一个练习。是为谁做的练习?您的问题到底是什么?我只是在寻找替代或更好的解决方案对于这个问题…首先,向我们展示你的解决方案,其次,是更好的网站张贴它。
    cout<<"\nNew String 2 is :";<<b
    
    #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;
    }