1 / 21
文档名称:

C 编程题.doc

格式:doc   大小:48KB   页数:21页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

C 编程题.doc

上传人:fy5186fy 2019/5/27 文件大小:48 KB

下载得到文件列表

C 编程题.doc

文档介绍

文档介绍:#include<>voidSwap(int*x,int*y){inta;a=*x;*x=*y;*y=a;}intmain(){intb,c;cin>>b>>c;cout<<b<<""<<c<<endl;Swap(&b,&c);cout<<b<<""<<c<<endl;}2,编写一个函数,实现找出形参中的最小值。#include<>voidmaxmin(inta,intb,intc,int*max,int*min){*max=a>b?(a>c?a:c):(b>c?b:c);*min=a<b?(a<c?a:c):(b<c?b:c);}voidmain(){inta,b,c,max,min;cin>>a>>b>>c;maxmin(a,b,c,&max,&min);cout<<"max:"<<max<<","<<"min:"<<min;}3,编写一个类LEI,实现打印私有变量a的值。classLET{public: display() { cin>>x; cout<<x; }private: intx;}4,下面的函数统计子字符串substr在字符串str中出现的次数,如果substr在str中不出现,则返回值0。请完成该函数。 intstr_count(char*substr,char*str) { }#include<>intstr_count(char*substr,char*str){intsum,len;char*p;len=strlen(substr);if(len<1)return-1;for(sum=0,p=str;;){p=strstr(p,substr);if(p!=NULL){sum++;p+=len;}elsebreak;}returnsum;}5,定义一个datetime类,使其对象可以显示当前系统时间和日期#include<>#include<>classdatetime{public:intyear;intmonth;intday;inthour;intmin;intsec;datetime(){structtm*ptm;time_tm;time(&m);ptm=localtime(&m);year=ptm->tm_year+1900;month=ptm->tm_mon+1;day=ptm->tm_mday;hour=ptm->tm_hour;min=ptm->tm_min;sec=ptm->tm_sec;}voidoutput(){printf("%.4d/%.2d/%.2d%.2d:%.2d:%.2d\n",year,month,day,hour,min,sec);}};voidmain(void){datetimed;();}6,设计一个汽车类Vehicle,包含数据成员车轮数和车重,由它派生出类Car和类Truck,前者包含载客数,后者包含载重量。编写程序实现。#include<>classvehicle//定义汽车类{protected:intwheels;//车轮数floatweight;//重量public:vehicle(intwheels,floatweight);intget_wheels();floatget_weight();floatwheel_load();voidshow();};classcar:publicvehicle//定义小车类{intpassenger_load;//载人数public:car(intwheels,floatweight,intpassengers=4);intget_passengers();voidshow();};classtruck:publicvehicle//定义卡车类{intpassenger_load;//载人数floatpayload;//载重量public:truck(intwheels,floatweight,intpassengers=2,floatmax_load=);intget_passengers();floatefficiency();voidshow();};vehicle::vehicle(intwheels,floatweight){vehicle::wheels=wheels;vehicle::weight=weight;}intvehicle::get_wheels(){returnwheels;}floatvehicle::get_weight(){returnweight/wheels;}voidvehicle::show(){cout<<"车轮:"<<wheels<