期末复习【C++】
- 前言
- 导航
- 收藏
- 期末复习
- STL测试
- 期中真题
- 判断题
- 单选题
- 填空题
- 函数题
- R6-1 对学生对象按照成绩降序排序
- R6-2 派生类的定义和使用
- R6-3 学生成绩的快速录入(构造函数)
- R6-4 计算圆柱体的表面积(函数名隐藏)
- R6-5 求最大值和最小值
- 编程题
- R7-1 字符串替换
- R7-2 函数重载(数据类型不同)
- 期末真题
- 判断题
- 单选题
- 函数题
- R6-1 创建函数模板实现求数组中的最小元素
- R6-2 从shape类派生出一个直角三角形类RTriangle
- R6-3 求正16边形的面积和周长
- R6-4 将学生对象按照姓名升序排序C++
- R6-5 定义一个矩形类(C++构造函数)
- 编程题
- R7-1 验证手机号码(C++ Java)
- R7-2 找出足球赛对阵方
- 最后
前言
以下内容源自pta
仅供学习交流使用
请您阅读文章声明,默认同意该声明
导航
1 基础【C++】
2 基础【C++】
3 类和对象【C++】
4 构造函数与析构函数【C++】
5 对象数组和对象指针【C++】
6 静态成员与友元【C++】
7 字符串类string【C++】
8 继承性:派生类【C++】
9.多继承【C++】
10 运算符重载【C++】
11 虚函数【C++】
12 模板【C++】
13 标准模板库STL【C++】
14 IO流与文件操作【C++】
15 异常处理【C++】
16 复制构造函数【C++】
综合练习题【C++】
收藏
《C++面向对象程序设计》✍千处细节、万字总结(建议收藏)
期末复习
无需复习
STL测试
#include <iostream>
#include <vector>
#include <set>
#include <map>
using namespace std;
void testVector(){
vector<int> v;
for(int i=0;i<5;i++){
v.push_back(i);
}
for(int i=0;i<5;i++){
cout<<v[i]<<" ";//i
}
cout<<"\n";
}
void testSet(){
set<int> s;
for(int i=0;i<5;i++){
s.insert(i);
}
set<int>:: iterator item;
item=s.begin();
// while(item!=s.end()){
// cout<<*item<<" ";//i
// item++;
// }
for(item=s.begin();item!=s.end();item++) cout<<(*item)<<" ";//i
cout<<"\n";
}
void testMap(){
map<int,int> m;
for(int i=0;i<5;i++){
m.insert(pair<int,int>(i,i));
}
for(int i=0;i<5;i++){
// cout<<m[i]<<" ";//i
cout<<m.at(i)<<" ";//i
}
cout<<"\n";
}
int main(){
void (*test)(); //一个函数指针
void(*funs[])() = //一个函数指针数组
{
testVector,
testSet,
testMap
};
for(int i=0;i<3;i++){
test=funs[i];
test();
}
}
期中真题
判断题
R1-1 F
预定义的插入符从键盘上接收数据是不带缓冲区的。
R1-2 T
函数的参数个数和类型都相同,只是返回值不同,这不是重载函数。
R1-3 T
The types of arguments in a function call must match the types of the corresponding parameters in the function prototype’s parameter list.。
R1-4- F
如果公有派生,则可以用基类对象初始化派生类的引用。
单选题
R2-1- D
在C++中,函数原型不能标识()。
A.函数的返回类型
B.函数参数类型
C.函数参数的个数
D.函数的功能
R2-2 D
下面说法正确的是()。
A.内联函数在运行时是将该函数的目标代码插入每个调用该函数的地方
B.类的内联函数必须在类体内定义
C.类的内联函数必须在类体外通过加关键字inline定义
D.内联函数在编译时是将该函数的目标代码插入每个调用该函数的地方
R2-3 C
对定义重载函数的下列要求中,( )是错误的。
A.要求参数个数相同时,参数类型不同
B.要求参数中至少有一个类型不同
C.要求函数的返回值不同
D.要求参数的个数不同
R2-4-- B
设A为自定义类,现有普通函数int fun(A& x)。则在该函数被调用]时:
A.仅在该函数为A类的友元函数时,无需初始化形参x
B.无需初始化形参x
C.将执行复制构造函数来初始化形参x
D.仅在实参为常量时,才会执行复制构造函数以初始化形参x
R2-5 D
以下程序存在的问题是:
void fun()
{
int *num1, *num2;
num1 = new int[10];
num2 = new int[20];
num1[0] = 100;
num2[0] = 300;
num1 = num2;
delete [] num1;
}
A.num2最初指向的空间没有释放
B.num2不能给num1赋值
C.程序没有问题
D.num1最初指向的空间没有释放
R2-6 A
一个类的私有成员
A.只能被该类的成员函数和友元函数访问
B.只能被该类的成员函数、友元函数和派生类访问
C.只能被该类的成员函数访问
D.以上答案都不对
R2-7 A
设void f1(int * m,long & n);int a;long b;则以下调用合法的是()。
A.f1(&a,b);
B.f1(&a,&b);
C.f1(a,b);
D.f1(a,&b);
R2-8 D
在C++中,关于下列设置缺省参数值的描述中,()是正确的。
A.只能在函数的定义性声明中指定参数的缺省值;
B.不允许设置缺省参数值;
C.设置缺省参数值时,必须全部都设置;
D.在指定了缺省值的参数右边,不能出现没有指定缺省值的参数;
R2-9 D
友元的作用是
A.实现数据的隐藏性
B.增加成员函数的种类
C.加强类的封装性
D.提高程序的运用效率
R2-10 A
在面向对象系统中,对象的属性是________。
A.和其他对象相互区分的特性
B.对象的行为特性
C.与其他对象交互的方式
D.和其他对象相关联的方式
R2-11 D
要说明标识符是属于哪个命名空间时,需要在标识符和命名空间名字之间加上:
A.->
B.( )
C…
D.::
R2-12 A
所谓数据封装就是将一组数据和与这组数据有关操作组装在一起,形成一个()
A.类
B.函数体
C.对象
D.程序块
R2-13 B
以下关于C++语言中继承的叙述中,错误的是( )。
A.继承是父类和子类之间共享数据和方法的机制
B.继承仅仅允许单继承,即不允许一个子类有多个父类
C.继承关系中的子类将拥有父类的全部属性和方法
D.继承定义了一种类与类之间的关系
填空题
R4-1
A binary operator can be defined by either a non-static member function taking one
argument or a nonmember function taking two
arguments.
函数题
R6-1 对学生对象按照成绩降序排序
R6-1 对学生对象按照成绩降序排序
下面这段代码要实现对学生对象按照成绩降序排序。 仔细阅读代码,要求实现编程实现输出运算符“<<”和小于“<”运算符,使本程序能完成预定的排序功能。
裁判测试程序样例:
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Student {
string name;
char sex;
int score;
string grade;
public:
Student(string name, char sex, int score, string grade);
friend ostream &operator<< (ostream& os, Student st) ;
friend bool operator<(Student &st1, Student &st2);
};
//你提交的代码将被嵌入到这里
Student::Student(string name, char sex, int score, string grade) {
this->name = name;
this->sex = sex;
this->score = score;
this->grade = grade;
}
int main() {
list<Student> st;
string name, grade;
char sex; int score;
for(int i = 0; i < 5; i++) {
cin>>name; cin>>sex;
cin>>score; cin>>grade;
st.push_back(Student(name, sex, score, grade));
}
st.sort();
list<Student>::iterator p = st.begin();
while(p != st.end()) {
cout<<*p;
p++;
}
return 0;
}
输入样例:
在这里给出一组输入。例如:
Bill M 86 JK1501
David M 98 JK1502
Mary F 78 JK1503
Adam M 83 JK1504
Rose F 96 JK1505
输出样例:
在这里给出相应的输出。例如:
David M 98 JK1502
Rose F 96 JK1505
Bill M 86 JK1501
Adam M 83 JK1504
Mary F 78 JK1503
仅供参考
ostream& operator<<(ostream& os,Student st){
cout<<st.name<<" "<<st.sex<<" "<<st.score<<" "<<st.grade<<endl;
return os;
}
bool operator<(Student &st1, Student &st2){
if(st1.score<st2.score){
return false;
}else{
return true;
}
}
仅供参考
class Animal{
public:
void speak(){
cout<<"animal language!"<<endl;
}
};
class Cat:public Animal{
private:
string m_strName;
public:
Cat(string m_strName){
this->m_strName=m_strName;
}
void print_name(){
cout<<"cat name: "<<m_strName<<endl;
}
};
R6-2 派生类的定义和使用
R6-2 派生类的定义和使用
按要求完成下面的程序:
1、定义一个Animal类,包含一个void类型的无参的speak方法,输出“animal language!”。
2、定义一个Cat类,公有继承自Animal类,其成员包括:
(1)私有string类型的成员m_strName;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;
(3)公有的成员函数print_name,无形参,void类型,功能是输出成员m_strName的值,具体输出格式参见main函数和样例输出。
类和函数接口定义:
参见题目描述。
裁判测试程序样例:
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
int main()
{
Cat cat("Persian"); //定义派生类对象
cat.print_name(); //派生类对象使用本类成员函数
cat.speak(); //派生类对象使用基类成员函数
return 0;
}
输入样例:
本题无输入。
输出样例:
cat name: Persian
animal language!
仅供参考
class Animal{
public:
void speak(){
cout<<"animal language!"<<endl;
}
};
class Cat:public Animal{
private:
string m_strName;
public:
Cat(string m_strName){
this->m_strName=m_strName;
}
void print_name(){
cout<<"cat name: "<<m_strName<<endl;
}
};
R6-3 学生成绩的快速录入(构造函数)
R6-3 学生成绩的快速录入(构造函数)
分数 10
作者 何振峰
单位 福州大学
现在需要录入一批学生的成绩(学号,成绩)。其中学号是正整数,并且录入时,后录入学生的学号会比前面的学号大;成绩分两等,通过(Pass,录入时用1代表),不通过(Fail,录入时用0代表)。
由于很多学号都是相邻的,并且学号相邻的学生成绩常常相同。所以在录入时,适当地加了速。如果当前学生的学号比前面的学号大1,且成绩与前面的成绩相同,则只输入0即可。
类定义:
完成Student类
裁判测试程序样例:
#include<iostream>
using namespace std;
/* 请在这里填写答案 */
int main(){
const int size=100;
int i, N, no, score;
Student *st[size];
cin>>N;
for(i=0; i<N; i++){
cin>>no;
if(no>0){
cin>>score;
st[i]=new Student(no, score);
}
else
st[i]=new Student(*st[i-1]);
}
cout<<Student::count<<" Students"<<endl;
for(i=0;i<N;i++) st[i]->display();
for(i=0;i<N;i++) delete st[i];
return 0;
}
输入样例:
5
3 0
0
7 1
0
12 1
输出样例:
5 Students
3 Fail
4 Fail
7 Pass
8 Pass
12 Pass
仅供参考
// static int count=0;
class Student{
private:
int no;
int score;
public:
static int count;
Student(Student& st){
no=st.no+1;
score=st.score;
count++;
}
Student(int no, int score){
this->no=no;
this->score=score;
count++;
}
void display(){
if(score==0){
cout<<no<<" "<<"Fail"<<endl;
}else{
cout<<no<<" "<<"Pass"<<endl;
}
}
};
int Student::count=0;
R6-4 计算圆柱体的表面积(函数名隐藏)
R6-4 计算圆柱体的表面积(函数名隐藏)
分数 6
作者 张德慧
单位 西安邮电大学
Cylinder类是Circle类的派生类,在下面的程序中计算并输出了圆柱体的表面积。请阅读理解下面的程序。将下面的Cylinder类补充完整后提交。
Cylinder类的定义:
class Cylinder :public Circle{
double height;
public:
......
};
你提交的Cylinder类的定义将嵌入到下面的程序中:
#include <iostream>
using namespace std;
const double PI=3.1415926;
class Circle{
protected:
double radius;
public:
Circle(double r){
radius=r;
}
double getArea(){
return PI*radius*radius;
}
};
// 你提交的代码将嵌入在这里
int main()
{
double r,h;
cin>>r>>h;
Cylinder Cy1(r,h) ;
cout<<Cy1.getArea()<<endl;
return 0;
}
输入样例:
3.5 4.2
输出样例:
106.369
仅供参考
class Cylinder :public Circle{
double height;
public:
Cylinder(double r,double h):Circle(r){
height=h;
}
double getArea(){
return PI*radius*radius*2+2*PI*radius*height;
}
};
R6-5 求最大值和最小值
R6-5 求最大值和最小值
本题要求实现一个函数f,可找出10个整数中最大值max和最小值min。
函数接口定义:
在主函数中将以下列形式调用该函数
f(a,10,max,min);
例如:其中a是数组名,max用来保存最大值,min用来保存最小值。
裁判测试程序样例:
#include <iostream>
using namespace std;
/* 你提交的代码将被嵌入到这里 */
int main( )
{
int a[10];
int max,min,i;
for(i=0;i<10;i++){
cin>>a[i];
}
f(a,10,max,min);
cout<<"Max: "<<max<<endl;
cout<<"Min: "<<min<<endl;
return 0;
}
输入样例:
2 5 8 1 4 7 3 6 9 0
输出样例:
Max: 9
Min: 0
仅供参考
void f(int a[],int n,int &max,int &min){
max=a[0];
min=a[0];
int i=1;
for(i=0;i<n;i++){
if(a[i]>max){
max=a[i];
}
if(a[i]<min){
min=a[i];
}
}
}
编程题
R7-1 字符串替换
R7-1 字符串替换
分数 10
作者 张德慧
单位 西安邮电大学
将文本文件中指定的字符串替换成新字符串。
由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。
输入格式:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end (表示结束)
Institute (第一个字符串,要求用第二个字符串替换)
University (第二个字符串)
输出格式:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
输入样例:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University
输出样例:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
仅供参考
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main () {
int found;
string s1, s2, s;
vector<string> v;
getline(cin, s); //接收一串子直到碰到换行符(不包括换行符)
v.push_back(s + '\n');
while (1) {
getline(cin, s);
if ( s.compare("end") == 0 ) //相等返回0.小于返回负数,反之正数
break;
else {
s += '\n';
v.push_back(s);
}
}
cin >> s1 >> s2;
for ( int i = 0; i < v.size(); i++ ) {
found = v[i].find(s1);
// std::string::npos = -1
while ( found != std::string::npos ) {
v[i].replace(found, s1.length(), s2);
found = v[i].find(s1, found + 1);
}
}
vector<string>::iterator it;
for ( it = v.begin(); it != v.end(); it++ ) {
cout << *it;
}
return 0;
}
未知正确性
#include <iostream>
#include <string>
using namespace std;
/**
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University
**/
int main()
{
string str;
string s;
getline(cin,s);
while(s!="end"){
str=str+s+"\n";
getline(cin,s);
}
// cout<<str<<endl;
string s1,s2;
cin>>s1;
cin>>s2;
// cout<<s1<<endl;
// cout<<s2<<endl;
while(1){
int index=str.find(s1);
if(index==-1){
// cout<<index<<endl; 最后一次肯定会输出
break;
}
str.replace(index,s1.length(),s2);
}
cout<<str<<endl;
}
R7-2 函数重载(数据类型不同)
R7-2 函数重载(数据类型不同)
用同一个函数名对n(n<=10)个数据进行从小到大排序,数据类型可以是整数、浮点数,用函数重载实现
输入格式:
输入n 例如 3
输入n个整数,例如 10 8 9
输入n个浮点数 例如 10.23 5.16 7.99
输出格式:
输出n个整数的升序排列:8 9 10
以空格间隔,并以空格结尾
换行,输出n个浮点数的升序排列:5.16 7.99 10.23
以空格间隔,并以空格结尾
输入样例:
在这里给出一组输入。例如:
3
10 8 9
10.23 5.16 7.89
输出样例:
在这里给出相应的输出。例如:
8 9 10
5.16 7.89 10.23
仅供参考
#include<iostream>
using namespace std;
void sort(int a[],int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++){
if(a[j]>a[j+1]){
int t;
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void sort(double b[],int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++){
if(b[j]>b[j+1]){
double t;
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
}
int main(){
int n;
cin>>n;
int a[n];
double b[n];
int i=0;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
cin>>b[i];
}
sort(a,n);
sort(b,n);
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
for(i=0;i<n;i++){
cout<<b[i]<<" ";
}
return 0;
}
期末真题
判断题
R1-1 T
类中的构造函数是一个特殊的成员函数,它由类的对象自动调用,它的作用是为对象分配内存空间,初始化类的数据成员并进行一些其它类的内部操作。
R1-2 T
this 指针是一个系统预定义的特殊指针,指向当前正在操作的对象。
R1-3 T
因为静态成员函数不能是虚函数,所以它们不能实现多态。
R1-4 T
某类的友元类的所有成员函数可以存取或修改该类中的私有成员。
R1-5 T
STL算法使用迭代器间接地对容器中的元素进行操作。
R1-6 T
静态成员用以解决同一个类的不同对象之间成员,包括数据成员和函数成员的共享问题。
单选题
R2-1 D
在C++中,关于下列设置缺省参数值的描述中,()是正确的。
A.只能在函数的定义性声明中指定参数的缺省值;
B.不允许设置缺省参数值;
C.设置缺省参数值时,必须全部都设置;
D.在指定了缺省值的参数右边,不能出现没有指定缺省值的参数;
R2-2 A
关于new运算符的下列描述中,()是错误的。
A.使用它创建对象数组时必须指定初始值;
B.使用它创建对象时要调用构造函数;
C.使用它创建的对象或对象数组可以使用运算符delete删除;
D.它可以用来动态创建对象和对象数组;
R2-3 D
关于虚函数的描述中,( )是正确的。
A.派生类的虚函数与基类的虚函数具有不同的参数个数和类型
B.虚函数是一个非成员函数
C.虚函数是一个static 类型的成员函数
D.基类中说明了虚函数后,派生类中与其对应的函数可不必说明为虚函数
R2-4 D
下列函数中,( )不能重载。
A.构造函数
B.非成员函数
C.成员函数
D.析构函数
R2-5 C
一个类的私有成员
A.只能被该类的成员函数、友元函数和派生类访问
B.以上答案都不对
C.只能被该类的成员函数和友元函数访问
D.只能被该类的成员函数访问
R2-6 D
以下关于C++语言中继承的叙述中,错误的是( )。
A.继承关系中的子类将拥有父类的全部属性和方法
B.继承是父类和子类之间共享数据和方法的机制
C.继承定义了一种类与类之间的关系
D.继承仅仅允许单继承,即不允许一个子类有多个父类
R2-7 D
对于类之间的友元关系:
A.如果类A是类B的友元,则B的成员函数可以访问A的私有成员
B.如果类A是类B的友元,并且类B是类C的友元,则类A也是类C的友元。
C.如果类A是类B的友元,则B也是A的友元。
D.以上答案都不对。
R2-8 C
下列表达错误的是( )。
A.cout<<setw(5)
B.cin.fill(‘#’)-
C.cout<<fill(‘#’)
D.cout.setf(ios::uppercase)
R2-9 C
下列描述中,( )是抽象类的特性。
A.可以说明虚函数
B.可以进行构造函数重载
C.不能定义该类对象
D.可以定义友元函数
R2-10 C
假定MyClass为一个类,则该类的复制构造函数的声明语句为()
A.MyClass(int x)
B.MyClass(MyClass x)
C.MyClass(const MyClass& x);
D.MyClass(MyClass *x)
R2-11B
命名空间应用于:
A.提高代码的执行速度
B.避免各个不同函数、变量等的名称冲突
C.在类外定义类的成员函数
D.以上答案都正确
R2-12 C
(2020final)一个函数功能不太复杂,但要求被频繁调用,可选用( )。
A.重载函数
B.递归函数
C.内联函数
D.嵌套函数
函数题
R6-1 创建函数模板实现求数组中的最小元素
R6-1 创建函数模板实现求数组中的最小元素
分数 10
作者 张德慧
单位 西安邮电大学
创建一个函数模板实现求数组中的最小元素,在主函数将分别使用该模板求整形数组和double型数组中的最小元素并输出。
输入:
6 (整型数组中的元素个数)
8 3 4 1 6 9 (整型数组中的元素)
5 (双精度浮点数数组中的元素个数)
2.7 3.1 9.8 5.3 7.6 (双精度浮点数数组中的元素)
输出:
1
2.7
函数模板接口定义:
T Min(T *p, int len)
其中 p 和 len 都是用户传入的参数。 p 是数组元素的起始地址; len 是数组的长度。
裁判测试程序样例:
#include <iostream>
using namespace std;
// 你提交的代码将嵌入到这里
int main( )
{
int n,m,*pn,i=0;
cin>>n;
pn=new int[n];
do{
cin>>pn[i];
i++;
}while(i<n);
double *pd;
i=0;
cin>>m;
pd=new double[m];
do{
cin>>pd[i];
i++;
}while(i<m);
cout<<Min(pn,n)<<endl;
cout<<Min(pd,m)<<endl;
delete [ ] pn;
delete [ ] pd;
return 0;
}
输入样例:
9
8 2 7 6 4 5 3 1 0
7
3.1 9.6 5.8 2.7 6.3 7.0 8.5
输出样例:
0
2.7
仅供参考
template <typename T>
T Min(T *p, int len){
T m=p[0];
for(int i=0;i<len;i++){
if(p[i]<m){
m=p[i];
}
}
return m;
}
R6-2 从shape类派生出一个直角三角形类RTriangle
R6-2 从shape类派生出一个直角三角形类RTriangle
分数 10
作者 张德慧
单位 西安邮电大学
从shape类派生出一个直角三角形类RTriangle类(regular triangle)。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
###直角三角形类名:RTriangle
直角三角形类的构造函数原型如下:
RTriangle(double a, double b);
其中 a 和 b 都是直角三角形的两条直角边。
裁判测试程序样例:
#include <iostream>
#include <cmath>
using namespace std;
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里
int main()
{
double a,b;
cin>>a>>b;
RTriangle t(a,b);
cout<<t.getArea()<<endl;
cout<<t.getPerimeter()<<endl;
}
输入样例:
3.0 4.0
输出样例:
6
12
仅供参考
class RTriangle{
public:
RTriangle(double a, double b){
h=a;
w=b;
}
double getArea(){
return h*w/2;
}
double getPerimeter(){
return h+w+sqrt(h*h+w*w);
}
private:
double h;
double w;
};
R6-3 求正16边形的面积和周长
R6-3 求正16边形的面积和周长
分数 10
作者 张德慧
单位 西安邮电大学
求正16边形的面积和周长
在一个正n边形(Regular Polygon)中,所有边的边长都相等,且所有角的度数相同(即这个多边形是等边、等角的)。我们已经从下列抽象类shape实现了一个正n边形类RegularPolygon。其构造方法为:RegularPolygon(int n,double side); 其中n为边数,side为边长。
从键盘输入正16边形的边长s,请补充下列程序计算该正16边形的面积和周长。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
裁判测试程序样例:
#include <iostream>
#include <cmath>
using namespace std;
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//这里定义了正多边形类RegularPolygon,其构造方法为:RegularPolygon(int n,double side); 其中n为边数,side为边长。
int main()
{
double s;
cin>>s;
/* 请在这里填写答案 */
return 0;
}
输入样例1:
在这里给出一组输入。例如:
6.1828
输出样例1:
在这里给出相应的输出。例如:
768.721
98.9248
输入样例2:
在这里给出一组输入。例如:
-1.234
输出样例2:
在这里给出相应的输出。例如:
Illegal input data.
仅供参考
if(s<0){
cout<<"Illegal input data."<<endl;
return 0;
}
RegularPolygon r(16,s);
cout<<r.getArea()<<endl;
cout<<r.getPerimeter()<<endl;
R6-4 将学生对象按照姓名升序排序C++
R6-4 将学生对象按照姓名升序排序C++
分数 10
作者 张德慧
单位 西安邮电大学
请阅读程序并补全源代码:先从键盘录入5个学生的数据,保存到向量vector容器对象v中,然后将学生对象按照姓名name的字典顺序(A~Z)排序后输出。
裁判测试程序样例:
//本程序将下面的学生类Student对象按照按照姓名name的字典顺序(A~Z)排序后输出。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Student {
string number;
string name;
float score;
public:
// Constructo
Student(string number1, string name1, float score1) {
number = number1;
name = name1;
score = score1;
}
string get_name(){
return name;
}
// Used to print student details in main()
void print() {
cout<<number << " " <<name << " " << score<<endl;
}
};
bool cmp_name(Student s1,Student s2);//比较姓名的函数 ,需要考生实现
int main()
{
vector<Student> v;
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
04031021 Zhang3 84
04031013 Li4 73
04031018 Wang5 98
04031038 Ma6 65
04031029 Chen7 96
输出样例:
在这里给出相应的输出。例如:
04031029 Chen7 96
04031013 Li4 73
04031038 Ma6 65
04031018 Wang5 98
04031021 Zhang3 84
仅供参考
for(int i=0;i<5;i++){
string number;
string name;
float score;
cin>>number>>name>>score;
Student stu(number,name,score);
v.push_back(stu);
}
sort(v.begin(),v.end(),cmp_name);
for(int i=0;i<5;i++){
v[i].print();
}
}
bool cmp_name(Student s1,Student s2){
if(s1.get_name()>s2.get_name()){
return false;
}
return true;
}
R6-5 定义一个矩形类(C++构造函数)
R6-5 定义一个矩形类(C++构造函数)
分数 10
作者 张德慧
单位 西安邮电大学
设计一个名为Rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getArea( )的函数返回矩形的面积;一个名为getPerimeter( )的函数返回矩形的周长。请实现这个类。
类名为:
Rectangle
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include <iostream>
using namespace std;
//你提交的代码将嵌入到这里
int main()
{
double m,n;
cin>>m;
cin>>n;
Rectangle a(m,n);
cout<<a.getArea()<<endl;
cout<<a.getPerimeter()<<endl;
return 0;
}
输入样例:
3.5 35.9
输出样例:
125.65
78.8
仅供参考
class Rectangle{
private:
double width=1;
double height=1;
public:
Rectangle(){
}
Rectangle(double w,double h){
width=w;
height=h;
}
double getArea(){
return width*height;
}
double getPerimeter(){
return (height+width)*2.0;
}
};
编程题
R7-1 验证手机号码(C++ Java)
R7-1 验证手机号码(C++ Java)
分数 10
作者 张德慧
单位 西安邮电大学
某系统在新用户注册时必须输入手机号,为了提高系统效率,防止输错手机号,需要对手机号进行验证。 验证规则为: (1)长度为11位 (2)由数字0~9组成 (3)必须是1开头 以上3个条件同时满足,则验证通过,否则为不通过。
输入格式:
在一行中一个字符串,长度不超过50个字符。例如:
13802988920
输出格式:
如果验证通过则输出Yes,否则输出No。
输入样例:
13812345678
输出样例:
Yes
仅供参考
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
getline(cin,str);
//cin>>str //13812345678 1x 不通过
int len=str.length();
if(len!=11){
cout<<"No"<<endl;
return 0;
}
if(str[0]!='1'){
cout<<"No"<<endl;
return 0;
}
int sign=1;
for(int i=0;i<11;i++){
int in=str[i]-'0';
if(in<0||in>9){
sign=0;
break;
}
}
if(sign==0){
cout<<"No"<<endl;
return 0;
}
cout<<"Yes"<<endl;
return 0;
}
R7-2 找出足球赛对阵方
R7-2 找出足球赛对阵方
分数 10
作者 张德慧
单位 浙江大学
2014年世界杯足球赛在6月24日星期二小组赛赛程如下所示:
Australian VS Spain; Holland VS Chile; Cameroon VS Brazil; Croatia VS Mexico
请你编写一个简单的C++程序,当输入其中任何一个国家的名字就能找出与其对阵的国家名。例如,输入“Holland”,就能找到对阵方是“Chile”。
输入格式:
Holland
输出格式:
Holland team's rival is Chile.
输入样例:
Germany
输出样例:
Germany's team has no match today.
仅供参考
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
cin>>s;
string str[4]={"Australian","Holland","Cameroon","Croatia"};
string ans[4]={"Spain","Chile","Brazil","Mexico"};
int i;
for(i=0;i<4;i++){
if(s==str[i]){
cout<<s<<" team's rival is "<<ans[i]<<"."<<endl;
break;
}
}
int j;
for(j=0;j<4;j++){
if(s==ans[j]){
cout<<s<<" team's rival is "<<str[j]<<"."<<endl;
break;
}
}
if(i==4&&j==4){
cout<<s<<"'s team has no match today."<<endl;
}
return 0;
}