Advertisement
Guest User

Untitled

a guest
May 26th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iterator>
  4. #include <fstream>
  5. #include <unordered_map>
  6. #include <vector>
  7. #include <list>
  8. #include <string>
  9. #include <sstream>
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. std::vector<std::string> splitroute(char* arg){ //bör ha throw exception om första
  17. std::string argstr;
  18. int counter = 0;
  19. std::string route[2];
  20. std::vector<std::string> routevec;
  21. //std::cout << arg << " char*" << std::endl; //temp
  22. argstr = arg;
  23. //std::cout << argstr << " string" << std::endl; //temp
  24. for (int i=0; i<argstr.length(); i++){
  25. if (isalnum(argstr[i]) || isblank(argstr[i])){
  26. route[counter]+=argstr[i];
  27. }
  28. else if(argstr[i]=='>'){
  29. //std::cout << "Hit '>' in argstr" << std::endl;
  30. counter++;
  31. }
  32. else{
  33. //std::cout << "Hit '-' in argstr" << std::endl; //temp
  34. }
  35. }
  36. routevec.push_back(route[0]);
  37. routevec.push_back(route[1]);
  38. //std::cout << routevec[0] << " : " << routevec[1] << std::endl;
  39.  
  40. return routevec;
  41. }
  42.  
  43. bool is_undirected(std::string value){
  44. if(value == "UNDIRECTED"){
  45. return true;
  46. }
  47. else{
  48. return false;
  49. }
  50.  
  51. }
  52.  
  53. std::vector<std::string> get_townslist(std::vector<std::string> filecontent){
  54. std::vector<std::string> townslist;
  55. for(int i = 1; i < filecontent.size(); i++){
  56. if(filecontent[i] == ""){
  57. break;
  58. }
  59. else
  60. {
  61. townslist.push_back(filecontent[i]);
  62. }
  63. }
  64. return townslist;
  65. }
  66.  
  67. bool is_route_in_townslist(std::vector<std::string>townslist, std::vector<std::string> route){ //THROW EXCEPTION???????
  68. int counter = 0;
  69. for(int j = 0; j < 2; j++){
  70. for(int i = 0; i < townslist.size();i++){
  71. if(route[j] == townslist[i])
  72. counter++;
  73. }
  74. }
  75. if(counter != 2){
  76. return false;
  77. }
  78. else if (counter == 2){
  79. return true;
  80. }
  81. return false;
  82. }
  83.  
  84. std::vector<std::vector<std::string>> split_data(std::vector<std::string>datavector){
  85. std::vector<std::vector<std::string>> towndata;
  86. /*std::string currentdata;
  87. int count = 0;
  88. for(int i = 0; i < datavector.size(); i++){
  89. for(int j = 0; j < datavector[i].length(); j++){
  90. if(isalnum(datavector[i][j]) || isspace(datavector[i][j])){
  91. currentdata += datavector[i][j];
  92. }
  93. else if(datavector[i][j]=='\t'){
  94. lesstowndata.push_back(currentdata);
  95. currentdata="";
  96. }
  97. else{
  98. lesstowndata.empty();
  99. currentdata="";
  100. }
  101. }
  102. towndata.push_back(lesstowndata);
  103. lesstowndata.empty();
  104. }*/
  105.  
  106. for(int i = 0; i < datavector.size(); i++){
  107. std::string input = datavector[i];
  108. std::istringstream ss(input);
  109. std::string currentdata;
  110.  
  111. std::vector<std::string> lesstowndata;
  112. while(std::getline(ss, currentdata, '\t')) {
  113. lesstowndata.push_back(currentdata);
  114. //std::cout << currentdata << '\n';
  115. }
  116. towndata.push_back(lesstowndata);
  117.  
  118. }
  119.  
  120. return towndata;
  121.  
  122. }
  123.  
  124. std::vector<std::string> datacontent(std::vector<std::string>filecontent){
  125. std::vector<std::string> content;
  126. int count = 0;
  127. for(int i=0; i < filecontent.size(); i++){
  128. if(!isalnum(filecontent[i][0])){
  129. count++;
  130. }
  131. else if(count==1){
  132. content.push_back(filecontent[i]);
  133. }
  134. }
  135. return content;
  136. }
  137.  
  138. std::vector<std::string> readfile(char* arg){
  139. std::vector<std::string> filecontent;
  140. std::string line;
  141. std::ifstream myfile;
  142. myfile.open(arg);
  143. if(myfile.is_open() & myfile.good()){
  144. while(std::getline(myfile, line)){
  145. filecontent.push_back(line);
  146. //std::cout << line << std::endl;
  147. }
  148. }
  149. else{
  150. std::cout << "Unable to open file" << std::endl;
  151. }
  152. myfile.close();
  153.  
  154. return filecontent;
  155. }
  156.  
  157.  
  158. int main(int argc, char* argv[]) {
  159. std::vector<std::string> filecontent;
  160. bool bidir;
  161. std::vector<std::string> townslist;
  162. std::vector<std::string> townsdatavector;
  163. std::vector<std::string> route;
  164. std::vector<std::vector<std::string>> towndata;
  165. if(argc != 3) {
  166. std::cerr << "Usage: " << argv[0]
  167. << " <FILEPATH> <ROUTE(X->Y)>" << std::endl;
  168. return 1;
  169. }
  170. filecontent = readfile(argv[1]);
  171.  
  172.  
  173. //std::cout << "argv[1]="<<argv[1]<<std::endl;
  174. //std::cout << "argv[2]="<<argv[2]<<std::endl;
  175.  
  176. route = splitroute(argv[2]);
  177. bidir = is_undirected(filecontent[0]);
  178. townslist = get_townslist(filecontent);
  179. /*std::cout << townslist.size() << "townslist size " << std::endl;
  180. std::cout << route[0] << ";" << route[1] << std::endl;*/
  181. is_route_in_townslist(townslist, route);
  182.  
  183. //print townslist
  184. std::cout << "List of all towns";
  185. for(int i = 0; i < townslist.size(); i++)
  186. std::cout << ":" << townslist[i];
  187. std::cout << std::endl;
  188. std::cout<< "Town size: " << townslist.size() << std::endl;
  189.  
  190. //print route
  191.  
  192. std::cout << "Route";
  193. for(int i = 0; i < route.size(); i++){
  194. std::cout << ": " << route[i];
  195. }
  196. std::cout << std::endl;
  197. std::cout << "The route contains " << route.size() << " elements" << std::endl;
  198.  
  199. //print routeintownslist
  200.  
  201. if(is_route_in_townslist(townslist, route)){
  202. std::cout << "Route is in townslist!"<< std::endl;
  203. }
  204. else{
  205. std::cout << "Route is not in townslist!" << std::endl;
  206. }
  207.  
  208.  
  209. townsdatavector = datacontent(filecontent);
  210.  
  211. /*for(int i = 0; i < townsdatavector.size(); i++){
  212. std::cout << townsdatavector[i]<< std::endl;
  213. }*/
  214.  
  215. towndata=split_data(townsdatavector);
  216.  
  217. //print towndata size
  218. std::cout << "Size of towndata(2d vector): "<< towndata.size() << std::endl;
  219. /*for(int i=0; i< towndata.size(); i++){
  220. std::cout << "Towndata:" << towndata[i][0] << ":" << towndata[i][1] << ":"<<towndata[i][2]<<std::endl;
  221. }*/
  222.  
  223.  
  224.  
  225.  
  226. /*
  227. for(int i=0; i < towndata.size(); i++){
  228. traingraph.addEdge(towndata[i][0],towndata[i][1],stoi(towndata[i][2]),bidir);
  229. }*/
  230.  
  231. return 0;
  232.  
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement