理论练习题
试题一
编写一个程序,输入一个整数,分别以十进制、八进制和十六进制数值输出,输出十六进制值时限用大写字母。
解:对应的程序如下:
#include <iostream.h>
#include <iomanip.h>
void main()
{
int n;
cout << "输入一个整数:";
cin >> n;
cout << "十 进 制:" << n << endl;
cout << "八 进 制:" << oct << n << endl;
cout << "十六进制:" << hex << setiosflags(ios::uppercase) << n << endl;
}
试题二
设计一个类Student,其说明如下:
class Student
class Student
{
int no;
char *name
char sex[2];
public:
Student();
friend istream & operator >> (istream & stream,Student & s)
friend ostream & operator << (ostream & stream,Student & s)
};
实现该类,并编写main()函数对该类进行操作。
解:对应的程序如下:
#include <iostream.h>
class Student
{
int no;
char *name;
char sex[2];
public:
Student() {
name=new char[10];
}
friend istream & operator >> (istream & stream,Student & s){
cout << "输入一个学生数据" << endl;
cout << " 学号:";
stream >> s.no;
cout << " 姓名:";
stream >> s.name;
cout << " 性别:";
stream >> s.sex;
return stream;
}
friend ostream & operator << (ostream & stream,Student & s){
cout << "输出一个学生数据" << endl;
cout << " 学号:" << s.no << endl;
cout << " 姓名:" << s.name << endl;
cout << " 性别:" << s.sex << endl;
return stream;
}
}
void main(){
Student A;
cin >> A;
cout << A;
}
上机实习题
试题一
编写一个程序,实现以下功能:
(1)输入一系列的数据(学号、姓名、成绩)存放在文件stud.dat中。
(2)从该文件中读出这些数据并显示出来。
解:先设计一个类Stud,其中包括一个学生数据的操作,然后建立两个普通函数inputfunc()、outputfunc(),分别实现功能(1)和(2)。
参考程序
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
class Stud {
int no; //学号
char name[10]; //姓名
int score; //成绩
public:
void getdata() {
cout << "(学号 姓名 成绩):";
cin >> no >> name >> score;
}
void disp() {
cout << setw(6) << no << setw(10) << name << setw(6) << score << endl;
}
};
void outputfunc() {
ofstream output("stud.dat");
Stud s;
int n;
cout << "输入数据" << endl;
cout << " 学生人数:";
cin >> n;
for (int i=0;i<n;i++) {
cout << " 第" << i+1 << "个学生";
s.getdata();
output.write((char *)&s,sizeof(s));
};
output.close();
}
void inputfunc() {
ifstream input("stud.dat");
Stud s;
cout << "输出数据" << endl;
cout << " 学号 姓名 成绩" << endl;
input.read((char *)&s,sizeof(s));
while (input) {
s.disp();
input.read((char *)&s,sizeof(s));
};
input.close();
}
int main() {
int sel;
do
{
cout << "选择(1:输入数据 2:输出数据 其他退出):";
cin >> sel;
switch(sel)
{
case 1: outputfunc(); break;
case 2: inputfunc(); break;
}
} while (sel==1 || sel==2);
return 0;
}
输出结果
试题二
编写一个程序,输入一系列的数据(学号、姓名、成绩)存放在文件stud.dat中。输出这些学生数据和相应的成绩等级(≥90为优,80~89为良,70~79为中,60~69为及格,≤59为不及格)。要求在上题的基础上实现。
解:在上题的Stud类中添加一个数据成员level,getdata()成员函数根据输入的score计算出该等级值。参考程序
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
class Stud
{
int no;
char name[10];
int score;
char level[7];
public:
void getdata() {
cout << "(学号 姓名 成绩):";
cin >> no >> name >> score;
if (score>=90)
strcpy(level,"优");
else if (score>=80)
strcpy(level,"良");
else if (score>=70)
strcpy(level,"中");
else if (score>=60)
strcpy(level,"及格");
else
strcpy(level,"不及格");
}
void disp() {
cout << setw(6) << no << setw(10) << name << setw(6) << score << setw(8) << level << endl;
}
};
void outputfunc() {
ofstream output("stud.dat");
Stud s;
int n;
cout << "输入数据" << endl;
cout << " 学生人数:";
cin >> n;
for (int i=0;i<n;i++) {
cout << " 第" << i+1 << "个学生";
s.getdata();
output.write((char *)&s,sizeof(s));
};
output.close();
}
void inputfunc() {
ifstream input("stud.dat");
Stud s;
cout << "输出数据" << endl;
cout << " 学生 姓名 成绩 等级" << endl;
input.read((char *)&s,sizeof(s));
while (input) {
s.disp();
input.read((char *)&s,sizeof(s));
};
input.close();
}
int main()
{
int sel;
do {
cout << "选择(1:输入数据 2:输出等级 其他退出):";
cin >> sel;
switch(sel) {
case 1:outputfunc();break;
case 2:inputfunc();break;
}
} while (sel==1 || sel==2);
return 0;
}
输出结果
关于Program Language更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.