Advertisement
EstEsca

Untitled

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