Taras9

by pif on December 28th, 2009
No notes
Syntax: C++
Show lines - Hide lines - Show in textbox - Download
  1. /*
  2.  * File: main.cpp
  3.  * Author: pif
  4.  *
  5.  * 1.Використати контейнер map або multimap для зберігання списку передплатних
  6.  * видань (назва, місце видання, передплатний індекс, вартість).
  7.  *
  8.  *
  9.  * пошук всіх елементів із заданим ключем;
  10.  * вставка нових елементів в контейнер;
  11.  * видалення елементів із заданим ключем.
  12.  *
  13.  *
  14.  * Created on 22 грудня 2009, 14:18
  15.  */
  16.  
  17. #include <iostream>
  18. #include <map>
  19. #include <string>
  20. #include <fstream>
  21. using namespace std;
  22.  
  23. struct magazine
  24. {
  25. string name;
  26. string place;
  27. double price;
  28. };
  29. /*
  30.  *
  31.  */
  32. int main(int argc, char** argv)
  33. {
  34. map<int, magazine> m;
  35. map<int, magazine>::iterator it;
  36. ifstream ff("in.txt");
  37. int n;
  38. ff>>n;
  39. for(int i=0;i<n;++i)
  40. {
  41. magazine value;
  42. int key;
  43. ff>>key;
  44. ff>>value.name>>value.place>>value.price;
  45. m.insert(make_pair(key,value));
  46. }
  47.  
  48. for(it=m.begin();it!=m.end();++it)
  49. {
  50. cout<<"Index: "<<(*it).first<<endl;
  51. cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
  52. }
  53.  
  54.  
  55. while(true)
  56. {
  57. int c=0;
  58. cout<<"Input command #: "<<endl;
  59. cout<<"1 - Search by key"<<endl<<"2 - Search by value"<<endl<<"3 - Insert new Magazine"<<endl<<"4 - Delete by key"<<endl<<"5 - Delete by value"<<endl<<"6 - Output all magazines"<<endl;
  60. cin>>c;
  61. int k=0,type=0;double dd=0;magazine value;
  62. string v="", n="";
  63. switch(c)
  64. {
  65. case 1:
  66. cout<<"Enter key: ";
  67. cin>>k;
  68. it=m.find(k);
  69. if(it!=m.end())
  70. {
  71. cout<<"Index: "<<(*it).first<<endl;
  72. cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
  73. }
  74. break;
  75. case 2:
  76. cout<<"Which parameter do you want to search: "<<endl<<"1 - Name"<<endl<<"2 - Place"<<endl<<"3 - Price"<<endl;
  77. cin>>type;
  78. switch(type)
  79. {
  80. case 1:
  81. cin>>n;
  82. for(it=m.begin();it!=m.end();++it)
  83. if((*it).second.name == n)
  84. {
  85. cout<<"Index: "<<(*it).first<<endl;
  86. cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
  87. }
  88. break;
  89. case 2:
  90. cin>>n;
  91. for(it=m.begin();it!=m.end();++it)
  92. if((*it).second.place==n)
  93. {
  94. cout<<"Index: "<<(*it).first<<endl;
  95. cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
  96. }
  97. break;
  98. case 3:
  99. cin>>dd;
  100. for(it=m.begin();it!=m.end();++it)
  101. if((*it).second.price==dd)
  102. {
  103. cout<<"Index: "<<(*it).first<<endl;
  104. cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
  105. }
  106. break;
  107. }
  108. break;
  109. case 3:
  110. cout<<"Enter index, name, place, price of magazine: "<<endl;
  111.  
  112. int key;
  113. cin>>key>>value.name>>value.place>>value.price;
  114. m.insert(make_pair(key,value));
  115. break;
  116. case 4:
  117. cout<<"Enter key: ";
  118. cin>>k;
  119. it=m.find(k);
  120. if(it!=m.end()) m.erase(it);
  121. break;
  122. case 5:
  123. cout<<"Which parameter do you want to search & delete: "<<endl<<"1 - Name"<<endl<<"2 - Place"<<endl<<"3 - Price"<<endl;
  124. cin>>type;
  125. switch(type)
  126. {
  127. case 1:
  128. cin>>n;
  129. for(it=m.begin();it!=m.end();++it)
  130. if((*it).second.name==n)
  131. m.erase(it);
  132. break;
  133. case 2:
  134. cin>>n;
  135. for(it=m.begin();it!=m.end();++it)
  136. if((*it).second.place==n)
  137. m.erase(it);
  138. break;
  139. case 3:
  140. cin>>dd;
  141. for(it=m.begin();it!=m.end();++it)
  142. if((*it).second.price==dd)
  143. m.erase(it);
  144. break;
  145. }
  146. break;
  147. case 6:
  148. for(it=m.begin();it!=m.end();++it)
  149. {
  150. cout<<"Index: "<<(*it).first<<endl;
  151. cout<<"Name: "<<(*it).second.name<<endl<<"Place: "<<(*it).second.place<<endl<<"Price: "<<(*it).second.price<<endl;cout<<endl;
  152. }
  153. break;
  154. default: return 0;
  155. }
  156. }
  157.  
  158.  
  159. ff.close();
  160. return 0;
  161. }
  162.  
  163.  
1 Comment

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS