Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Code for problem 1
- ---------------------------------------------------------------------------------------------------
- #include <iostream>
- #include <cstdlib>
- #include <iomanip>
- #include <fstream>
- #include <tuple>
- #include <map>
- #include <string>
- using namespace std;
- int main(int argc, char* argv[])
- {
- if(argc!=2 && argc!=3)//1 for program name, 1 for file name, 1 for argument name
- cout<<"You have too many arguments."<<endl;
- map<string, tuple<int, int, int>> phonebook;
- ifstream in;
- string param;
- try
- {
- in.open(argv[1]);
- }
- catch(...)
- {
- cout<<"No such file exists."<<endl;
- return 1;
- }
- if(argc==3)
- param=argv[2];
- while(in)
- {
- string name;
- int areaCode, threeDigits, fourDigits;
- in>>name;
- in>>areaCode;
- in>>threeDigits;
- in>>fourDigits;
- if(in)
- {
- phonebook[name]=make_tuple(areaCode, threeDigits, fourDigits);
- }
- }
- if(phonebook.count(param)!=0)
- {
- int x,y,z;
- tie(x,y,z)=phonebook[param];
- cout<<param<<": ("<<x<<") "<<y<<"-"<<z<<endl;;
- }
- else
- {
- for(auto i:phonebook)
- {
- int x,y,z;
- tie(x,y,z)=i.second;
- cout<<i.first<<" "<<x<<" "<<y<<" 0"<<z<<endl;
- }
- }
- return 0;
- }
- --------------------------------------------------------------------------------------------------
- Code for problem 2
- -----------------------------------------------------------------------------------------------------
- #include <iostream>
- #include <cstdlib>
- #include <iomanip>
- #include <fstream>
- #include <tuple>
- #include <map>
- #include <string>
- #include <set>
- using namespace std;
- int main(int argc, char* argv[])
- {
- if(argc!=2)//1 for program name, 1 for file name
- cout<<"You have too many arguments."<<endl;
- set<int> codes;
- ifstream in;
- string param;
- try
- {
- in.open(argv[1]);
- }
- catch(...)
- {
- cout<<"No such file exists."<<endl;
- return 1;
- }
- while(in)
- {
- string name;
- int areaCode;
- int threeDigits, fourDigits;
- in>>name;
- in>>areaCode;
- in>>threeDigits;
- in>>fourDigits;
- codes.insert(areaCode);
- }
- for(auto i:codes)
- {
- cout<<i<<endl;
- }
- return 0;
- }
- --------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement