Advertisement
rooq37

ZAJEBISTOSC

Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1.  
  2. // ConsoleApplication4.cpp : Defines the entry point for the console application.
  3. //
  4. //#include "stdafx.h"
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <string>
  8. #include <math.h>
  9. #include <fstream>
  10. using namespace std;
  11.  
  12. double ** read(int &numberOfPoints);
  13. void save(int numberOfPoints, double ** table);
  14. void insertsort(double **table, int numberOfPoints);
  15. void bubblesort(int numberOfPoints, double **table);
  16. void compute(int numberOfPoints, double **table);
  17. void print(int precision, int numberOfPoints, double **table);
  18. void printHeader(int precision);
  19. void initialize(double & variabilityOfX, int & numberOfPoints, int & precision);
  20. int instruction();
  21.  
  22.  
  23. void initialize(double & variabilityOfX, int & numberOfPoints, int & precision) {
  24. std::cout << "Podaj liczbe punktow: " << std::endl;
  25. std::cin >> numberOfPoints;
  26. std::cout << "Podaj zmiennosc x: " << std::endl;
  27. std::cin >> variabilityOfX;
  28. std::cout << "Podaj dokladnosc: " << std::endl;
  29. std::cin >> precision;
  30. }
  31.  
  32.  
  33. void printHeader(int precision) {
  34. int prec = 7;
  35. if (precision > prec) {
  36. prec = precision;
  37. }
  38. printf("|%-3s", "i");
  39. printf("|%*s", (-1)*(precision + 2), "x");
  40. printf("|%*s", (-1)*(precision + 2), "sin");
  41. printf("|%*s", (-1)*(precision + 2), "cos");
  42. printf("|%*s", (-1)*(precision + 2), "tg");
  43. printf("|%*s", (-1)*(precision + 2), "ctg");
  44. printf("|%*s", (-1)*(precision + 2), "asin");
  45. printf("|%*s", (-1)*(precision + 2), "acos");
  46. printf("|%*s", (-1)*(precision + 2), "atg");
  47. printf("|%*s|", (-1)*(precision + 2), "actg");
  48. printf("%\n");
  49. }
  50.  
  51. void print(int precision, int numberOfPoints, double **table) {
  52. for (int i = 0; i < numberOfPoints; i++) {
  53. printf("|%*d", -3, (int)table[i][0]);
  54. for (int j = 1; j < 10; j++) {
  55. int num = (int)(table[i][j]);
  56. int c = 0;
  57. while ((num /= 10) != 0)
  58. c++;
  59. if (std::isnan(table[i][j])) {
  60. printf("|%*s", precision + 2, "-");
  61. }
  62. else if (std::isinf(table[i][j])) {
  63. printf("|%*s", precision + 2, "inf");
  64. }
  65. else if (table[i][j] < 0) {
  66. printf("|%.*f", precision - 1 - c, table[i][j]);
  67. }
  68. else
  69. printf("|%.*f", precision - c, table[i][j]);
  70. }
  71. printf("|%\n");
  72. }
  73. }
  74.  
  75. void compute(int numberOfPoints, double **table) {
  76.  
  77. for (int i = 0; i < numberOfPoints; i++) {
  78. table[i][2] = sin(table[i][1]);
  79. table[i][3] = cos(table[i][1]);
  80. table[i][4] = tan(table[i][1]);
  81. table[i][5] = 1.0 / (tan(table[i][1]));
  82. table[i][6] = asin(table[i][1]);
  83. table[i][7] = acos(table[i][1]);
  84. table[i][8] = atan(table[i][1]);
  85. table[i][9] = 1.0 / (atan(table[i][1]));
  86. }
  87. }
  88.  
  89. void bubblesort(int numberOfPoints, double **table) {
  90. cout << "Wybierz kryterium sortowania" << endl;
  91. cout << "1 - sin" << endl;
  92. cout << "2 - cos" << endl;
  93. cout << "3 - tan" << endl;
  94. cout << "4 - ctg" << endl;
  95. cout << "5 - arcsin" << endl;
  96. cout << "6 - arccos" << endl;
  97. cout << "7 - arctan" << endl;
  98. cout << "8 - arctg" << endl;
  99.  
  100. int ask1;
  101. cin >> ask1;
  102. int i, j;
  103. int index = ask1 + 1;
  104. double *temp;
  105. double temp1, temp2;
  106. for (i = (numberOfPoints - 2); i >= 0 ; i--)
  107. {
  108. for (j = 0; j <= i; j++) {
  109. temp1 = table[j][index], temp2 = table[j + 1][index];
  110. if (temp1 > temp2) {
  111. double *temp = table[j];
  112. table[j] = table[j+1];
  113. table[j + 1] = temp;
  114. }
  115. }
  116. }
  117. for (int i = 0; i < numberOfPoints; i++) table[i][0] = i;
  118. }
  119.  
  120.  
  121. void insertsort(double **table, int numberOfPoints) {
  122. cout << "Wybierz kryterium sortowania" << endl;
  123. cout << "1 - sin" << endl;
  124. cout << "2 - cos" << endl;
  125. cout << "3 - tan" << endl;
  126. cout << "4 - ctg" << endl;
  127. cout << "5 - arcsin" << endl;
  128. cout << "6 - arccos" << endl;
  129. cout << "7 - arctan" << endl;
  130. cout << "8 - arctg" << endl;
  131.  
  132. int ask1;
  133. cin >> ask1;
  134.  
  135. int j;
  136. int index = ask1 + 1;
  137. double *currentLine;
  138. for (int i = 1; i < numberOfPoints; i++) {
  139. currentLine = table[i];
  140. for (j = i - 1; j >= 0 && table[j][index] > currentLine[index]; j--) {
  141. table[j + 1] = table[j];
  142. }
  143. table[j + 1] = currentLine;
  144. }
  145. for (int i = 0; i < numberOfPoints; i++) table[i][0] = i;
  146. }
  147.  
  148. void save(int numberOfPoints, double ** table) {
  149.  
  150. fstream file;
  151. file.open("wynik.txt", ios::out);
  152. if (file.good() == true)
  153. {
  154. file << numberOfPoints << endl;
  155. for (int i = 0; i < numberOfPoints; i++) {
  156. for (int j = 1; j < 10; j++) {
  157. file << table[i][j]<<endl;
  158. }
  159. }
  160. file.close();
  161. }
  162. }
  163.  
  164. double ** read(int &numberOfPoints)
  165. {
  166. fstream file;
  167. string temp;
  168. double ** table = NULL;
  169. file.open("wynik.txt", ios::in | ios::out);
  170. if (file.good() == true)
  171. {
  172. file >> numberOfPoints;
  173. table = new double*[numberOfPoints];
  174.  
  175. for (int i = 0; i < numberOfPoints; i++) {
  176. table[i] = new double[10];
  177. table[i][0] = i;
  178. }
  179.  
  180. for (int i = 0; i < numberOfPoints && !file.eof(); i++) {
  181. for (int j = 1; j < 10 && !file.eof(); j++) {
  182. file >> temp;
  183. if (temp != "inf") table[i][j] = stod(temp.c_str());
  184. else table[i][j] = INFINITY;
  185. }
  186. }
  187. file.close();
  188. }
  189. return table;
  190. }
  191.  
  192. int instruction() {
  193. int answer = 0;
  194. while (answer < 1 || answer > 7) {
  195. cout << "***MENU***" << endl;
  196. cout << "[1] Utworz nowa tablice" << endl;
  197. cout << "[2] Wczytaj tablice z pliku" << endl;
  198. cout << "[3] Wyswietl tablice" << endl;
  199. cout << "[4] Sortuj (bubble sort)" << endl;
  200. cout << "[5] Sorutuj (insert sort)" << endl;
  201. cout << "[6] Zapisz do pliku" << endl;
  202. cout << "[7] Wyjdz" << endl;
  203. cin >> answer;
  204. }
  205. return answer;
  206. }
  207.  
  208. int main() {
  209. double variabilityOfX;
  210. int numberOfPoints;
  211. int precision = 3;
  212. double **table = NULL;
  213. int answer = 0;
  214.  
  215. while (answer != 7) {
  216. system("cls");
  217. answer = instruction();
  218. if (answer == 1) {
  219. if (table != NULL) {
  220. for (int i = 0; i < numberOfPoints; i++) {
  221. delete table[i];
  222. }
  223. delete table;
  224. }
  225. initialize(variabilityOfX, numberOfPoints, precision);
  226. table = new double*[numberOfPoints];
  227. for (int i = 0; i < numberOfPoints; i++) {
  228. table[i] = new double[10];
  229. table[i][0] = i;
  230. table[i][1] = i*variabilityOfX;
  231. }
  232. compute(numberOfPoints, table);
  233. }
  234. else if (answer == 2) {
  235. if (table != NULL) {
  236. for (int i = 0; i < numberOfPoints; i++) {
  237. delete table[i];
  238. }
  239. delete table;
  240. }
  241. table = read(numberOfPoints);
  242. }
  243. else if (answer == 3) {
  244. if (table != NULL) {
  245. printHeader(precision);
  246. print(precision, numberOfPoints, table);
  247. }
  248. else cout << "Brak tablicy!" << endl;
  249. }
  250. else if (answer == 4) {
  251. if(table != NULL) bubblesort(numberOfPoints, table);
  252. else cout << "Brak tablicy!" << endl;
  253. }
  254. else if (answer == 5) {
  255. if(table != NULL) insertsort(table, numberOfPoints);
  256. else cout << "Brak tablicy!" << endl;
  257. }
  258. else if (answer == 6) {
  259. if(table != NULL) save(numberOfPoints, table);
  260. else cout << "Brak tablicy!" << endl;
  261. }
  262. getchar();
  263. getchar();
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement