面向对象程序设计语言-期末试题.docx
面向对象程序设计语言-期末试题 “面向对象程序设计语言”期末试题 (2022年1月23日) (共6题、4页。考试时间:120分钟) 姓名_ 学号_ 1.(15分) 对下面的函数findChar,改正你所发现的错误,并写出另一个函数find2Char,使之改用指针方式来访问cpString的元素,且功能与改正后的函数findChar等价。 void findChar(const char cpString, const char cChar) for (int i = 0; cpStringi != 0; i+) if (cpStringi = cChar) printf("The index is %d.n", i); return; printf("In the string "%s" having %d chars, no %chas been found.n", cpString, i, cChar); 答案: void findChar(const char cpString, const char cChar) int i; / 作用域概念 for (i = 0; cpStringi != 0; i+) if (cpStringi = cChar) / 常见的操作符使用熟练程度 / void find2Char(const char cpString, const char cChar) int i = 0; for (const char* p = cpString; *p != 0; p+, i+)/ 类型匹配概念;dereferencing;指针操作 if (*p = cChar) / dereferencing printf("The index is %d.n", i); return; printf("In the string "%s" having %d chars, no %chas been found.n", cpString, i, cChar); 2.(20分) 阅读下面的程序,写出main的输出。 int h(int iCount, const int* ipIndex, int& irObj) iCount %= 2; if (*ipIndex >= 5) irObj += iCount; return *ipIndex + irObj; main() int i, j = 1, k = 2, m; for (i = 0; i i; j-) tBodyj = tBodyj-1; / 元素后移 break; tBodyi = t; iCurrentElmts+; return true; template T* SortedSet:get(T t) for (int i = 0; i i; j-) tpBodyj = tpBodyj-1; / 元素后移 break; tpBodyi = t; iCurrentElmts+; return true; template T* SortedSet:get(T t) for (int i = 0; i < iCurrentElmts; i+) if (tpBodyi <= t) continue; return &(tpBodyi); return 0; template void SortedSet:del(T t) for (int i = 0; i < iCurrentElmts; i+) if (tpBodyi != t) continue; for (int j = i; j < iCurrentElmts - 1; j+) tpBodyj = tpBodyj+1; / 元素前移 iCurrentElmts-; break;