C++面向对象程序设计试题.doc_第1页
C++面向对象程序设计试题.doc_第2页
C++面向对象程序设计试题.doc_第3页
C++面向对象程序设计试题.doc_第4页
C++面向对象程序设计试题.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

面向对象程序设计操作题说明: C+实现一、假定一般居民的基本数据包括身份证号、姓名、性别和出生日期,而居民中的成年人又多两项数据:最高学历和职业,成人中的党员又多一项数据:党派类别。现要求建立三个类,让成人类继承居民类,而党员类又继承成人类,并要求在每个类中都提供有数据输入和输出的功能。20分class People /居民类char num19; /身份证号char name11; /姓名int sex; /性别char birth11; /出生日期public:void Input() coutInput People data: numnamesexbirth;void Output() coutnum name ;coutsex birthendl;class Adult: public People /成人类char sch11; /最高学历char prof11; /从事职业public:void Input() People:Input();coutInput sch & prof data:schprof;void Output() People:Output();coutsch profendl;class Party: public Adult /党员类char parties15; /党派类别public:void Input() Adult:Input();coutInput parties data: parties;void Output() Adult:Output();coutpartiesendl;二、向字符文件 ”d:userwr1.dat”中存入100以内的所有奇数。10分public class Class33public static void main(String args)for(int i=0;i100;i+)if(i%2!=0)System.out.println(i);三、铁路进行列车调度时,常把站台设计成栈式结构的的站台,试问如果有编号为1、2、3、4、5、6的六辆列车,顺序开入栈式结构的站台,则可能的出栈序列有多少种? 10分#define MAX_LENGTH 9 #include char tmpMAX_LENGTH; int g_nTotal = 0; void FillPos(int pos, int nFixLength, char s) int i, j, nIndex = 0; char c, tMAX_LENGTH; if (nFixLength = strlen(s)-1) nIndex = nFixLength - 1; tmppos = snFixLength; for (j=1;jnFixLength+1;j+) tmppos+j = snIndex; nIndex-; printf(%sn, tmp); g_nTotal+; return; tmppos = snFixLength; memset(t, 0, sizeof(t); for(i=0;i0) nFixLength-; for(i=nFixLength;istrlen(t);i+) FillPos(pos+1, i, t); return; main() int i = 0, n = 0; char sMAX_LENGTH; memset(s, 0, sizeof(s); printf(input length:n); scanf(%d, &n); for(i=0;in;i+) si = i + 1 ; printf(nStack string is: %sn, s); printf(Result is:nn); for(i=0;in;i+) FillPos(0, i, s); printf(n); printf(nTotal: %dn, g_nTotal); 四、用选择法对10个整数排序 10分#include #include using namespace std; int main() void order(int *p,int n); int a10,i; coutenter 10 numbers:endl; for(i=0;i*(a+i); order(a,10); coutNow the order:endl; for(i=0;i10;i+) coutsetw(5)ai; coutendl; void order(int *p,int n) int j,k,t,m; for(m=0;mn-1;m+) j=m; for(k=m+1;k*(p+k) t=*(p+m); *(p+m)=*(p+k); *(p+k)=t; 五、建立采用递归求和的函数 20分#include #define N 100int sum(unsigned int n)if ( n = 0) /递归出口return 0;elsereturn n + sum( n -1 );int main(int argc, char *argv)printf(sum is: %d n, sum(N);return 0;六、建立一个链表,每个节点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的节点所包含的年龄等于此年龄,将此节点删除。30分#include#include#define LEN sizeof(struct student)struct student/*定义结构体*/ char num6;char name8;char sex2;int age;struct student*next;stu10;void main() struct student*p,*pt,*head;FILE *fp;int i,length,iage,flag=1;int find=0;char filename10,numone10=first,second,third,fourth,fifth,sixth,senventh,eighth,ninth,tenth;printf(tt);for(i=20;i71;i+)printf(003);printf(n);while(flag=1)/*输入链表长度*/printf(tttinput length of list(10):);scanf(%d,&length);if(length10)flag=0;printf(tt);for(i=20;i71;i+)printf(003);printf(n);for(i=0;inext=p;pt=p;printf(nttinput score of student %s:,numonei);printf(tNo. :);scanf(%s,p-num);printf(ttttttname:);scanf(%s,p-name);printf(ttttttsex :);scanf(%s,p-sex);printf(ttttttage :);scanf(%d,&p-age);p-next=NULL;printf(tt);for(i=20;i71;i+)printf(003);printf(n);printf(nttplease scanf filename:t);/*输入文件名*/scanf(%s,filename);printf(n);fp=fopen(filename,w);/*建立文件并将数据写入磁盘上*/for(i=0;ilength;i+)if(fwrite(&stui,sizeof(struct student),1,fp)!=1);printf(ttttfile srite errovn);fclose(fp);p=head;i=0;printf(tt);for(i=20;inum,p-name,p-sex,p-age);p=p-next;fclose(fp);printf(tt);for(i=20;iage=iage)/*链表头是待删元素*/p=pt-next;head=pt=p;find=1;else/*链表头不是待删元素*/pt=pt-next;while(pt!=NULL)if(pt-age=iage)p-next=pt-next;find=1;else/*中间结点不是待删元素*/p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论