2012年 浙工大考研计算机专业课试题C++(专硕)

时间:2022-01-09 15:00:36

2012年 浙工大考研计算机专业课试题C++(专硕)

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

 

1读程序写结果(5*9=45)

1-1

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

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

using namespace std;

int a[8]={1,2,3,4,5,6,7};

void fun(int *pa,int n);

int main()

{

       intm=8;

       fun(a,m);

       cout<<a[7]<<endl;

       return0;

}

void fun (int *pa,int n)

{

       for(intj=0;j<n-1;j++)

              *(pa+7)+=*(pa+j);

}

结果

28

 

 

1-2

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

using namespace std;

class A

{

public:

       A(inti,int j)

       {

              a=i;b=j;

       }

       voidMove(int x,int y)

       {

              a+=x;b+=y;

       }

       voidShow()

       {

              cout<<"("<<a<<","<<b<<")"<<endl;

       }

private:

       inta,b;

};

class B:public A

{

public:

       B(inti,int j,int k,int l):A(i,j),x(k),y(l){}

       voidShow()

       {

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

       }

       voidF1()

       {

              Move(3,5);

              A::Show();

       }

private:

       intx,y;

};

 

void main()

{

       Aa(1,2);

       a.Show();

       Bb(3,4,5,6);

       b.A::Show();

       b.B::Show();

       b.F1();

}

结果

(1,2)

(3,4)

5,6

(6,9)

 

1-3

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include "math.h"

using namespace std;

class Point

{

private:

       doubleX,Y;

public:

       Point(doublexx=0,double yy=0)

       {

              X=xx;Y=yy;

              cout<<"Point("<<X<<","<<Y<<")"<<endl;

       }

       Point(Point&p)

       {

              X=p.X;

              Y=p.Y;

              cout<<"Pointis copied."<<endl;

       }

       doubleDistance (Point &p);        

};

double Point::Distance(Point &p)

{

       doubledx,dy;

       dx=X-p.X;

       dy=Y-p.Y;

       returnsqrt(dx*dx+dy*dy);

}

Point f(double x,double y)

{

       Pointp(x,y);

       returnp;

}

void main()

{

       PointA(0,0);

       Point&B=f(3,4);

       cout<<"Distanceis"<<A.Distance(B)<<endl;

}

结果

Point(0,0)

Point(3,4)

Distance is5

 

1-4

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include "math.h"

using namespace std;

class A

{

public:

       A(){}

       A(constA &){cout<<"A::A(const A&)"<<endl;}

       A& operator = (const A &)

       {

              cout<<"A::operator="<<endl;

              return*this;

       }

};

int main()

{

       Aa;

       Ab=a;

       b=b;

       return0;

}

结果:

A::A(const A&)

A::operator=

 

 

1-5

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include "math.h"

using namespace std;

class Sample

{

       intA;

       staticint B;

public:

       Sample(inta)

       {A=a,B+=a;}

       staticvoid func(Sample& s);

};

int Sample::B=0;

void Sample::func(Sample& s)

{

       cout<<"A="<<s.A<<",B="<<B<<endl;

}

int main()

{

       Samples1(2),s2(7);

       Sample::func(s1);

       Sample::func(s2);

}

A=2,B=9

A=7,B=9

 

2改错(5*6=30分)

2-1

#include <iostream>

using namespace std;

f(int *a[],int *b[],int n)

// int f(double a[],double b[],int n)

{

int s=0;

for(int i=0;i<n;i++)

s+=a[i]*b[i];

return s;

}

void main()

{

doublec[4]={1.1,2.2,3.3,4.4},d[4]={10,0,100,0};

cout<<f(c,d,4)<<endl;

}

结果:

341

 

2-2

#include <iostream>

using namespace std;

class AA

{

public:

AA(int i,int j)

{

A=i;B=j;

cout<<"Constructor.\n";

}

~AA()

{

cout<<"Destructor.\n";

}

void Print();

private:

int A=0,B=0;

}

void AA::Print()

{

cout<<A<<","<<B<<endl;

}

void main()

{

AA *a1,*a2;

a1=new AA(1,2);

a2=new AA(3,4);

a1.Print();  // a1->Print();

a2.Print();  // a2->Print();

delete a1;

delete a2;

}

结果:

Constructor.

Constructor

1,2

3,4

Destructor

Destructor

 

2-3

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

void f(char *p)  // void f(char p)

{

//p++;本身就是要操作当前指针,在操作前执行p++,让指针指向下一个char字符,那么当前的字符就被跳过了。

char &c=*p;

c+='A'-'a';

}

void main()

{

char str[]="abcde";

//f(str+3); for循环中,终止条件改为i < sizeof(str) - 1
sizeof(str)在这里面是6,因为还包含了结尾的'\0',所以要-1,否则会造成数组越界,结果就是显示乱码。

for(int i=0;i<sizeof(str)-1;i++)

{

       f(str+i);

}

cout<<str<<endl;

//return 0;  

}

结果:

