class A{ int i; public:static int count; A(int a=0) {
i=a+count; count++;
cout<int A::count=0;void main(void) {
A a(100); A b; A c(200);
cout<<”count=”<(6)下列程序的功能是:判断一个字符串是否匹配另一个含”通配符”的字符串,统配符”*”表示该位置上可以出现任意多个字符(包括0个字符),例如:一个具有通配符的字符串是”ab*t”,则字符串”about”、“abort”和”abt”与之匹配,而字符串”abuse”和”aboutan”与之不匹配,规定具有通配符的字符串中必须有一且只有一个”*”。 # include # include int like(char *s1, char *s2) {
char *p1=s1, *p2=s1+strlen(s1)-1; int i=0, j=(strlen(s2)-1 ); while(*p1!=’*’ && *p1==s2[i]) i++, p1++;
while(*p2!= ‘*’ && *p2==s2[j]) j--,p2--;
if(i-1( return 0; ) }void main() {
char s1[ ]=”ab*t”, s2[20]; cout<cout<<”\\nPlease input a string:”; cin>>s2;if(like(s1,s2 )) cout<<”ok!”<cout<<”Not matched”<(7) 以下程序对反映学生情况的数组a进行排序,按其数组元素的成员id(学号)从小到大排序# include class Elem{ //描述一名学生的成绩和学号
int score; int id; public:
Elem(int s=0, int i=0) {
score=s; id=i; }
int key() //获取学号 {
return id; }
int GetScore() //获取成绩 {
return score; }
void Select(Elem a[], int n) {
for(int i=0;iint min=a[i].key; int p=i,j; Elem temp;for(j=i+1; jif (a[j].key()if(p!=i){( ); a[j]=a[p];a[p]=temp; } }
} a[i]=( ); } void main(void) Select(a,8); { for(int j=0; j<8; j++) int s[ ]={56,78,65,90,88,76,72,80}; int id[ ]={8,1,2,6,7,4,3,5}; cout<(8)输入一行字符串,用单向链表统计该字符串中每个字符出现的次数。方法是:对该字符串中的每个字符,依次查找链表上的结点。若链表结点上有该字符,则将该结点的count值加1;否则产生一个新结点,存放该字符,置count为1,并将该新结点放入链首,最后,输出链表上的每个结点的字符以及出现的次数,链表的结构如下图所式:head c count next c count next c count next
# include struct node{ char c; int count; node *next; }
void print(node *head) {
while(head){
cout<<”字符:”<c<<”\出现”<count<<”次\\n”; head=head->next; } }void dele(node *head) {
node *p;
while(head!=null) {
p=head;
head=( ); delete p; } }
node *search(node *head, char ch) {
node *p; p=head; while(p){
if(p->c==ch){ p->count++; break; }
( ); }
if(p==0){
p=new node; p->c=ch; p->count=1;
if(head) ( ); else p->next=0; ( ); }
return head; }
void main(void) {
char s[300],*p=s; node *h=0; char c;
cout<<”请输入一行字符串”; cin.getline(s,300);
while(c=*p++) h=search(h,c); print(h); dele(h) }