”,*(cp+i));” ④ “int i;”应为”int i=0;” (2)程序功能是将各个平方根值放入数组中。 #include void main() { int max,a,i; scanf(“%d%d”,max,a); double x[max]; for (i=0;i x=sqrt(a*i); } ① 增加”#include ” ② “scanf(“%d%d”,max,a);”应为”scanf(“%d%d”,&max,&a);” ③ “double x[max];”改为: “double *x=new double[max];” … “delete []x;” 四、(8分)下列shape类是一个表示形状的抽象类,area( )为求图形面积的函数,total( )则是一个通用的用以求不同形状的图形面积总和的函数。请从shape类派生三角形类(triangle)、矩形类(rectangle),并给出具体的求面积函数 class shape{ public: virtual float area( )=0; };
float total(shape *s[ ],int n) { float sum=0.0; for(int i=0;i sum+=s->area( ); return sum; } class Triangle:public Shape { public: Triangle(double h,double w){H=h;W=w;} double Area() const{return H*W*0.5;} private: double H,W; }; class Rectangle:public Shape { public: Rectangle(double h,double w){H=h;W=w;} double Area()const{return H*W;} private: double H,W; };
五、(6分)完成顺序查找函数f_seq( )。其过程是:从表头开始,根据给定的模式,逐项与表中元素比较。如果找到所需元素,则查找成功,并打印出它在表中的顺序号。如果查找整个表仍未找到所需对象,则查找失败 #include void f_seq(char *list[],char *object,int len) //list 指针数组,指向字符串 //object 模式串 //len 表的长度 { char **p; int strcmp(char *s,char *t); p=list; while (_____①______) //p if (strcmp(*p,object)==0) break; else ______②_______; //p++ if (p printf( “Success! **% d/n”,p-list); else printf(“Unsuccess!/n”); } int strcmp(char *s,char *t) { for (;*s==*t; s++,t++) if (*s==’/0′) return(0); return(_____③______); //s-t或*s-*t或1 } 六、(8分)完成使链表逆置函数reverse,若有链表: 链表结点的结构如下: struct node { int num; struct node *next; } struct node* reverse(struct node *head) //head 链表头结点 { struct node *p,*temp1,*temp2; if(head==NULL____①____) return head; //||head->next==NULL p=head->next;head->next=NULL; while(____②____) //p!=NULL或p { temp1=head; ____③____; //head=p; temp2=p; p=p->next; ____④____; //temp2->next=temp1;或head->next=temp1; }//Match while statenment return head; //返回逆置后的链表的头结点 }
|