ABCDE

 

2-4

#include <iostream>

using namespace std;

class local

{     intx,y;

       intprintY(cout<<y;)  //删掉

public:

       voidinit(){x=0;y=1}

// 改为:void init() {x=0;y=1;}

       intprintX(){cout<<x;}

//改为:

void printX(){cout<<x;}

void printY(){cout<<y;}

}

void main(void)

{

local a,b,c;

a.init();

a.printX();

b.printY();// a.printY();

}

结果:

01

原结果b.printY();:

01753358978

 

2-5

#include <iostream>

using namespace std;

class Base

{

public:

       Base(){}

       Base(intc):count(c){}

       virtualvoid print() const=0;

private:

       intcount;

}

class Derived:public Base

{

public:

       Derived(){}

       Derived(intc):Base(c){}

       voidprint() const{cout<<"Derived"<<endl;}

};

void main( )

{

Derived d(10);

Base dd(1);

Base *pb;

pb=&d;

pb->print();

Base &cb=d;

Derived ddd=*pb;

}

 

3,编程题(30+45=75分)

3-1

设计一个时间类time,包括三个数据成员,时(h),分(m),秒(s),另外包括存取各个数据成员和设置时间的成员函数,按12小时输出时间设计成员函数disp12,以及默认构造函数,默认时间为0时0分0秒。运行结果按如下格式输出。

 

main函数部分代码和运行输出如下所示:

int main()

{

Time t1(13,45,12),t2(9,30,50);

t1.disp12();

t2.disp12();

}

运行结果为:

01:45:12PM

09:30:50AM

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

class Time

{

public:

       inth,m,s,dx;

       stringstr;

       Time():h(00),m(00),s(00)

       {    

       }

       Time(inth1,int m1,int s1)

       {

              getTime(h1,m1,s1);

              setTime(h1,m1,s1);

              voiddisp12();

       }

       voidgetTime(int h1,int m1,int s1)

       {

              h=h1;m=m1;s=s1;

       }

       intsetTime(int h1,int m1,int s1)

       {

              dx=h%12;

              if(h>=12)

              {

                     str="PM";

              }

              else

              {

                     str="AM";

              }

              returndx;

       }

       voiddisp12()

       {                          cout<<setw(2)<<setfill('0')<<dx<<":"<<m<<":"<<s<<str<<endl;

       }

};

void main()

{

Time t1(13,45,12),t2(9,30,50);

t1.disp12();

t2.disp12();

}

 

3-2

编写一个按照平均分对学生进行排名的程序。

输入数据放在名为abc.txt的文件中,学生数量<=100。输出时按学生成绩平均分递减排好序的学生序号和名字,每个学生占一行。输入和输出格式如下:

输入格式:

<学生个数> <课程数目>

<学生姓名> <课程1成绩> <课程2成绩> ...<课程n成绩>...

输出格式:

<名次> <学生姓名>

...

例如输入:

2 3

smith 60 50 80

frank 80 60 70

输出

1 frank

2 smith

 

我用STL给你写了个。
如果你不想用C++里面的任何东西,
就把类变成结构体 struct Student,
vector变成数组 Struct Student *students,
string变成char*
ifstream 变成 File*

 

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <string>

#include <fstream>

#include <iomanip>

#include <vector>

#include <algorithm>

using namespace std;

 

class Student

{

public:

   string name;

   double avg;   

};

 

vector<Student > students;  //存放学生的信息

 

void Input()

{

   ifstream in("c:\\abc.txt");        //文件可以用绝对地址

   if( !in )                    //如果找不到文件,就提醒

    {

       cout << "Can not find file abc.txt" <<endl;

       return;

    }

   int numStu, numCourse;

   in >> numStu >> numCourse;

   for( int i=0; i<numStu; i++)

    {

       Student stu;

       in >> stu.name;

       double sum=0;

       for( int j=0; j<numCourse; j++ )

       {

           double score;

           in >> score;

           sum += score;

       }

       stu.avg = sum/numCourse;

       students.push_back( stu );        //存入数据

    }

}

 

bool fun( Student a, Student b )

{

   return a.avg>b.avg;

}

 

void Sort()

{

   sort(students.begin(), students.end(), fun );     //使用STL algorithm,也可以使用下面自己写的

 

   /*

   //选择排序

   int numStu = students.size();

   for( int i=0; i<numStu; i++)

    {

       int max = i;

       //找到最大的,已平均值为关键字比较

       for( int j=i; j<numStu; j++)

       {

           if( students[j].avg > students[i].avg )

                max=j;

       }

       if( max != i )      //交换

       {

           Student temp;

           temp = students[i];

           students[i] = students[max];

           students[max] = temp;

       }

    }

   */

 

}

 

void Output()

{

   ofstream out("c:\\result.txt");

   for( int i=0; i<(int)students.size(); i++)

    {

       out << i+1 << ""<<students[i].name<<endl;

    }

}

 

int main()

{

   Input();

   Sort();

Output();

}


undoner

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