Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.37 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <progbase.h>
  4. #include <progbase/console.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include <assert.h>
  10. #include <time.h>
  11.  
  12. #define N 10
  13.  
  14. typedef struct PlanetInfo
  15. {
  16. float orb_radius;
  17. float period_rotate;
  18. } PlanetInfo;
  19.  
  20. typedef struct PlanetWithSputnik
  21. {
  22. char Name[30];
  23. int sputnik_count;
  24. float mass;
  25. }PlanetWithSputnik;
  26.  
  27. typedef struct Planet
  28. {
  29.  
  30. char Name[30];
  31. int sputnik_count;
  32. float mass;
  33. PlanetInfo add_info;
  34. } Planet;
  35.  
  36. void PlanetToString(Planet *planet, char buf[200]);
  37. void StringToPlanet(char buf[200], Planet *planet);
  38. void PlanetArrayToString(Planet **planet, char buf[500]);
  39. void StringToPlanetArray(char buf[500], Planet **planet);
  40. int ArrSize(Planet **arr);
  41. void RemoveAt(Planet **planet, int i);
  42. void ReplaceAt(Planet **planet, int i, Planet *src);
  43. void SetValue(Planet *p, char field[50], char value[50]);
  44. void PlanetsSputnikCountLessX(Planet **&planets, int x);
  45. void TestPlanetToString(); //done
  46. void TestStringToPlanet(); //done
  47. void TestPlanetArrayToString(); // done
  48. void TestStringToPlanetArray(); //done
  49. void TestRemoveAt(); // done
  50. void TestReplaceAt(); //done
  51. void TestSetValue(); //done
  52. void Testing();
  53. void LoadFromFile(char *filename, Planet **planets);
  54. void SaveToFile(char *filename, Planet **planets);
  55. void Action();
  56. void Menu();
  57. void ActMenu(Planet **planets, int c, Planet **planets2);
  58. void AddUserPlanet(Planet **planets);
  59. void RemoveUserPlanet(Planet **planets);
  60. void ReplaceUserPlanet(Planet **planets);
  61. void PrintPlanets(Planet **planets);
  62. void ReplaceField(Planet **planets);
  63. void FindPlanets(Planet **planets, Planet** planets2);
  64. void SavePlanetsToFile(Planet **planets);
  65.  
  66. int main(int argc, char *argv[])
  67. {
  68. if (argv[1] == NULL)
  69. Action();
  70. else if (strcmp(argv[1], "-t") == 0)
  71. Testing();
  72. else
  73.  
  74. return 0;
  75. }
  76.  
  77. void PlanetsSputnikCountLessX(Planet **&planets, int x)
  78. {
  79. int k = 0;
  80.  
  81. for (int i = 0; i < ArrSize(planets); i++)
  82. if (planets[i]->sputnik_count < x)
  83. k++;
  84. Planet ** planet2 = malloc(sizeof(Planet)*k);
  85.  
  86. k = 0;
  87. for (int i = 0; i < ArrSize(planets); i++)
  88. if (planets[i]->sputnik_count < x)
  89. planet2[k++] = planets[i];
  90.  
  91. for (int i = 0; i < ArrSize(planets); i++)
  92. if (planets[i] != NULL)
  93. free(planets[i]);
  94.  
  95. planets = planet2;
  96. // puts("Press any key...");
  97. // getchar();
  98. }
  99.  
  100. void Testing()
  101. {
  102. void TestPlanetToString();
  103. void TestStringToPlanet();
  104. void TestPlanetArrayToString();
  105. void TestStringToPlanetArray();
  106. void TestRemoveAt();
  107. void TestReplaceAt();
  108. void TestSetValue();
  109. }
  110.  
  111. void FindPlanets(Planet **planets, Planet** planets2)
  112. {
  113.  
  114. int x;
  115. Planet * planet1[10];
  116. printf("Input count of sattelites: ");
  117. scanf("%d", &x);
  118. getchar();
  119. PrintPlanetsSputnikCountLessX(planets, x, planet1);
  120. }
  121.  
  122. void ReplaceField(Planet **planets)
  123. {
  124. int i;
  125. printf("Input index of planet for field to replace: ");
  126. scanf("%d", &i);
  127. if (planets[i] == NULL || (i < 0) || (i >= N))
  128. {
  129. printf("Error input!");
  130. return;
  131. }
  132.  
  133. char field[50];
  134. char newvalue[50];
  135. printf("Input field name: ");
  136. scanf("%s", field);
  137. printf("Input field value: ");
  138. scanf("%s", newvalue);
  139.  
  140. SetValue(planets[i], field, newvalue);
  141. }
  142.  
  143. void SavePlanetsToFile(Planet **planets)
  144. {
  145. char filename[50];
  146. printf("Input file name: ");
  147. scanf("%s", filename);
  148. SaveToFile(filename, planets);
  149. }
  150. void ReplaceUserPlanet(Planet **planets)
  151. {
  152. int i;
  153. printf("Input index of planet to replace: ");
  154. scanf("%d", &i);
  155. if (planets[i] == NULL || (i < 0) || (i >= N))
  156. {
  157. printf("Error input!");
  158. return;
  159. }
  160.  
  161. printf("Input planet's name: ");
  162. scanf("%s", planets[i]->Name);
  163. printf("Input planet's sattelite count: ");
  164. scanf("%d", &planets[i]->sputnik_count);
  165. printf("Input planet's mass: ");
  166. scanf("%f", &planets[i]->mass);
  167. printf("Input planet's orb radius: ");
  168. scanf("%f", &planets[i]->add_info.orb_radius);
  169. printf("Input planet's period rotate: ");
  170. scanf("%f", &planets[i]->add_info.period_rotate);
  171. }
  172. void RemoveUserPlanet(Planet **planets)
  173. {
  174. int i;
  175. printf("Input index of planet to delete: ");
  176. scanf("%d", &i);
  177. if (planets[i] == NULL || (i < 0) || (i >= N))
  178. {
  179. printf("Error input!\n");
  180. return;
  181. }
  182.  
  183. RemoveAt(planets, i);
  184. }
  185.  
  186. void AddUserPlanet(Planet **planets)
  187. {
  188. int len = ArrSize(planets);
  189. if (len > N - 1)
  190. {
  191. printf("List if full!");
  192. return;
  193. }
  194.  
  195. planets[len] = malloc(sizeof(Planet));
  196.  
  197. printf("Input planet's name: ");
  198. scanf("%s", planets[len]->Name);
  199. getchar();
  200. int flag = true;
  201. printf("Input planet's sattelite count: ");
  202. do
  203. {
  204. flag = true;
  205. char a[20];
  206. fgets(a, 20, stdin);
  207. a[strlen(a) - 1] = '\0';
  208. int size = strlen(a);
  209. for (int i = 0; i < size; i++)
  210. {
  211. if (!isdigit(a[i]))
  212. flag = false;
  213. }
  214. if (flag && atoi(a) >= 0)
  215. {
  216. planets[len]->sputnik_count = atoi(a);
  217. }
  218. else
  219. flag = false;
  220.  
  221. } while (!flag);
  222. printf("Input planet's mass: ");
  223. do
  224. {
  225. flag = true;
  226. char a[20];
  227. fgets(a, 20, stdin);
  228. a[strlen(a) - 1] = '\0';
  229. int size = strlen(a);
  230. for (int i = 0; i < size; i++)
  231. {
  232. if (!isdigit(a[i]) && a[i] != '.')
  233. flag = false;
  234. }
  235. if (flag && atof(a) >= 0)
  236. {
  237. planets[len]->mass = atof(a);
  238. }
  239. else
  240. flag = false;
  241.  
  242. } while (!flag);
  243. printf("Input planet's orb radius: ");
  244. do
  245. {
  246. flag = true;
  247. char a[20];
  248. fgets(a, 20, stdin);
  249. a[strlen(a) - 1] = '\0';
  250. int size = strlen(a);
  251. for (int i = 0; i < size; i++)
  252. {
  253. if (!isdigit(a[i]) && a[i] != '.')
  254. flag = false;
  255. }
  256. if (flag && atof(a) >= 0)
  257. {
  258. planets[len]->add_info.orb_radius = atof(a);
  259. }
  260. else
  261. flag = false;
  262.  
  263. } while (!flag);
  264. printf("Input planet's period rotate: ");
  265. do
  266. {
  267. flag = true;
  268. char a[20];
  269. fgets(a, 20, stdin);
  270. a[strlen(a) - 1] = '\0';
  271. int size = strlen(a);
  272. for (int i = 0; i < size; i++)
  273. {
  274. if (!isdigit(a[i]) && a[i] != '.')
  275. flag = false;
  276. }
  277. if (flag && atof(a) >= 0)
  278. {
  279. planets[len]->add_info.period_rotate = atof(a);
  280. }
  281. else
  282. flag = false;
  283.  
  284. } while (!flag);
  285. }
  286. void Action()
  287. {
  288. Planet *planets[N] = { NULL };
  289. Planet *planets2[N] = { NULL };
  290. int choise = 0;
  291. do
  292. {
  293. Console_clear();
  294. puts("1. Create empty list of planets\n2. Load planets from file\n3. Exit\nWaiting fot input...");
  295. scanf("%i", &choise);
  296.  
  297. switch (choise)
  298. {
  299. case 2:
  300. {
  301. Console_clear();
  302. char filename[100] = "";
  303. puts("Input file name: ");
  304. scanf("%s", filename);
  305. getchar();
  306. LoadFromFile(filename, planets);
  307. }
  308. case 1:
  309. {
  310. do
  311. {
  312. Console_clear();
  313. PrintPlanets(planets);
  314. Menu();
  315. scanf("%i", &choise);
  316. getchar();
  317. ActMenu(planets, choise, planets2);
  318. } while (choise != 0);
  319. break;
  320. }
  321. case 3:
  322. Console_clear();
  323. puts("Bye bye...");
  324. sleepMillis(1500);
  325. choise = 3;
  326. break;
  327. default:
  328. break;
  329. }
  330. } while (choise != 3);
  331. Console_clear();
  332. for (int i = 0; i < ArrSize(planets); i++)
  333. free(planets[i]);
  334. }
  335.  
  336. void ActMenu(Planet **planets, int c, Planet **planets2)
  337. {
  338. switch (c)
  339. {
  340. case 1:
  341. Console_clear();
  342. AddUserPlanet(planets);
  343. Console_clear();
  344. break;
  345. case 2:
  346. Console_clear();
  347. RemoveUserPlanet(planets);
  348. Console_clear();
  349. break;
  350. case 3:
  351. Console_clear();
  352. ReplaceUserPlanet(planets);
  353. Console_clear();
  354. break;
  355. case 4:
  356. Console_clear();
  357. ReplaceField(planets);
  358. Console_clear();
  359. break;
  360. case 5:
  361. // Planet **planets2;
  362. Console_clear();
  363.  
  364. FindPlanets(planets, planets2);
  365. Console_clear();
  366. break;
  367. case 6:
  368. Console_clear();
  369. SavePlanetsToFile(planets);
  370. Console_clear();
  371.  
  372.  
  373. break;
  374. case 7:
  375. PrintPlanets(planets2);
  376. break;
  377. default:
  378. break;
  379. }
  380. }
  381. void PrintPlanets(Planet **planets)
  382. {
  383. if (ArrSize(planets) != 0)
  384. {
  385. puts("Your list\n-------------------------------------------");
  386. for (int i = 0; i < ArrSize(planets); i++)
  387. printf("(Planet %i)\n Name: %s Sattelites: %i Mass: %.2f Radius: %.2f Period: %.2f\n", i, planets[i]->Name, planets[i]->sputnik_count, planets[i]->mass,
  388. planets[i]->add_info.orb_radius, planets[i]->add_info.period_rotate);
  389. puts("-------------------------------------------");
  390. }
  391. else
  392. puts("This list is empty");
  393. }
  394. void Menu()
  395. {
  396. printf("1. Add new planet\n");
  397. printf("2. Remove planet at index\n");
  398. printf("3. Edit planet at index\n");
  399. printf("4. Edit planet field at index\n");
  400. printf("5. planet sattelites count less than X\n");
  401. printf("6. Save To File\n");
  402. printf("0. Exit\n");
  403. printf("7. Print planet sattelites count less than X\n");
  404. }
  405. void SaveToFile(char *filename, Planet **planets)
  406. {
  407. FILE *f = fopen(filename, "w");
  408.  
  409. char buf[200];
  410. for (int i = 0; i < ArrSize(planets); i++)
  411. {
  412. PlanetToString(planets[i], buf);
  413. fputs(buf, f);
  414. }
  415.  
  416. fclose(f);
  417. }
  418.  
  419. void LoadFromFile(char *filename, Planet **planets)
  420. {
  421.  
  422. FILE *f = fopen(filename, "r");
  423.  
  424. if (f == 0)
  425. return;
  426. int count_planets_in_file = 0;
  427. char buf[200];
  428. while (fgets(buf, 200, f))
  429. count_planets_in_file++;
  430. rewind(f);
  431.  
  432. for (int i = 0; i < count_planets_in_file; i++)
  433. planets[i] = malloc(sizeof(Planet));
  434.  
  435. int i = 0;
  436. while (fgets(buf, 200, f))
  437. {
  438. StringToPlanet(buf, planets[i]);
  439. i++;
  440. }
  441.  
  442. fclose(f);
  443. }
  444.  
  445. void TestSetValue()
  446. {
  447. Planet *planets[N] = { NULL };
  448.  
  449. char buf[500] = "Venera;8;4.50;14.60;13.00\nMars;8;6.50;14.60;13.00\n";
  450. StringToPlanetArray(buf, planets);
  451.  
  452. SetValue(planets[0], "Name", "Venera-12");
  453. SetValue(planets[1], "sputnik_count", "5");
  454. assert(strcmp(planets[0]->Name, "Venera-12") == 0);
  455. assert(planets[1]->sputnik_count == 5);
  456.  
  457. free(planets[0]);
  458. free(planets[1]);
  459. }
  460.  
  461. void TestReplaceAt()
  462. {
  463. Planet *planets[N] = { NULL };
  464. planets[0] = malloc(sizeof(Planet));
  465. planets[1] = malloc(sizeof(Planet));
  466. char buf[500] = "Venera;8;4.50;14.60;13.00\nMars;8;6.50;14.60;13.00\n";
  467. StringToPlanetArray(buf, planets);
  468.  
  469. Planet p = { "Neptun", 9, 8.5,{ 24.6, 23.0 } };
  470.  
  471. ReplaceAt(planets, 1, &p);
  472.  
  473. assert(strcmp(planets[1]->Name, "Neptun") == 0);
  474. assert(planets[1]->sputnik_count == 9);
  475. free(planets[0]);
  476. free(planets[1]);
  477. }
  478.  
  479. void TestRemoveAt()
  480. {
  481. Planet *planets[N] = { NULL };
  482. planets[0] = malloc(sizeof(Planet));
  483. planets[1] = malloc(sizeof(Planet));
  484. planets[2] = malloc(sizeof(Planet));
  485. char buf[500] = "Venera;8;4.50;14.60;13.00\nMars;8;6.50;14.60;13.00\nNeptun;9;8.50;24.60;23.00\n";
  486. StringToPlanetArray(buf, planets);
  487. RemoveAt(planets, 1);
  488. assert(strcmp(planets[1]->Name, "Neptun") == 0);
  489.  
  490. free(planets[0]);
  491. free(planets[1]);
  492. }
  493. void TestPlanetArrayToString()
  494. {
  495. Planet *planets[N] = { NULL };
  496. planets[0] = malloc(sizeof(Planet));
  497. planets[1] = malloc(sizeof(Planet));
  498. planets[2] = malloc(sizeof(Planet));
  499. strcpy(planets[0]->Name, "Venera");
  500. strcpy(planets[1]->Name, "Mars");
  501. strcpy(planets[2]->Name, "Neptun");
  502. planets[0]->mass = 4.5;
  503. planets[0]->sputnik_count = 8;
  504. planets[0]->add_info.orb_radius = 14.6;
  505. planets[0]->add_info.period_rotate = 13;
  506.  
  507. planets[1]->mass = 6.5;
  508. planets[1]->sputnik_count = 8;
  509. planets[1]->add_info.orb_radius = 14.6;
  510. planets[1]->add_info.period_rotate = 13;
  511.  
  512. planets[2]->mass = 8.5;
  513. planets[2]->sputnik_count = 9;
  514. planets[2]->add_info.orb_radius = 24.6;
  515. planets[2]->add_info.period_rotate = 23;
  516.  
  517. //s;fsdsdsd
  518. char buf[500] = "";
  519. PlanetArrayToString(planets, buf);
  520.  
  521. assert(strcmp(buf, "Venera;8;4.50;14.60;13.00\nMars;8;6.50;14.60;13.00\nNeptun;9;8.50;24.60;23.00\n") == 0);
  522.  
  523. free(planets[0]);
  524. free(planets[1]);
  525. free(planets[2]);
  526. }
  527.  
  528. void TestStringToPlanetArray()
  529. {
  530. Planet *planets[N] = { NULL };
  531.  
  532. char buf[500] = "";
  533.  
  534. strcpy(buf, "Venera;8;4.50;14.60;13.00\nMars;8;6.50;14.60;13.00\nNeptun;9;8.50;24.60;23.00\n");
  535. StringToPlanetArray(buf, planets);
  536.  
  537. assert(planets[0]->sputnik_count == 8);
  538. assert(strcmp(planets[1]->Name, "Mars") == 0);
  539. assert(planets[2]->add_info.period_rotate == 23.0);
  540.  
  541. free(planets[0]);
  542. free(planets[1]);
  543. free(planets[2]);
  544. }
  545.  
  546. void TestStringToPlanet()
  547. {
  548. char buf[100];
  549. Planet *p = malloc(sizeof(Planet));
  550. strcpy(buf, "Venera;10;5.60;3.20;16.00\n");
  551. StringToPlanet(buf, p);
  552. //-----------------
  553. strcpy(buf, "Venera;10;5.60;3.20;16.00\n");
  554. StringToPlanet(buf, p);
  555. assert(p->sputnik_count == 10);
  556. //------------
  557.  
  558. free(p);
  559. }
  560.  
  561. void TestPlanetToString()
  562. {
  563. Planet *p = malloc(sizeof(Planet));
  564. p->mass = 5.6;
  565. strcpy(p->Name, "Venera");
  566. p->sputnik_count = 10;
  567. p->add_info.orb_radius = 3.2;
  568. p->add_info.period_rotate = 16;
  569.  
  570. char str[100];
  571. PlanetToString(p, str);
  572. assert(strcmp("Venera;10;5.60;3.20;16.00\n", str) == 0);
  573. free(p);
  574. //------------------------
  575. p = malloc(sizeof(Planet));
  576. p->mass = 125.6;
  577. strcpy(p->Name, "Mars");
  578. p->sputnik_count = 10;
  579. p->add_info.orb_radius = 3.2;
  580. p->add_info.period_rotate = 16;
  581. PlanetToString(p, str);
  582. assert(strcmp("Venera;10;5.125.60;3.20;16.00\n", str) == 0);
  583. free(p);
  584. //------------------------
  585.  
  586. p = malloc(sizeof(Planet));
  587. p->mass = 125.6;
  588. strcpy(p->Name, "Neptun");
  589. p->sputnik_count = 5;
  590. p->add_info.orb_radius = 3.8;
  591. p->add_info.period_rotate = 26;
  592. PlanetToString(p, str);
  593. assert(strcmp("Venera;10;5.125.60;3.20;16.00\n", str) == 0);
  594. free(p);
  595.  
  596. p = malloc(sizeof(Planet));
  597. p->mass = 125.6;
  598. strcpy(p->Name, "Pluton");
  599. p->sputnik_count = 8;
  600. p->add_info.orb_radius = 5.9;
  601. p->add_info.period_rotate = 45;
  602. PlanetToString(p, str);
  603. assert(strcmp("Venera;10;5.125.60;3.20;16.00\n", str) == 0); //?
  604. free(p);
  605. }
  606.  
  607. void SetValue(Planet *p, char field[50], char value[50])
  608. {
  609. if (strcmp(field, "Name") == 0)
  610. strcpy(p->Name, value);
  611. else if (strcmp(field, "sputnik_count") == 0)
  612. sscanf(value, "%d", &p->sputnik_count);
  613. else if (strcmp(field, "mass") == 0)
  614. sscanf(value, "%f", &p->mass);
  615. else if (strcmp(field, "orb_radius") == 0)
  616. sscanf(value, "%f", &p->add_info.orb_radius);
  617. else if (strcmp(field, "period_rotate") == 0)
  618. sscanf(value, "%f", &p->add_info.period_rotate);
  619. }
  620.  
  621. void PlanetArrayToString(Planet **planet, char buf[500])
  622. {
  623. int len = 0;
  624. while (planet[len] != NULL)
  625. ++len;
  626.  
  627. for (int i = 0; i < len; i++)
  628. {
  629. char str[100] = "";
  630.  
  631. PlanetToString(planet[i], str);
  632. strcat(buf, str);
  633. }
  634. }
  635.  
  636.  
  637.  
  638. void ReplaceAt(Planet **planet, int i, Planet *src)
  639. {
  640. if ((planet[i] == NULL) || (i < 0) || (i > ArrSize(planet)))
  641. return;
  642.  
  643. *planet[i] = *src;
  644. }
  645.  
  646. void StringToPlanetArray(char buf[500], Planet **planet)
  647. {
  648.  
  649. int pcount = 0;
  650. for (int i = 0; i < strlen(buf); i++)
  651. if (buf[i] == '\n')
  652. pcount++;
  653. char *oneplanet = strtok(buf, "\n");
  654. for (int i = 0; i < pcount; i++)
  655. {
  656. Planet *p = malloc(sizeof(Planet));
  657. StringToPlanet(oneplanet, p);
  658. planet[i] = p;
  659. oneplanet = strtok(NULL, "\n");
  660. }
  661. }
  662.  
  663. void PlanetToString(Planet *planet, char buf[200])
  664. {
  665.  
  666. sprintf(buf, "%s;%i;%.2f;%.2f;%.2f\n", planet->Name, planet->sputnik_count, planet->mass,
  667. planet->add_info.orb_radius, planet->add_info.period_rotate);
  668. }
  669.  
  670. void StringToPlanet(char buf[200], Planet *planet)
  671. {
  672.  
  673. sscanf(buf, "%[^;];%i;%f;%f;%f", planet->Name, &planet->sputnik_count, &planet->mass,
  674. &planet->add_info.orb_radius, &planet->add_info.period_rotate);
  675. }
  676.  
  677. int ArrSize(Planet **planets)
  678. {
  679. int len = 0;
  680.  
  681. while (planets[len] != NULL)
  682. ++len;
  683.  
  684. return len;
  685. }
  686.  
  687. void RemoveAt(Planet **planet, int i)
  688. {
  689.  
  690. if ((planet[i] == NULL) || (i < 0) || (i > ArrSize(planet)))
  691. return;
  692.  
  693. Planet *temp = planet[i];
  694. for (int j = i + 1; j <= ArrSize(planet); j++)
  695. planet[j - 1] = planet[j];
  696.  
  697. free(temp);
  698.  
  699. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement