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

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

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

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

1-1

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

//

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

using namespace std;

void other();

int main()

{

       intx=0,y=3;

       other();

       cout<<x<<""<<"\n";

       other();

}

void other()

{

       staticint x=2;

       inty=6;

       x+=2;

       cout<<x<<""<<y<<"\n";

}

结果

4 6

6 6

1-2

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

using namespace std;

void other();

int main()

{

       inta[]={2,4,6,8,10};

       inty=1,x,*p;

       p=&a[1];

       for(x=0;x<3;x++)

              y+=*(p+x);

       cout<<y<<endl;

}

结果

19

1-3

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

using namespace std;

int s=10;

int func(int a)

{

       intc;

       c=a+s;

       returnc;

}

int main()

{

       intx=9,y=9,t;

       t=func((x+y,x--,y++));

       cout<<t<<endl;

}

结果

19

1-4

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

using namespace std;

class Sample

{

       intx;

public:

       Sample()

       {

              x=0;

       }

       Sample(inta)

       {

              cout<<(x=a);

       }

       ~Sample()

       {

              cout<<++x;

       }

       voiddisp()

       {

              cout<<x;

       }

};

void main()

{

       Samples1(2);

       s1.disp();

       s1.~Sample();

       system("pause");

}

结果:

2234

1-5

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

using namespace std;

class Class

{

       intx,y;

public:

       Class(intn=6,int m=7);

       ~Class();

       voidSet(int n,int m)

       {

              x=n;y=m;

       }

};

Class::Class(int n,int m)

{

       Set(n,m);

       cout<<"Construct:x="<<x<<",y="<<y<<"\n";

}

Class::~Class()

{

       cout<<"Destruct:x="<<x<<",y="<<y<<"\n";

}

int main()

{

       Classa(5);

       Classb=Class(1,3);

       cout<<"exiting...\n";

       system("pause");

}

结果

Construct:x=5,y=7

Construct:x=1,y=3

exiting...

Destruct:x=1,y=3

Destruct:x=5,y=7

2改错(3*10=30分)

2-1

下述程序按公式:1/k(k=1;k<=10;k++)求累加和。

main()

{

int k; //改为 doublek;   double s;

for(k=1;k<=10;k++)

s+=1/k;

cout<<"sum"<<s;//希望输出浮点数

}

结果:

sum2.92897

2-2

下列程序的功能是从键盘上输入若干个字符(以回车键为结束)组成一个字符串存入一个字符数组,然后输出该数组中的字符串,请补充全程序画线的部分。

#include <iostream>

using namespace std;

int main()

{

char str[81],*ptr;

int i;

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

{

str[i]=getchar();

if(str[i]=='\n')break;

}

str[i]=________;  //str[i]='\0';

ptr=str;

while(*ptr) putchar(________);//putchar(*ptr++);

}

2-3

指出下面程序中有错误的地方,解释错误原因并改正。

#include <iostream>

using namespace std;

class A

{

int i,j;

public:

void get();

}

class B:public A

{

int k;

public:

void make();

};

void b::make()

{

k=i*j;

}

改正:

class A

{

public:

       inti,j;

       voidget();

};

class B:public A

{

public:

       intk;

       intmake(int m,int n);

};

int B::make(int m,int n)

{

       i=m,j=n;

       k=i*j;

       returnk;

};

void main()

{

       Bb;

       cout<<b.make(1,2)<<endl;

}

3,编程题(3*25=75分)

3-1

按要求输出一些斜纹布似的字符方块。斜纹布是指每一行的每一个字符出现在下一行的最后一个字符上,而第二个字符出现在下一行的第一个字符上,而第三个字符出现在下一行的第二个字符上,等等。例如,输入为“abcd”,则输出为

abcd

bcda

cdab

dabc

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

void main()

{

       stringstr;

       strings2;

       cout<<"请输入一些字符";

       cin>>str;

       intnum=str.size();

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

       {

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

              {

                     inti=(n%num)+j;

                     s2+=str[i>(num-1)?i-num:i];

              }

              printf(s2.c_str());

              printf("\n");

              s2="";

       }

}

或者:

int main()

{

      

   string t;

       cout<<"请输入一些字符";

       cin>>t;

   for(string::size_type i = 0; i < t.size(); ++i)

    {

       cout<<t<<endl;

       t = t.substr(1, t.size() - 1) + t[0];       

    }

   return 0; 

}

3-2

编写程序,从键盘输入一行字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <fstream>

using namespace std;

#define N 50

void main(){

 chars[N];

 inti=0;

 printf("输入字符串:\n");

 scanf("%s",&s);

 while(s[i]!='\0')

 {

 if(s[i]>='a'&&s[i]<='z')

 s[i]-=32;

 i++;

 };

ofstream fs("c:\\test");

 fs<<s;

 fs.close();

}

3-3

银行允许开设多种不同类型的账户,并且针对取款交易,制定了不同的手续费规则,客户可以支付一定的费用,从一个账户取款,并将款项转账到另一个账户,请编写一个程序,为一个银行账户定义一个基类Account,并定义两个派生类Saving和Checking,分别代表储蓄和支票。为提高软件的可重用性,所以决定采用抽象类的方法来编写。再写一个主程序来创建2种账户,然后测试转账函数。

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

class Account//账户

{

public:

       doublefeeTotal;//总资金

       doublefeeLast;//剩余资金

       doublezMon;//转账金额

       stringname;//账户名

       virtualdouble ZhuanZhang(double Mon)=0;//转账纯虚函数,子类必须实现它

};

class Saving:Account//储蓄

{

public:

       doublefeeSav;

       doublerate;//转账费率

       Saving(stringstr,double fee)

       {

              name=str;

              feeTotal=fee;

       }

        double ZhuanZhang(double Mon)

        {

               rate=0.1;

               zMon=Mon;

               feeSav=zMon*rate;

               feeLast=feeTotal-feeSav;

               return feeLast;

        }

};

class Checking:Account//支票

{

public:

       doublefeeAcc;

       doublerate;//转账费率

       Checking(stringstr,double fee)

       {

              name=str;

              feeTotal=fee;

       }

        double ZhuanZhang(double Mon)

        {   

               rate=0.15;

               zMon=Mon;

               feeAcc=zMon*rate;

               feeLast=feeTotal-feeAcc;

               return feeLast;

        }

};

void main()

{

       stringstr1, str2;

       doublet1,t2;

       doublez1,z2;

       cout<<"输入储蓄账户名:";

       cin>>str1;

       cout<<"存入储蓄账户金额为:";

       cin>>t1;

       cout<<"输入转账金额为:";

       cin>>z1;

       cout<<endl;

       cout<<"输入支票账户名:";

       cin>>str2;

       cout<<"存入支票账户金额为:";

       cin>>t2;

       cout<<"输入转账金额为:";

       cin>>z2;

       Savingsav(str1,t1);

       Checkingche(str2,t2);

       cout<<endl;

       cout<<"储蓄账户:"<<str1<<",转账100后,账户余额:"<<sav.ZhuanZhang(z1)<<endl;

       cout<<"支票账户:"<<str2<<",转账100后,账户余额:"<<che.ZhuanZhang(z2)<<endl;

}


undoner

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3026005.html