- 前言
- 14-
- 判断题
- 单选题
- 填空题
- 函数题
- 6-1 学生成绩的输入和输出(运算符重载)
- 6-2 对学生对象按照成绩升序排序
- 6-3 矩阵与向量的乘法
- 14+
- 判断题
- 单选题
- 填空题
- 函数题
- 6-1 对学生对象按照姓名升序排序
- 最后
以下内容源自xxx
仅供学习交流使用
请您阅读文章声明,默认同意该声明
1-1 T
使用提取符(<<)可以输出各种基本数据类型的变量的值,也可以输出指针值。
1-2 F
预定义的插入符从键盘上接收数据是不带缓冲区的。
1-3 F
The cin stream normally is connected to the display screen.
1-4 T
这是一个判断题的样例。答案为T,分值为5分。
2-1 B
cout 是由I/O 流库预定义的( )。
A.类
B.对象
C.包含文件
D.常量
2-2 B
分析以下程序:程序的输出结果是
#includeusing namespace std; void fun(int num) { cout << num << endl; } void fun(char ch) { cout << (ch + 1) << endl; } int main() { fun('A'); return 0; }
A.65
B.66
C.A
D.B
2-3 C
下列关于cin和cout的说法中,错误的是____。
A.cin用于读入用户输入的数据
B.cout用于输出数据
C.cin比C语言中的scanf()函数更有优势,它可以读取空格
D.cout通常与<<运算符结合
#include <fstream>
#include < iostream >
using namespace std;
int main ()
{
char data[100];
// 以写模式打开文件
ofstream outfile;
outfile.open(“afile.dat”);
cout << “Writing to the file” << endl;
cout << “Enter your name:”;
cin.getline(data, 100);
// 向文件写入用户输入的数据
outfile << data << endl;
cout << “Enter your age:”;
cin >> data;
cin.ignore();
// 再次向文件写入用户输入的数据
outfile << data << endl;
// 关闭打开的文件
outfile.close();
// 以读模式打开文件
ifstream infile;
infile.open(“afile.dat”);
cout << “Reading from the file”<< endl;
infile>> data;
// 在屏幕上写入数据
cout << data << endl;
// 再次从文件读取数据,并显示它
infile >> data;
cout << data << endl;
// 关闭打开的文件
infile.close();
return 0;
}
6-1 学生成绩的输入和输出(运算符重载) 现在需要输入一组学生的姓名和成绩,然后输出这些学生的姓名和等级。 输入时,首先要输入学生数(正整数)N。接着输入N组学生成绩,每组成绩包括两项:第一项是学生姓名,第二项是学生的成绩(整数)。 输出时,依次输出各个学生的序号(从1开始顺序编号),学生姓名,成绩等级(不小于60为PASS,否则为FAIL) 函数接口定义: 面向Student类对象的流插入和流提取运算符 裁判测试程序样例: #include#include using namespace std; int main(){ int i, repeat; Student st; cin>>repeat; for(i=0;i cin>>st; cout< class Student{ public : Student (string ,int); friend istream & operator >>(istream&,Student &); friend ostream & operator <<(ostream&,Student &); private: string name; int score; }; Student::Student( string name="def",int score=0){ } istream &operator >>(istream & is ,Student &s ){ is>> s.name>>s.score; return is; } ostream &operator <<(ostream & os ,Student &s){ static int sum;sum=sum+1; os<6-2 对学生对象按照成绩升序排序=60)os<<"PASS"; else{os<<"FAIL";} return os; } 6-2 对学生对象按照成绩升序排序 下面这段代码要实现对学生对象按照成绩升序排序。 仔细阅读代码,要求实现编程实现输出运算符“<<”和小于“<”运算符,使本程序能完成预定的排序功能。 裁判测试程序样例: #include#include #include 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
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 ::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 输出样例: Mary F 78 JK1503 Adam M 83 JK1504 Bill M 86 JK1501 Rose F 96 JK1505 David M 98 JK1502 ostream &operator<< (ostream& os, Student st){ os<6-3 矩阵与向量的乘法if(st1.score return true; }else{ return false; } } 6-3 矩阵与向量的乘法 矩阵与向量相乘:当矩阵的列数与向量的行数相等时,用矩阵的第i行乘以向量,并输出结果向量,否则输出"Bad multiplying Matrix with Vector."。 函数接口定义: int& Matrix::operator()(int i,int j); ostream& operator<<(ostream& out,const Vector& x); Vector operator*(Matrix& mat,Vector& vex); int& Matrix::operator()(int i,int j):返回第i行第j列的矩阵元素(可作左值),应有下标越界检查,如果越界则输出信息"Matrix index out of range."; ostream& operator<<(ostream& out,const Vector& x):将向量x输出到输出流out对象。为能达到连续输入对象的效果,设置返回类型为引用; Vector operator*(Matrix& mat,Vector& vex):参数为矩阵mat、向量vex,返回二者的乘积。 裁判测试程序样例: #include#include using namespace std; class Matrix; //类的向前声明 class Vector { private: int* v; int sz; public: Vector(int n) { v=new int[sz=n]; } Vector(const Vector& t) { v=new int[sz=t.sz]; memcpy(v,t.v,sz*sizeof(int)); //深拷贝 } ~Vector() { delete []v; } int& operator[](int i) { if(i>=0 &&i cout<<"Vector index out of range.n"; exit(0); } } friend ostream& operator<<(ostream& out,const Vector& x); friend Vector operator*(Matrix& m,Vector& v);//重载乘法运算符 }; class Matrix { private: int* m; int szl,szr; public: Matrix(int a,int b) { szl=a; szr=b; m=new int[a*b]; //用一维数组模拟矩阵 } ~Matrix() { delete []m; } int& operator()(int i,int j) ; friend Vector operator*(Matrix& m,Vector& v); }; int main() { int n,m; cin>>n>>m; Matrix ma(n,m); for(int i=0; i >ma(i,j); cin>>n; Vector ve(n); for(int i=0; i >ve[i]; Vector va = ma * ve; cout< //矩阵到数组的转换输入 int& Matrix::operator()(int i,int j){ return this->m[i*this->szr+j]; } //输出向量 ostream& operator<<(ostream& out,const Vector& x){ for(int i=0;i14+ 判断题out< //当矩阵的列数与向量的行数不相等时 if(mat.szr!=vex.sz){ cout<<"Bad multiplying Matrix with Vector."; return NULL; } Vector v(mat.szl); for(int i=0;i int sum=0; for(int j=0;j sum+=mat.m[i*mat.szr+j]*vex.v[j]; } v.v[i]=sum; } return v; } 1-1 F
记录流的当前格式化状态标志字中的每一位用于记录一种格式,这种格式是不能被设置或清除的。1-2 T
设置和清除格式标志字的成员函数需要通过对象来引用它们,输出显示格式的对象通常是cout。1-3 T
操纵符本身是一个对象,它可以直接被提取符或插入符操作。1-4 T
get()函数不能从流中提取终止字符,终止字符仍留在流中。getline()函数可以从流中提取终止字符,但终止字符被丢弃。1-5 T
使用打开文件函数open()之前,需要定义一个流类对象,使用open()函数来操作该对象。1-6 F
打开ASCⅡ码流文件和二进制流文件时,打开方式是相同的。1-7 T
read()和write()函数可以读写文本文件,也可以读写二进制文件。1-8 F
单选题
seekg()函数和seekp()函数分别用来定位读指针和写指针的。如果使用seek()函数可以同时定义读写指针。
1-9 F
预定义的插入符从键盘上接收数据是不带缓冲区的。2-1 A
下面是关于ios 类的叙述,正确的是( )。
A.它是istream 类和ostream 类的虚基类
B.它只是istream 类的虚基类
C.它只是ostream 类的虚基类
D.它是iostrearm 类的虚基类2-2 D
关于read()函数的下列描述中,正确的是( )。
A.该函数只能用来从键盘输入中获取字符串
B.该函数所获取的字符多少是不受限制的
C.该函数只能用于文本文件的操作中
D.该函数只能按规定读取所指定的字符数2-3 A
当使用ifstream 流类定义一个流对象并打开一个磁盘文件时,文件的隐含打开方式为( )。
A.ios::in
B.ios::out
C.ios::in|ios::out
D.ios::binary2-4 D
下列函数中,( )是对文件进行写操作的。
A.get
B.read
C.seekg
D.put2-5 B
在C++中,打开一个文件,就是将整个文件与一个( )建立关联,关闭一个文件,就是取消这种关联。
A.类
B.流
C.对象
D.结构2-6 D
下列打开文件的表达式中,错误的是:
A.ofstream ofile; ofile.open(“C:vcabc.txt”,ios::binary);
B.fstream iofile;iofile.open(“abc.txt”,ios::ate);
C.ifstream ifile (“C:vcabc.txt”);
D.cout.open(“C:vcabc.txt”,ios::binary);2-7 B
以下关于文件操作的叙述中,不正确的是:
A.打开文件的目的是使文件对象与磁盘文件建立联系
B.文件读写过程中,程序将直接与磁盘文件进行数据交换
C.关闭文件的目的之一是保证将输出的数据写入硬盘文件
D.关闭文件的目的之一是释放内存中的文件对象2-8 C
填空题
使用操作符setw对数据进行格式输出时,需要包含()文件。
A.iostream.h
B.fstream.h
C.iomanip.h
D.stdlib.h4-1
文本文件输入输出
#include <fstream>
#include < iostream >
using namespace std;int main ()
{
char data[100];// 以写模式打开文件
ofstream outfile;
outfile.open(“afile.dat”);
cout << “Writing to the file” << endl;
cout << “Enter your name:”;
cin.getline(data, 100);// 向文件写入用户输入的数据
outfile << data << endl;cout << “Enter your age:”;
cin >> data;
cin.ignore();// 再次向文件写入用户输入的数据
outfile << data << endl;// 关闭打开的文件
outfile.close();// 以读模式打开文件
ifstream infile;
infile.open(“afile.dat”);cout << “Reading from the file”<< endl;
infile>> data;
// 在屏幕上写入数据
cout << data << endl;// 再次从文件读取数据,并显示它
infile >> data;
cout << data << endl;// 关闭打开的文件
infile.close();return 0;
}4-2
分数 10
作者 张德慧
单位 西安邮电大学
二进制文件输入输出#include
#include
using namespace std;
class Date
{
int month, day, year;
public:
Date( int m=1, int d=1, int y=1901)
{
month = m; day = d; year = y;
}
friend ostream &operator<< ( ostream & os, Date dt)
{
os << dt.month << ‘/’ << dt.day << ‘/’<< dt.year<<‘n’;
return os;
}
};int main()
{
Date dt1,dt2( 2, 2, 2002);
cout << "dt1 is: "<ofstream fout(“date.dat”,ios::binary);
fout.write((char*)&dt1,sizeof(dt1));
fout.write((char*)&dt2,sizeof(dt2));fout.close( );
ifstream fin(“date.dat”,ios::binary);
fin.read((char*)&dt2,sizeof(dt2));//第一次读入的数据块存放到dt2中
fin.read((char*)&dt1,sizeof(dt1));
cout << "dt1 is: "<fin.close( ); return 0;
函数题 6-1 对学生对象按照姓名升序排序
}6-1 对学生对象按照姓名升序排序 分数 10 作者 张德慧 单位 西安邮电大学 下面这段代码要实现对学生对象按照姓名升序排序。 仔细阅读代码,要求实现编程实现输出运算符“<<”和小于“<”运算符,使本程序能完成预定的排序功能。 裁判测试程序样例: #include#include #include 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
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 ::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 输出样例: 在这里给出相应的输出。例如: Adam M 83 JK1504 Bill M 86 JK1501 David M 98 JK1502 Mary F 78 JK1503 Rose F 96 JK1505 ostream &operator<< (ostream& os, Student st){ os<最后 请您阅读文章声明,默认同意该声明
打赏通道



