[問題] 麻煩大家幫我看看問題在哪

看板C_and_CPP作者 ( )時間14年前 (2011/10/06 07:03), 編輯推噓3(3021)
留言24則, 11人參與, 最新討論串1/1
小弟不才 讀了資工相關科系將近七年 最近找工作卻連連被拒絕 如果被一間拒絕可能剛好只是我的某個地方沒有達到他們要求 但被兩間拒絕應該就是我本身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
題目的變數宣告 XD
10/06 08:43, 2F

10/06 08:57, , 3F
2間也還好, 再多找找吧.
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
bool wideChar 那邊一定會被打槍
10/06 10:30, 6F

10/06 10:31, , 7F
用 overload 或是 template 吧
10/06 10:31, 7F

10/06 10:34, , 8F
看到reinterpret_cast就覺得非常毛...
10/06 10:34, 8F

10/06 11:02, , 9F
大大來自德國柏林嗎? 現在那裡很冷吧? 比倫敦還高緯度?!
10/06 11:02, 9F

10/06 15:06, , 10F
你覺得要怎麼用C++寫? http://codepad.org/4b7xwmJk
10/06 15:06, 10F
※ 編輯: alexer619 來自: 85.178.4.124 (10/06 15:57)

10/06 15:59, , 11F
我知道看緣份不過兩間公司都在看完code的情況下拒絕我想
10/06 15:59, 11F

10/06 15:59, , 12F
coding的style一定有什麼問題存在
10/06 15:59, 12F

10/06 16:01, , 13F
其實我沒用過c++這是我靠對c+java的認識寫出來的
10/06 16:01, 13F

10/06 16:03, , 14F
w_char跟cast都是看document或tutorial這樣寫問題是在?
10/06 16:03, 14F

10/06 16:04, , 15F
對阿我在柏林這是德國一間小公司的題目現在還不冷啦...
10/06 16:04, 15F

10/06 16:05, , 16F
沒想到在這裡也被查ip!?XD 謝謝愛我大大我會研究一下
10/06 16:05, 16F

10/06 16:12, , 17F
c++是絕對不同於c和java語言的.它是惡魔語言.
10/06 16:12, 17F

10/06 16:13, , 18F
用得好,你上天堂.用不好,天天爆肝還是被罵
10/06 16:13, 18F

10/07 10:58, , 19F
1.7~13度不冷嗎? 2.「c++是絕對不同於c和java語言的」
10/07 10:58, 19F

10/08 01:40, , 20F
短袖+外套就夠了XD 請問有沒有什麼網站或書可以看的?我
10/08 01:40, 20F

10/08 01:41, , 21F
看c++的官網tutorial感覺跟java或c沒有很大的不同
10/08 01:41, 21F

10/08 12:37, , 22F
你以為C++只有你想像的那樣就大錯特錯了....
10/08 12:37, 22F

10/08 20:14, , 23F
C/C++/Java個人覺得這幾乎是三個世界...雖然有部份重疊XD
10/08 20:14, 23F

10/09 16:19, , 24F
完全同意樓上的看法,雖然有互相影響,但畢竟不同
10/09 16:19, 24F
文章代碼(AID): #1EZE8sm3 (C_and_CPP)