2020春C++考试答案

一、给出程序的运行结果(本题 20 分,每小题 5 分)

1、数组

#include <iostream>
using namespace std;
class test
{
private:
    int a[10];
public:
    test()
    {
        int i;
        for(i=0; i<10; ++i)
        {
            a[i]=i*10;
        }
    }
    int turn()
    {
        int temp=a[0],i;
        for(i=0; i<10; ++i)
        {
            if(temp<a[i])
                temp=a[i];
        }
        return temp;
    }
};
int main()
{
    test t;
    cout<<t.turn()<<endl;
    return 0;
}
//90

2、判断 2020 年某月的天数

#include <iostream>
using namespace std;
class test
{
private:
    int m;
public:
    test()
    {
        m=1;
    }
    test(int mm)
    {
        m=mm;
    }
    void input()
    {
        cout<<"input:";
        cin>>m;
    }
    int output()
    {
        if(m==2)
            return 29;
        else if(m==4||m==6||m==9||m==11)
            return 30;
        else
            return 31;
    }
};
int main()
{
    test t(7);
    cout<<t.output()<<endl;
    return 0;
}
//31

3、找零钱的问题

#include <iostream>
#include <cstdlib>
using namespace std;
class test
{
private:
    int n;
public:
    test()
    {
        n=23;
    }
    test(int nn)
    {
        n=nn;
    }
    int output()
    {
        int sum=0,i,j;
        i=n/5;
        j=n%5;
        sum=sum+i;
        i=j/2;
        j=j%2;
        sum=sum+i;
        sum=sum+j;
        cout<<"total number:"<<sum<<endl;
    }
};
int main()
{
    test t;
    t.output();
    system("pause");
    return 0;
}
//6

4、类模板

#include <iostream>
#include <typeinfo>
#include <string> 
using namespace std;
template <class T>//模板语句
class test
{
private:
    T n;
public:
    test(T nn)
    {
        n=nn;
    }
    void turn()
    {
        if(typeid(T)==typeid(int))
        {
            cout<<n*n<<endl;
        }
        if(typeid(T)==typeid(string))
        {
            cout<<n+n<<endl;
        }
    }
};
int main()
{
    test <int> t1(8);
    test <string> t2("hello!");
    t1.turn();
    return 0;
}
//64
//模板感觉就是不事先声明参数的类型,让它自己去判断参数类型,更有通用性

二、程序改错(本题 20 分,每小题 5 分)

下列每个程序中只有一处或一个语句错误,请写出 错误的语句和修改后的正确语句(直接写在答题纸上)。

1、二进制文件-倒序

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class File
{
private:
    fstream f;
    string s;
    int len;
public:
    File()
    {
        f.open("test.doc",ios::binary|ios::in|ios::out);
    }
    ~File()
    {
        f.close();
    }
    void Reverse()
    {
        string temp;
        //temp.resize(s.size()); //加上这句就对了
        //string temp(s.size(),' ');//改法2把上面定义改成这句    
        for(int i=0; i<s.size(); ++i)
        {
            temp[i]=s[s.size()-i-1];
        }
        s=temp;
    }
    void Read()
    {
        f.seekg(0,ios::end);
        len=f.tellg();
        s.resize(len);
        f.seekg(0,ios::beg);
        f.read((char*)s.c_str(),len);
    }
    void Write()
    {
        f.seekp(0,ios::beg);
        f.write((char*)s.c_str(),s.size());
    }
};
int main()
{
    File f;
    f.Read();
    f.Reverse();
    f.Write();
    return 0;
}
//见注释,下同

2 、继承与派生

#include <iostream>
using namespace std;
class point
{
protected:
    int x1,y1;
public:
    point()
    {
        x1=0;
        y1=0;
    }
    point(int xx1,int yy1)
    {
        x1=xx1;
        y1=yy1;
    }

    void display()
    {
        cout<<"x1="<<x1<<endl;
        cout<<"y1="<<y1<<endl;
    }
};
class line:public point
{
protected:

    int x2,y2;
public:
    line()
    {
        x2=0;
        y2=0;
    }

