请各位考生从课程信息发布网站下载数据文件location.txt,然后将数据文件手动保存在D盘根目录下。并按要求编写一个类Record,存储文件location.txt中的数据。该文件各个字段的信息如下:
Record类的要求如下:
class Record
{
private:
string name;
int hour, minute, second;
double latitude, longitude;
public:
…………
程序的main函数(注意:不得修改main函数!对main函数每修改一处扣2分,最多扣10分。)
int main()
{
Record record1("Randy", 11, 12, 13, 34.15, 117.21);
Record record2(record1);
cout << record2 << endl;
vector
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
- #include
- #include
- #include
- #include
- #include
- 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<
setw(10)< - cout<
setw(2)<":"<setw(2)<":"<setw(2)< - cout<
setprecision(10)<setw(20)< - cout<
setprecision(10)<setw(20)< - 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
&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
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
&v, int n) { - for(int i=0;i
size();i++){ - cout<
- 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
&v) { - for(vector
::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
&v) { - for(vector
::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
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;
- }
算是理解了吧。。。
-
相关阅读:
网络安全(黑客)自学
Day715. 适配不同的类型的switch匹配 -Java8后最重要新特性
vue-cli创建
JVM调优参数设置步骤
TouchGFX之Mixins
java判断字符串是否为数字或中文或字母
C 基础知识内容
单点登录SSO
从私有Git仓库的搭建到命令的使用再到分支管理,全流程全套服务包您满意
ubuntn20.4安装git
-
原文地址:https://blog.csdn.net/weixin_51305111/article/details/130914204