zoukankan      html  css  js  c++  java
  • 2011年 浙工大考研计算机专业课试题C++

    2011年 浙工大考研计算机专业课试题C++

    个人闲暇之余整理,可能会有许多细节问题且题目实现代码不唯一,若有需要可邮件与我交流。

    一,读程序写结果5*9=45分

    // kaoshi.cpp : 定义控制台应用程序的入口点。

    //2011-1-1

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    void Swap1(int a,int b)

    {

           intt=a;a=b;b=t;

    }

    void Swap2(int &a,int &b)

    {

           intt=a;a=b;b=t;

    }

    int main()

    {

           intx(3),y(30);

           Swap1(x,y);

           cout<<"x="<<x<<",y="<<y<<endl;

           Swap2(x,y);

           cout<<"x="<<x<<",y="<<y<<endl;

          

           return0;

    }

    结果:

    x=3,y=30

    x=30,y=3

    // kaoshi.cpp : 定义控制台应用程序的入口点。

    //2011-1-2

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    class BaseClass

    {

    public:

           BaseClass()

           {

                  cout<<"构造A对象!"<<endl;

           }

           ~BaseClass()

           {

                  cout<<"析构A对象!"<<endl;

           }

    };

    class DerivedClass:public BaseClass

    {

    public:

           DerivedClass()

           {

                  cout<<"构造C对象!"<<endl;

           }

           ~DerivedClass()

           {

                  cout<<"析构C对象!"<<endl;

           }

    };

    void main()

    {

           DerivedClassd;

          

    }

    结果:

    构造A对象!

    构造C对象!

    构造C对象!

    构造A对象!

    // kaoshi.cpp : 定义控制台应用程序的入口点。

    //2011-1-3

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    class A

    {

    public:

           A()

           {           

           }

           virtualvoid f()

           {

                  cout<<"DestructorA"<<endl;

           }

           ~A()

           {

                  f();

           }

    };

    class B:public A

    {

    public:

           B()

           {           

           }

           virtualvoid f()

           {

                  cout<<"DestructorB"<<endl;

           }

           ~B()

           {

                  f();

           }

    };

    void main()

    {

           Bb;

           Aa=b;

    }

    结果:

    Destructor A

    Destructor B

    Destructor A

    // kaoshi.cpp : 定义控制台应用程序的入口点。

    //2011-1-4

    #include "stdafx.h"

    #include <iostream>

    #include <string>

    using namespace std;

    class sample

    {

    public:

           inti,j;

           sample(inta=0,int b=0)

           {

                  i=a;

                  j=b;

                  cout<<"constructor1"<<endl;

           }

           sample(constsample& H)

           {

                  i=H.i;j=H.j;

                  cout<<"constructor2"<<endl;

           }

           voidplus(sample H )

           {

                  i+=H.i;j+=H.j;

           }

           voiddisplay( )

           {

                  cout<<i<<""<<j<<endl;

           }

    };

    int main()

    {

           samples1(3,3),s2(4,4),s3(5,5);

           s2.display();

           s1.plus(s3);

           s1.display();

           return0;

    }

    结果:

    constructor1

    constructor1

    constructor1

    4 4

    constructor2

    8 8

    // kaoshi.cpp : 定义控制台应用程序的入口点。

    //2011-1-5

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    void f1()

    {

           staticint x=0;

           x++;

           cout<<"Duringcall to f1,x="<<x<<endl;

    }

    int main()

    {

           intx=10;

           cout<<"Initially,x="<<x<<endl;

           f1();

           f1();

           cout<<"Atthe end,x="<<x<<endl;

           return0;

    }

    结果:

    Initially,x=10

    During call to f1,x=1

    During call to f1,x=2

    At the end,x=10

    二,修改程序错误   5*6=30分

    // kaoshi.cpp : Defines the entry point forthe console application.

    //2011-2-1

    #include <iostream.h>

    class Tany

    {

           intx,y;

    public:

           Tany(inta,int b)  //Tany(T a,T b){x=a,y=b.}

           {

                  x=a,y=b;

           }

           intTsum()  //Tsum(){return x+y.}

           {

                  returnx+y;

           }

    };

    void main()

    {

           Tanyp(1,2);

          

           cout<<p.Tsum()<<endl;  //cout<<p.Tsum<<endl;

    }

    结果:

    3

    // kaoshi.cpp : Defines the entry point forthe console application.

    //2011-2-2

    #include <iostream.h>

    class A

    {

    public:

           voidfun()

           {

                  cout<<"a.fun"<<endl;

           }

    };

    class B

    {

    public:

           voidfun()

           {

                  cout<<"b.fun"<<endl;

           }

           voidgun()

           {

                  cout<<"b.gun"<<endl;

           }

    };

    class C:public A,public B

    {

    private:

           intb;

    public:

           voidgun()

           {

                  cout<<"c.gun"<<endl;

           }

           voidhun()

           {

                  A::fun();  //fun();

           }

    };

    void main()

    {

           Ccc;

           cc.gun();

           cc.A::fun();  //cc.fun();

    }

    结果:

    c.gun

    a.fun

    // kaoshi.cpp : Defines the entry point forthe console application.

    //2011-2-3

    #include <iostream.h>

    class A

    {

    public:

           A(intx=0,int y=0)  //A(int x=0,inty=0):a=x,b=y, {}

           {

                  a=x;

                  b=y;

           }

           voidshow()

           {

                  cout<<a<<""<<b<<endl;

           }

    private:

           inta,b;

    };

    int main(void)

    {

           Aobj(18);

           obj.show();

           return0;

    }

    结果:

    180

    // kaoshi.cpp : Defines the entry point forthe console application.

    //2011-2-4

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    class CComplex

    {

    public:

           doublem_real;

           doublem_image;

    public:

           ~CComplex()

           {

          

           }

           voidsetValue(double r=0,double i=0)

           {

                  m_real=r;

                  m_image=i;

           }

           doublegetreal()

           {

                  returnm_real;

           }

           doublegetimage()

           {

                  returnthis->m_image;  //return this.m_image;

           }

           voidshow()

           {

                  cout<<m_real<<"+"<<m_image<<"*i"<<endl;

           }

    };

    void main()

    {

           CComplexcl;

           cl.setValue(2.5,7.5);

           cl.show();  //null 无此句

           cout<<cl.m_image<<endl;

    }

    结果:

    2.5+7.5*i

    7.5

    // kaoshi.cpp : Defines the entry point forthe console application.

    //2011-2-5

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    void main()

    {

           int*a;

           int*&p=a;

           intb=20;

           *p=b;  //p=&b;

           cout<<*a;

    }

    结果:

    20

    三,程序题:75分

    1  45分

    请按如下要求编写程序

    要求:依次从键盘上输入20个无序整数并放入到数组中,删除数组中重复元素,对数组进行递增排序后输出。

    #include <iostream.h>

    #define N 20

    void paixu(int a[N])

    {

           inti,j,d;

           for(i=0;i<N;i++)

           {

                  for(j= i + 1;j<N;j++)

                         if(a[i]>a[j])

                         {

                                d=a[i];a[i]=a[j];a[j]=d;}

           }

           cout<<"将数组递增排序后:"<<endl;

           for(i=0;i<N;i++)

                  cout<<a[i];

           cout<<endl;

    }

    void shanchu(int a[N])

    {

       int i = 0, j;

       for (j = 1; j < N; j ++)

           if (a[j] != a[j - 1])

           {

              i ++;

              a[i] = a[j];

           }

           cout<<"删除相同元素后的数组是:"<<endl;

           for(j=0;j<=i;j++)

           {

                  cout<<a[j];

           }

           cout<<endl;

    }

    void main()

    {

           inta[N];

           inti;

           for( i = 0; i < N; i++ )         /* 输入N个原始数据 */

           {

                  cout<<"请输入一个整数:" ;

                  cin>>a[i];

           }           

           cout<<"您刚输入的数组是:"<<endl;

           for(i=0;i<N;i++)

           {

                  cout<<a[i];

           }

           cout<<endl;

           paixu(a);

           shanchu(a);

    }

    2  30分

    请设计一个矩形类(Rectangle),包含长和宽两个私有数据成员。要求重载运算符“>”,以实现比较两个矩形对象面积的大小。请编写类,使得如下main函数可以正常运行。

    #include <iostream>

    using namespace std;

    int main()

    {

    Rectangle r1(5,6),r2(4,5);

    cout<<"r1面积=";

    r1.area();

    cout<<endl;

    cout<<"r2面积=";

    r2.area();

    cout<<endl;

    if(r1>r2)

    cout<<"r1>r2"<<endl;

    else

    cout<<"r1<r2"<<endl;

    }

    输出结果:

    r1面积=30

    r2面积=20

    r1>r2

    // kaoshi.cpp : Defines the entry point forthe console application.

    //2011-3-2

    #include <iostream>

    using namespace std;

    class Rectangle 

    {

    public:

     Rectangle(int length, int width)

     {

     nLength = length;

     nWidth = width;

     }

     voidarea()

     {

     cout<<("%d",nLength * nWidth);

     }

     booloperator >(Rectangle r) {

     if((nLength * nWidth) > (r.nLength *r.nWidth))

     return true;

     else

     return false;

     }

    private:

     intnLength;

     intnWidth;

    };

    int main()

    {

    Rectangle r1(5,6),r2(4,5);

    cout<<"r1面积=";

    r1.area();

    cout<<endl;

    cout<<"r2面积=";

    r2.area();

    cout<<endl;

    if(r1>r2)

    cout<<"r1>r2"<<endl;

    else

    cout<<"r1<r2"<<endl;

    }


     

    undoner

    LSOFT.CN (琅软中国) - 创造玉石般的软件,完美的用户体验!


  • 相关阅读:
    收藏的日历js算法 很实用
    autofac system.core 的版本问题
    and 组件ui等
    vc相关
    live传264流
    录转rtsphan
    ndk errno
    cpp all记录
    and 录音等+live等
    cmake
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3026101.html
Copyright © 2011-2022 走看看