C++实验_c语言实验册

其他范文 时间:2020-02-27 08:05:15 收藏本文下载本文
【www.daodoc.com - 其他范文】

C++实验由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言实验册”。

上机实验:

1、回文是指正读,反读均相同的字符序列,如“abba”和“abdba”均是回文,但是“good”不是回文,试用STACK类编写该程序。

#include #include #include int IsPalindrome(const char *cScr);void main(void){ char cStr[21];while(1){ gets(cStr);printf(“%dn”,IsPalindrome(cStr));} } int IsPalindrome(const char *cScr){ int iLen = strlen(cScr);//预留数组首元素,栈顶从第二元素开始

int top = 1;char *cMyStack =(char *)malloc((iLen/2+1)*sizeof(char));//定位对原始数组的检测索引初始位置 cMyStack[0] = iLen/2;if(1 == iLen%2){ ++cMyStack[0];}

//将原始数组的一半元素入栈 for(top=1;top

while(*(cScr+cMyStack[0])== cMyStack[--top] && cMyStack[0]++

free(cMyStack);return 0;} } 运行结果:

2.利用两个栈类S1、S2模拟一个队列时,编写一程序利用栈的运算实现队列的插入、删除以及判断队列空的运算。

#include #include #include using namespace std;template cla stack2queue{ public: void pushBack(T);void popFront();T& front();bool empty()const;private: stack mStack1;stack mStack2;};template void stack2queue::pushBack(T x){ mStack1.push(x);}

template void stack2queue::popFront(){ if(mStack2.empty()){ while(!mStack1.empty()){ mStack2.push(mStack1.top());mStack1.pop();} }

aert(!mStack2.empty());mStack2.pop();} template T& stack2queue::front(){ if(mStack2.empty()){ while(!mStack1.empty()){ mStack2.push(mStack1.top());mStack1.pop();} } aert(!mStack2.empty());return mStack2.top();} template bool stack2queue::empty()const{ return(mStack1.empty()&& mStack2.empty());} template void printQueue(stack2queue q){ cout sq;

sq.pushBack(1);printQueue(sq);sq.pushBack(2);printQueue(sq);sq.pushBack(3);printQueue(sq);sq.popFront();printQueue(sq);sq.popFront();printQueue(sq);sq.popFront();printQueue(sq);return 0;} 运行结果:

实验2:

声明复数的类Complex,使用友元函数add实现复数的加法。

#include using namespace std;

cla Complex { private:

double real, image;public :

Complex(){}

Complex(double a,double b)

{

real = a;image = b;}

void setRI(double a, double b){

real = a;image = b;} double getReal(){ return real;}

double getImage(){ return image;} void print(){ if(image>0)

cout

cout

friend Complex add(Complex ,Complex);//声明友元函数 };

Complex add(Complex c1, Complex c2)//定义友元函数

{

Complex c3;

c3.real = c1.real + c2.real;//访问Complex类中的私有成员

c3.image = c1.image + c2.image;return c3;}

void main(){

Complex c1(29, 0.634), c2, c3;c2.setRI(85,106.012);c3 = add(c1, c2);

cout

结果:

实验三:

7-5 定义一个基类Shape,在此基础上派生出一个Rectangle和Circle,二者都有getArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square.#include using namespace std;#define PI 3.1415926 cla Shape {

public: Shape(){}

double GetArea()

{

return 0.1;}

};cla Rectangle: public Shape {

public:

Rectangle(double w,double h)

{

width=w;height=h;}

double GetArea(){

return width*height;}

private: double width,height;};cla Circle:public Shape { private: double r;

public: Circle(double rr){ r=rr;}

double GetArea(){

return PI*r*r;} };

int main(){

Rectangle * rec=new Rectangle(5,6);

Circle * cir=new Circle(5);

coutGetArea()

coutGetArea()

return 1;

} 运行结果:

7-10.定义一个Object类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及相应的操作函数,声明一个Box对象,观察构造函数和析构函数的调用顺序。#include cla object { private: int Weight;public:

object(){ cout

cla box:public object

{ private: int Height,Width;public: box(){

cout

下载C++实验word格式文档
下载C++实验.doc
将本文档下载到自己电脑,方便修改和收藏。
点此处下载文档

文档为doc格式

相关专题 c语言实验册
    热门文章
      整站推荐
        点击下载本文