您的位置:首页 > 理论基础

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

2013-04-17 09:58 253 查看

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

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



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

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

//2010-1-1



#include "stdafx.h"

#include <iostream>



using namespace std;



class Sample

{

inti;

doubled;

public:

voidsetdata(int n){i=n;}

voidsetdata(double x){d=x;}

voiddisp(){ cout<<i<<endl;cout<<d;cout<<endl;}



};



void main()

{

Samples;

s.setdata(10);

s.setdata(15.6);

s.disp();



}

结果:

10

15.6



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

//2010-1-2

#include "stdafx.h"

#include <iostream>

using namespace std;

int i=0;

int x=0;

class Sample

{



public:

Sample(){}

Sample(inta ){x=a;}

Sample(Sample&a){i++;}

voiddisp(){cout<<++i;}



};

void fun(Sample &s1,Sample &s2)

{

s1.disp();s2.disp();

return;

}

Sample fun(Sample s)

{

s.disp();

returns;

}



void main()

{

Samples1(2),s2=s1;

fun(s1,s2);

s1.disp();

s2.disp();



}

结果;

2345



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

//2010-1-3

#include "stdafx.h"

#include <iostream>

using namespace std;

class CMyExcept

{

voidwriteout()

{

cout<<"XXX\n";

}

};



class CExcept0 : public CMyExcept



{ public:

voidwriteout()

{

cout<<"AAA\n";

}

};

class CExcept1 : public CExcept0



{ public:

voidwriteout()

{

cout<<"BBB\n";

}

};

class CExcept2 : public CExcept1



{ public:

voidwriteout()

{

cout<<"CCC\n";

}

};



void main()

{

charch;

try{

cin>>ch;

switch(ch){

case'0':throwCExcept0();

case'1':throwCExcept1();

case'2':throwCExcept2();

}

}

catch(CExcept0)

{

cout<<'A'<<endl;

CExcept0 a;

a.writeout();

}

catch(CExcept1)

{

cout<<'B'<<endl;

CExcept1b;

b.writeout();

}

catch(CExcept2)

{

cout<<'C'<<endl;

CExcept2 c;

c.writeout();

}

}



结果:

2

A

AAA



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

//2010-1-4

#include "stdafx.h"

#include <iostream>

using namespace std;

void func();

int n=1;



void main()

{

staticint a;

intb=-10;

printf("a=%d,b=%d,n=%d",a,b,n);

b+=4;

func();

printf("a=%d,b=%d,n=%d",a,b,n);

n+=10;

func();

}

void func()

{

staticint a=2;

intb=5;

a+=2;

b+=5;

n+=12;

printf("a=%d,b=%d,n=%d\n",a,b,n);

}



结果:

a=0,b=-10,n=1a=4,b=10,n=13

a=0,b=-6,n=13a=6,b=10,n=35



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

//2010-1-5

#include "stdafx.h"

#include <iostream>

using namespace std;

class Sample

{

public:

Sample(){cout<<(x=2);};

Sample(inti,int j){cout<<(x=i+j);};

~Sample(){cout<<++x;};

private:

intx;

};



void main()

{

Sample*p1=new Sample[3];

delete[]p1;

}



结果:

222333



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

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

//2010-2-1

#include <iostream.h>

void main()

{

intx=5,y=6;

int*p=&x; //const int *p=&x;

*p=y;

cout<<*p<<endl;

}

结果:

6



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

//2010-2-2

#include <stdio.h>

int main()

{

floatx,max,min;int i;

for(i=1;i<=3;i++)

{

scanf("%f",&x);

}

if(i=1) {max=x;min=x;}

if(x>max) max=x;

if(x<min) min=x;



printf("%f,%f\n",max,min);

return0;



}



修改:

for(i=1;i<=3;i++)

{

scanf("%f",&x);

if(i==1) {max=x;min=x;}

if(x>max) max=x;

if(x<min) min=x;

}





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

//2010-2-3

#include <iostream.h>



class f

{

private:

intx,y; //int x=0,y=0;

public:

voidf1(int a,int b){x=a;y=b;}

voidget(){cout<<x<<y<<endl;}

};



