Created
September 24, 2011 16:00
-
-
Save torazuka/1239481 to your computer and use it in GitHub Desktop.
Chapter10_drill_PPPC++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
『ストラウストラップのプログラミング入門』 第10章 ドリル | |
*/ | |
#include "../../std_lib_facilities.h" | |
struct Point{ | |
double x; | |
double y; | |
}; | |
ostream& operator<<(ostream& os, const Point& p) | |
// Pointを出力する | |
{ | |
return os << '(' << p.x << ',' << p.y << ")\n"; | |
} | |
istream& operator>>(istream& is, Point& p) | |
{ | |
char c_open; | |
if(is >> c_open && c_open != '('){ | |
is.unget(); | |
is.clear(ios_base::badbit); | |
return is; | |
} | |
if(is.eof()){ | |
return is; | |
} | |
double x; | |
double y; | |
char c; // カンマ | |
char c_close; // 閉じ括弧 | |
is >> x >> c >> y >> c_close; | |
if(is.fail() || c != ',' || c_close != ')'){ | |
error("bad point"); | |
} | |
p.x = x; | |
p.y = y; | |
return is; | |
} | |
void write_mydata(vector<Point>& ps, string name) | |
{ | |
ofstream ofs(name.c_str()); | |
if(!ofs){ | |
error("ファイルが開けません。", name); | |
} | |
for(int i = 0; i < ps.size(); i++){ | |
ofs << ps[i]; | |
} | |
return; | |
} | |
bool operator==(Point p1, Point p2){ | |
if(p1.x == p2.x && p1.y == p2.y){ | |
return true; | |
} | |
return false; | |
} | |
bool operator!=(Point p1, Point p2){ | |
return !(p1 == p2); | |
} | |
int main() | |
{ | |
cout << "(x, y)形式で7つの座標データを入力してください。\n"; | |
vector<Point> original_points; | |
// ドリル(2) 7つの座標値を受け取る | |
int count = 0; | |
while(true){ | |
if(count == 7){ | |
break; | |
} | |
++count; | |
Point p; | |
if(!(cin >> p)){ | |
cout << "break"; | |
break; | |
} | |
original_points.push_back(p); | |
} | |
// ドリル(3) 受け取った座標値を出力する | |
cout << "入力値:\n"; | |
for(int i = 0; i < original_points.size(); i++){ | |
cout << original_points[i]; | |
} | |
// ドリル(4) 受け取った座標値をファイルmydata.txtに出力する | |
string name = "mydata.txt"; | |
write_mydata(original_points, name); | |
// ドリル(5) mydata.txtからデータを読み取り、processed_pointsに格納する | |
ifstream ifs(name.c_str()); | |
if(!ifs){ | |
error("ファイルを開けません。", name); | |
} | |
ifs.exceptions(ifs.exceptions()|ios_base::badbit); | |
vector<Point> processed_points; | |
while(true){ | |
Point p; | |
if(!(ifs >> p)){ | |
break; | |
} | |
processed_points.push_back(p); | |
} | |
// ドリル(6) 両方のvector要素を出力する | |
cout << "original_point: \n"; | |
for(int i = 0; i < original_points.size(); i++){ | |
cout << original_points[i]; | |
} | |
cout << "processed_points: \n"; | |
for(int i = 0; i < processed_points.size(); i++){ | |
cout << processed_points[i]; | |
} | |
// ドリル(7) 2つのvectorに差異があればメッセージを出力する | |
if(original_points.size() != processed_points.size()){ | |
cout << "Something's wrong!\n"; | |
} else { | |
for(int i = 0; i < original_points.size(); i++){ | |
if(original_points[i] != processed_points[i]){ | |
cout << "Something's wrong!\n"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment