c题库编程题.docx

上传人:b****1 文档编号:3125637 上传时间:2023-05-05 格式:DOCX 页数:39 大小:20.17KB
下载 相关 举报
c题库编程题.docx_第1页
第1页 / 共39页
c题库编程题.docx_第2页
第2页 / 共39页
c题库编程题.docx_第3页
第3页 / 共39页
c题库编程题.docx_第4页
第4页 / 共39页
c题库编程题.docx_第5页
第5页 / 共39页
c题库编程题.docx_第6页
第6页 / 共39页
c题库编程题.docx_第7页
第7页 / 共39页
c题库编程题.docx_第8页
第8页 / 共39页
c题库编程题.docx_第9页
第9页 / 共39页
c题库编程题.docx_第10页
第10页 / 共39页
c题库编程题.docx_第11页
第11页 / 共39页
c题库编程题.docx_第12页
第12页 / 共39页
c题库编程题.docx_第13页
第13页 / 共39页
c题库编程题.docx_第14页
第14页 / 共39页
c题库编程题.docx_第15页
第15页 / 共39页
c题库编程题.docx_第16页
第16页 / 共39页
c题库编程题.docx_第17页
第17页 / 共39页
c题库编程题.docx_第18页
第18页 / 共39页
c题库编程题.docx_第19页
第19页 / 共39页
c题库编程题.docx_第20页
第20页 / 共39页
亲,该文档总共39页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c题库编程题.docx

《c题库编程题.docx》由会员分享,可在线阅读,更多相关《c题库编程题.docx(39页珍藏版)》请在冰点文库上搜索。

c题库编程题.docx

c题库编程题

//2-5

#include

#include

#include

usingnamespacestd;

classTriangle

{inta,b,c;public:

doubleArea()

{doubles=(a+b+c)/2;

returnsqrt(s*(s-a)*(s-b)*(s-c));};

intPerimeter(){returna+b+c;

}

voidInput(){cin>>a>>b>>c;}};

voidmain()

{Trianglet1;

t1.Input();

cout<

}

//2-3

#include

inlineintmax(inta,intb)

{

if(a>b)returna;

elsereturnb;}

inlineintmax(inta,intb,intc)

{

returnmax(a,max(b,c));

}

voidmain()

{

intx=21,y=15,z=22,d;

d=max(x,max(y,z));

cout<

}

//2-4

#include

intoverload(intn);

charoverload(charn);

voidmain()

{

intn;

chars;

cout<<"输入5";

cin>>n;

cout<<"n="<

cout<<"输入a"<

cin>>s;

cout<<"a=";

cout<

}

intoverload(intn)

{

returnn;

}

charoverload(charn)

{

cout<<"b"<

return0;

}

//2-2

#include"iostream"

usingnamespacestd;

inlinechartrans(charch);

intmain()

{

charch;

while((ch=getchar())!

='\n')

cout<

cout<

return0;

}

inlinechartrans(charch)

{

if(ch>='a'&&ch<='z')

returnch-32;

else

returnch+32;

}

//3-1

#include

usingstd:

:

cout;

usingstd:

:

endl;

intMin(inta,intb)

{

return(a

b);

}

intMin(inta,intb,intc)

{

return(Min(a,b)

c);

}

intMin(inta,intb,intc,intd)

{

return(Min(a,b,c)

d);

}

voidmain(void)

{

cout<<"MIN(1,2)="<

cout<<"MIN(3,4,5)="<

cout<<"MIN(4,5,6,7)="<

}

//3-2

#include

usingnamespacestd;

voidArea(floatr)

{

cout<<"thecircle'sareais:

"<<3.14*r*r<

}

voidArea(floata,floatb)

{

cout<<"therectangle's(square's)areais:

"<

}

voidArea(floata,floatb,floath)

{

cout<<"thetrapzoid'sareais:

"<<(a+b)*h/2<

}

voidmain()

{

Area(5);

Area(3,3);

Area(3,5,4);

}

//3-3

#include

usingnamespacestd;

voidSort(inta[],intn)