    line(int xx2,int yy2)
   //line(int xx2,int yy2,int xx1,int yy1):point(xx1,yy1)//改成这句就对了
    {
        x2=xx2;
        y2=yy2;
    }

    void display()
    {
        cout<<"x1="<<x1<<endl;
        cout<<"y1="<<y1<<endl;
        cout<<"x2="<<x2<<endl;
        cout<<"y2="<<y2<<endl;
    }

};

int main()

{

    line L1(2,3,4,5);
    L1.display();
    return 0;

}

3 、字符串 -取数字子串并在其数值上加 上 100

#include<iostream> #include <sstream>
#include <string>
using namespace std;
class test
{
private:
    string s;
    int pos;
public:
    void input()
    {
        cout<<"input a string:";
        cin>>s;
    } int str2num(string s)
    {
        stringstream ss;
        int temp;
        ss<<s;
        ss>>temp;
        return temp;
    } void output()
    {
        int i=0;
        while(i<s.size())
        {
            if(s[i]>='0'&&s[i]<='9')
            //if(!(s[i]>='0'&&s[i]<='9'))//不是数字就erase,就只剩数字了
            {
                s.erase(i,1);  
                
            }
            else ++i;
        }
        cout<<s<<endl;
        cout<<str2num(s)+100<<endl;
    }
};
int main()
{
    test A;
    A.input();
    A.output();
    system("pause");
    return 0;
}

4、容器-抽奖程序-在一个班级中随机抽取三名同学获奖

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
class test
{
private:
    int n;
    vector<int>a;
    vector<int>::operator p;
    //vector<int>::iterator p;
public:
    test(int nn)
    {
        n=nn;
        srand(time(0));
        for(int i=0; i<n; ++i)
        {
            a.push_back(i+1);
        }
    } 
    void select()
    {
        int i,temp;
        for(i=0; i<3; ++i)
        {
            p=a.begin();
            temp=rand()%a.size();
            p=p+temp;
            cout<<*p<<endl;
            a.erase(p);
        }
    }
};
int main()
{
    test t(30);
    t.select();
    return 0;
}
//迭代器定义错了

三、程序填空(本题 20 分,每空 5 分)

在下列程序每个空白处填上一条语句,使程序完整。 请标注题号后,直接将答案写在答题纸上。

1、动态数组-给不及格的同学加分

#include <iostream>
#include <cstdlib>
using namespace std;
class test
{
private:
    int n;
    int* a;
public:
    test(int nn)
    {
        n=nn;
        //------------------------------------------
          a=new int[5] ;
        //------------------------------------------
    }
    void input()
    {
        int i;
        for(i=0; i<n; ++i)
        {
            cout<<"input "<<i+1<<" :";
            cin>>a[i];
        }
    }
    void output()
    {
        int i;
        cout<<"----------------"<<endl;
        for(i=0; i<n; ++i)
        {
            cout<<"input "<<i+1<<" :"<<a[i]<<endl;
        }
    }
    void add()
    {
        int i;
        for(i=0; i<n; ++i)
        {
            if(a[i]==60)
                a[i]=61;
            else if(a[i]<60)
                a[i]=60;
        }
    }
    ~test()

    {
        delete a;
    }
};
int main()
{
    test t(5);
    t.input();
    t.add();
    t.output();
    system("pause");
    return 0;
}
//加上横线中间那句就对了,下同

2、容器-复合元素类型-成绩管理-输出最高最低分

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
struct student
{
    int number;
    double score;
};
class test
{
private:
    int n;
    vector<student>a;
    vector<student>::iterator p;
public:
    test(int nn)
    {
        n=nn;
        student temp;
        for(int i=0; i<n; ++i)
        {
            cout<<"input number:";
            cin>>temp.number;
            cout<<"input score:";
            cin>>temp.score;
            a.push_back(temp);
        }
    }
    void browse()

