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"
}
任务描述:编程任务要求
- 实现这两种功能
getNiCount
和类MyClass
的replaceNiWithNI
:getNiCount
应在szTestString1/2
返回“倪”的出现次数(区分大小写)replaceNiWithNI
应将szTestString1/2
中所有出现的“Ni”替换为“NI”(区分大小写)
- 调用
getNiCount
和replaceNiWithNI
这两个函数。 - 显示屏幕上最后评论中给出的字符串。应该用实际值替换
X
和Y
。 - 类
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;
}
这是你的功课,不是我们的。你尝试了什么? – amit 2012-08-19 10:58:34
不,它是一个运动。 – user1609824 2012-08-19 11:13:44
为谁练习?你的问题到底是什么? – amit 2012-08-19 11:14:42