{

boolflag;

for(inti=0;i

{

flag=false;

for(intj=1;j

if(a[j-1]>a[j])

{

inttemp=a[j-1];

a[j-1]=a[j];

a[j]=temp;

flag=true;

}

if(!

flag)

break;

}

for(intm=0;m

cout<

cout<

}

voidSort(floatb[],intn)

{

boolflag;

for(inti=0;i

{

flag=false;

for(intj=1;j

if(b[j-1]>b[j])

{

floattemp=b[j-1];

b[j-1]=b[j];

b[j]=temp;

flag=true;

}

if(!

flag)

break;

}

for(intm=0;m

cout<

cout<

}

voidmain()

{

inta[10]={3,5,1,2,9,0,8,6,4,7};

floatb[10]={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};

Sort(a,10);

Sort(b,10);

}

//3-4

#include

usingnamespacestd;

classRectangle

{

floata,b;

public:

Rectangle()

{

cout<<"inputthetwosideoftherectangle:

"<

cin>>a>>b;

}

Rectangle(floatx,floaty):

a(x),b(y){}

floatCir()

{

return(2*(a+b));

}

floatArea()

{

return(a*b);

}

};

voidmain()

{

RectangleRe;

cout<<"therectangle'scircumferenceis:

"<

<<"therectangle'sareais:

"<

}

//3-5

#include

usingnamespacestd;

classCircle

{

floatr;

public:

Circle()

{

cout<<"inputtheradiusofthecircle:

"<

cin>>r;

}

Circle(floatx):

r(x){}

floatCir()

{return2*3.14*r;}

floatArea()

{return3.14*r*r;}

};

voidmain()

{

CircleC(5);

cout<<"theCircle'scircumferenceis:

"<

<<"theCircle'sAreais:

"<

}

//4-1

classstudent

{

intno;

charname[20];

public:

student();

intgetno()const;

char*getname();

};

#include

student:

:

student()

{

no=1;

strcpy(name,"wang");

}

intstudent:

:

getno()const

{

returnno;

}

char*student:

:

getname()

{

returnname;

}

intmain()

{

students1;

s1.getno();

s1.getname();

conststudents2;

s2.getno();

return0;

}

//4-3

#include

classobject

{

floata;

floatweight;

public:

object(floatw);

~object(){cout<<"destructweight"<

};

classbox:

publicobject

{

floatb;

floatheight,widt;

public:

box(floatw,floath,floatwi):

object(w),height(h),widt(wi){cout<<"constructheight"<

~box(){cout<<"destructheight"<

};

object:

:

object(floatw)

{

weight=w;

cout<<"constructweight"<

}

intmain()

{

boxb(1.1,2.2,3.3);

return0;

}

//4-4

#include"iostream"

usingnamespacestd;

classbaseclass

{

public:

baseclass(){cout<<"a....c"<

virtual~baseclass(){cout<<"a......d"<

};

classderivedclass:

publicbaseclass

{

public:

derivedclass(){cout<<"b....c"<

~derivedclass(){cout<<"b....d"<

};

intmain()

{

derivedclassobj;

baseclass*p;

p=&obj;

return0;

}

//4-5

#include"iostream"

#include

usingnamespacestd;

classDoument

{

char*name;

public:

Doument(char*na)

{

name=newchar[strlen(na)+1];

strcpy(name,na);

}

virtualvoidprint()const{cout<<"姓名:

"<

};

classBook:

publicDoument

{

intpagecount;

char*name;

public:

Book(intp,char*na):

Doument(na)

{

p=pagecount;

name=newchar[strlen(na)+1];

strcpy(name,na);

}

voidprint()const{cout<<"页码:

"<

"<

};

voiddisp(Doument&i)

{

i.print();

}

intmain()

{

Bookb(23,"小明");

disp(b);

return0;

}

//5-1

#include"iostream"

usingnamespacestd;

classpoint

{private:

intx;

inty;

intz;

staticintnum;

public:

point(inta,intb,intc);

intprint();

};

intpoint:

:

num=0;

intmain()

{

pointp1(1,2,3);

p1.print();

pointp2(5,3,2);

p1.print();

pointp3(3,6,4);

p1.print();

return0;

}

point:

:

point(inta,intb,intc)

{

x=a;

y=b;

z=c;

num++;

}

intpoint:

:

print()

{

cout<<"第"<

return0;

}

//5-2

#include

#include

usingnamespacestd;

classStu

{

charname[15];

intage;

floatscore;

public:

staticintcount;

staticfloatsum;

Stu(char*p,inta,floats):

age(a),score(s)

{

strcpy(name,p);

count++;

sum+=score;

}

Stu()

{

cout<<"inputthestudent'sname,age,score:

"<

cin>>name>>age>>score;

count++;

sum+=score;

}

};

intStu:

:

count=0;

floatStu:

:

sum=0;

voidmain()

{

Stup("子机",19,98),q;

cout<<"thenumberofstudentsis:

"<

:

count<

<<"thetotalscoreis:

"<

:

sum<

}

//5-3

#include

usingnamespacestd;

classPoint

{

floatx,y,z;

public:

Point(floata=0,floatb=0,floatc=0):

x(a),y(b),z(c){}

Pointoperator++();

Pointoperator--();

Pointoperator++(int);

Pointoperator--(int);

voidshow()

{

cout<<"(x,y,z)="

<<"("<

}

};

PointPoint:

:

operator++()

{

returnPoint(++x,++y,++z);

}

PointPoint:

:

operator--()

{

returnPoint(--x,--y,--z);

}

PointPoint:

:

operator++(int)

{

Pointtemp;

temp.x=x++;

temp.y=y++;

temp.z=z++;

returntemp;

}

PointPoint:

:

operator--(int)

{

Pointtemp;

temp.x=x--;

temp.y=y--;

temp.z=z--;

returntemp;

}

voidmain()

{

Pointa(1,1,1),b(2,2,2),c,d;

cout<<"a:

";

a.show();

cout<<"b:

";

b.show();

cout<

c=a++;

cout<<"c=a++\n";

cout<<"c:

";

c.show();

cout<

d=++b;

cout<<"d=++b\n";

cout<<"d:

";

d.show();

cout<<"Newaandb:

\n";

a.show();

b.show();

cout<

c=a--;

cout<<"c=a--\n";

cout<<"c:

";

c.show();

cout<

d=--b;

cout<<"d=--b\n";

cout<<"d:

";

d.show();

cout<<"Newaandb:

\n";

a.show();

b.show();

}

//5-4

#include

usingnamespacestd;

classComplex

{

floatreal,image;

public:

Complex(floata,floatb):

real(a),image(b){}

Complex(){}

friendbooloperator==(Complex&x,Complex&y)

{return(x.real==y.real&&x.image==y.image);}

};

voidmain()

{

ComplexCp1(1,2),Cp2(3,4),Cp3(1,2);

cout<<(Cp1==Cp2)<

<<(Cp1==Cp3)<

}

//6-1

#include

usingnamespacestd;

classComplex

{

floatreal,image;

public:

Complex(floatx,floaty):

real(x),image(y){}

Complex(){}

Complexoperator+(Complex&a)

{returnComplex(real+a.real,image+a.image);}

Complexoperator-(Complex&b)

{returnComplex(real-b.real,image-b.image);}

friendostream&operator<<(ostream&output,Complex&c)

{

output<<'('<

returnoutput;

}

};

voidmain()

{

ComplexCp1(1,-2),Cp2(2,5),Cp3,Cp4;

Cp3=Cp1+Cp2;

Cp4=Cp1-Cp2;

cout<

<

}

//6-2

#include

usingnamespacestd;

classcomputer

{

floatx,y;

public:

computer(floata=0,floatb=0):

x(a),y(b){}

computeroperator++();

computeroperator--();

computeroperator++(int);

computeroperator--(int);

voidshow()

{

cout<<"(x,y)="

<<"("<

}

};

computercomputer:

:

operator++()

{

returncomputer(++x,++y);

}

computercomputer:

:

operator--()

{

returncomputer(--x,--y);

}

computercomputer:

:

operator++(int)

{

computertemp;

temp.x=x++;

temp.y=y++;

returntemp;

}

computercomputer:

:

operator--(int)

{

computertemp;

temp.x=x--;

temp.y=y--;

returntemp;

}

voidmain()

{

computera(1,1),b(2,2),c,d;

cout<<"a:

";

a.show();

cout<<"b:

";

b.show();

cout<

c=a++;

cout<<"c=a++\n";

cout<<"c:

";

c.show();

cout<

d=++b;

cout<<"d=++b\n";

cout<<"d:

";

d.show();

cout<<"Newaandb:

\n";

a.show();

b.show();

cout<

c=a--;

cout<<"c=a--\n";

cout<<"c:

";

c.show();

cout<

d=--b;

cout<<"d=--b\n";

cout<<"d:

";

d.show();

cout<<"Newaandb:

\n";

a.show();

b.show();

}

//6-3

#include

#include

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2