Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. //////////////////////////////////////////////////
  2. // Files
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int fromSTRtoINT(string str) { // преобразование string --> int
  10. int answ = 0, len = str.length();
  11. for (int i = 0; i < len; i++) {
  12. answ += (str[i] - '0') * pow(10, (len - 1) - i);
  13. }
  14. return answ;
  15. }
  16.  
  17. string exA(string str) {
  18. string name, priceStr, fromAgeStr, toAgeStr;
  19. int price, fromAge, toAge, point = 0;
  20.  
  21. while (str[point] != ' ') { // определение имени
  22. name += str[point];
  23. point++;
  24. }
  25. point++;
  26.  
  27. while (str[point] != ' ') { // определение цены
  28. priceStr += str[point];
  29. point++;
  30. }
  31. price = fromSTRtoINT(priceStr);
  32. point++;
  33.  
  34. while (str[point] != ' ') { // определение минимального возраста
  35. fromAgeStr += str[point];
  36. point++;
  37. }
  38. fromAge = fromSTRtoINT(fromAgeStr);
  39. point++;
  40.  
  41. while ((str[point] != ' ') and (point < str.length())) { // определение максимального возраста
  42. toAgeStr += str[point];
  43. point++;
  44. }
  45. toAge = fromSTRtoINT(toAgeStr);
  46.  
  47. if ((price <= 400) and ((fromAge <= 8) and (toAge >= 8))) {
  48. return name + "; ";
  49. }
  50. else return "";
  51. }
  52.  
  53. int exB(string str) {
  54. string priceStr;
  55. int point = 0;
  56.  
  57. while (str[point] != ' ') { // определение имени
  58. point++;
  59. }
  60. point++;
  61.  
  62. while (str[point] != ' ') { // определение цены
  63. priceStr += str[point];
  64. point++;
  65. }
  66. return fromSTRtoINT(priceStr);
  67. }
  68.  
  69. string exC(string str) {
  70. string name, priceStr, fromAgeStr, toAgeStr;
  71. int price, fromAge, toAge, point = 0;
  72.  
  73. while (str[point] != ' ') { // определение имени
  74. name += str[point];
  75. point++;
  76. }
  77. point++;
  78. while (str[point] != ' ') { // определение цены
  79. priceStr += str[point];
  80. point++;
  81. }
  82. point++;
  83.  
  84. while (str[point] != ' ') { // определение минимального возраста
  85. fromAgeStr += str[point];
  86. point++;
  87. }
  88. fromAge = fromSTRtoINT(fromAgeStr);
  89. point++;
  90.  
  91. while ((str[point] != ' ') and (point < str.length())) { // определение максимального возраста
  92. toAgeStr += str[point];
  93. point++;
  94. }
  95. toAge = fromSTRtoINT(toAgeStr);
  96.  
  97. if ((fromAge <= 4) and (toAge >= 10)) {
  98. return name + " " + priceStr + "; ";
  99. }
  100. else return "";
  101. }
  102.  
  103. int main()
  104. {
  105. setlocale(0, "");
  106.  
  107. int n, price, fromAge, toAge;
  108. string name;
  109.  
  110. cout << "Введите кол-во игрушек: ";
  111. cin >> n;
  112. cout << endl;
  113.  
  114. ofstream file("file.txt"); // создание файла с инфой про игрушки
  115. if (file.is_open()) {
  116. cout << "Формат ввода: <имя>_<цена>_<возраст от>_<возраст до>" << endl << endl;
  117. for (int i = 0; i < n; i++) {
  118. cout << "Введите сведения об игрушке через пробел: ";
  119. cin >> name >> price >> fromAge >> toAge;
  120. file << name << " " << price << " " << fromAge << " " << toAge << endl;
  121. }
  122. file.close();
  123. cout << endl;
  124. }
  125. else {
  126. cout << "Не удалось открыть файл!";
  127. exit(1);
  128. }
  129.  
  130. string line;
  131. ifstream toy("file.txt");
  132. int* arrB = new int[n], max = 0;
  133. string* arrA = new string[n];
  134. string* arrC = new string[n];
  135. if (toy.is_open()) {
  136. for (int i = 0; i < n; i++) {
  137. getline(toy, line); // считывает по 1 строке из файла
  138. arrA[i] = exA(line); // a)
  139. arrB[i] = exB(line); // b)
  140. arrC[i] = exC(line); // c)
  141. }
  142. cout << "а) названия игрушек, цена которых не превышает 400 р., и которые подходят детям 8 лет: ";
  143. for (int j = 0; j < n; j++) {
  144. if (arrB[j] > max) {
  145. max = arrB[j];
  146. }
  147. cout << arrA[j] << " ";
  148. }
  149. cout << endl;
  150. cout << "б) Цена самой дорогой игрушки: " << max << endl;
  151. cout << "c) Названия и цены игрушек, которые подходят одновременно детям 4-х и 10-и лет: ";
  152. for (int i = 0; i < n; i++) {
  153. cout << arrC[i];
  154. }
  155. cout << endl;
  156. }
  157. else {
  158. cout << "Не удалось открыть файл!";
  159. exit(1);
  160. }
  161.  
  162. return 0;
  163. }
  164. ////////////////////////////////////////////////
  165. // Ryads
  166.  
  167. #include <iostream>
  168. using namespace std;
  169.  
  170. int main()
  171. {
  172. setlocale(0, "");
  173.  
  174. int n, a;
  175. double y = 0, sumSin = 0;
  176.  
  177. cout << "n = ";
  178. cin >> n;
  179.  
  180. while (n > 0) {
  181. a = n;
  182. while (a > 0) { // считает знаменатель
  183. sumSin += sin(a);
  184. a -= 1;
  185. }
  186. y += n / sumSin; // считает всю дробь
  187. n -= 1;
  188. }
  189.  
  190. cout << endl;
  191. cout << "y = " << y << endl;
  192.  
  193. return 0;
  194. }
  195. ///////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement