编程任务问
programming task ask
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"
}
任务描述:
- 实现类
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(
您可以使用基于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;
}
这里有几个问题:
-
您的程序无法编译,因为分号在
cout<<"nNew String 2 is :";<<b
-
您的程序在
strcpy(a,x);
崩溃,因为您正在复制到未初始化的a
- 它没有分配内存。 您需要在a
上调用new
才能使其正常工作,这也意味着您需要知道所需数组的大小(可能是该函数的另一个参数(。 -
使用
std::string
和std::wstring
几乎总是比处理原始字符数组更可取。例如,请参阅此问题。 我看你可能已经考虑过了,因为你已经有了#include <string>
-
由于您需要对不同类型的类型执行相同的操作,因此我怀疑练习的重点可能是使用模板。
-
你说
getNiCount
应返回出现次数...然而,您的
getNiCount
不会返回任何内容。 -
using namespace std;
通常被认为是不好的做法。 -
通常值得支持前增量而不是后增量,尽管在这种特殊情况下,没有开销。
举个例子,包括上面的建议:
#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>
库中提供了更复杂的选项。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 有没有任务栏API可以立即应用注册表更改
- 模板元编程:如何将参数包组合成新的参数包
- Qt Q串行端口未编程设备未关闭
- 模板元编程 - 尝试实现维度分析
- 我是编程新手
- 如何以编程方式获取任务管理器进程CPU使用率(不是PerfMon API)
- 我是 c++ 编程的新手,我一直在做以下任务:
- 使用BFS/DFS解决编程任务
- 当我打开一个新对话框时,如何禁止在Qt编程的Windows任务栏中显示对话框
- 以编程方式简化 Windows 的计划任务
- 如何验证竞争性编程任务的动态编程解决方案的正确性
- 编程任务问
- 如何在正常用户环境中以编程方式隐藏 Windows 8 上的任务栏
- 编程任务:子矩阵的总和
- 在编程中,这两项任务中的任何一项都有算法方法吗
- 以编程方式更改任务栏图标(Win32, c++)
- 在TBB中使用递归、基于任务的并行编程获得不同的输出
- 以编程方式为任务调度程序创建/删除任务