C++语言程序设计.docx
- 文档编号:5690928
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:46
- 大小:26.68KB
C++语言程序设计.docx
《C++语言程序设计.docx》由会员分享,可在线阅读,更多相关《C++语言程序设计.docx(46页珍藏版)》请在冰豆网上搜索。
C++语言程序设计
C++语言程序设计
题目1
内容:
1-00
辗转相除法求最大公约数。
设计思路:
两尺,用长的a减去短的b的长度作另一把尺c,用该尺量短的,如果为整数k倍,则长尺为其(k+1)倍,若不行,则将原来的短尺b和取余求得的尺c作为长尺和短尺,依次循环,直到最后余数为0。
程序代码:
#include
usingnamespacestd;
intmain()
{
intx,y,a,b,convention=1;
cout<<"请输入第一个数:
";
cin>>x;
cout<<"请输入第二个数:
";
cin>>y;
a=x;
b=y;
for(;x;)//只要余数不为0则一直循环
{
if(ab
{
x=a;
a=b;
b=x;
}
x=a%b;//取余数为a的,第二个数位b
if(x!
=0)
{
y=b;
b=x;
a=y;
}
else
{
convention=b;
cout<<"最大公约数:
";
cout< } } return(0); } 运行结果: 请输入第一个数: 7 请输入第二个数: 21 最大公约数: 7 结论: 具体方法可以做成自定义类便于之后使用。 第四章类与对象 题目2 内容: 4-9 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两点的坐标,并能计算矩形的面积。 程序思路: 定义一个Rectangle类,以它的左上和右下两点坐标作为私有成员,以成员函数来计算面积。 程序代码: #include usingnamespacestd; classRectangle { public: Rectangle(inttop,intleft,intbottom,intright); ~Rectangle(); intShowTop()const { return(CurrentTop); } intShowLeft()const { return(CurrentLeft); } intShowBottom()const { return(CurrentBottom); } intShowRight()const { return(CurrentRight); } voidSetTop(inttop) { CurrentTop=top; } voidSetLeft(intleft) { CurrentLeft=left; } voidSetBottom(intbottom) { CurrentBottom=bottom; } voidSetRight(intright) { CurrentRight=right; } intShowArea()const; private: intCurrentTop; intCurrentLeft; intCurrentBottom; intCurrentRight; }; Rectangle: : Rectangle(inttop,intleft,intbottom,intright) { CurrentTop=top; CurrentLeft=left; CurrentBottom=bottom; CurrentRight=right; } Rectangle: : ~Rectangle() { } intRectangle: : ShowArea()const { intWidth=CurrentRight-CurrentLeft; intHeight=CurrentTop-CurrentBottom; return(Width*Height); } intmain() { intNow_top,Now_left,Now_bottom,Now_right; cout<<"Pleaseinputthetop: "; cin>>Now_top; cout<<"Pleaseinputtheleft: "; cin>>Now_left; cout<<"Pleaseinputthebottom: "; cin>>Now_bottom; cout<<"Pleaseinputtheright: "; cin>>Now_right; RectangleMyRectangle(Now_top,Now_left,Now_bottom,Now_right); cout<<"Theareais: "< return(0); } 运行结果: Pleaseinputthetop: 100 Pleaseinputtheleft: 20 Pleaseinputthebottom: 20 Pleaseinputtheright: 80 Theareais: 4800 结论: 类的定义结束时需用分号表示结束。 可以在类的构造时使用构造函数进行初始化。 之后创建无参对象时自动使用改初始化值。 Set时用void型,Show时用相应类型(float,int等)。 对象名.成员函数名 /*对类的成员函数进行调用时要用进行调用*/ 类型说明符函数名(参数表)const /*定义该函数为常成员函数,常成员函数不能更新对象的数据成员*/ 题目3 内容: 4-11 定义并实现一个矩形类,有长宽两个属性,用成员函数计算其面积。 设计思路: 定义一个矩形类,有长宽两个属性(私有成员),用成员函数计算其面积。 程序代码: #include usingnamespacestd; classRectangle { public: Rectangle(floatCurrentLength=0,floatCurrentWidth=0) /*定义构造函数时可以进行初始化和赋值,当无参数是为(0,0),有参数时将参数带入*/ { Length=CurrentLength; Width=CurrentWidth; } ~Rectangle() { } floatShowArea(floatlength,floatwidth) { return(length*width); } private: floatLength; floatWidth; }; intmain() { RectangleRectangle_1;/*定义一个对象,对该对象进行操作*/ floatlength,width; cout<<"请输入矩形的长度: "; cin>>length; cout<<"请输入矩形的宽度: "; cin>>width; cout<<"长为"< < return(0); } 运行结果: 请输入矩形的长度: 10 请输入矩形的宽度: 2 长为10,宽为2的矩形的面积为20 结论: 如需返回某值,则定义函数时的返回类型要与该值的类型相同或符合类型兼容性原则。 不可直接对类进行操作,需先建立相应对象然后对该对象进行操作。 第五章C++程序的结构 题目4 内容: 5-7 定义一个Cat类,拥有静态数据成员HowManyCats,记录Cat的个体数目;静态成员函数GetHowMany(),存取HowManyCats。 设计思路: 定义一个Cat类,拥有静态数据成员HowManyCats,记录Cat的个体数目;初始时HowManyCats的值为0,当用户对Cat的age进行赋值操作时HownManyCats+1,当所有对象完成使用时HowManyCats-1,使用一个指向Cat类的数组指针Cats[TotalCats],为其动态分配空间。 当使用完成时清空该空间。 程序代码: #include usingnamespacestd; classCat { public: Cat(intage): CurrentAge(age){HowManyCats++;}//对静态数据成员赋值? ? ? ~Cat() { HowManyCats--;? ? ? } intShowAge() { returnCurrentAge; } voidSetAge(intage) { CurrentAge=age; } staticintGetHowMany() { returnHowManyCats; } private: intCurrentAge; staticintHowManyCats;/*定义HowManyCats为静态数据成员*/ }; intCat: : HowManyCats=0; /*静态数据成员不属于任何一个对象,只能通过类名进行访问,一般采用“类名: : 标识符”*/ intmain() { constintTotalCats=5; /* 符号常量定义 const类型说明符常量名=常量值 类型说明符const常量名=常量值 */ Cat*Cats[TotalCats]; for(inti=0;i { Cats[i]=newCat(i);/*动态创建堆对象“new类型名(初值列表)”*/ cout<<"Thereare"< : GetHowMany()/*静态成员函数的使用要加类名*/ <<"catsinthere.\n"; } for(intj=0;j { deleteCats[j];/*清空Cats[j]所占内存空间*/ } cout<<"Thereare"< : GetHowMany()<<"catsinthere.\n"; return(0); } 运行结果: Thereare1catsinthere. Thereare2catsinthere. Thereare3catsinthere. Thereare4catsinthere. Thereare5catsinthere. Thereare0catsinthere. 结论: new类型名[下标表达式] /* 动态分配内存使用,下标表达式指出数组元素的个数,动态为数组分配内存时不能指定数组的初值。 */ 析构函数用来完成对象被删除前的一些清理工作。 析构函数被调用后,相应对象消失,相应内存空间被释放。 ? ? ? ? 题目5 内容: 5-14 定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数totalWeight(),计算二者的重量和。 设计思路: 定义Boat与Car两个类,二者都有weight属性(私有成员),定义二者的一个友元函数totalWeight(),使其参数为Boat类和Car类的引用,用该友元函数来计算二者的重量和。 程序代码: #include usingnamespacestd; classBoat;/*前向引用声明*/ classCar { private: intweight; public: Car(inti) { weight=i; } friendinttotalWeight(Car&CurrentCar,Boat&CurrentBoat); /* 友元函数不是类的成员函数,但在它(该友元函数)的函数体中可以通过对象名访问私有和保护成员*/ }; classBoat { private: intweight; public: Boat(intj) { weight=j; } friendinttotalWeight(Car&CurrentCar,Boat&CurrentBoat); }; inttotalWeight(Car&CurrentCar,Boat&CurrentBoat) /*引用(别名)当输入一个Car类的对象是,将它的认为是CurrentCar的别名*/ { return(CurrentCar.weight+CurrentBoat.weight); } intmain() { intx,y; cout<<"Pleaseinputtheweightofthecar: "; cin>>x; cout<<"PleaseinputtheweightoftheBoat: "; cin>>y; Carc1(x); Boatb1(y); cout<<"Thetotalweightis: "< return(0); } 运行结果: Pleaseinputtheweightofthecar: 50 PleaseinputtheweightoftheBoat: 80 Thetotalweightis: 130 结论: clssB { classA;/*A是B的友元类*/ } 若A类为B类的友元类,则A类的所有成员函数都是B类的成员函数,都可以访问B类的私有和保护成员。 第六章数组、指针与字符串 题目6 内容: 6-21 编写一个程序,统计一个英文句子中字母的个数,在主程序中实现输入输出。 设计思路: 定义一个count()函数,其中含number用来统计字母的个数,当输入的为“a-z”或“A-Z”时,number+1主函数中用gets截获用户输入,用count()进行统计并输出。 程序代码: #include #include usingnamespacestd; intcount(char*string) /* 定义一个指向char型的指针变量string,string为指针变量(地址)。 */ { inti,number=0; for(i=0;string[i];i++) { if((string[i]>='a'&&string[i]<='z')||(string[i]>='A'&&string[i]<='Z')) { number++; } } return(number); } intmain() { charword[100]; cout<<"输入一个句子: "; gets(word); /* gets(字符数组) 从终端输入一个字符串到字符数组,并得到一个函数值。 该函数值是字符数组的首地址。 */ cout<<"这个句子里有"< "< return(0); } 运行结果: 输入一个句子: HellowWorld! 这个句子里有11个字母。 结论: 用gets截获时用户输入,使用指向char的指针进行逐一比较。 第七章继承与派生 题目7 内容: 7-0 设计一个类,要求记录人员信息。 设计思路: 创建一个对象数组,通过指针访问数组的成员。 程序代码: #include #include #include usingnamespacestd; //Student.h classStudent { public: Student() { Number=0; strcpy(Name,"unknown");/*赋初值unknown给Name*/ Age=0; } Student(intNewNumber,char*NewName,intNewAge) { Number=NewNumber; strcpy(Name,NewName); Age=NewAge; } voidSetInformation(intCurrentNumber,charCurrentName[30],intCurrentAge); intStudent: : ShowNumber(); char*Student: : ShowName(); intStudent: : ShowAge(); private: intNumber; charName[30]; intAge; }; //Student.cpp voidStudent: : SetInformation(intCurrentNumber,charCurrentName[30],intCurrentAge) { Number=CurrentNumber; strcpy(Name,CurrentName); Age=CurrentAge; } intStudent: : ShowNumber() { return(Number); } char*Student: : ShowName() { returnName; } intStudent: : ShowAge() { returnAge; } //main intmain() { intn; cout<<"Pleaseinputthenumberofstudent(s)whichyouwanttosave: "; cin>>n; Student*student=newStudent[n]; /* 创建指向Student类的对象数组student,其中包含n个元素 分别为student[0],student[1],…,student[n],并且student为数组名。 */ for(inti(0);i { cout<<"Pleaseinputthenumberofthisstudent: "; intnumber; cin>>number; cout<<"Pleaseinputthenameofthisstudent: "; charname[30]; cin>>name; cout<<"Pleaseinputtheageofthisstudent: "; intage; cin>>age; student[i].SetInformation(number,name,age);/*通过指针访问数组元素的成员*/ } cout< "< for(intj(0);j { cout<<"Number: "< "< "< } return(0); } 运行结果: Pleaseinputthenumberofstudent(s)whichyouwanttosave: 1 Pleaseinputthenumberofthisstudent: 01 Pleaseinputthenameofthisstudent: zhang Pleaseinputtheageofthisstudent: 19 Informations: Number: 1Name: zhangAge: 19 结论: 使用指针型数组动态创建对象数组的使用。 题目8 内容: 7-5 定义一个Shape基类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。 使用Rectangle类创建一个派生类Square。 设计思路: 设计一个Shape基类,通过功能的增加派生出Rentangle和Circle,在派生类中定义GetArea()来计算面积。 程序代码: #include usingnamespacestd; constdoublePI=3.1415926;//符号常量 classShape { private: floatX,Y; public: Shape(); Shape(floatx,floaty); ~Shape(); voidInitialPoint(floatx,floaty); floatShowX(); floatShowY(); }; Shape: : Shape()//*构造函数无返回类型* { X=0; Y=0; } Shape: : Shape(floatx,floaty)//*构造函数无返回类型* { X=x; Y=y; } Shape: : ~Shape()//*析构函数无返回类型* { } voidShape: : InitialPoint(floatx,floaty)//*只赋值,返回值为空* { X=x; Y=y; } floatShape: : ShowX()//*注意注明基类名及返回类型* { return(X); } floatShape: : ShowY() { return(Y); } classRectangle: publicShape/*注意格式*/ { private: floatH,W; public: Rectangle();//*含有4个参数,分别对应基类的点的x,y和自身的h,w* Rectangle(floatx,floaty,floatlength,floatwidth); /*含有4个参数,分别对应基类的点的x,y和自身的h,w*/ ~Rectangle(); voidInitialRentangle(floatx,floaty,floatlength,floatwidth); /*含有4个参数,分别对应基类的点的x,y和
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 语言程序设计
![提示](https://static.bdocx.com/images/bang_tan.gif)