void main()

{

fa;

a.f1(1,3);

a.get();

}



结果

13



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

//2010-2-4

#include <iostream.h>

class f

{

public: //private:

doublex,y;

public:

voidf1(double a,double b){x=a,y=b;}

doublemax(){return(x>y)?x:y;}

};



void main()

{

fa;

a.f1(1.5,3.8);

cout<<a.x<<a.y<<a.max()<<endl;

}



结果

1.53.83.8





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

//2010-2-5

#include <iostream.h>

#include <stdio.h> //null

#define n 3 //null

void print_a(int c
)

{

inti=0; //注释掉不要此句

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

{

printf("c[%d]=%d\n",i,c[i]);

//printf("c[%d]=%d\n",i,c);

}

}



int main()

{

inta[3]={1,2,3};

print_a(a);//print_a(a[3]);

}



结果:

c[0]=1

c[1]=2

c[2]=3





三,编程题: 75分

1 30分

建立两个磁盘文件f1.dat和f2.dat,试编程序实现以下工作:

a)从键盘键入20个不同的整数,奇数放入f1.dat,偶数放入f2.dat;

b)分别读入f1.dat和f2.dat中的奇数,对这20个整数按递增方式排序后,依次写入f1.dat中,实现f1.dat中的数据的更新。

#include <fstream>

#include <iostream>

using namespace std;

void save_to_file( )// save_to_file函数从键盘读入一行字符,并将其中的字母存入磁盘文件

{

ofstreamoutfile1("f1.dat"); //定义输出文件流对象outfile,以输出方式打开磁盘文件f1.dat

ofstreamoutfile2("f2.dat"); //定义输出文件流对象outfile,以输出方式打开磁盘文件f2.dat

if(!outfile1)

{

cerr<<"openf1.dat error!"<<endl;

exit(1);

}

if(!outfile2)

{

cerr<<"openf2.dat error!"<<endl;

exit(1);

}

charc[80];

cout<<"请任意输入一些数字:";

cin.getline(c,80); //从键盘读入一行字符

cout<<"您刚才输入的数字是:";

for(inti=0;c[i]!=0;i++) //对字符逐个处理,直到遇′/0′为止

if(c[i]>=49&& c[i]<=58) //如果是数字字符

{

if(c[i]%2==0)

{

outfile2.put(c[i]); //将偶数存入磁盘文件f2.dat

cout<<c[i];

}

else

{

outfile1.put(c[i]); //将奇数存入磁盘文件f2.dat

cout<<c[i];

}



} //同时送显示器显示

cout<<endl;

outfile1.close(); //关闭f1.dat

outfile2.close(); //关闭f2.dat

}



void get_from_file()//从f1.dat,f2.dat读入所有数字,按递增排序后,再存入f1.dat .

{

charch,m=0,c[80];

ifstreaminfile_f1("f1.dat",ios::in);

ifstreaminfile_f2("f2.dat",ios::in); //定义输入文件流infile,以输入方式打开磁盘文件f1.dat,f2.dat

ofstreamoutfile_f3; outfile_f3.open("f3.dat",ios::app);//定义输出文件流outfile,以追加输出方式打开磁盘文件f3.dat

ifstreaminfile_f3("f3.dat",ios::in);



if(!infile_f1)

{

cerr<<"openf1.dat error!"<<endl;

exit(1);

}

if(!infile_f2)

{

cerr<<"openf2.dat error!"<<endl;

exit(1);

}

if(!infile_f3)

{

cerr<<"openf3.dat error!"<<endl;

exit(1);

}

if(!outfile_f3)

{

cerr<<"openf3.dat error!"<<endl;

exit(1);

}

cout<<"奇数:";

while(infile_f1.get(ch)) //当读取字符成功时执行下面的复合语句

{

outfile_f3.put(ch); //将奇数存入磁盘文件f3.dat

cout<<ch; //同时在显示器输出

}

cout<<endl;

infile_f1.close(); //关闭磁盘文件f1.dat

cout<<"偶数:";

while(infile_f2.get(ch)) //当读取字符成功时执行下面的复合语句

{

outfile_f3.put(ch); //将偶数存入磁盘文件f3.dat

cout<<ch; //同时在显示器输出

}

cout<<endl;

infile_f2.close(); //关闭磁盘文件f2.dat

outfile_f3.close(); //关闭磁盘文件f3.dat



while(infile_f3.get(ch))//当读取字符成功时执行下面的复合语句

{

c[m]=ch;

m++;

}

infile_f3.close(); //关闭磁盘文件f3.dat

ofstreamoutfile_f1("f1.dat");

if(!outfile_f1)

{

cerr<<"openf1.dat error!"<<endl;

exit(1);

}



inti,j,d;

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

{

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

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

{

d=c[i];c[i]=c[j];c[j]=d;

}

}



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

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

{

cout<<c[i];

outfile_f1.put(c[i]); //将排序数存入磁盘文件f1.dat

}

cout<<endl;

outfile_f1.close();//关闭磁盘文件f1.dat

}

