Advertisement
userxbw

std::list manipulation c++

Sep 8th, 2022 (edited)
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <list>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. /* alist.cpp */
  8.  
  9. /* g++ filename -std=17 */
  10.  
  11. using std::cin,std::cout,std::endl,
  12. std::cerr,std::list,std::getline,
  13. std::string,std::ios,std::fstream;
  14.  
  15.  
  16. /*
  17.   for c++ 11 comment out the using line above
  18.  and uncomment the line below and make
  19. further adjustments if needed maybe fstream
  20. or just compile it like it is and watch it work
  21. */
  22.  
  23. // using namespace std;
  24.  
  25. void usage();
  26. int parse_options( list<int> &l,
  27. string options,fstream &file);
  28. int search_list(list<int> &s,int v);
  29. void stars(int s);
  30. int data_storage(fstream &d);
  31. int write_to_file(list<int> &l,
  32. fstream &file);
  33. int print_file(fstream &file);
  34. void windex();
  35.  
  36.  
  37. /*  work in progress */
  38.  
  39. int
  40. main () {
  41.     char run='y';
  42.     list<int> the_list{};
  43.     string options;
  44. /*
  45.     let's save this crap
  46.     data in a file, and
  47.     recall it for further
  48.     processing
  49.     appending to file
  50.     clearing file
  51.     updating file
  52. */
  53. string crap="alist_crap_data_file";
  54.  
  55. /*  works just fine here
  56.     but not in a function
  57.     BS
  58. */
  59.   fstream
  60. f(crap,ios::in|ios::out|ios::app);
  61.  
  62. if(data_storage(f))
  63.     cout<<"that worked!\n";
  64. else
  65.     cout<<"ha crap \n";
  66.  
  67. while(run=='y'){
  68. usage();
  69. cout<<"enter your slection"
  70. <<endl;
  71. getline(cin,options);
  72.     if(!parse_options(the_list,options,f))
  73.         run='n';
  74. } // end while
  75.  
  76. return 0;
  77. } // end main
  78.  
  79. int parse_options(list<int> &l,string options, fstream &file){
  80. int error=-1;
  81. char c='\0';
  82. int pos,value=0;;
  83. string crap,opt,val;
  84.     /* find the command */
  85. if( (pos=options.find("-") !=
  86.      string::npos)){
  87.     crap=options.substr(pos);
  88.     opt=crap.substr(0,pos);
  89.     val=options.substr(pos + 1);
  90.     if(opt.size() == 1)
  91.         c  = opt[0];
  92.     try{
  93.     if(val.size()>=1)
  94.         value=std::stoi(val);
  95.     }catch(std::invalid_argument& stoi){
  96.     cout<<
  97.        "terminating"
  98.        " exception of type"
  99.        " std::invalid_argument:"
  100.        " stoi: no conversion"
  101.        <<endl;
  102.        /* just drop it into default
  103.           switch and give feed back
  104.           of orginal command given */
  105.        c='?';
  106.     } // end try catch
  107. }
  108.         switch(c){
  109.             case 'i':
  110.                 windex();
  111.                 stars(45);
  112.                 cout<<"insert\n";
  113.                 stars(45);
  114.                 if(value>=1 &&
  115.                     value<=pow(10,3)){
  116.                       l.push_back(value);
  117.                       error=1;
  118.                }else{
  119.                windex();
  120.                 stars(45);
  121.                cout<<"Value "
  122.                <<value
  123.                <<" is not within limits "
  124.                <<"1 - "
  125.                <<pow(10,3)
  126.                <<endl;
  127.                stars(45);
  128.                error=1;
  129.                }
  130.                 break;
  131.             case 'd':
  132.                 windex();
  133.                 stars(45);
  134.                 cout<<"delete\n";
  135.                 stars(45);
  136.             break;
  137.             case 'r':
  138.                 windex();
  139.                 stars(45);
  140.                 cout<<"refresh\n";
  141.                 stars(45);
  142.             break;
  143.             case 's':
  144.             windex();
  145.             stars(45);
  146.             cout<<"searching "
  147.             <<endl;
  148.             stars(45);
  149.             if(
  150.             search_list(l,value)
  151.             ){
  152.                 windex();
  153.                 stars(54);
  154.                 cout<<"found "
  155.                 <<value
  156.                 <<endl;
  157.                 stars(54);
  158.                error=1;
  159.                 }else{
  160.                 windex();
  161.                 stars(45);
  162.                 cout<<"not found"
  163.                 <<endl;
  164.                 stars(45);
  165.                 error=1;
  166.                 }
  167.             break;
  168.             case 'x':
  169.                 windex();
  170.                 stars(45);
  171.                 cout<<"search and destroy[ete]"
  172.                 <<endl;
  173.                 stars(45);
  174.             break;
  175.             case 'q':
  176.                 windex();
  177.                 stars(54);
  178.                 cout<<"QUITING: ohhh nooooo!\n";
  179.                 stars(54);
  180.                 error=0;
  181.                 file.close();
  182.                 break;
  183.            case 'w':
  184.                windex();
  185.                 stars(45);
  186.                 cout<<"write to file\n";
  187.                 stars(45);
  188.                 write_to_file(l,file);
  189.             break;
  190.             case 'p':
  191.                 windex();
  192.                 stars(56);
  193.                 cout<<"print file\n";
  194.                 print_file(file);
  195.                 break;
  196.  
  197.            default:
  198.                 windex();
  199.                 stars(45);
  200.                 cerr
  201.                 <<"Invalid command "
  202.                 <<options
  203.                 <<endl;
  204.                 stars(45);
  205.                 error=1;
  206.            break;
  207.            } // end switch
  208.  
  209.  
  210.   /* left overs
  211.  
  212. for(auto it : the_list){
  213.     cout<<"list "<<it<<endl;
  214.     }
  215.     */
  216.     if(error)
  217.         return 1;
  218.    else
  219.         return 0;
  220.  
  221. } // end parse
  222.  
  223. int search_list(list<int> &s,int v){
  224.    auto it =
  225.    find(s.begin(),s.end(),v);
  226.                  if(it != s.end())
  227.                     return 1;
  228.                  else
  229.                     return 0;
  230. }
  231.  
  232. void usage(){
  233.     cout<<"\nUSAGE:\n"
  234.     <<" <option> [argument] \n"
  235.     <<endl
  236.     <<"insert  < -i > [argumen]\n"
  237.     <<"delete  < -d > no argument\n"
  238.     <<"refresh < -r > no argument\n"
  239.     <<"search  < -s > [ argument ]\n"
  240.     <<"search and delete:\n"
  241.     <<"\t"<<"< -x > [ argument ]\n"
  242.     <<"write to file\n"
  243.     <<"\t"<<"< -w > no argument\n"
  244.     <<"print file\n"
  245.     <<"\t"<<"< -p > no argument\n"
  246.     <<"quit     < -q >"
  247.     <<endl;
  248. }
  249. void stars(int s){
  250. for(int i=0;i<s;i++)
  251.     cout<<"*";
  252. cout<<endl;
  253. }
  254.  
  255.  
  256. int data_storage(fstream &d){
  257. // works in main just not in function???
  258. //fstream
  259. //d(s,ios::in|ios::out|ios::app);
  260. if(!d.is_open())
  261.     return 0;
  262. else
  263.     return 1;
  264. }
  265.  
  266. int write_to_file(list<int> &l,fstream &file){
  267. if(file){
  268. for(auto const &it:l){
  269.     file << it << endl;
  270. }
  271. return 1;
  272. }else{ return 0;}
  273.  
  274. }
  275.  
  276. int print_file(fstream &file){
  277.     if(file){
  278. string st;
  279. file.seekp(0,ios::beg);
  280. stars(55);
  281. while(getline(file,st)){
  282.     cout<< st <<endl;
  283.     stars(55);
  284.     return 1;
  285.     }
  286. }else
  287.     return 0;
  288. return -1;
  289. }
  290.  
  291. void windex(){
  292. #ifdef __linux__
  293.     system("clear");
  294. #elif _WIN32
  295.     system("cls");
  296. #endif
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement