Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <fstream>
  5. #include <map>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. typedef map <string, void*> row;
  10. typedef map <string, void*> :: iterator itRow;
  11. typedef map <string, string> Header;
  12. typedef map <string, string> :: iterator itHeader;
  13.  
  14. struct DBTable {
  15. string tableName; // имя таблицы
  16. Header hdr; // заголовок таблиц (шапка)
  17. vector <row> data;// вектор из строк (записей) таблицы
  18. row& operator[](int index);
  19. };
  20.  
  21. row& DBTable::operator[](int index)
  22. {
  23. return data[index];
  24. }
  25.  
  26. map <string, DBTable> tab;
  27. string TableName="Student";
  28.  
  29. int GetTypeCode(string colName) {
  30. string mass[4]={"NoType", "int1", "double1", "string1"};
  31. for (int i=0; i<4; i++) {
  32. if (mass[i]==colName) {
  33. return i;
  34. }
  35. }
  36. return 0;
  37. }
  38.  
  39. void* GetValue (string colName, char *value)
  40. {
  41. void* val = NULL;
  42. switch (GetTypeCode(colName)) {
  43. case 0:
  44. val = NULL;
  45. break;
  46. case 1:
  47. {
  48. int *a = new int (atoi(value));
  49. val = a;
  50. break;
  51. }
  52. case 2:
  53. {
  54. double *a = new double (atof(value));
  55. val = a;
  56. break;
  57. }
  58. case 3:
  59. {
  60. char *buffer = new char[strlen(value)+1];
  61. memcpy(buffer, value, strlen(value)+1);
  62. val = buffer;
  63. break;
  64. }
  65. }
  66. return val;
  67. }
  68.  
  69. void printValue(DBTable tab, string colName, size_t numRow)
  70. {
  71. switch (GetTypeCode(tab.hdr[colName]) ) {
  72. case 1:
  73. cout << *((int*)tab[numRow][colName]);
  74. break;
  75. case 2:
  76. cout << *((double*)tab[numRow][colName]);
  77. break;
  78. case 3:
  79. cout << (char*)(tab[numRow][colName]);
  80. break;
  81. }
  82. }
  83.  
  84. DBTable readTable (DBTable tab) {
  85.  
  86. ifstream fin;
  87. fin.open("Student.txt"); // Открытие файла для чтения
  88. // map <string, string> Students;
  89. vector <string> header;
  90. char line [500];// тут храним саму считанную из файла строку
  91. char* buf=line; // указатель на следующее слово
  92. char* token; // указатель для хранения слова строки
  93. fin.getline (buf, 500);
  94. while (token=strtok_s(buf,"|",&buf)) {
  95. header.push_back(token);
  96. }
  97. for (unsigned int i=0; i<header.size(); i+=2) {
  98. tab.hdr[header[i]]=header[i+1];
  99. }
  100. typedef map <string, void*> Row;
  101. while (fin.getline(line,500)) {
  102. buf = line;
  103. Row row;
  104. int i = 0;
  105. while (token = strtok_s(buf,"|",&buf))
  106. {
  107. row[header[i]] = GetValue(header[i+1], token);
  108. i+=2;
  109. }
  110. tab.data.push_back(row);
  111. }
  112. ofstream fo("StudentNewFormat.txt");
  113. /*
  114. char line [500];// тут храним саму считанную из файла строку
  115. char* buf; // указатель на следующее слово
  116. char* token; // указатель для хранения слова строки
  117. // Students.insert (pair <string, string> ("Петров Олег", "ИУ6-23"));
  118. // cout << Students.first << ": " << Students.second << endl;
  119. if (fin.fail()){
  120. cout<<"Ошибка открытия файла Student.txt\n";
  121. system("pause");
  122. return ;}
  123. while (fin.getline(line, 500, '\n')){
  124. buf = line;
  125. token = strtok_s (buf, "|", &buf);
  126. pair <string, string> p;
  127. p.first=token;
  128. p.second=buf;
  129. Students.insert (p);
  130. }
  131. for (auto it = Students.begin(); it != Students.end(); ++it) {
  132. fo << it->first << ": " << it->second << endl;
  133. }
  134. */
  135. fin.close();
  136. fo.close();
  137. return tab;
  138. }
  139.  
  140. int main(){
  141. system("chcp 1251 > nul");
  142. tab[TableName] = readTable (tab[TableName]);
  143. for (itHeader i = tab[TableName].hdr.begin(); i != tab[TableName].hdr.end(); i++)
  144. {
  145. cout << setw(15) << i->first << setw(15) << i->second;
  146. }
  147. cout << endl;
  148. for (size_t i = 0; i < tab[TableName].data.size(); ++i)
  149. {
  150. cout << i+1;
  151. for (itRow cell = tab[TableName][i].begin(); cell != tab[TableName][i].end(); ++cell)
  152. {
  153. cout << "\t";
  154. printValue(tab[TableName], cell->first, i);
  155. }
  156. cout << endl;
  157. }
  158. // for (itHeader i = tab[TableName].data.begin(); i != tab[TableName].data.end(); i++) {
  159. // cout << setw(15) << i->first << setw(15) << i->second << "\n";
  160. // }
  161. // FILE *fl1; // Описание указателя – дескриптора файла
  162. /* struct Student {
  163. string imfamil;
  164. string group;
  165. };
  166. */
  167. system("pause");
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement