Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 #include <iostream>
- 2 #include <fstream>
- 3 #include <map>
- 4
- 5
- 6 class Commodity {
- 7 private:
- 8 int id_;
- 9 std::string name_;
- 10 int price_;
- 11 public:
- 12 Commodity() {
- 13 id_ = 0;
- 14 name_ = std::string("");
- 15 price_ = 0;
- 16
- 17 }
- 18 Commodity(int id, std::string name, int price) {
- 19 id_ = id;
- 20 name_ = name;
- 21 price_ = price;
- 22
- 23 }
- 24 ~Commodity() {}
- 25 void saveFile(std::string filename) {
- 26 std::ofstream output(filename.c_str(), std::ios::app);
- 27 output << id_ << " " << name_ << " " << price_ << std::endl;
- 28
- 29
- 30 output.close();
- 31 return;
- 32
- 33
- 34 }
- 35
- 36
- 37 };
- 38
- 39
- 40 class Pos {
- 41 private:
- 42 std::map<int, Commodity> Mcommodity;
- 43 public:
- 44 Pos(){
- 45 std::map<int, Commodity> Mcommodity;
- 46 }
- 47
- 48 int function(int id, Commodity co)
- 49 {Mcommodity[id]=co;
- 50
- 51 };
- 52
- 58 int main(){
- 59 Pos Pos =pos();
- 60
- 61 std::ifsteam commodity("commodity.txt");
- 62 while( commodity>>co_id>>co_name>>co_price) { //여기에서 co_id가 결정됨!
- 63 Commodity co( co_id, co_name, co_price);
- 64 (int id; 779999<id<880023) //따라서 여기서 id라는 변수를 넣을 필요가 없음
- {Pos.function(id,co)} //id를 co_id로...
- 65
- 66 }
- 67
- 68 int co_id; //변수 선언은 변수를 쓰기 전에(co_id가 위에 쓰였으니 그줄 위 아무데나 선언)
- 69 std::string co_name; //마찬가지...
- 70 int co_price;
- 71
- 72 std::ifstream commodity("commodity.txt"); //이제는 지워도 되는 예제부분
- 73 while( commodity>>co_id>>co_name>>co_price) {
- 74 std::cout<<co_id<<" "<<co_name<<" "<<co_price<<std::endl;
- 75 Commodity co( co_id, co_name, co_price );
- 76 if ( co_id == 880001) {
- 77 co.saveFile("output.txt");
- 78 }
- 79 }
- 80
- 81
- 82
- 83
- 84 return 0;
- 85 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement