Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. Code for problem 1
  2. ---------------------------------------------------------------------------------------------------
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <tuple>
  8. #include <map>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. int main(int argc, char* argv[])
  14. {
  15. if(argc!=2 && argc!=3)//1 for program name, 1 for file name, 1 for argument name
  16. cout<<"You have too many arguments."<<endl;
  17. map<string, tuple<int, int, int>> phonebook;
  18. ifstream in;
  19. string param;
  20. try
  21. {
  22. in.open(argv[1]);
  23. }
  24. catch(...)
  25. {
  26. cout<<"No such file exists."<<endl;
  27. return 1;
  28. }
  29. if(argc==3)
  30. param=argv[2];
  31.  
  32. while(in)
  33. {
  34. string name;
  35. int areaCode, threeDigits, fourDigits;
  36. in>>name;
  37. in>>areaCode;
  38. in>>threeDigits;
  39. in>>fourDigits;
  40. if(in)
  41. {
  42. phonebook[name]=make_tuple(areaCode, threeDigits, fourDigits);
  43. }
  44. }
  45. if(phonebook.count(param)!=0)
  46. {
  47. int x,y,z;
  48. tie(x,y,z)=phonebook[param];
  49. cout<<param<<": ("<<x<<") "<<y<<"-"<<z<<endl;;
  50. }
  51. else
  52. {
  53. for(auto i:phonebook)
  54. {
  55. int x,y,z;
  56. tie(x,y,z)=i.second;
  57. cout<<i.first<<" "<<x<<" "<<y<<" 0"<<z<<endl;
  58. }
  59. }
  60. return 0;
  61. }
  62. --------------------------------------------------------------------------------------------------
  63. Code for problem 2
  64. -----------------------------------------------------------------------------------------------------
  65.  
  66. #include <iostream>
  67. #include <cstdlib>
  68. #include <iomanip>
  69. #include <fstream>
  70. #include <tuple>
  71. #include <map>
  72. #include <string>
  73. #include <set>
  74.  
  75. using namespace std;
  76.  
  77. int main(int argc, char* argv[])
  78. {
  79. if(argc!=2)//1 for program name, 1 for file name
  80. cout<<"You have too many arguments."<<endl;
  81. set<int> codes;
  82. ifstream in;
  83. string param;
  84. try
  85. {
  86. in.open(argv[1]);
  87. }
  88. catch(...)
  89. {
  90. cout<<"No such file exists."<<endl;
  91. return 1;
  92. }
  93. while(in)
  94. {
  95. string name;
  96. int areaCode;
  97. int threeDigits, fourDigits;
  98. in>>name;
  99. in>>areaCode;
  100. in>>threeDigits;
  101. in>>fourDigits;
  102. codes.insert(areaCode);
  103. }
  104. for(auto i:codes)
  105. {
  106. cout<<i<<endl;
  107. }
  108.  
  109. return 0;
  110. }
  111.  
  112. --------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement