Advertisement
eric11144

ni_container

Jan 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5.  
  6. #include "container.h"
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. container_t user;
  13.  
  14. int count;
  15. int i;
  16. int size;
  17. char word;
  18.  
  19. cout << "input the malloc size: " << endl;
  20. cin >> size ;
  21.  
  22. char *str = (char*)malloc(sizeof(char) * (size+1));
  23.  
  24. cout << "you want to show the word and time" << endl;
  25. cin >> str ;
  26.  
  27. while(*str != '\0')
  28. {
  29. if(isalpha(*str))
  30. {
  31. word = *str;
  32. }
  33. else
  34. {
  35. count = atoi(str);
  36.  
  37. for(i = 0 ; i < count; i++)
  38. {
  39. user.insert(word);
  40. }
  41. }
  42.  
  43. str++;
  44. }
  45.  
  46. user.all_delete();
  47.  
  48. user.printlist();
  49. }
  50.  
  51. ==============================================================================================
  52. //container.cpp
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <assert.h>
  57. #include <iostream>
  58.  
  59. #include <vector>
  60.  
  61. #include "container.h"
  62.  
  63. using namespace std;
  64.  
  65. void container_t::insert(char c)
  66. {
  67. container.push_back(c);
  68. }
  69.  
  70. char container_t::all_delete()
  71. {
  72. vector<char>::iterator itr;
  73.  
  74. for(itr = container.begin(); itr != container.end(); itr++)
  75. cout << *itr << ' ';
  76. cout << '\n';
  77. }
  78.  
  79. void container_t::printlist()
  80. {
  81. container.erase(container.begin(), container.end());
  82.  
  83. if(container.empty())
  84. printf("Is empty\n");
  85. }
  86. //=========================================================================================
  87. //container.h
  88. #ifndef _LIB_PUSH_H_
  89. #define _LIB_PUSH_H_
  90.  
  91. #include <vector>
  92.  
  93. struct container_t
  94. {
  95. public:
  96. char all_delete();
  97. void insert(char);
  98. void printlist();
  99. std::vector<char> container;
  100. };
  101.  
  102. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement