第10
下面是不完整的类定义:
class A {
public:
virtual void print(){
cout << “print come form class A” << endl;
}
};
B类:公共A{
私人:
字符*buf;
public:
void print() {
cout << “print come from class B” << endl;
}
};
void fun(A *a) {
delete a;
}
试完成其定义(需要增加必要的构造函数、析构函数)。
类A析构函数输出:A::~A()
类B析构函数输出:B::~B()
给定如下main函数:
int main() {
A *a = new B(10);
a->print();
fun(a);
B *b = 新 B(20);
乐趣(b);
返回 0;
}
【输入】
没有输入。
【输出】
主函数的输出已经写好。
/* 请在此处分别编写A、B类 */
class A
{
public:
virtual void print() {
cout << "print come form class A" << endl;
}
virtual ~A(){
cout << "A::~A() called" << '\n';
}
};
class B : public A {
private:
char* buf;
public:
B(int x){
}
void print() {
cout << "print come from class B" << endl;
}
~B()
{
cout << "B::~B() called" << '\n';
}
};
(1)声明并实现一个名为Arc的类,在二维空间中以某一个点为中心,绘制圆弧。Arc类包含私有数据成员p(圆心,Point类型),radius(圆弧半径,double类型);有参构造函数,将圆心、圆弧半径设置为给定的参数;成员函数draw,输出圆心和半径。
(2)声明并实现一个名为Circle的类,在二维空间中以某一个点为中心,绘制圆。Circle类包含私有数据成员p(圆心,Point类型),radius(圆半径,double类型);有参构造函数,将圆心、圆半径设置为给定的参数;成员函数draw,输出圆心和半径。
【输入】
没有输入。
【输出】
【提示】
需要自定义Point类。
给定如下main函数:
int main() {
Point p(320, 240);
混合混合(p, 100, 70);
mix.draw();
返回 0;
/* 请在此处分别编写Point、Arc、Circle、Ellipse、Rectangle、Mix类 */
class Point
{
public:
int x, y;
public:
Point(){}
Point(int x, int y)
{
this->x = x;
this->y = y;
}
};
class Arc
{
private:
Point p;
double radius;
public:
Arc(Point p, double radius)
{
this->p = p;
this->radius = radius;
}
void draw()
{
cout << "Drawing an arc: Center(" << p.x << ", " << p.y << ")" << ",radius(" << radius << ")" << '\n';
}
};
class Circle
{
private:
Point p;
double radius;
public:
Circle(Point p, double radius)
{
this->p = p;
this->radius = radius;
}
void draw()
{
cout << "Drawing an circle: Center(" << p.x << ", " << p.y << "),radius(" << radius << ")" << '\n';
}
};
class Ellipse
{
private:
Point p;
double xRadius, yRadius;
public:
Ellipse(Point p, double xRadius, double yRadius)
{
this->p = p;
this->xRadius = xRadius;
this->yRadius = yRadius;
}
void draw()
{
cout << "Drawing an ellipse: Center(" << p.x << ", " << p.y << "),x-axis(" << xRadius << "),y-axis(" << yRadius << ")" << '\n';
}
};
class Rectangle
{
private:
Point p;
double width, height;
public:
Rectangle(Point p, double width, double height)
{
this->p = p;
this->width = width;
this->height = height;
}
void draw()
{
cout << "Drawing an rectangle: Upper left corner coordinates("
<< p.x << ", " << p.y << ")width(" << width
<< "), height(" << height << ")" << '\n';
}
};
class Mix : public Arc, public Circle, public Ellipse, public Rectangle
{
public:
Mix(Point p, int a, int b):Arc(p, a), Circle(p, b), Ellipse(p, a, b), Rectangle(p, a, b)
{}
void draw()
{
Arc::draw();
Circle::draw();
Ellipse::draw();
Rectangle::draw();
}
};
( 1 )声明并实现一个名为 Person 的基类, Person 类有保护数据成员 name (姓名, string 类型)、(性别, char 类型, ' M ' 表示男, ' F ' 表示女)。以及有参构造函数,将姓名、性别设置为给定的参数;成员函数print,输出姓名和性别。
(2)从Person类派生出Student类,Student类有私有数据成员status(状态,枚举类型),表示年级(FRESHMAN、SOPHOMORE、JUNIOR、SENIOR),表示大一、大二、大三、大四学生。以及有参构造函数,将姓名、性别、状态设置为给定的参数;成员函数print,print函数输出姓名、性别和状态。
【输入】
没有输入。
【输出】
<span style="color:#000000">Name:ZhangSan, Sex:M
Name:LiSi, Sex:F</span>
/* 请在此处分别编写Person、Student、MyDate、Employee、Faculty、Staff类 */
class Person {
protected:
string name;
char sex;
public:
Person() {}
Person(string name, char sex)
{
this->name = name;
this->sex = sex;
}
void print()
{
cout << "Name:" << name << ", Sex:" << sex << endl;
}
};
enum Status { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
class Student : public Person {
private:
Status status;
public:
Student() {}
Student(string name, char sex, Status status)
{
this->name = name;
this->sex = sex;
this->status = status;
}
void print()
{
cout << "Name:" << name << ", Sex:" << sex << endl;
cout << "Status:" << (status == 0 ? "Freshman" : status == 1 ? "Sophomore" : status == 2 ? "Junior" : "Senior") << endl;
}
};
class MyDate {
private:
int year, month, day;
public:
MyDate(int year = 1900, int month = 1, int day = 1)
{
this->year = year;
this->month = month;
this->day = day;
}
void print()
{
cout << year << "-" << month << "-" << day << endl;
}
};
class Employee : public Person {
protected:
int salary;
MyDate dateHired;
public:
Employee() {}
Employee(string name, char sex, int salary, MyDate dateHired)
{
this->name = name;
this->sex = sex;
this->salary = salary;
this->dateHired = dateHired;
}
void print()
{
cout << "Name:" << name << ", Sex:" << sex << endl;
cout << "Salary:" << salary << ", Hire date:";
dateHired.print();
}
};
enum Rank { PROFESSOR, ASSOCIATE_PROFESSOR, LECTURER };
class Faculty : public Employee {
private:
Rank rank;
public:
Faculty() {}
Faculty(string name, char sex, int salary, MyDate dateHired, Rank rank)
{
this->name = name;
this->sex = sex;
this->salary = salary;
this->dateHired = dateHired;
this->rank = rank;
}
void print()
{
cout << "Name:" << name << ", Sex:" << sex << endl;
cout << "Salary:" << salary << ", Hire date:";
dateHired.print();
cout << "Rank:" << (rank == 0 ? "Professor" : rank == 1 ? "Associate_Professor" : "Lecturer") << endl;
}
};
enum Headship { PRESIDENT, DEAN, DEPARTMENT_CHAIRMAN };
class Staff : public Employee {
private:
Headship headship;
public:
Staff() {}
Staff(string name, char sex, int salary, MyDate dateHired, Headship headship)
{
this->name = name;
this->sex = sex;
this->salary = salary;
this->dateHired = dateHired;
this->headship = headship;
}
void print()
{
cout << "Name:" << name << ", Sex:" << sex << endl;
cout << "Salary:" << salary << ", Hire date:";
dateHired.print();
cout << "Headship:" << (headship == 0 ? "President" : headship == 1 ? "Dean" : "Department_Chairman" ) << endl;
}
};
(1)员工类是抽象类。Employee类的派生类有Boss类、CommissionWorker类、PieceWorker类和HourlyWorker类。
(2)Employee类包括私有数据成员name(姓名,string类型);有参构造函数,将name设置为给定的参数;访问器函数getName;还有纯虚函数show和earnings。show和earning函数将在每个派生类中实现,因为每个派生类显示的信息不同、计算工资的方法不同。
【输入】
输入Boss的姓名和周工资。
输入CommissonWorker的姓名、工资、佣金和销售数量。
输入PieceWorker的姓名、每件产品工资和生产数量。
输入HourlyWorker的姓名、小时工资、工作时数。
【输出】
见【输出示例】
【输入示例】
<span style="color:#000000">ZhangSan 800.0
LiSi 400.0 3.0 150
WangWu 2.5 200
LiuLiu 13.75 40</span>
class Employee {
private:
string name;
public:
Employee() {}
Employee(string name)
{
this->name = name;
}
string getName()
{
return name;
}
virtual void show() {}
virtual double earnings()
{
return 1.0;
}
};
class Boss : public Employee {
private:
double weeklySalary;
public:
Boss() {}
Boss(string name, double weeklySalary) : Employee(name)
{
this->weeklySalary = weeklySalary;
}
void setWeeklySalary(double weeklySalary)
{
this->weeklySalary = weeklySalary;
}
void show()
{
cout << "Boss: " << getName() << endl;
}
double earnings()
{
return weeklySalary;
}
};
class CommissionWorker : public Employee {
private:
double salary;
double commission;
int quantity;
public:
CommissionWorker() {}
CommissionWorker(string name, double salary, double commission, int quantity) : Employee(name)
{
this->salary = salary;
this->commission = commission;
this->quantity = quantity;
}
void setSalary(double salary)
{
this->salary = salary;
}
void setCommision(double commision)
{
this->commission = commision;
}
void setQuantity(int quantity)
{
this->quantity = quantity;
}
void show()
{
cout << "Commision Worker: " << getName() << endl;
}
double earnings()
{
return salary + commission * quantity;
}
};
class PieceWorker : public Employee {
private:
double wagePerPiece;
int quantity;
public:
PieceWorker() {}
PieceWorker(string name, double wagePerPiece, int quantity) : Employee(name)
{
this->wagePerPiece = wagePerPiece;
this->quantity = quantity;
}
void setWage()
{
this->wagePerPiece = wagePerPiece;
}
void setQuantity()
{
this->quantity = quantity;
}
void show()
{
cout << "Piece Worker: " << getName() << endl;
}
double earnings()
{
return wagePerPiece * quantity;
}
};
class HourlyWorker : public Employee {
private:
double wage;
double hours;
public:
HourlyWorker() {}
HourlyWorker(string name, double wage, double hours) : Employee(name)
{
this->wage = wage;
this->hours = hours;
}
void setWage()
{
this->wage = wage;
}
void setHours()
{
this->hours = hours;
}
void show()
{
cout << "Hourly Worker: " << getName() << endl;
}
double earnings()
{
return wage * hours;
}
};
(1)声明并实现一个名为Vehicle的基类,表示汽车。Vehicle类包括:
int类型的私有数据成员numberOfDoors,表示车门数量。
int类型的私有数据成员数OfCylinders,表示气缸数量。
string类型的私有数据成员color,表示汽车颜色。
double类型的私有数据成员fuelLevel,表示油位,即燃油数量。
int类型的私有数据成员transmissionType,表示变速箱类型,即0表示手动、1表示自动。
string类型的私有数据成员className,表示汽车类别。
【输入】
输入车门数量、气缸数量,颜色、燃油数量、变速箱类型。
【输出】
见【输出示例】
【输入示例】
2 6 red 50 1 4 6 yellow 60 0 2 16 black 100 0
/* 请在此处分别编写Vehicle、Taxi和Truck类 */
/* 请在此处分别编写Vehicle*/
class Vehicle
{
private:
int numberOfDoors, numberOfCylinders, transmissionType;
string color, className;
double fuelLevel;
public:
Vehicle(){}
Vehicle(int numberOfDoors, int numberOfCylinders, string color,
double fuelLevel, int transmissionType)
{
this->color = color;
this->fuelLevel = fuelLevel;
this->numberOfCylinders = numberOfCylinders;
this->numberOfDoors = numberOfDoors;
this->transmissionType = transmissionType;
}
int getNumberOfDoors()
{
return numberOfDoors;
}
int getNumberOfCylinders()
{
return numberOfCylinders;
}
int getTransmissionType()
{
return transmissionType;
}
string getColor()
{
return color;
}
string getClassName()
{
return className;
}
double getFuelLevel()
{
return fuelLevel;
}
void setColor(string color)
{
this->color = color;
}
void setClassName(string className)
{
this->className = className;
}
void setFuelLevel(double fuelLevel)
{
this->fuelLevel = fuelLevel;
}
friend ostream& operator << (ostream& out, const Vehicle& a)
{
out << "Vehicle" << '\n'
<< "Number of doors:" << a.numberOfDoors << '\n'
<< "Number of cylinders:" << a.numberOfCylinders << '\n'
<< "Transmission type:";
if (a.transmissionType)
out << "Automatic";
else
out << "Manual";
out << '\n'
<< "Color:" << a.color << '\n'
<< "Fuel level:" << a.fuelLevel << '\n';
return out;
}
};
class Taxi : public Vehicle
{
private:
bool customers;
int numberOfDoors, numberOfCylinders, transmissionType;
string color, className;
double fuelLevel;
public:
Taxi(int numberOfDoors, int numberOfCylinders, string color,
double fuelLevel, int transmissionType, bool customers)
{
this->color = color;
this->fuelLevel = fuelLevel;
this->numberOfCylinders = numberOfCylinders;
this->numberOfDoors = numberOfDoors;
this->transmissionType = transmissionType;
this->customers = customers;
}
bool hasCustomers()
{
return customers;
}
void setCustomers(bool customers)
{
this->customers = customers;
}
friend ostream& operator << (ostream& out, const Taxi& a)
{
out << "Vehicle" << '\n'
<< "Number of doors:" << a.numberOfDoors << '\n'
<< "Number of cylinders:" << a.numberOfCylinders << '\n'
<< "Transmission type:";
if (a.transmissionType)
out << "Automatic";
else
out << "Manual";
out << '\n'
<< "Color:" << a.color << '\n'
<< "Fuel level:" << a.fuelLevel << '\n';
if (a.customers)
out << "Has passengers" << '\n';
else
out << "Has no passengers" << '\n';
return out;
}
};
class Truck : public Vehicle
{
private:
bool cargo;
int numberOfDoors, numberOfCylinders, transmissionType;
string color, className;
double fuelLevel;
public:
Truck(int numberOfDoors, int numberOfCylinders, string color,
double fuelLevel, int transmissionType, bool cargo)
{
this->color = color;
this->fuelLevel = fuelLevel;
this->numberOfCylinders = numberOfCylinders;
this->numberOfDoors = numberOfDoors;
this->transmissionType = transmissionType;
this->cargo = cargo;
}
bool hasCargo()
{
return cargo;
}
void setCargo(bool cargo)
{
this->cargo = cargo;
}
friend ostream& operator << (ostream& out, const Truck& a)
{
out << "Vehicle" << '\n'
<< "Number of doors:" << a.numberOfDoors << '\n'
<< "Number of cylinders:" << a.numberOfCylinders << '\n'
<< "Transmission type:";
if (a.transmissionType)
out << "Automatic";
else
out << "Manual";
out << '\n'
<< "Color:" << a.color << '\n'
<< "Fuel level:" << a.fuelLevel << '\n';
if (a.cargo)
out << "Has passengers" << '\n';
else
out << "Has no passengers" << '\n';
return out;
}
};
下面是类A的定义,需要补充或增加完整的无参构造函数、虚析构函数以及fun()虚函数。
A 类 {
公共:
// ...
void g() {
fun();
}
};
构造函数输出:A constructor,并调用g()函数
析构函数输出:A destructor
fun()函数输出:Call class A's fun
下面是类B的定义,继承类A,需要补充或增加完整的无参构造函数、析构函数。
B类 {
公共:
// ...
};
无参构造函数输出:B constructor
析构函数输出:B destructor
/* 请在此处编写A、B、C类 */
class A
{
public:
A()
{
cout << "A constructor" << '\n';
g();
}
virtual ~A()
{
cout << "A destructor" << '\n';
}
virtual void fun()
{
cout << "Call class A's fun" << '\n';
}
void g()
{
fun();
}
};
class B : public A
{
public:
B()
{
cout << "B constructor" << '\n';
}
virtual ~B()
{
cout << "B destructor" << '\n';
}
};
class C : public B
{
public:
C()
{
cout << "C constructor" << '\n';
}
virtual~C()
{
cout << "C destructor" << '\n';
}
virtual void fun()
{
cout << "Call class C's fun" << '\n';
}
};
按要求计算数值。
【输入】
第一行是整数n,表示第二行有n个整数。
第二行:n个整数。
所有整数都在0和100之间。
【输出】
先输出:
1
100
101
101
对于输入中第二行的每个整数x,输出两行:
第一行:k=x;
第二行:x的平方。
【输入示例】
/* 请在此处编写相关代码 */
/* 请在此处编写相关代码 */
A(int x)
{
n = x;
}
A operator ++ (int)
{
A temp = *this;
n++;
return temp;
}
A& operator ++ ()
{
n++;
return *this;
}
friend ostream& operator<<(ostream& output,const A& a)
{
output << a.n;
return output;
}
operator int() const
{
return n;
}
有5个类A、B、C、D、E。它们的形式均为:
class T {
public:
T() {
cout << “T()” << endl;
}
~T() {
cout << “~T()” << endl;
}
};
这里T代表类名A、B、C、D、E。
给定如下main函数:
int main() {
E e;
return 0;
}
/* 请在此处分别编写A、B、C、D、E类,注意:类A、B、C、D、E之间的继承关系 */
/* 请在此处分别编写A、B、C、D、E类,注意:类A、B、C、D、E之间的继承关系 */
class C {
public:
C() {
cout << "C()" << endl;
}
~C() {
cout << "~C()" << endl;
}
};
class B : public C{
public:
B() {
cout << "B()" << endl;
}
~B() {
cout << "~B()" << endl;
}
};
class A : public C {
public:
A() {
cout << "A()" << endl;
}
~A() {
cout << "~A()" << endl;
}
};
class D : public A {
public:
D() {
cout << "D()" << endl;
}
~D() {
cout << "~D()" << endl;
}
};
class E : public B, public D {
public:
E() {
cout << "E()" << endl;
}
~E() {
cout << "~E()" << endl;
}
};
将输入数据按特定要求原样输出。
【输入】
第一行是整数t,表明一共t组数据。
对每组数据:
第一行是整数n,表示下面一共有n行,0<n<100。
下面的每行,以一个字母开头,然后跟着一个整数,两者用空格分隔。字母只会是'A'或'B'。整数范围0到100。
【输出】
对每组输入数据,将其原样输出,每组数据的最后输出一行“****”。
【输入示例】
【输出示例】
/* 请在此处编写A、B类和PrintInfo函数,并定义a数组 */
/* 请在此处编写A、B类和PrintInfo函数,并定义a数组 */
class A
{
public:
int x;
char op;
public:
A() {};
A(int x)
{
this->x = x;
op = 'A';
}
//~A() {}
};
class B : public A
{
public:
B(int x)
{
this->x = x;
op = 'B';
}
//~B() {};
};
A * a[1000];
void PrintInfo(A* a)
{
cout << a->op << ' ' << a->x << '\n';
}
(1)Shape类是抽象类,包括了纯虚函数getArea(求面积)、getPerimeter(求周长)、show(输出对象信息)以及成员函数getClassName(返回类名“Shape”)。
(2)Circle类继承了Shape类,包括了double类型的私有数据成员radius,表示圆半径;带默认参数的有参构造函数,radius的默认参数值为1;访问器函数getRadius和更改器函数setRadius;重定义了getArea(求圆面积)
【输入示例】
3.5
5.8 11.8
1 1.5 1
/* 请在此处编写Shape、Circle、Rectangle和Triangle类 */
class Shape
{
public:
virtual double getArea() { return 0; }
virtual double getPerimeter() { return 0; }
virtual void show() {}
virtual string getClassName() { return ""; }
};
class Circle : public Shape
{
private:
double radius;
public:
Circle(double r = 1)
{
radius = r;
}
double getRadius()
{
return radius;
}
void setRadius(double r)
{
radius = r;
}
double getArea()
{
return PI * radius * radius;
}
double getPerimeter()
{
return PI * radius * 2;
}
void show()
{
cout << "Radius:" << radius << '\n';
}
string getClassName()
{
return "Circle";
}
};
class Rectangle : public Shape
{
private:
double width, height;
public:
Rectangle(double w = 1, double h = 1)
{
width = w;
height = h;
}
double getWidth()
{
return width;
}
void setWidth(double w)
{
width = w;
}
double getHeight()
{
return height;
}
void setHeight(double r)
{
height = r;
}
double getArea()
{
return width * height;
}
double getPerimeter()
{
return (width + height) * 2;
}
void show()
{
cout << "Width:" << width << ", Height:" << height << '\n';
}
string getClassName()
{
return "Rectangle";
}
};
class Triangle : public Shape
{
private:
double side1, side2, side3;
public:
Triangle(double a, double b, double c)
{
side1 = a;
side2 = b;
side3 = c;
}
double getArea()
{
double p = (side1 + side2 + side3) / 2;
return sqrt(p * (side1 - p) * (side2 - p)* (side3 - p));
}
double getPerimeter()
{
return side1 + side2 + side3;
}
void show()
{
cout << "Side:" << side1 << ", " << side2 << ", " << side3 << '\n';
}
string getClassName()
{
return "Triangle";
}
};
十一
声明并实现一个Student类,表示学生信息。Student类包括:
int类型的私有数据成员num,表示学号。
string类型的私有数据成员name,表示姓名。
int类型的私有数据成员score,表示成绩
char类型的私有数据成员grade,表示等级。
无参(默认)构造函数。
【输入】
输入若干个学生的信息。
每行一个学生信息,学号、姓名和成绩之间以空格间隔。
学号为0时,输入结束。
【输出】
文件学生.txt。
不需要在屏幕上显示信息。
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
class Student {
public:
Student(int num, string name, int score, char grade) {
this->num = num;
this->name = name;
this->score = score;
this->grade = grade;
}
int getNum() const {
return num;
}
string getName() const {
return name;
}
int getScore() const {
return score;
}
char getGrade() const {
return grade;
}
friend ostream &operator<<(ostream &out, const Student &student);
friend istream &operator>>(istream &in, Student &student);
private:
int num;
string name;
int score;
char grade;
};
ostream &operator<<(ostream &out, const Student &student) {
out << student.num << " " << student.name << " "
<< student.score << " " << student.grade << endl;
return out;
}
istream &operator>>(istream &in, Student &student) {
in >> student.num >> student.name >> student.score >> student.grade;
return in;
}
int main() {
ofstream outFile;
outFile.open("student.txt");
if(!outFile.is_open()) {
exit(EXIT_FAILURE);
}
int num;
string name;
int score;
char grade;
cin >> num;
while(num != 0) {
cin >> name >> score;
switch(score / 10) {
case 10:
case 9: grade = 'A';
break;
case 8: grade = 'B';
break;
case 7: grade = 'C';
break;
case 6: grade = 'D';
break;
default: grade = 'E';
break;
}
Student student(num, name, score, grade);
outFile << student;
cin >> num;
}
outFile.close();
return 0;
}
以二进制方式打开图片文件并读取该文件中的第 13(从1开始计数,后同),49, 80 个字节处的值,求这3个二进制数按位异或的结果(16进制表示)。
可以鼠标右键另存为下载图片文件:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin("image.jpg", ios::binary);
char byte1, byte2, byte3;
fin.seekg(12, ios::beg);
fin.read((char *)&byte1, 1);
fin.seekg(48, ios::beg);
fin.read((char *)&byte2, 1);
fin.seekg(79, ios::beg);
fin.read((char *)&byte3, 1);
cout << hex << ((byte1 ^ byte2 ^ byte3) & 0x000000ff) << endl;
return 0;
}
将一个明文文件plaintext.txt中的内容,按照一定的方法,对每个字符加密后存放到另一个密文文件ciphertext.txt中。
【输入】
文件.txt。(该文件已经存在,无需自己创建)
注意:本地调试程序时,则要求自己预先建立好plaintext.txt文件。在Windows下,可以使用记事本。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
ifstream ifs;
ofstream ofs;
ifs.open("plaintext.txt");
ofs.open("ciphertext.txt");
string buf;
while (getline(ifs, buf)) {
for (int i = 0; i < buf.length(); i++) {
ofs << char(buf[i] + 2);
}
ofs<< '\n';
}
ifs.close();
ofs.close();
return 0;
}
【描述】
输入一篇英文文章,求出现次数前三名的字母,不区分大小写(即出现A也计入出现a的次数中),按照次数由大到小输出,如果出现次数一样,按字典顺序输出。其它字符不予考虑。
附带说明:英文中,每个字母出现的频率是不一样的,在破译英文密码时,尤其是凯撒密码,统计每个字母出现的频率有着重要意义。
可以下载英文文章:
#include<bits/stdc++.h>
#include<unordered_map>
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
struct node {
char ch;
ll cnt;
bool operator < (const node& b) const
{
return cnt > b.cnt;
}
}a[26];
int main()
{
fstream f;
f.open("news.txt");
string s;
for (ll i = 0; i <= 25; i++)
a[i].ch = 'a' + i;
while (getline(f, s))
{
for (char i : s)
{
if (!isalpha(i))
continue;
if (i <= 'Z')
a[i + 32 - 'a'].cnt++;
else
a[i - 'a'].cnt++;
}
}
sort(a, a + 26);
for (ll i = 0; i <= 2; i++)
cout << a[i].ch << ":" << a[i].cnt << '\n';
return 0;
}
处理日志文件,日志文件的储存格式为“年/月/日 时:分:秒 用户名 操作”。
日志文件有多条记录:
2015/4/218:00:33 37c3b6b58c6ac3 LOGIN
2015/4/218:15:35 11734e186f24fe4c LOGIN
2015/4/218:34:57 9f3cf331d19a9f LOGIN
2015/4/219:00:29 389bcca2159f5de7 LOGIN
2015/4/219:08:29 c3bde693fdb3c3d LOGIN
文件times.txt,注意:这里只给出了其中部分内容。
37c3b6b58c6ac 31887052
11734e186f24fe4c 2088931
9f3cf331d19a9f 2147511
389bcca2159f5de 71398643
c3bde693fdb3c3d 1297166
#include<bitsdc++.h>
using namespace std;
typedef long long ll;
const ll daytime = 86400;
int pos;
struct node{
string id;
ll sum,last;
} a[10100];
ll work(string date, string time) {
ll ans = 0, day, hour, min, sec,month;
month = date[5] - '0';
if (date.size() == 9) {
day = (date[7] - '0') * 10 + date[8] - '0';
} else {
day = date[7] - '0';
}
if (month == 4) {
ans += (day - 21) * daytime;
} else if (month == 5) {
ans += daytime * (9 + day );
} else {
ans += daytime * (40 + day);
}
if (time.size() == 7) {
hour = time[0] - '0';
min = (time[2] - '0') * 10 + time[3] - '0';
sec = (time[5] - '0') * 10 + time[6] - '0';
} else {
hour = (time[0] - '0') * 10 + time[1] - '0';
min = (time[3] - '0') * 10 + time[4] - '0';
sec = (time[6] - '0') * 10 + time[7] - '0';
}
ans += hour * 3600 + min * 60 + sec;
return ans;
}
int add(string name) {
for (int i = 1; i <= pos; i++) {
if (name== a[i].id){
return i;
}
}
a[++pos]={name,0,0};
return pos;
}
int main() {
ifstream inFile;
inFile.open("log.txt");
ofstream outFile;
outFile.open("times.txt");
string date, time, name, op;
pos = 0;
while (inFile>> date>> time>> name>> op) {
//cout<<" "<<date<<" "<<time<<" "<<name<<" "<<op<<endl;
int x = add(name);
int tmp = work(date, time);
if (op== "LOGIN") {
a[x].last = tmp;
} else {
a[x].sum += tmp - a[x].last;
}
}
for (int i = 1; i <= pos; ++i) {
outFile<< a[i].id<<" "<<a[i].sum<<endl;
//cout<< a[i].id<<" "<<a[i].sum<<endl;;
}
return 0;
}
【描述】
输入10个整数存入文本文件example.txt中,文件每行存放5个整数,每行整数之间用一个空格间隔。行末不能有多余的空格。
【输入】
输入10个整数。
【输出】
生成文件示例.txt,里面存放输入的10个整数。
不需要在屏幕上显示整数。
【输入示例】
1 2 3 4 5 6 7 8 9 10
【输出示例】
生成文件example.txt,其中内容:
1 2 3 4
6 7 8 9 10
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
ofstream outFile;
outFile.open("example.txt");
if(!outFile.is_open()) {
exit(EXIT_FAILURE);
}
int count = 1;
int x;
for(int i = 0; i < 10; ++i) {
cin >> x;
outFile << x;
if(i==5)
outFile << endl;
else
outFile << " ";
}
outFile.close();
return 0;
}
声明并实现了一个矩形类,表示矩形。Rectangle类包括:
double类型的私有数据成员width和height,表示矩形的宽和高。
带默认参数的构造函数,将矩形的宽和高设置为给定的参数。宽和高的默认参数值为1。
更改器函数setWidth和setHeight,分别用于修改矩形的宽和高。
访问器函数getWidth和getHeight,分别用于访问矩形的宽和高。
成员函数computeArea,返回矩形的面积。
成员函数computePerimeter,返回矩形的周长。
创建5个Rectangle对象(每个Rectangle对象的宽和高分别为1、2、3、4、5)并将它们写入二进制文件object.dat中。修改第3个对象的宽为10、高为3.5,再把修改后的该对象写回文件原位置。
【输入】
没有输入。
【输出】
生成文件object.dat
#include<iostream>
#include<fstream>
using namespace std;
class Rectangle {
private:
double width;
double height;
public:
Rectangle(double w = 1, double h = 1) {
width = w;
height = h;
}
double getWidth() {
return width;
}
double getHeight() {
return height;
}
void setWidth(double newWidth) {
width = newWidth;
}
void setHeight(double newHeight) {
height = newHeight;
}
double computeArea() {
return width * height;
}
double computePerimeter() {
return 2 * (height + width);
}
};
int main () {
ofstream fout;
ifstream fin;
fout.open("object.dat", ios::out | ios::binary);
Rectangle r[5];
for (int i = 0; i < 5; i++) {
r[i] = Rectangle(i + 1, i + 1);
}
fout.write((const char *)(&r), sizeof(r));
fout.close();
Rectangle res[5];
fin.open("object.dat", ios::in | ios::binary);
fin.read(reinterpret_cast<char *>(res), sizeof(res));
res[2].setHeight(3.5);
res[2].setWidth(10);
fin.close();
fout.open("object.dat", ios::out | ios::binary);
fout.write((const char *)(&r), sizeof(r));
fout.close();
return 0;
}
需要计算一些学生的加权平均分。给定输入文件gpa.dat,文件中每个学生的信息都由两行内容组成。第一行是学生姓名,第二行是他几门课的成绩。下面是某个输入文件gpa.dat的内容:
张三
3 2.8 4 3.9 3 3.1
李思
3 3.9 3 4.0 4 3.9
王武
2 4.0 3 3.6 4 3.8 1 2.8
刘刘
3 3.0 4 2.9 3 3.2 2 2.5
#include<bits/stdc++.h>
#include<unordered_map>
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll inf = 0x3f3f3f3f;
const double eps = 1e-4;
const ll N = 2e5 + 5;
const int mod = 1e9 + 7;
signed main()
{
ifstream infile;
infile.open("gpa.dat");
string s;
map<string, ll> mp;
double mx = 0, mi = inf;
//while(getline(cin, s))
while (getline(infile, s))
{
string sc;
//getline(cin, sc);
getline(infile, sc);
double aver = 0, sumxf = 0, cnt = 0;
for (ll i = 0; i < sc.size();)
{
//cnt++;
sumxf += sc[i] - '0';
aver += (sc[i] - '0') * (sc[i + 2] - '0' + (sc[i + 4] - '0') * 0.1);
i += 6;
}
//aver /= cnt;
double ans = aver / sumxf;
mi = min(ans, mi);
mx = max(ans, mx);
cout << "GPA for " << s << " = " << fixed << setprecision(2) << ans << '\n';
}
cout << "max GPA" << " = " << mx << '\n';
cout << "min GPA" << " = " << mi << '\n';
infile.close();
cout << mp.size();
return 0;
}
处理日志文件,日志文件的储存格式为“年/月/日 时:分:秒 用户名 操作”。
日志文件有多条记录:
2015/4/218:00:33 37c3b6b58c6ac3 LOGIN
2015/4/218:15:35 11734e186f24fe4c LOGIN
2015/4/218:34:57 9f3cf331d19a9f LOGIN
2015/4/219:00:29 389bcca2159f5de7 LOGIN
2015/4/219:08:29 c3bde693fdb3c3d LOGIN
【输入示例】
无
【输出示例】
123
#include<iostream>
#include<fstream>
#include<string>
#include<set>
using namespace std;
set<string> st;
string s;
int main () {
ifstream ifs;
ifs.open("log.txt");
while (ifs >> s) {
ifs >> s;
ifs >> s;
st.insert(s);
ifs>> s;
}
cout << st.size();
ifs.close();
return 0;
}
给定文件hours.txt,其中包含每个员工工作时间的记录。每一行表示一周的工作时间。每周有7天,所以每行最多有7个数字。规定每周从周一开始,文件中的每一行都是从周一的工作小时数开始,后面是周二,等等,周日的数字放在这一行的最后。每行中的数字可以少于7个,因为员工并不是每天都要工作。下面是文件hours.txt的内容:
8 8 8 8 8
8 4 8 4 4 4 8
4 8 4 8
3 0 0 8 6 4 4
8 8
0 0 8 8
8 8 8 4 4 4
#include<bits/stdc++.h>
#include<unordered_map>
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll inf = 0x3f3f3f3f;
const double eps = 1e-4;
const ll N = 2e5 + 5;
const int mod = 1e9 + 7;
signed main()
{
fstream f;
f.open("hours.txt");
ll a[10] = { 0 };
string s;
while (getline(f, s))
{
s += ' ';
ll num = 0, ans = 0;
for (ll i = -1, cnt = 1; i < (int)s.size() - 1; cnt++)
{
num = 0;
while (s[++i] != ' ')
num = num * 10 + s[i] - '0';
ans += num;
a[cnt] += num;
}
cout << "Total hours = " << ans << '\n';
}
cout << '\n';
cout << "Mon hours = " << a[1] << '\n';
cout << "Tue hours = " << a[2] << '\n';
cout << "Wed hours = " << a[3] << '\n';
cout << "Thu hours = " << a[4] << '\n';
cout << "Fri hours = " << a[5] << '\n';
cout << "Sat hours = " << a[6] << '\n';
cout << "Sun hours = " << a[7] << '\n';
cout << "Total hours = " << a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7];
return 0;
}