int main( )

{

save_to_file(); //调用save_to_file( ),从键盘读入一行数字并将其中的奇数放入磁盘文件f1.dat,偶数存入磁盘文件f2.dat

get_from_file(); //调用get_from_file(),从f1.dat,f2.dat读入所有数字,按递增排序后,再存入f1.dat .

return0;

}



2 45分

声明和实现一个时间类time24:

a)实现获得和设置由时,分组成的24小时制时间的方法;

b)编写程序实现“+”运算,即实现time24对象与整型分钟数相加的运算,要求该运算返回一个time24对象;

c)实现time24类的等价运算符重载,即比较两个time24对象是否相等;

d)实现输入和输出重载。

要求该类具有构造函数,并适当使用友元函数和运算符重载方法。

#include <iostream>

class time24

{

public:

time24()

:_hour(0), _min(0)

{

}

time24(inthour, int min)

:_hour(0), _min(0)

{

set(hour, min);

}

~time24()

{

}

voidset(int hour, int min)

{

_hour = hour;

_min = min;

}

voidadd_min(int min)

{

int dh = (_min + min) / 60;

_hour = (_hour + dh) % 24;

_min = (_min + min) % 60;

}

inthour() const

{

return _hour;

}

intmin() const

{

return _min;

}

private:

int _hour;

int _min;

friend bool operator==(time24 const&, time24 const&);

};

time24&operator+(time24& t, int min)

{

t.add_min(min);

return t;

}

time24&operator+(int min, time24& t)

{

return operator+(t, min);

}

booloperator==(time24 const& lhs, time24 const& rhs)

{

return lhs._hour == rhs._hour

&& lhs._min == rhs._min;

}

std::ostream&operator<<(std::ostream& os, time24 const& t)

{

os << t.hour() << ":" << t.min();

return os;

}

std::istream&operator>>(std::istream& is, time24& t)

{

char c;

int h, m;

is >> h >> c >> m;

t.set( h, m );

return is;

}

int main()

{

time24 a, b;

//使用重载的>>输入时间,格式为: 小时:分钟

std::cout << "请输入时间a(格式:23:59): ";

std::cin >> a;

std::cout << "请输入时间b(格式:23:59): ";

std::cin >> b;

//使用重载<<输出a,b两个时间值

std::cout << "您输入的时间 a 是: " << a <<std::endl;

std::cout << "您输入的时间 b 是: " << b <<std::endl; //使用operator==判断输入的两个时间是否相等

if( a == b )

std::cout << "时间 a 等于 时间 b " << std::endl;

else

std::cout << "时间 a 不等于 时间 b " << std::endl;

//输入一个整形值,用于测试对时间的加运算符

int addMin;

std::cout << std::endl <<"请输入一个分钟数:";

std::cin >> addMin;

a = a + addMin;

b = b + addMin;

std::cout << "时间a: " << " +" << addMin << " 分钟 = " << a << std::endl;

std::cout << "时间b: " << " +" << addMin << " 分钟 = " << b << std::endl;

return 0;

}

undoner
( )-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: