[問題] 麻煩大家幫我看看問題在哪
小弟不才 讀了資工相關科系將近七年
最近找工作卻連連被拒絕
如果被一間拒絕可能剛好只是我的某個地方沒有達到他們要求
但被兩間拒絕應該就是我本身coding的問題了
一直以來作業或project只看結果不會看內容
也許是因為這樣coding才會有一堆自己看不到的問題在
兩間公司都是給了我測試題收到我的code之後拒絕的
但是無法從他們得知問題在哪邊所以只好來這邊求教
以下先舉其中一間公司給的問題 希望大家可以給點意見
題目:
class MyClass
{
public:
getNICount(...){}
replaceNiWithNI(...){}
};
int main()
{
const char *testString1 = "Ni nI NI nI Ni";
const wchar_t *testString2 = L"Ni nI NI nI Ni";
// call getNiCount(...) of MyClass
// call replaceNiWithNI(...) of MyClass
// output : "There are X times Ni. New string: Y"
}
1. implement getNiCount 和 replaceNiWithNI 兩個函式:
- getNiCount 計算 testString1/2 的 Ni個數(case sensitive)
- replaceNiWithNI 將 testString1/2 的Ni 替換成NI(case sensitive)
2. 兩個函式都要被呼叫
3. 印出output的string, X和Y要代換成真正的值
4. MyClass要能處理testString1(ASCII) 和 testString2(Unicode)
5. 所有c++ based的toolkit和framework都可以使用
以下是我的source code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class MyClass {
string input;
vector<size_t> posOfNi;
string convert(wchar_t*);
void findNi();
public:
MyClass(void*, bool);
int getNiCount();
string replaceNiWithNI();
};
MyClass::MyClass (void *inputString, bool wideChar) {
// check the input string format and assign it to variable input
if (wideChar == false) { // ASCII string
input = string(reinterpret_cast<char*>(inputString));
}
else { // unicode string
input = convert(reinterpret_cast<wchar_t*>(inputString));
}
// execute findNi() to find Nis in this string
findNi();
}
string MyClass::convert (wchar_t* inputString) {
ostringstream stm;
const ctype<char>& ctfacet = use_facet< ctype<char> >(stm.getloc());
// convert each wchar_t to char and write to the output string stream
for(size_t i=0 ; i<wcslen(inputString) ; i++) {
stm << ctfacet.narrow(inputString[i], 0);
}
return stm.str();
}
// find the Nis in the input stirng and keep the record
// of the position for later use
void MyClass::findNi() {
size_t pos = 0, foundPos;
while ((foundPos = input.find("Ni", pos)) != string::npos) {
posOfNi.push_back(foundPos);
// Ni oppupies 2 positions so we have to set out
// the next search from 2 after the found position
pos = foundPos + 2;
}
}
int MyClass::getNiCount() {
return posOfNi.size();
}
string MyClass::replaceNiWithNI() {
string resultString = string(input);
for (int i=0; i<posOfNi.size(); i++) {
resultString[posOfNi[i]+1] = 'I'; // simply replace i with I
}
return resultString;
}
int main () {
const char *testString1 = "Ni nI NI nI Ni";
const wchar_t *testString2 = L"Ni nI NI nI Ni";
// initialize with the two strings
MyClass string1(const_cast<char*>(testString1), false);
MyClass string2(const_cast<wchar_t*>(testString2), true);
// print the result
cout << "There are " << string1.getNiCount()
<< " times Ni. New string: " << string1.replaceNiWithNI() << "\n";
cout << "There are " << string2.getNiCount()
<< " times Ni. New string: " << string2.replaceNiWithNI() << "\n";
return 0;
}
雖然在PTT上看起來有點多不過其實是個不到一百行的小程式
請大家踴躍給意見 先在此謝謝大家
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 85.178.15.110
→
10/06 08:06, , 1F
10/06 08:06, 1F
推
10/06 08:43, , 2F
10/06 08:43, 2F
推
10/06 08:57, , 3F
10/06 08:57, 3F
→
10/06 09:10, , 4F
10/06 09:10, 4F
→
10/06 09:10, , 5F
10/06 09:10, 5F
推
10/06 10:30, , 6F
10/06 10:30, 6F
→
10/06 10:31, , 7F
10/06 10:31, 7F
→
10/06 10:34, , 8F
10/06 10:34, 8F
→
10/06 11:02, , 9F
10/06 11:02, 9F
→
10/06 15:06, , 10F
10/06 15:06, 10F
※ 編輯: alexer619 來自: 85.178.4.124 (10/06 15:57)
→
10/06 15:59, , 11F
10/06 15:59, 11F
→
10/06 15:59, , 12F
10/06 15:59, 12F
→
10/06 16:01, , 13F
10/06 16:01, 13F
→
10/06 16:03, , 14F
10/06 16:03, 14F
→
10/06 16:04, , 15F
10/06 16:04, 15F
→
10/06 16:05, , 16F
10/06 16:05, 16F
→
10/06 16:12, , 17F
10/06 16:12, 17F
→
10/06 16:13, , 18F
10/06 16:13, 18F
→
10/07 10:58, , 19F
10/07 10:58, 19F
→
10/08 01:40, , 20F
10/08 01:40, 20F
→
10/08 01:41, , 21F
10/08 01:41, 21F
→
10/08 12:37, , 22F
10/08 12:37, 22F
→
10/08 20:14, , 23F
10/08 20:14, 23F
→
10/09 16:19, , 24F
10/09 16:19, 24F