Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <utility>
  6. #include <algorithm>
  7.  
  8.  
  9. bool isDigitOrX(char s, std::string &digitsOrXs, std::string & otherChars){
  10. if (!(isdigit(s)||s=='X')){
  11. digitsOrXs.push_back(' ');
  12. otherChars.push_back(s);
  13. return false;
  14. }
  15. digitsOrXs.push_back(s);
  16. return true;
  17. }
  18. int formatNumbers(std::string in = "input.txt", std::string out="output.txt"){
  19. std::ifstream infile(in);
  20. std::ofstream outfile(out);
  21.  
  22. std::string line;
  23. if (!infile.is_open()) {
  24. std::cerr<<"file can't open\n";
  25.  
  26. }
  27. std::getline(infile, line);
  28.  
  29. std::cout<<"l:"<<line<<"\n";
  30. int N = std::stoi(line);
  31. std::vector<std::string> numbers(N);
  32. std::cout<<"mllmlm\n";
  33. for (int i=0; i<N; i++){
  34.  
  35. std::getline(infile, line);
  36. std::cout<<"number"<<i<<" "<<line<<"\n";
  37. line.erase(remove_if(line.begin(), line.end(), [](char c) { return !isdigit(c); } ), line.end());
  38. numbers.push_back(line);
  39. }
  40. std::cout<<"l:"<<line<<"\n";
  41. std::getline(infile, line);
  42. N = std::stoi(line);
  43. std::vector<std::pair<std::string, std::string>> patterns(N);
  44. for (int i=0; i<N; i++){
  45. std::getline(infile, line);
  46. std::string otherChars, digitsOrXs;
  47. line.erase(remove_if(line.begin(), line.end(), [](char c) { return !/*isDigitOrX(c,digitsOrXs,otherChars)*/isdigit(c); } ), line.end());
  48. patterns.push_back(std::make_pair(digitsOrXs, otherChars));
  49. }
  50. for (auto const number:numbers){
  51. int maxConcordance = 0;
  52. int indexMaxConcordancePattern = -1;
  53. int concordance=0;
  54. for(int i=0; i<patterns.size(); i++){
  55. auto const pattern = patterns[i];
  56. concordance=0;
  57. bool cont = true;
  58. for(int j = 0; j<pattern.first.size()&&cont; j++){
  59. auto const charFromPattern = pattern.first[j];
  60. for(int h= 0; h<number.size()&&cont; h++){
  61. auto const charFromNumber = number[h];
  62. if (charFromPattern!=' '){
  63. if(charFromPattern == 'X'){
  64. concordance++;
  65. }
  66. else if(charFromPattern == charFromNumber){
  67. concordance++;
  68. }
  69. else{
  70. cont=false;
  71. }
  72. }
  73. }
  74. }
  75. std::cout<<" "<<patterns[indexMaxConcordancePattern].first;
  76. if(concordance>maxConcordance){
  77. maxConcordance = concordance;
  78. indexMaxConcordancePattern = i;
  79. }
  80. }
  81. auto dOrX = patterns[indexMaxConcordancePattern].first;
  82. auto othersIt = patterns[indexMaxConcordancePattern].second.begin();
  83. auto numbIt = number.begin();
  84. std::string result;
  85. for (auto const s:dOrX){
  86. if (s==' '){
  87. result.push_back(/**(othersIt++)*/'j');
  88. }
  89. else {
  90. result.push_back(/**(numbIt++)*/'k');
  91. }
  92. }
  93. outfile<<result<<"\n";
  94. }
  95.  
  96. }
  97.  
  98.  
  99. int main(int argc, char *argv[])
  100. {
  101. formatNumbers("D:\\QtCreatorSources\\build-phoneNumbers-Desktop_Qt_5_7_0_MinGW_32bit-Debug\\debug\\input.txt", "D:\\QtCreatorSources\\build-phoneNumbers-Desktop_Qt_5_7_0_MinGW_32bit-Debug\\debug\\output.txt");
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement