【苏大c++第二次考试模拟】

  1. 按以下要求编写程序

请各位考生课程信息发布网站下载数据文件location.txt,然后将数据文件手动保存D盘根目录下。并按要求编写一个类Record,存储文件location.txt中的数据。该文件各个字段的信息如下:

  1. 第一列表示用户的名字name;
  2. 第二列形如A:B:C,A表示小时hour、 B表示分钟minute、C表示秒钟second;
  3. 第三列表示纬度latitude;
  4. 第四列表示经度longitude;

Record类的要求如下:

  1. 该类的私有数据成员如下:

class Record

{

private:

string name;

int hour, minute, second;

double latitude, longitude;

public:

…………

  1. 该类public部分包含的函数如下:
  1. 构造函数,name默认值为“noName”,其余参数默认值为0;
  2. 六个常成员函数,分别返回名字、时、分、秒、纬度、经度;
  3. 友元函数,重载前自增操作符++,用于将当前对象的秒数加1。在实现的过程中需要注意进位问题。例如:13:59:59执行该操作的结果应该是14:00:00;23:59:59执行该操作的结果应该是00:00:00
  4. 友元函数,重载加操作符+,用于将当前对象的经度和纬度都加上一个double类型的数据;
  5. 拷贝构造函数,实现对象复制;
  1. 编载流输出运算符,用于显示一个对象的信息。其中,名字占10列并左对齐,时钟hour、分钟minute、秒钟second各占2列并右对齐,纬度latitude、经度longitude各占20列、精确到小数点后10位并右对齐。
  2. 编写函数inputData,完成数据的读入,结果存放在向量vec_record中。
  3. 编写函数outPut,用于把向量vec_record的前n条数据显示在屏幕上。注意:显示格式在重载流输出运算符中已有表述;如果向量中的数据不足n条时,则显示向量中的全部数据。
  4. 编写函数processTime,对向量vec_record中每个对象进行如下处理:如果对象的时间不在[6:00:00—22:00:00]范围内,则认为该对象是异常的,需要从vec_record中删除该对象;如果对象的时间在[6:00:00—22:00:00]范围内,则认为该对象是正常的并保留在vec_record中。
  5. 编写函数processLocation,完成如下功能:首先,调用重载的加号操作符,将vec_record中每个对象的经度和纬度各加0.1; 然后,判断对象的纬度是否在[34.0, 41.0]范围内、经度是否在[96.0,122.0]范围内如果经度和纬度都在上述范围内,则保留该对象,否则从向量中删除该对象。
  6. main函数的具体内容如下:

程序的main函数注意:不得修改main函数!对main函数每修改一处扣2分,最多扣10分。

int main()

{

Record  record1("Randy", 11, 12, 13, 34.15, 117.21);

Record  record2(record1);

cout << record2 << endl;

vector<Record> vec_record;

string path = "D:\\location.txt";

inputData(path, vec_record);

processTime(vec_record);

cout<< "After time processing: " << endl;

outPut(vec_record, 10);

processLocation(vec_record); //

cout<<"After location processing: " << endl;

outPut(vec_record, 10);

return 0;

}

程序运行结果:(不一定是标准答案

编程题评分标准

大项

子项

评分项

应得分

实得分

结果(65分)

1. 类结构

4

2. 带缺省值的构造函数

5

3. 拷贝构造函数

5

4. 常成员函数

6x1

5. 前自增运算符重载

5

6. 流输出运算符重载函数

5

7. 函数inputData

10

8. 函数processTime

10

9. 函数processLocation

10

10. 函数outPut

5

程序运行异常中断、死循环或无任何结果

-10

main函数修改处(最多扣10分)

-2 x n

缩进对齐(2分)

正确运用缩进对齐规则

2

有缩进对齐但不完全符合要求

1

没有使用缩进对齐规则

0

函数说明(2分)

有较完整的函数说明

2

有函数说明但不够完整

1

没有函数说明

0

注释(1分)

有注释

1

无注释

0

总分

#include<iostream>
#include<cmath>
#include<iomanip>
#include<vector>
#include<fstream>
#include<sstream>
using namespace std;
class Record
{
private:
	string name;
	int hour, minute, second;
	double latitude, longitude;
public:
/*
构造函数,name默认值为“noName”,其余参数默认值为0;
六个常成员函数,分别返回名字、时、分、秒、纬度、经度;
(暂不做)友元函数,重载前自增操作符++,用于将当前对象的秒数加1。在实现的过程中需要注意进位问题。例如:13:59:59执行该操作的结果应该是14:00:00;23:59:59执行该操作的结果应该是00:00:00。
(暂不做)友元函数,重载加号操作符+,用于将当前对象的经度和纬度都加上一个double类型的数据;
拷贝构造函数,实现对象复制;
*/
    Record(string m_name="noName",int m_hour=0,int m_minute=0, int m_second=0,double m_latitude=0, double m_longitude=0){
        this->name=m_name;
        this->hour=m_hour;
        this->minute=m_minute;
        this->second=m_second;
        this->latitude=m_latitude;
        this->longitude=m_longitude;
    }
    Record(const Record &r1){
        this->name=r1.name;
        this->hour=r1.hour;
        this->minute=r1.minute;
        this->second=r1.second;
        this->latitude=r1.latitude;
        this->longitude=r1.longitude;
    }
    void operator=(const Record&r);
    string getName(){return this->name;}
    int getHour(){return this->hour;}
    int getMinute(){return this->minute;}
    int getSecond(){return this->second;}
    double getLatitude(){return this->latitude;}
    double getLongitude(){return this->longitude;}
    friend ostream& operator<<(ostream&cout, const Record &r);
    friend Record operator+(const Record&r,double n);
    friend Record& operator++(Record &r);
};
/*
3.(暂不做,老师提供代码)重编载流输出运算符,用于显示一个对象的信息。其中,名字占10列并左对齐,时钟hour、分钟minute、秒钟second各占2列并右对齐,纬度latitude、经度longitude各占20列、精确到小数点后10位并右对齐。
*/

ostream& operator<<(ostream&cout, const Record &r){
    cout<<left<<setw(10)<<r.name;
    cout<<right<<setw(2)<<r.hour<<":"<<right<<setw(2)<<r.minute<<":"<<right<<setw(2)<<r.second;
    cout<<fixed<<setprecision(10)<<right<<setw(20)<<r.latitude;
    cout<<fixed<<setprecision(10)<<right<<setw(20)<<r.longitude;
    return cout;
}

Record operator+(const Record&r,double n){
/*友元函数,重载加号操作符+,用于将当前对象的经度和纬度都加上一个double类型的数据;*/
    Record r1(r);
    r1.latitude+=n;
    r1.longitude+=n;
    return r1;
}
Record& operator++(Record &r){
/*友元函数,重载前自增操作符++,用于将当前对象的秒数加1。
在实现的过程中需要注意进位问题。
例如:13:59:59执行该操作的结果应该是14:00:00;23:59:59执行该操作的结果应该是00:00:00。*/
    r.second+=1;
    if(r.second==60){
        r.second=0;
        r.minute++;
        if(r.minute==60){
            r.minute=0;
            r.hour++;
            if(r.hour==24){
                r.hour=0;
            }
        }
    }
    return r;
}
void Record::operator=(const Record &r1){
    this->name=r1.name;
    this->hour=r1.hour;
    this->minute=r1.minute;
    this->second=r1.second;
    this->latitude=r1.latitude;
    this->longitude=r1.longitude;
    
}
/*
4.编写函数inputData,完成数据的读入,结果存放在向量vec_record中。
*/
void inputData(string path,vector<Record> &v){
    ifstream ifile(path);
    while(!ifile.eof()){
        string m_name;
        string time;
        int m_hour,m_minute,m_second;
        double m_latitude, m_longitude;
        ifile>>m_name>>time>>m_latitude>>m_longitude;
        for(int i=0;i<time.length();i++){
            if(time[i]==':'){time[i]=' ';}
        }
        stringstream ss(time);
        ss>>m_hour>>m_minute>>m_second;
        Record tempR(m_name,m_hour,m_minute,m_second,m_latitude,m_longitude);
        v.push_back(tempR);
    }
    ifile.close();
}
/*
5.编写函数outPut,用于把向量vec_record的前n条数据显示在屏幕上。注意:显示格式在重载流输出运算符中已有表述;如果向量中的数据不足n条时,则显示向量中的全部数据。
*/
void outPut(const vector<Record>&v, int n){
    for(int i=0;i<v.size();i++){
        cout<<v[i]<<endl;
        if(i+1==n){
            return ;
        }
    }
}
/*
6.编写函数processTime,对向量vec_record中的每个对象进行如下处理:如果对象的时间不在[6:00:00—22:00:00]范围内,则认为该对象是异常的,需要从vec_record中删除该对象;如果对象的时间在[6:00:00—22:00:00]范围内,则认为该对象是正常的并保留在vec_record中。
*/
void processTime(vector<Record>&v){
    for(vector<Record>::iterator it=v.begin();it!=v.end();){
        if(it->getHour()>22 or it->getHour()<6){
            it=v.erase(it);
        }
        else{
            it++;
        }
    }
}
/*
7.(暂不做)编写函数processLocation,完成如下功能:首先,调用重载的加号操作符,将vec_record中每个对象的经度和纬度各加0.1; 然后,判断该对象的纬度是否在[34.0, 41.0]范围内、经度是否在[96.0,122.0]范围内;如果经度和纬度都在上述范围内,则保留该对象,否则从向量中删除该对象。
*/
void processLocation(vector<Record>&v){
    for(vector<Record>::iterator it=v.begin();it!=v.end();){
        (*it)=(*it)+0.1;
        if(it->getLatitude()<=41 and it->getLatitude()>=34 and it->getLongitude()>=96 and it->getLongitude()<=122){
            it++;
        }
        else{
            it=v.erase(it);
        }
    }  
}
int main()
{
Record  record1("Randy", 11, 12, 13, 34.15, 117.21);
	Record  record2(record1);
	cout << record2 << endl;

	vector<Record> vec_record;
	string path = "/Users/apple/Desktop/第一次模拟/location.txt";
	inputData(path, vec_record);

	processTime(vec_record);
	cout<< "After time processing: " << endl;
	outPut(vec_record, 10);

	processLocation(vec_record);	//(暂不做)
	cout<<"After location processing: " << endl;
	outPut(vec_record, 10);
	return 0;
} 

算是理解了吧。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值