// Question3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <ostream>
#include <istream>
#include <list>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include<sstream>
using namespace std;
struct Aneurysm
{
int x;
int y;
};
struct Subject
{
string id;
list<Aneurysm> locations;
};
Subject* InitSubject()
{
Subject* pSubject = new Subject;
pSubject->id = "101";
list<Aneurysm> listLocation;
for (int i = 0; i < 10; ++i)
{
Aneurysm *pAneurysm = new Aneurysm();
pAneurysm->x = i;
pAneurysm->y = i + 10;
listLocation.push_back(*pAneurysm);
}
pSubject->locations = listLocation;
return pSubject;
}
// Question3-A
ostream & operator << (ostream& ost, const Aneurysm& a)
{
ost << "x=" << a.x <<";" << "y=" << a.y << endl;
return ost;
}
ostream & operator << (ostream& ost, const Subject& s)
{
ost << "id=" << s.id.c_str()<< endl;
for (Aneurysm a : s.locations)
{
ost << "x=" << a.x <<";" << "y=" << a.y << endl;
}
return ost;
}
/
// Question3-B
// Aneurysm与Subject作为入参,只是读取不需要改变其值,所以作为const
// ost 为读取入参的值,而需要改变作为返回值,不能作为const
void split(string &s, vector<string> &vecData)
{
istringstream tmp_string(s);
string ss;
while (getline(tmp_string, ss, ';'))
{
vecData.push_back(ss);
}
}
// Question3-C, to do
istream& operator >> (istream& ist, Aneurysm& a)
{
std::string str;
int count = 0;
while (!ist.eof())
{
if (ist.bad())
{
std::cout << "input stream corrupted" << std::endl;
break;
}
if (ist.fail())
{
std::cout << "bad data" << std::endl;
ist.clear(std::istream::failbit);
ist.ignore(256, '\n');
continue;
}
std::getline(ist, str);
cout << "current Line:" << str << endl;
vector<string> vecData;
split(str, vecData);
string strX = vecData.at(0);
string strY = vecData.at(1);
int nPos = strX.find("=");
a.x = atoi(strX.substr(nPos + 1).c_str());
nPos = strY.find("=");
a.y = atoi(strY.substr(nPos + 1).c_str());
return ist;
}
}
// x=10;y=100 for istream.data content
void TestQuestion3_C_Aneurysm()
{
filebuf in;
if (!in.open("istream.data", ios::in))
{
cout << "fail to open file" << endl;
return;
}
istream iss(&in);
Aneurysm a;
iss >> a;
cout << "TestQuestion3_C_Aneurysm:" << endl;
cout << "a.x=" << a.x << ";" << "a.y" << a.y << endl;
}
// 1. C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的
/* // Question3-D
2.使用了explicit关键字,则无法进行隐式转换
如
Aneurysm a;
BoundingBox bb = a; // error, explicit关键字,则无法进行隐式转换
bb = BoundingBox(a) // OK,显示类型转换
*/
struct BoundingBox {
int min_x, min_y, max_x, max_y;
explicit BoundingBox(const Aneurysm & a):min_x(a.x),min_y{a.y}, max_x(a.x), max_y{ a.y }
{
}
// for question3-E
void operator() (const Aneurysm& a)
{
/get min x, y /
if (min_x > a.x)
{
min_x = a.x;
}
if (min_y > a.y)
{
min_y = a.y;
}
//get max x ,y /
if (max_x < a.x)
{
max_x = a.x;
}
if (max_y < a.y)
{
max_y = a.y;
}
}
};
// Test Question3-E,
void TestQuestion3_E()
{
Subject s = *InitSubject();
auto bb = BoundingBox{ s.locations.front() };
bb = for_each(s.locations.begin(), s.locations.end(), bb);
cout << "TestQuestion3_E:" << endl;
/* out put result
min_x :0,min_y :100
max_x :99,max_y :199
*/
cout << "min_x :" << bb.min_x << "," << "min_y :" << bb.min_y << endl;
cout << "max_x :" << bb.max_x << "," << "max_y :" << bb.max_y << endl;
}
// Question-F
struct RGB
{
RGB(int nR,int nG,int nB)
{
nRed = nR;
nGreen = nG;
nBlue = nB;
}
int nRed;
int nGreen;
int nBlue;
};
void setPixel(int nXpos, int nYPox, RGB color)
{
// draw pixel to image.
}
void TestQuestion3_F()
{
RGB rgb(178, 55, 255);
setPixel(250,200,rgb);
}
// Question-G, to do
void Question3_G()
{
Subject *pSubject = InitSubject();
auto bb = BoundingBox{ pSubject->locations.front() };
bb = for_each(pSubject->locations.begin(), pSubject->locations.end(), bb);
int nMinX = bb.min_x;
int nMiny = bb.min_y;
int nMaxX = bb.max_x;
int nMaxY = bb.max_y;
cout << "Question3_G:" << endl;
// check ,weather is out of the boundings
if(nMinX < 0 || nMinX < 0 || nMaxX > 400 || nMaxY > 300)
{
cout << "Error Data, because the data is out of the image bounding !" << endl;
}
else
{
cout << "The data check success !" << endl;
}
// delete heap memory
if (NULL != pSubject)
{
delete pSubject;
pSubject = NULL;
}
}
// Question3-H
bool cmp(Subject a, Subject b)
{
//按aneurysms from highest to lowest count
return a.locations.size() > b.locations.size();
}
void Question3_H()
{
vector<Subject> dataset;
sort(dataset.begin(),dataset.end(),cmp);
}
// x = 120 y = 240
void TestQuestion3_A_Aneurysm()
{
Aneurysm *pAneurysm = new Aneurysm();
pAneurysm->x = 120;
pAneurysm->y = 240;
cout << "TestQuestion3_A_Aneurysm:" << endl;
// output x=120 y = 240;
cout << *pAneurysm << endl;
if (NULL == pAneurysm)
{
delete pAneurysm;
pAneurysm = NULL;
}
}
/*
id = 101
x = 0 y = 100
x = 1 y = 101
x = 2 y = 102
x = 3 y = 103
x = 4 y = 104
x = 5 y = 105
x = 6 y = 106
x = 7 y = 107
x = 8 y = 108
x = 9 y = 109
*/
void TestQuestion3_A_Subject()
{
Subject* pSubject = InitSubject();
cout << "TestQuestion3_A_Subject:" << endl;
cout << *pSubject << endl;
// delete heap memory
if (NULL != pSubject)
{
delete pSubject;
pSubject = NULL;
}
}
int main()
{
// for Test Question3_A
TestQuestion3_A_Aneurysm();
TestQuestion3_A_Subject();
TestQuestion3_C_Aneurysm();
TestQuestion3_E();
TestQuestion3_F();
Question3_G();
}
3508

被折叠的 条评论
为什么被折叠?