    {
        for(p=a.begin(); p!=a.end(); ++p)
        {
            cout<<"number:"<<p->number<<"-"<<p->score<<endl;
        }
    }
    static sort1(student s1,student s2)
    {
        //------------------------------------------
           return s1.score>s2.score;//倒序,从大到小
        //------------------------------------------

    }
    void SORT()
    {
        sort(a.begin(),a.end(),sort1);
    }
    void max_min()
    {
        SORT();
        p=a.begin();
        cout<<"max:"<<p->number<<"-"<<p->score<<endl;
        p=a.end()-1;
        cout<<"min:"<<p->number<<"-"<<p->score<<endl;
    }
};
int main()
{
    test t(3);
    t.max_min();
    return 0;
}

3、文本文件-从键盘输入一个整数并追加到 test.txt 中成为一个新行

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class test
{
private:
    string s;
    int n;
    ofstream f;
public:
    test()
    {

//------------------------------------------
       f.open("test.txt",ios::out|ios::app); //app是追加写
       //f.open("test.txt",ios::out|ios::in); 
       //f.seekp(0,ios::end);//seekp是写指针,移到文件尾
       //只写ios::out会覆盖写(原内容清0),所以写都要加上ios::in
//------------------------------------------
        if(!f)
            cout<<"文件建立错误!"<<endl;
    }
    ~test()
    {
        f.close();
    }
    void turn()
    {
        cout<<"input a number:";
        cin>>n;
        f<<endl<<n;//这句
    }
};
int main()
{
    test t;
    t.turn();
    return 0;
}

4、异常处理-数组越界检查

#include <iostream>
using namespace std;

class test
{
private:
    int n;
    int a[10];
public:
    test(int nn)throw(int)
    {
        n=nn;
        if(n>=10)
        {
            throw 100;//这句
        }
        else
        {
            cout<<"OK!"<<endl;
        }
    }
};


int main()
{
    try
    {
        test(50);
    }
//------------------------------------------
    catch(int error)
//------------------------------------------
    {
        if(error==100)
        {
            cout<<"subscript out of range!"<<endl;
        }
    }
    return 0;
}

四、编程综合题(本题 40 分,每个函数或程序体 10 分)

注意:不允许另外编写自定义函数。 请标注题号后,直接将答案写在答题纸上。

1、某项体育赛事共有 N 个评委打分,满分为 10 分,不允许打负分。给某个选手打分后,去除一个最高分和一个最低分,剩余评委的分数相加为该选手的本轮得分。用类和链表实现该程序,构造函数规定评委人数 N。input 函数输入 N 个评委的打分,output 函数输出某选手的本轮得分。

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
struct score
{
    int number;
    score* next;
};

class test
{
private:
    int n;
    score* head,* end,* p;
public:
    test()
    {
        n=6;
    }


    void input()
    {
        head=new score;
        end=head;
        for(int i=1; i<=n; ++i)
        {
//------------------------------------------
            p=new score;
            cin>>p->number;    //这里注意头指针是空的,里面没数,但尾指针插入以后有数
            end->next=p;
            end=p;
//------------------------------------------
        }
        end->next=0;
    }
    void output()
    {
        int i=0;
        int max=0,min=11;
        int max_number=0,min_number=0;
        p=head;
        while(p->next!=0)
        {
            ++i;
            p=p->next;
            if(p->number>max)
            {
                max=p->number;
                max_number=i;
            }
            if(p->number<min)
            {
                min=p->number;
                min_number=i;
            }
        }
        p=head;
        i=0;
        int sum=0;
        while(p->next!=0)
        {
            p=p->next;
            ++i;
            if(i!=max_number&&i!=min_number)
            {

                sum=sum+p->number;
            }
        }
        cout<<"score="<<sum<<endl;
    }
};
int main()
{
    test t;
    t.input();
    t.output();
    system("pause");
    return 0;
}

2、基类为点类,数据为一个点的坐标 x1、y1。子类为线段类,数据为两个端点的坐标 x1、y1 和 x2、y2。在类中对输出运算符进行重载,输出两个端点的坐标。对“>”运算符进行重载,返回长度较长的线段。

#include <iostream>
using namespace std;
class point
{
protected:
    int x1,y1;
public:
    point()
    {
        x1=0;
        y1=0;
    }
     point(int xx1,int yy1)
    {
        x1=xx1;
        y1=yy1;
    }
     void output()
    {
        cout<<"x1="<<x1<<endl;
        cout<<"y1="<<y1<<endl;
    }
};

