![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
上機(jī)練習(xí)1 |
上機(jī)模擬試卷(1) (1) 改錯(cuò)題: 函數(shù)fun的功能是:求出s所指字符串中最后一次出現(xiàn)t所指子字符串的地址,通過(guò)函數(shù)值返回,在主函數(shù)中輸出從此地址開(kāi)始字符串,若未找到,則函數(shù)值為NULL. 例如:當(dāng)字符串中的內(nèi)容為 abcdabfabcdx,t中的內(nèi)容為ab時(shí),輸出結(jié)果為abcdx. 含有錯(cuò)誤的源程序如下: # include <iostream.h> #include <string.h> char *fun(char *s, char *t) { char *p,*r,*a; a=Null; while(*s) { p=s;r=t; while(*r) if(r==p) {r++;p++} else break; if(*r==’\ p++; } return a; } main() { char s[100], t[100], *p; cout<<”P(pán)lease enter string S:”; cin>>s; cout<<”P(pán)lease enter substring t:”; cin>>t; p=fun(s,t); if(p) cout<<”The result is:”<<p; else cout<<”Not found!”; }
二:編程題: 建立一個(gè)類Array,動(dòng)態(tài)生成數(shù)組,按數(shù)組元素后兩位上值大大小進(jìn)行降序排序,數(shù)組中的每個(gè)數(shù)均是四位數(shù),具體如下: 1. 私有數(shù)據(jù)成員. int *a:指向根據(jù)len動(dòng)態(tài)申請(qǐng)的數(shù)組空間 int len:有效數(shù)組元素的個(gè)數(shù) 2. 公有成員函數(shù): Array(int b[],int length):構(gòu)造函數(shù),使用數(shù)組b初始化a所指的動(dòng)態(tài)數(shù)組,length初始化len void sort():按題意對(duì)動(dòng)態(tài)數(shù)組中的元素進(jìn)行排序 void print():輸出a所指向的數(shù)組 ~Array():析構(gòu)函數(shù), 釋放動(dòng)態(tài)數(shù)組空間 3.在主函數(shù)中定義一個(gè)Array類的對(duì)象test,調(diào)用成員函數(shù)完成排序和輸出
|