Advertisement
Guest User

Lab3

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