class line:public point
{
protected:
    int x2,y2;
public:

    line()
    {
        x2=0;
        y2=0;
    }
    line(int xx1,int yy1,int xx2,int yy2):point(xx1,yy1)
    {
        x2=xx2;
        y2=yy2;
    }
    void output()
    {
        cout<<"x1="<<x1<<endl;
        cout<<"y1="<<y1<<endl;
        cout<<"x2="<<x2<<endl;
        cout<<"y2="<<y2<<endl;
    }
     friend ostream& operator<<(ostream& os,line a)
    {
        a.output();
        return os;
    }
    line operator >(line L2)
    {
//------------------------------------------
      if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)>(L2.x1-L2.x2)*(L2.x1-L2.x2)+(L2.y1-L2.y2)*(L2.y1-L2.y2))
      {
        return *this;   //直接返回当前类,不需要temp
      }
      else return L2;
        
//------------------------------------------
    }
};
int main()
{
    line L1(1,1,2,2);
    line L2(3,3,5,5);
    line L3=L1>L2;
    cout<<L3;
    return 0;
}

3、四个方向控制小球运动,每按一下键盘,小球运动一步, ESC 键盘退出程序。

#include <windows.h>
#include <iostream>
#include <ctime>
#include<conio.h>
using namespace std;
class test
{
private:
    int x,y;
    clock_t t;
public:
    test()
    {
        x=5;
        y=5;
        t=clock();
    }
    void gotoxy(int x,int y)
    {
        HANDLE h;//句柄,对象的索引
        COORD c;//结构体,坐标值
        c.X=x;
        c.Y=y;
        h=GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleCursorPosition(h,c);
    }
    void draw()
    {
        gotoxy(x,y);
        cout<<"●";
    }
    void erase()
    {
        gotoxy(x,y);
        cout<<"  ";
    }
    void key(clock_t& t,int tt)
    {
        if(clock()-t>tt)
        {
//------------------------------------------
            draw();
            char c1,c2;
            c1=getch();
            if(c1==27)
            {
                exit(0);
            }
            else if(c1==-32)
            {
                erase();
                c2=getch();
                switch(c2)
                {
                case 72:
                    y=(--y+25)%25;  //注意减到0以后并不会自动变正数,所以手动变正
                    break;
                case 75:
                    x=(--x+80)%80;
                    break;
                case 77:
                    x=(++x)%80;
                    break;
                case 80:
                    y=(++y)%25;
                    break;
                }
            }

//------------------------------------------
        }

    }
    void move()
    {
        while(true)
        {
            key(t,10);
        }
    }
};
int main()
{
    test t;
    t.move();
    return 0;
}
//这里注意小球占俩字符,erase要输出俩空格

4、文本文件“英汉词典.txt”的每一行形如“abandonment n.放弃”。使用 map 装载英汉词典文件,使用.find 函数精确查找一个英文单词。

#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
class test
{
private:
    fstream f;
    string::size_type n;
    map<string,string>a;
    map<string,string>::iterator p;
public:
    test()
    {
        string s,s1;
        f.open("英汉词典.txt",ios::in);
        while(true)
        {
//------------------------------------------
            string::size_type n,n1;  //换int也能跑
            string temp;
            getline(f,temp);
            n=temp.find(" ",0);
            s=temp.substr(0,n);
            n1=temp.rfind(" ",temp.size()); //逆序查找
            s1=temp.substr(n1+1,temp.size()-n1-1);
            a.insert(pair<string,string>(s,s1));
            if(f.eof())   //文件指针走到尾退出循环
            {
                break;
            }
//------------------------------------------
        }

    }
    ~test()
    {
        f.close();
    }
    void FIND()
    {
        string word;
        cout<<"input word:";
        cin>>word;
        p=a.find(word);
        if(p!=a.end())
        {
            cout<<p->second<<endl;
        }
        else
            cout<<"no word!"<<endl;
    }
};
int main()
{
    test t;
    t.FIND();
    return 0;
}

一只古灵古灵的精怪