7-
判断题
1-1 F
对静态数据成员初始化可以在类内进行。
1-2 T
静态数据成员不属于某个对象,在给对象分配存储空间时,不包括静态数据成员所占的空间。
1-3 T
静态成员函数属于类而不是类的对象,没有this指针,静态成员函数中不能使用thiss指针。
1-4 F
静态成员函数的实现必须在类体外实现,不能在类体内实现。
1-5 T
由于静态成员函数不属于某个特定的对象,因此。不能像一般的成员函数那样随意的访问对象中的非静态数据成员。只能引用类中声明的静态数据成员。如果要引用非静态数据成员,可通过对象引用。
1-6 T
常数据成员的值必须初始化,且不能改变。
1-7 F
常成员函数既可以被常对象调用,也可以被非常对象调用。
单选题
2-1 C
关于用string定义字符串,下列选项中错误的是____。
A.string s; s = “hello C++”;
B.string s = “hello C++”;
C.string s(“hello C++”);
D.string s[“hello C++”];
2-2 C
使用C++标准string类定义一个字符串,需要包含的头文件____。
A.string.h
B.string
C.cstring
D.stdlib.h
2-3 B
Variables that are shared by every instances of a class are __________.
A.public variables
B.private variables
C.instance variables
D.static variables
2-4 A
若有下面的语句:
string s="Hello";
s.append("123");
cout << s.at(5) << endl;
则执行后程序的输出结果是
A.o
B.1
C.2
D.3
编程题
7-1 字符串替换
7-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;
}
7-2 求解给定字符串的前缀
7-2 求解给定字符串的前缀
分数 10
作者 张德慧
单位 西安邮电大学
求解给定字符串的前缀。
输入格式:
输入数目不定的多对字符串,每行两个,以空格分开。 例如:
filename filepath
Tom Jack
输出格式:
返回两个字符串的最大前缀,例如:
The common prefix is file
No common prefix
输入样例:
filename filepath
Tom Jack
输出样例:
The common prefix is file
No common prefix
仅供参考
#include"iostream"
#include"cstring"
using namespace std;
int main()
{
char a[20];
char b[20];
int n;
while(cin>>a)
{
cin>>b;
n=(strlen(a)>strlen(b))?strlen(a):strlen(b);
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
{
a[i]='\0';
}
}
if(a[0]=='\0')
cout<<"No common prefix"<<endl;
else
cout<<"The common prefix is "<<a<<endl;
}
return 0;
}
7-3 学号解析
7-3 学号解析
分数 10
作者 杨军
单位 四川师范大学
川师的学号的某些位有特殊的含义,如从2016110101中可以看出该学生为2016级,就读于11系,班级为1班。根据输入的学号,利用程序进行解析,输出对应的信息。
输入格式:
一个学号
输出格式:
相关信息
输入样例:
在这里给出一组输入。例如:
2016110101
输出样例:
在这里给出相应的输出。例如:
year:2016
department:11
class:01
仅供参考
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,i,j=1,k=1;
vector<string> a;
string s,s1,s2,s3;
cin>>s;
a.push_back(s.substr(0,4));
cout<<"year:"<<a[0]<<endl;
a.push_back(s.substr(4,2));
cout<<"department:"<<a[1]<<endl;
a.push_back(s.substr(6,2));
cout<<"class:"<<a[2]<<endl;
return 0;
}
7+
判断题
1-1 T
get()函数不能从流中提取终止字符,终止字符仍留在流中。getline()函数可以从流中提取终止字符,但终止字符被丢弃。(
单选题
2-1 B
下列String类的( )方法返回指定字符串的一部分。
A.extractstring()
B.substring()
C.Substring()
D.Middlestring()
2-2 A
C++语言支持过程程序设计方法和( )设计方法。
A.面向对象
B.面向函数
C.面向用户
D.面向问题
2-3 B
在C++中,实现封装性需借助于()。
A.枚举
B.类
C.数组
D.函数
2-4 D
关于用string定义字符串,下列选项中错误的是____。
A.string s; s = “hello C++”;
B.string s = “hello C++”;
C.string s(“hello C++”);
D.string s[“hello C++”];
编程题
7-1 分离目录路径和文件名
7-1 分离目录路径和文件名
分数 10
作者 张德慧
单位 西安邮电大学
输入文件目录路径和文件名,要求分离成目录路径和文件名分别输出
输入格式:
例如:输入
c:\windows\winhelp.exe
输出格式:
c:\windows (目录路径)
winhelp.exe (文件名)
输入样例:
/usr/bin/man
输出样例:
/usr/bin
man
仅供参考
#include <iostream>
#include<string>
using namespace std;
void filename(const string & str)
{
size_t found=str.find_last_of("/\\");//Point
cout<<str.substr(0,found)<<endl;
cout<<str.substr(found+1)<<endl;
}
int main()
{
string str1;
getline(cin,str1);
filename(str1);
return 0;
}
7-2 验证手机号码(C++ Java)
7-2 验证手机号码(C++ Java)
分数 10
作者 张德慧
单位 西安邮电大学
某系统在新用户注册时必须输入手机号,为了提高系统效率,防止输错手机号,需要对手机号进行验证。 验证规则为: (1)长度为11位 (2)由数字0~9组成 (3)必须是1开头 以上3个条件同时满足,则验证通过,否则为不通过。
输入格式:
在一行中一个字符串,长度不超过50个字符。例如:
13802988920
输出格式:
如果验证通过则输出Yes,否则输出No。
输入样例:
13812345678
输出样例:
Yes
仅供参考
JAVA
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
String PhoneNumber = cin.nextLine();
char[] ch_str = PhoneNumber.toCharArray();
boolean flag = true;
for(int i=0;i<PhoneNumber.length();i++)
{
if(!(ch_str[i]>='0'&&ch_str[i]<='9'))
{
flag = false;
break;
}
}
if( PhoneNumber.length()==11 && ch_str[0]=='1' && flag == true )
System.out.print("Yes");
else
System.out.print("No");
cin.close();
}
}
7-3 对字符串进行排序输出
7-3 对字符串进行排序输出
分数 10
作者 王博
单位 西安邮电大学
给定一个字符串,对该字符串进行排序,请输出排好序的字符串。要求能够连续输入输出的字符串。
输入格式:
在一行输入一个字符串
输出格式:
输出排好序的字符串的序列
输入样例:
fecbad
输出样例:
abcdef
仅供参考
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str;
getline(cin, str);
sort(str.begin(), str.end());
cout << str << endl;
return 0;
}