Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 38.78 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <progbase.h>
  4. #include <progbase/console.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <stdbool.h>
  8. #include <assert.h>
  9. #include <math.h>
  10.  
  11. #define LENGTH 10
  12. #define BUFF_LENGTH 500
  13.  
  14. struct Staff
  15. {
  16.     char staffName[50];
  17.     int staffAmmount;
  18. };
  19.  
  20. struct Organisation
  21. {
  22.     char organisationName[50];
  23.     int income;
  24.     float rating;
  25.     char country[50];
  26.     struct Staff staff;
  27. };
  28.  
  29. struct Organisation *fillTheStruct(char *nameO, int income, float rating, char *country, char *name, int ammount);
  30. void freeStruct(struct Organisation *organisations[]);
  31. struct Organisation *rewriteTheOrganisation(char *nameO, int income, float rating, char *country, char *name, int ammount, int index, struct Organisation *organisations[]);
  32. struct Organisation *rewriteTheField(char *str, char *strNew, struct Organisation *organisation);
  33. void newArray(struct Organisation *organisation, int length);
  34. void cleanBuffer();
  35. int getTheIndex(int pos, int count);
  36. void getTheInd(int *pos, int *n, struct Organisation *organisations[]);
  37. float getFloatValue(int pos);
  38. int getTheValue(int pos);
  39. int countStructs(int length, struct Organisation *array[]);
  40. bool addNewStruct(int length, struct Organisation *newOrganisation, struct Organisation *array[]);
  41. struct Organisation *removeOrganisation(int length, int index, struct Organisation *array[]);
  42. bool compTheStruct(struct Organisation *organisation1, struct Organisation *organisation2);
  43. void printTheOrganisation(struct Organisation *organisation, int count, int *pos);
  44. void getTheStruct(char *nameO, int *income, float *rating, char *country, char *staffName, int *ammount, int *pos);
  45. void fromSomeCountry(int length, struct Organisation *organisations[], char *country, struct Organisation *fromSomeCountry[]);
  46. struct Organisation *copyTheOrganisation(struct Organisation *organisations[], int indexFrom, int indexTo);
  47. void printTheCommand();
  48. void printTheMenu();
  49. void cleanTheInput2();
  50. void cleanTheInput3();
  51. void cleanTheInput4();
  52. void cleanTheOutput();
  53. void cleanTheCommand();
  54. void task1(struct Organisation *organisations[]);
  55. void task2();
  56. int staffToStr(struct  Staff * staff, char * buffer, int BUFF__LENGTH);
  57. int strToStaff(const char *buffer, struct Staff * staff);
  58. bool staffEquals(struct Staff *a, struct Staff *b);
  59. int organisationToStr(struct  Organisation * organisation, char * buffer, int BUFF__LENGTH);
  60. int strToOrganisation(const char *buffer, struct Organisation * organisation);
  61. bool organisationEquals(struct Organisation *a, struct Organisation *b);
  62. void printTheOrganisation2(struct Organisation * organisation);
  63. bool organisationsEquals(struct Organisation *a[], struct Organisation *b[]);
  64. int organisationsToStr(struct  Organisation * organisations[], char * buffer, int BUFF__LENGTH);
  65. int strToOrganisations(const char *buffer, struct Organisation * organisations[]);
  66.  
  67.  
  68. int main(void)
  69. {
  70.     struct Staff staff = {"Anna", 54};
  71.     char str[50] = "";
  72.     staffToStr(&staff,str,BUFF_LENGTH);
  73.     puts(str);
  74.     struct Staff test;
  75.     char strstr[50] = "Anna#54";
  76.     int nread = strToStaff(str,&test);
  77.     assert(staffEquals(&test,&((struct Staff) {"Anna",54})) == 1);
  78.     printf("%i\n",nread);
  79.     struct Organisation organisation = {"name", 50,1.20, "country", {"Ivan",54}};
  80.     char strtest[50] = "";
  81.     organisationToStr(&organisation,strtest,BUFF_LENGTH);
  82.     struct Organisation org;
  83.     strToOrganisation(strtest,&org);
  84.     puts(strtest);
  85.     printTheOrganisation2(&org);
  86.     assert(organisationEquals(&org,&((struct Organisation) {"name", 50,1.20, "country", {"Ivan",54}})) == 1);
  87.     struct Organisation s1 = {"name", 50,1.20, "country", {"Ivan",54}};
  88.     struct Organisation s2 = {"name1", 51,1.30, "countrie", {"Petro",55}};
  89.  
  90.     struct Organisation * organisations[LENGTH] = {
  91.         &s1,&s2
  92.     };
  93.     char buff[BUFF_LENGTH] = "";
  94.     organisationsToStr(organisations,buff,BUFF_LENGTH);
  95.     puts(buff);
  96.     struct Organisation * orgtest[LENGTH]= {0};
  97.     strToOrganisations(buff,orgtest);
  98.  
  99.     //assert(organisationsEquals(organisations,orgtest) == 1);
  100.  
  101.    
  102.  
  103.  
  104.  
  105.  
  106.     // struct Organisation organisation = {"Svitoch", 500000,9.5,"Ukraine",{"Ivan","Accountant"}};
  107.     // char str[50] = "staffName";
  108.     // char strNew[50] = "Petro";
  109.     // struct Organisation * org = rewriteTheField(str,strNew,&organisation);
  110.     // printTheOrganisation(org);
  111.     // struct Organisation org2 = {"Svitoch", 500000,9.5,"Ukraine",{"Petro","Accountant"}};
  112.  
  113.     //ASSERTS
  114.     //assert(compTheStruct(org,&org2) == 1);
  115. //     printTheMenu();
  116. //     char n;
  117. //     while (n != 27)
  118. //     {
  119. //         struct ConsoleSize consoleSize = Console_size();
  120. //         int maxRows = consoleSize.rows;
  121. //         int maxColumns = consoleSize.columns;
  122. //         Console_setCursorPosition(maxRows - 1, 13);
  123. //         n = Console_getChar();
  124. //         switch (n)
  125. //         {
  126. //         case 49:
  127. //         {
  128. //             struct Organisation *organisations[LENGTH] = {0};
  129. //             task1(organisations);
  130. //             freeStruct(organisations);
  131. //             printTheMenu();
  132. //             break;
  133. //         }
  134. //         case 50:
  135. //         {
  136. //             task2();
  137. //             break;
  138. //         }
  139. //         default:
  140. //         {
  141. //             Console_setCursorPosition(maxRows - 2, 13);
  142. //             puts("ERROR : Invalid input. Please, try again:");
  143.  
  144. //             Console_setCursorPosition(maxRows - 1, 13);
  145. //         }
  146. //         }
  147. //     }
  148. //     Console_clear();
  149. //     Console_setCursorPosition(0, 0);
  150.      return EXIT_SUCCESS;
  151. }
  152.  
  153. bool organisationsEquals(struct Organisation *a[], struct Organisation *b[]){
  154.    
  155.     if (countStructs(LENGTH,a) != countStructs(LENGTH,b)) return false;
  156.     for (int i = 0;i < countStructs(LENGTH,a);i++)
  157.     {
  158.         if (strcmp(a[i]->organisationName, b[i]->organisationName) == 1
  159.         && a[i]->income != b[i]->income && fabs(a[i]->rating - b[i]->rating) >= 1e-4 && strcmp(a[i]->country,b[i]->country) != 0 && !staffEquals(&a[i]->staff,&b[i]->staff))
  160.         return false;
  161.  
  162.     }
  163.     return true;
  164. }
  165.  
  166.  
  167. int organisationsToStr(struct  Organisation * organisations[], char * buffer, int BUFF__LENGTH)
  168. {
  169.     int nwrite = snprintf(buffer, BUFF__LENGTH, "%i|\n", countStructs(LENGTH,organisations));
  170.     int totalWrite = nwrite;
  171.     for (int i = 0; i < countStructs(LENGTH,organisations);i++)
  172.     {
  173.         struct Organisation * st = organisations[i];        
  174.         totalWrite += organisationToStr(st ,buffer + totalWrite, BUFF__LENGTH - totalWrite);
  175.         totalWrite += snprintf(buffer  + totalWrite,BUFF__LENGTH - totalWrite, "\n");
  176.     }
  177.     return totalWrite;
  178. }
  179.  
  180. int strToOrganisations(const char *buffer, struct Organisation * organisations[]){
  181.     int nread = 0;
  182.     int count = 0;
  183.     sscanf(buffer, "%i|\n%n", &count, &nread);
  184.     int totalRead = nread;
  185.     printf("%i\n",nread);
  186.     printf("%i\n",count);
  187.     printf("%s\n",buffer + totalRead);
  188.     // for (int i = 0;i < count;i++)
  189.     // {
  190.         struct Organisation * org = {0};
  191.         char p[50];
  192.  
  193.           totalRead += strToOrganisation(buffer + totalRead,org);
  194.           //sscanf(buffer + totalRead, "\n%n", &nread);
  195.           // totalRead += nread;
  196.     // }
  197.     return totalRead;
  198. }
  199.  
  200. bool organisationEquals(struct Organisation *a, struct Organisation *b){
  201.     return strcmp(a->organisationName, b->organisationName) == 0
  202.     && a->income == b->income && fabs(a->rating - b->rating) < 1e-4 && strcmp(a->country,b->country) == 0 && staffEquals(&a->staff,&b->staff);
  203. }
  204.  
  205. int strToStaff(const char *buffer, struct Staff * staff){
  206.     int nread = 0;
  207.     puts(buffer + 2);
  208.     sscanf(buffer, "%49[^#]%*c%i%n",
  209.         staff->staffName,
  210.         &(staff->staffAmmount),
  211.         &nread);
  212.     return nread;
  213. }
  214. int strToOrganisation(const char *buffer, struct Organisation * organisation){
  215.     int nread = 0;
  216.     puts(buffer);
  217.     sscanf(buffer, "%49[^:]%*c%i:%f:%49[^:]%*c%n",
  218.         organisation->organisationName,
  219.         &(organisation->income),
  220.         &(organisation->rating),
  221.         organisation->country,
  222.         &nread);
  223.         printf("%s\n",organisation->organisationName);
  224.         printf("%i\n",nread);
  225.         puts(buffer + 10);
  226.         int staffRead = strToStaff(buffer + nread,&organisation->staff);
  227.     return nread ;
  228. }
  229.  
  230.  
  231. int organisationToStr(struct  Organisation * organisation, char * buffer, int BUFF__LENGTH)
  232. {
  233.     int nwrite = snprintf(buffer, BUFF__LENGTH, "%s:%i:%f:%s:", organisation->organisationName,organisation->income,organisation->rating,organisation->country);
  234.     int staffWrite = staffToStr(&organisation->staff,buffer + nwrite, BUFF__LENGTH - nwrite);
  235.     return nwrite + staffWrite;
  236. }
  237.  
  238. int staffToStr(struct  Staff * staff, char * buffer, int BUFF__LENGTH)
  239. {
  240.     int nwrite = snprintf(buffer,BUFF_LENGTH, "%s#%i",
  241.     staff->staffName,
  242.     staff->staffAmmount);
  243.     return nwrite;
  244. }
  245.  
  246.  
  247.  
  248. bool staffEquals(struct Staff *a, struct Staff *b){
  249.     return strcmp(a->staffName, b->staffName) == 0
  250.     && a->staffAmmount == b->staffAmmount;
  251. }
  252.  
  253. void printTheOrganisation2(struct Organisation * organisation)
  254. {
  255.      
  256.  
  257.     printf("Organisation Name: %s", organisation->organisationName);
  258.    
  259.     printf("Income: %i", organisation->income);
  260.  
  261.  
  262.     printf("Rating: %.2f", organisation->rating);
  263.  
  264.     printf("Country: %s", organisation->country);
  265.    
  266.     printf("Headmaster Name: %s", organisation->staff.staffName);
  267.  
  268.     printf("Ammount of staff: %i", organisation->staff.staffAmmount);
  269. }
  270.  
  271.  
  272. // void printTheMenu()
  273. // {
  274. //     Console_clear();
  275. //     struct ConsoleSize consoleSize = Console_size();
  276. //     int maxRows = consoleSize.rows;
  277. //     int maxColumns = consoleSize.columns;
  278. //     Console_setCursorPosition(maxRows / 2 - 5, maxColumns / 2 - 50);
  279. //     printf("HELLO! WELCOME INTO MY PROGRAMM!PLEASE, CHOOSE BEST CASE FOR YOU:");
  280. //     Console_setCursorPosition(maxRows / 2 - 4, maxColumns / 2 - 30);
  281. //     printf("1.CREATE NEW EMPTY LIST OF ORGANISATIONS\n");
  282. //     Console_setCursorPosition(maxRows / 2 - 3, maxColumns / 2 - 30);
  283. //     printf("2.READ EXISTING LIST FROM FILE\n");
  284. //     Console_setCursorPosition(maxRows / 2 - 2, maxColumns / 2 - 30);
  285. //     printf("----To EXIT press \"ESC\"---- \n");
  286. //     Console_setCursorPosition(maxRows - 1, 0);
  287. //     printf("Your input:");
  288. // }
  289.  
  290. // void task1(struct Organisation *organisations[])
  291. // {
  292. //     struct ConsoleSize consoleSize = Console_size();
  293. //     int maxRows = consoleSize.rows;
  294. //     Console_clear();
  295. //     Console_setCursorPosition(0, 0);
  296. //     printTheCommand();
  297. //     Console_setCursorPosition(12, 0);
  298. //     printf("Your input:");
  299. //     Console_setCursorPosition(12, 13);
  300. //     char command;
  301. //     while (command != 27)
  302. //     {
  303. //         Console_setCursorPosition(12, 13);
  304. //         command = Console_getChar();
  305. //         cleanTheInput3();
  306. //         switch (command)
  307. //         {
  308. //         case 49:
  309. //         {
  310. //             cleanTheCommand();
  311. //             Console_setCursorPosition(0, 0);
  312. //             printTheCommand();
  313. //             Console_setCursorAttribute(FG_INTENSITY_CYAN);
  314. //             Console_setCursorPosition(2, 0);
  315. //             printf("~1.ADD NEW ORGANISATION WITH YOUR DATA");
  316. //             Console_setCursorAttribute(FG_DEFAULT);
  317. //             cleanTheInput3();
  318. //             int pos = 12;
  319. //             if (countStructs(LENGTH, organisations) < LENGTH)
  320. //             {
  321. //                 char name[50] = "";
  322. //                 int income;
  323. //                 float rating;
  324. //                 char country[50] = "";
  325. //                 char staffName[50] = "";
  326. //                 int ammount;
  327. //                 getTheStruct(name, &income, &rating, country, staffName, &ammount, &pos);
  328. //                 if (addNewStruct(LENGTH, fillTheStruct(name, income, rating, country, staffName, ammount), organisations))
  329. //                 {
  330. //                     cleanTheOutput();
  331. //                     int k = 1;
  332. //                     printTheOrganisation(organisations[countStructs(LENGTH, organisations) - 1], countStructs(LENGTH, organisations) - 1, &k);
  333. //                 }
  334. //             }
  335. //             else
  336. //             {
  337. //                 Console_setCursorPosition(11, 13);
  338. //                 puts("ERROR : Too many organisations. Please, try another command:");
  339. //                 cleanTheInput4();
  340. //                 Console_setCursorPosition(10, 13);
  341. //             }
  342. //             break;
  343. //         }
  344. //         case 50:
  345. //         {
  346. //             cleanTheCommand();
  347. //             Console_setCursorPosition(0, 0);
  348. //             printTheCommand();
  349. //             Console_setCursorAttribute(FG_INTENSITY_CYAN);
  350. //             Console_setCursorPosition(3, 0);
  351. //             printf("~2.DELETE ORGANISATION ON SOME POSITION");
  352. //             Console_setCursorAttribute(FG_DEFAULT);
  353. //             int pos = 12;
  354. //             int n;
  355. //             if (countStructs(LENGTH, organisations) == 0)
  356. //             {
  357. //                 Console_setCursorPosition(11, 13);
  358. //                 puts("ERROR : There is no organisations to delete. Please, try another command:");
  359. //                 cleanTheInput4();
  360. //                 Console_setCursorPosition(10, 13);
  361. //             }
  362. //             else
  363. //             {
  364. //                 getTheInd(&pos, &n, organisations);
  365. //                 struct Organisation *removedOrganisation = removeOrganisation(LENGTH, n, organisations);
  366. //                 cleanTheOutput();
  367. //                 Console_setCursorPosition(1, 80);
  368. //                 puts("Removed organisation:");
  369. //                 int k = 2;
  370. //                 printTheOrganisation(removedOrganisation, n, &k);
  371. //             }
  372. //             break;
  373. //         }
  374. //         case 51:
  375. //         {
  376. //             cleanTheCommand();
  377. //             Console_setCursorPosition(0, 0);
  378. //             printTheCommand();
  379. //             Console_setCursorAttribute(FG_INTENSITY_CYAN);
  380. //             Console_setCursorPosition(4, 0);
  381. //             printf("~3.REWRITE SOME ORGANISATION");
  382. //             Console_setCursorAttribute(FG_DEFAULT);
  383. //             cleanTheInput3();
  384. //             int pos = 12;
  385. //             int n;
  386. //             getTheInd(&pos, &n, organisations);
  387. //             char name[50] = "";
  388. //             int income;
  389. //             float rating;
  390. //             char country[50] = "";
  391. //             char staffName[50] = "";
  392. //             int ammount;
  393. //             getTheStruct(name, &income, &rating, country, staffName, &ammount, &pos);
  394. //             struct Organisation *rewritten = rewriteTheOrganisation(name, income, rating, country, staffName, ammount, n, organisations);
  395. //             cleanTheOutput();
  396. //             Console_setCursorPosition(1, 80);
  397. //             puts("Rewritten organisation:");
  398. //             int k = 2;
  399. //             printTheOrganisation(rewritten, n, &k);
  400. //             break;
  401. //         }
  402. //         case 52:
  403. //         {
  404. //             cleanTheCommand();
  405. //             Console_setCursorPosition(0, 0);
  406. //             printTheCommand();
  407. //             Console_setCursorAttribute(FG_INTENSITY_CYAN);
  408. //             Console_setCursorPosition(5, 0);
  409. //             printf("~4.REWRITE THE FIELD OF SOME ORGANISATION");
  410. //             Console_setCursorAttribute(FG_DEFAULT);
  411. //             cleanTheInput3();
  412. //             int pos = 12;
  413. //             int n;
  414. //             getTheInd(&pos, &n, organisations);
  415. //             Console_setCursorPosition(pos++, 13);
  416. //             puts("Enter the name of the field you want to rewrite:");
  417. //             Console_setCursorPosition(pos++, 13);
  418. //             char nameField[50];
  419. //             fgets(nameField, 50, stdin);
  420. //             nameField[strlen(nameField) - 1] = '\0';
  421. //             while (strcmp(nameField, "Organisation Name") && strcmp(nameField, "Income") && strcmp(nameField, "Rating") && strcmp(nameField, "Country") && strcmp(nameField, "Headmaster Name") && strcmp(nameField, "Ammount of staff"))
  422. //             {
  423. //                 for (int i = 12; i < consoleSize.columns; i++)
  424. //                 {
  425. //                     Console_setCursorPosition(11, i);
  426. //                     puts(" ");
  427. //                 }
  428. //                 for (int i = 12; i < consoleSize.columns; i++)
  429. //                 {
  430. //                     Console_setCursorPosition(pos - 1, i);
  431. //                     puts(" ");
  432. //                 }
  433. //                 Console_setCursorPosition(11, 13);
  434. //                 puts("ERROR : Invalid input. Please, try again:");
  435. //                 for (int i = 12; i < consoleSize.columns; i++)
  436. //                 {
  437. //                     Console_setCursorPosition(pos - 1, i);
  438. //                     puts(" ");
  439. //                 }
  440. //                 Console_setCursorPosition(pos - 1, 13);
  441. //                 strcpy(nameField, "");
  442. //                 fgets(nameField, 50, stdin);
  443. //                 nameField[strlen(nameField) - 1] = '\0';
  444. //             }
  445. //             for (int i = 12; i < consoleSize.columns; i++)
  446. //             {
  447. //                 Console_setCursorPosition(11, i);
  448. //                 puts(" ");
  449. //             }
  450. //             Console_setCursorPosition(pos++, 13);
  451. //             puts("Enter the new value:");
  452. //             Console_setCursorPosition(pos++, 13);
  453. //             char newValue[50];
  454. //             fgets(newValue, 50, stdin);
  455. //             newValue[strlen(newValue) - 1] = '\0';
  456. //             if (!strcmp(nameField, "Income") || !strcmp(nameField, "Ammount of staff"))
  457. //             {
  458. //                 int nread = atoi(newValue);
  459. //                 while (nread == 0 || atoi(newValue) < 0)
  460. //                 {
  461. //                     if (newValue[0] == '0')
  462. //                     {
  463. //                         break;
  464. //                     }
  465. //                     else
  466. //                     {
  467. //                         for (int i = 12; i < consoleSize.columns; i++)
  468. //                         {
  469. //                             Console_setCursorPosition(11, i);
  470. //                             puts(" ");
  471. //                         }
  472. //                         for (int i = 12; i < consoleSize.columns; i++)
  473. //                         {
  474. //                             Console_setCursorPosition(pos - 1, i);
  475. //                             puts(" ");
  476. //                         }
  477. //                         Console_setCursorPosition(11, 13);
  478. //                         puts("ERROR : Invalid input. Please, try again:");
  479. //                         for (int i = 12; i < consoleSize.columns; i++)
  480. //                         {
  481. //                             Console_setCursorPosition(pos - 1, i);
  482. //                             puts(" ");
  483. //                         }
  484. //                         Console_setCursorPosition(pos - 1, 13);
  485. //                         strcpy(newValue, "");
  486. //                         fgets(newValue, 50, stdin);
  487. //                         newValue[strlen(newValue) - 1] = '\0';
  488. //                         nread = atoi(newValue);
  489. //                     }
  490. //                 }
  491. //             }
  492. //             for (int i = 12; i < consoleSize.columns; i++)
  493. //             {
  494. //                 Console_setCursorPosition(9, i);
  495. //                 puts(" ");
  496. //             }
  497. //             if (!strcmp(nameField, "Rating"))
  498. //             {
  499. //                 float nread = atof(newValue);
  500. //                 while (nread == 0)
  501. //                 {
  502. //                     if (newValue[0] == '0' || atof(newValue) < 0)
  503. //                     {
  504. //                         break;
  505. //                     }
  506. //                     else
  507. //                     {
  508. //                         for (int i = 12; i < consoleSize.columns; i++)
  509. //                         {
  510. //                             Console_setCursorPosition(11, i);
  511. //                             puts(" ");
  512. //                         }
  513. //                         for (int i = 12; i < consoleSize.columns; i++)
  514. //                         {
  515. //                             Console_setCursorPosition(pos - 1, i);
  516. //                             puts(" ");
  517. //                         }
  518. //                         Console_setCursorPosition(11, 13);
  519. //                         puts("ERROR : Invalid input. Please, try again:");
  520. //                         for (int i = 12; i < consoleSize.columns; i++)
  521. //                         {
  522. //                             Console_setCursorPosition(pos - 1, i);
  523. //                             puts(" ");
  524. //                         }
  525. //                         Console_setCursorPosition(pos - 1, 13);
  526. //                         strcpy(newValue, "");
  527. //                         fgets(newValue, 50, stdin);
  528. //                         newValue[strlen(newValue) - 1] = '\0';
  529. //                         nread = atof(newValue);
  530. //                     }
  531. //                 }
  532. //             }
  533. //             for (int i = 12; i < consoleSize.columns; i++)
  534. //             {
  535. //                 Console_setCursorPosition(11, i);
  536. //                 puts(" ");
  537. //             }
  538. //             struct Organisation *rewritten1 = rewriteTheField(nameField, newValue, organisations[n]);
  539. //             cleanTheOutput();
  540. //             Console_setCursorPosition(1, 80);
  541. //             puts("Rewritten organisation:");
  542. //             int k = 2;
  543. //             printTheOrganisation(rewritten1, n, &k);
  544. //             break;
  545. //         }
  546. //         case 53:
  547. //         {
  548. //             cleanTheCommand();
  549. //             Console_setCursorPosition(0, 0);
  550. //             printTheCommand();
  551. //             Console_setCursorAttribute(FG_INTENSITY_CYAN);
  552. //             Console_setCursorPosition(6, 0);
  553. //             printf("~5.FIND ALL ORGANISATIONS FROM SOME COUNTRY");
  554. //             Console_setCursorAttribute(FG_DEFAULT);
  555. //             cleanTheInput3();
  556. //             struct Organisation *FCountry[LENGTH] = {0};
  557. //             char country[50];
  558. //             int pos = 12;
  559. //             Console_setCursorPosition(pos++, 13);
  560. //             puts("Enter the name of the country:");
  561. //             Console_setCursorPosition(pos++, 13);
  562. //             fgets(country, 50, stdin);
  563. //             country[strlen(country) - 1] = '\0';
  564. //             cleanTheOutput();
  565. //             int ps = 1;
  566. //             fromSomeCountry(LENGTH, organisations, country, FCountry);
  567. //             for (int i = 0; i < countStructs(LENGTH, FCountry); i++)
  568. //             {
  569. //                 printTheOrganisation(FCountry[i], 1, &ps);
  570. //             }
  571. //             break;
  572. //         }
  573. //         case 54:
  574. //         {
  575. //             cleanTheCommand();
  576. //             Console_setCursorPosition(0, 0);
  577. //             printTheCommand();
  578. //             Console_setCursorAttribute(FG_INTENSITY_CYAN);
  579. //             Console_setCursorPosition(7, 0);
  580. //             printf("~6.COPY THE DATA FROM ONE ORGANISATION TO ANOTHER");
  581. //             Console_setCursorAttribute(FG_DEFAULT);
  582. //             cleanTheInput3();
  583. //             int pos = 12;
  584. //             Console_setCursorPosition(pos++, 13);
  585. //             puts("The organisation you want to copy data from:");
  586. //             int nFrom;
  587. //             getTheInd(&pos, &nFrom, organisations);
  588. //             Console_setCursorPosition(pos++, 13);
  589. //             puts("The organisation you want to copy data to:");
  590. //             int nTo;
  591. //             getTheInd(&pos, &nTo, organisations);
  592. //             struct Organisation *copied = copyTheOrganisation(organisations, nFrom, nTo);
  593. //             cleanTheOutput();
  594. //             Console_setCursorPosition(1, 80);
  595. //             puts("Rewritten organisation:");
  596. //             int ps = 2;
  597. //             printTheOrganisation(copied, nTo, &ps);
  598. //             break;
  599. //         }
  600. //         case 55:
  601. //         {
  602. //             cleanTheCommand();
  603. //             Console_setCursorPosition(0, 0);
  604. //             printTheCommand();
  605. //             Console_setCursorAttribute(FG_INTENSITY_CYAN);
  606. //             Console_setCursorPosition(7, 0);
  607. //             printf("~7.SAVE DATA TO SOME COMPUTER FILE");
  608. //             Console_setCursorAttribute(FG_DEFAULT);
  609. //             cleanTheInput3();
  610.  
  611. //             break;
  612. //         }
  613. //         case 27:
  614. //         {
  615. //             break;
  616. //         }
  617. //         default:
  618. //         {
  619. //             Console_setCursorPosition(11, 13);
  620. //             puts("ERROR : Invalid input. Please, try again:");
  621. //             cleanTheInput4();
  622. //             Console_setCursorPosition(12, 13);
  623. //             break;
  624. //         }
  625. //         }
  626. //         conHideCursor();
  627. //         for (int i = 12; i < consoleSize.rows; i++)
  628. //         {
  629. //             for (int x = 12; x < consoleSize.columns; x++)
  630. //             {
  631. //                 Console_setCursorPosition(i, x);
  632. //                 puts(" ");
  633. //             }
  634. //         }
  635. //         conShowCursor();
  636. //         Console_setCursorPosition(12, 13);
  637. //     }
  638. // }
  639.  
  640. // void task2()
  641. // {
  642. // }
  643.  
  644. // struct Organisation *copyTheOrganisation(struct Organisation *organisations[], int indexFrom, int indexTo)
  645. // {
  646. //     strcpy(organisations[indexTo]->organisationName, organisations[indexFrom]->organisationName);
  647. //     organisations[indexTo]->income = organisations[indexFrom]->income;
  648. //     organisations[indexTo]->rating = organisations[indexFrom]->rating;
  649. //     strcpy(organisations[indexTo]->country, organisations[indexFrom]->country);
  650. //     strcpy(organisations[indexTo]->staff.staffName, organisations[indexFrom]->staff.staffName);
  651. //     organisations[indexTo]->staff.staffAmmount = organisations[indexFrom]->staff.staffAmmount;
  652. //     return organisations[indexTo];
  653. // }
  654.  
  655. // struct Organisation *rewriteTheOrganisation(char *nameO, int income, float rating, char *country, char *name, int ammount, int index, struct Organisation *organisations[])
  656. // {
  657. //     strcpy(organisations[index]->organisationName, nameO);
  658. //     organisations[index]->income = income;
  659. //     organisations[index]->rating = rating;
  660. //     strcpy(organisations[index]->country, country);
  661. //     strcpy(organisations[index]->staff.staffName, name);
  662. //     organisations[index]->staff.staffAmmount = ammount;
  663. //     return organisations[index];
  664. // }
  665.  
  666. // void getTheInd(int *pos, int *n, struct Organisation *organisations[])
  667. // {
  668. //     Console_setCursorPosition(*pos, 13);
  669. //     *pos = *pos + 1;
  670. //     puts("Enter index:");
  671. //     Console_setCursorPosition(*pos, 13);
  672. //     *pos = *pos + 1;
  673. //     *n = getTheIndex(*pos - 1, countStructs(LENGTH, organisations) - 1);
  674. // }
  675.  
  676. // void getTheStruct(char *nameO, int *income, float *rating, char *country, char *staffName, int *ammount, int *pos)
  677. // {
  678. //     Console_setCursorPosition(*pos, 13);
  679. //     *pos = *pos + 1;
  680. //     puts("Enter name:");
  681. //     Console_setCursorPosition(*pos, 13);
  682. //     *pos = *pos + 1;
  683. //     fgets(nameO, 50, stdin);
  684. //     nameO[strlen(nameO) - 1] = '\0';
  685. //     Console_setCursorPosition(*pos, 13);
  686. //     *pos = *pos + 1;
  687. //     puts("Enter income:");
  688. //     Console_setCursorPosition(*pos, 13);
  689. //     *pos = *pos + 1;
  690. //     *income = getTheValue(*pos - 1);
  691. //     Console_setCursorPosition(*pos, 13);
  692. //     *pos = *pos + 1;
  693. //     puts("Enter rating:");
  694. //     Console_setCursorPosition(*pos, 13);
  695. //     *pos = *pos + 1;
  696. //     *rating = getFloatValue(*pos - 1);
  697. //     Console_setCursorPosition(*pos, 13);
  698. //     *pos = *pos + 1;
  699. //     puts("Enter country:");
  700. //     Console_setCursorPosition(*pos, 13);
  701. //     *pos = *pos + 1;
  702. //     fgets(country, 50, stdin);
  703. //     country[strlen(country) - 1] = '\0';
  704. //     Console_setCursorPosition(*pos, 13);
  705. //     *pos = *pos + 1;
  706. //     puts("Enter headmaster name:");
  707. //     Console_setCursorPosition(*pos, 13);
  708. //     *pos = *pos + 1;
  709. //     fgets(staffName, 50, stdin);
  710. //     staffName[strlen(staffName) - 1] = '\0';
  711. //     Console_setCursorPosition(*pos, 13);
  712. //     *pos = *pos + 1;
  713. //     puts("Enter ammount of staff:");
  714. //     Console_setCursorPosition(*pos, 13);
  715. //     *pos = *pos + 1;
  716. //     *ammount = getTheValue(*pos - 1);
  717. // }
  718.  
  719. // void fromSomeCountry(int length, struct Organisation *organisations[], char *country, struct Organisation *fromSomeCountry[])
  720. // {
  721. //     int j = 0;
  722. //     for (int i = 0; i < countStructs(length, organisations); i++)
  723. //     {
  724. //         if (!strcmp(organisations[i]->country, country))
  725. //         {
  726.  
  727. //             fromSomeCountry[j++] = organisations[i];
  728. //         }
  729. //     }
  730. // }
  731.  
  732. // void printTheCommand()
  733. // {
  734. //     printf("AVAILABLE COMMANDS:\n1.ADD NEW ORGANISATION WITH YOUR DATA\n2.DELETE ORGANISATION ON SOME POSITION\n"
  735. //            "3.REWRITE SOME ORGANISATION\n4.REWRITE THE FIELD OF SOME ORGANISATION\n5.FIND ALL ORGANISATIONS FROM SOME COUNTRY\n"
  736. //            "6.COPY THE DATA FROM ONE ORGANISATION TO ANOTHER\n7.SAVE DATA TO SOME COMPUTER FILE\n---If you want to return to the MENU press \"ESC\"---");
  737. // }
  738.  
  739. // void printTheOrganisation(struct Organisation *organisation, int count, int *pos)
  740. // {
  741. //     int output = 80;
  742. //     Console_setCursorPosition(*pos, output);
  743. //     *pos = *pos + 1;
  744. //     printf("============%i organisation============", count);
  745. //     Console_setCursorPosition(*pos, output);
  746. //     *pos = *pos + 1;
  747. //     printf("Organisation Name: %s", organisation->organisationName);
  748. //     Console_setCursorPosition(*pos, output);
  749. //     *pos = *pos + 1;
  750. //     printf("Income: %i", organisation->income);
  751. //     Console_setCursorPosition(*pos, output);
  752. //     *pos = *pos + 1;
  753. //     printf("Rating: %.2f", organisation->rating);
  754. //     Console_setCursorPosition(*pos, output);
  755. //     *pos = *pos + 1;
  756. //     printf("Country: %s", organisation->country);
  757. //     Console_setCursorPosition(*pos, output);
  758. //     *pos = *pos + 1;
  759. //     printf("Headmaster Name: %s", organisation->staff.staffName);
  760. //     Console_setCursorPosition(*pos, output);
  761. //     *pos = *pos + 1;
  762. //     printf("Ammount of staff: %i", organisation->staff.staffAmmount);
  763. // }
  764.  
  765. // bool compTheStruct(struct Organisation *organisation1, struct Organisation *organisation2)
  766. // {
  767. //     if (!strcmp(organisation1->organisationName, organisation2->organisationName) && organisation1->income == organisation2->income
  768. //     && organisation1->rating == organisation2->rating
  769. //     && !strcmp(organisation1->country, organisation2->country)
  770. //     && !strcmp(organisation1->staff.staffName, organisation2->staff.staffName) && organisation1->staff.staffAmmount == organisation2->staff.staffAmmount)
  771. //         return 1;
  772. //     return 0;
  773. // }
  774.  
  775. // struct Organisation *fillTheStruct(char *nameO, int income, float rating, char *country, char *name, int ammount)
  776. // {
  777. //     struct Organisation *organisation = malloc(sizeof(struct Organisation));
  778. //     strcpy(organisation->organisationName, nameO);
  779. //     organisation->income = income;
  780. //     organisation->rating = rating;
  781. //     strcpy(organisation->country, country);
  782. //     strcpy(organisation->staff.staffName, name);
  783. //     organisation->staff.staffAmmount = ammount;
  784. //     return organisation;
  785. // }
  786.  
  787. // void freeStruct(struct Organisation *organisations[])
  788. // {
  789. //     for (int i = 0; i < countStructs(LENGTH, organisations); i++)
  790. //     {
  791. //         free(organisations[i]);
  792. //     }
  793. // }
  794.  
  795. // struct Organisation *rewriteTheField(char *str, char *strNew, struct Organisation *organisation)
  796. // {
  797. //     if (!strcmp(str, "Organisation Name"))
  798. //     {
  799. //         strcpy(organisation->organisationName, strNew);
  800. //     }
  801. //     else if (!strcmp(str, "Income"))
  802. //     {
  803. //         organisation->income = atoi(strNew);
  804. //     }
  805. //     else if (!strcmp(str, "Rating"))
  806. //     {
  807. //         organisation->rating = atof(strNew);
  808. //     }
  809. //     else if (!strcmp(str, "Country"))
  810. //     {
  811. //         strcpy(organisation->country, strNew);
  812. //     }
  813. //     else if (!strcmp(str, "Headmaster Name"))
  814. //     {
  815. //         strcpy(organisation->staff.staffName, strNew);
  816. //     }
  817. //     else if (!strcmp(str, "Ammount of staff"))
  818. //     {
  819. //         organisation->staff.staffAmmount = atoi(strNew);
  820. //     }
  821. //     return organisation;
  822. // }
  823.  
  824. // int getTheValue(int pos)
  825. // {
  826. //     struct ConsoleSize consoleSize = Console_size();
  827. //     int maxRows = consoleSize.rows;
  828. //     int n;
  829. //     Console_setCursorPosition(pos, 13);
  830. //     int nread = scanf("%i", &n);
  831. //     cleanBuffer();
  832. //     while (nread != 1)
  833. //     {
  834. //         for (int i = 12; i < consoleSize.columns; i++)
  835. //         {
  836. //             Console_setCursorPosition(11, i);
  837. //             puts(" ");
  838. //         }
  839. //         for (int i = 12; i < consoleSize.columns; i++)
  840. //         {
  841. //             Console_setCursorPosition(pos, i);
  842. //             puts(" ");
  843. //         }
  844. //         Console_setCursorPosition(11, 13);
  845. //         puts("ERROR : Invalid input. Please, try again:");
  846. //         for (int i = 12; i < consoleSize.columns; i++)
  847. //         {
  848. //             Console_setCursorPosition(pos, i);
  849. //             puts(" ");
  850. //         }
  851. //         Console_setCursorPosition(pos, 13);
  852. //         nread = scanf("%i", &n);
  853. //         cleanBuffer();
  854. //     }
  855. //     for (int i = 12; i < consoleSize.columns; i++)
  856. //     {
  857. //         Console_setCursorPosition(11, i);
  858. //         puts(" ");
  859. //     }
  860. //     return n;
  861. // }
  862.  
  863. // int getTheIndex(int pos, int count)
  864. // {
  865. //     struct ConsoleSize consoleSize = Console_size();
  866. //     int maxRows = consoleSize.rows;
  867. //     int n;
  868. //     Console_setCursorPosition(pos, 13);
  869. //     int nread = scanf("%i", &n);
  870. //     cleanBuffer();
  871. //     while (n < 0 || n > count)
  872. //     {
  873. //         for (int i = 12; i < consoleSize.columns; i++)
  874. //         {
  875. //             Console_setCursorPosition(11, i);
  876. //             puts(" ");
  877. //         }
  878. //         for (int i = 12; i < consoleSize.columns; i++)
  879. //         {
  880. //             Console_setCursorPosition(pos, i);
  881. //             puts(" ");
  882. //         }
  883. //         Console_setCursorPosition(11, 13);
  884. //         puts("ERROR : There is no organisation with such an index. Please, try again:");
  885. //         for (int i = 12; i < consoleSize.columns; i++)
  886. //         {
  887. //             Console_setCursorPosition(pos, i);
  888. //             puts(" ");
  889. //         }
  890. //         Console_setCursorPosition(pos, 13);
  891. //         nread = scanf("%i", &n);
  892. //         cleanBuffer();
  893. //     }
  894. //     for (int i = 12; i < consoleSize.columns; i++)
  895. //     {
  896. //         Console_setCursorPosition(11, i);
  897. //         puts(" ");
  898. //     }
  899. //     return n;
  900. // }
  901.  
  902. // void cleanTheCommand()
  903. // {
  904. //     conHideCursor();
  905. //     for (int y = 0; y < 60; y++)
  906. //     {
  907. //         for (int x = 0; x < 8; x++)
  908. //         {
  909. //             conHideCursor();
  910. //             Console_setCursorPosition(x, y);
  911. //             puts(" ");
  912. //         }
  913. //     }
  914. //     conShowCursor();
  915. // }
  916.  
  917. // void cleanTheOutput()
  918. // {
  919. //     conHideCursor();
  920. //     struct ConsoleSize consoleSize = Console_size();
  921. //     for (int y = 49; y < consoleSize.columns; y++)
  922. //     {
  923. //         for (int x = 0; x < consoleSize.rows; x++)
  924. //         {
  925. //             conHideCursor();
  926. //             Console_setCursorPosition(x, y);
  927. //             puts(" ");
  928. //         }
  929. //     }
  930. //     conShowCursor();
  931. // }
  932.  
  933. // void cleanTheInput2()
  934. // {
  935. //     conHideCursor();
  936. //     struct ConsoleSize consoleSize = Console_size();
  937. //     int maxRows = consoleSize.rows;
  938. //     int maxColumn = consoleSize.columns;
  939.  
  940. //     int y = maxRows - 1;
  941. //     for (int x = 12; x < consoleSize.columns; x++)
  942. //     {
  943. //         Console_setCursorPosition(y, x);
  944. //         puts(" ");
  945. //     }
  946. //     conShowCursor();
  947. // }
  948.  
  949. // void cleanTheInput3()
  950. // {
  951. //     conHideCursor();
  952. //     struct ConsoleSize consoleSize = Console_size();
  953. //     int maxColumn = consoleSize.columns;
  954. //     for (int y = 11; y < consoleSize.rows; y++)
  955. //     {
  956. //         for (int x = 0; x < consoleSize.columns; x++)
  957. //         {
  958. //             Console_setCursorPosition(y, x);
  959. //             puts(" ");
  960. //         }
  961. //     }
  962. //     Console_setCursorPosition(12, 0);
  963. //     puts("Your input:");
  964. //     Console_setCursorPosition(12, 13);
  965. //     conShowCursor();
  966. // }
  967.  
  968. // void cleanTheInput4()
  969. // {
  970. //     conHideCursor();
  971. //     struct ConsoleSize consoleSize = Console_size();
  972. //     int maxColumn = consoleSize.columns;
  973. //     for (int x = 12; x < consoleSize.columns; x++)
  974. //     {
  975. //         Console_setCursorPosition(12, x);
  976. //         puts(" ");
  977. //     }
  978. //     Console_setCursorPosition(12, 13);
  979. //     conShowCursor();
  980. // }
  981.  
  982. // void cleanBuffer()
  983. // {
  984. //     char c = 0;
  985. //     while (c != '\n')
  986. //     {
  987. //         c = getchar();
  988. //     }
  989. // }
  990.  
  991. // float getFloatValue(int pos)
  992. // {
  993. //     struct ConsoleSize consoleSize = Console_size();
  994. //     int maxRows = consoleSize.rows;
  995. //     float n;
  996. //     Console_setCursorPosition(pos, 13);
  997. //     int nread = scanf("%f", &n);
  998. //     cleanBuffer();
  999. //     while (nread != 1)
  1000. //     {
  1001. //         conHideCursor();
  1002. //         for (int i = 12; i < consoleSize.columns; i++)
  1003. //         {
  1004. //             Console_setCursorPosition(11, i);
  1005. //             puts(" ");
  1006. //         }
  1007. //         for (int i = 12; i < consoleSize.columns; i++)
  1008. //         {
  1009. //             Console_setCursorPosition(pos, i);
  1010. //             puts(" ");
  1011. //         }
  1012. //         Console_setCursorPosition(11, 13);
  1013. //         puts("ERROR : Invalid input. Please, try again:");
  1014. //         for (int i = 12; i < consoleSize.columns; i++)
  1015. //         {
  1016. //             Console_setCursorPosition(pos, i);
  1017. //             puts(" ");
  1018. //         }
  1019. //         conShowCursor();
  1020. //         Console_setCursorPosition(pos, 13);
  1021. //         nread = scanf("%f", &n);
  1022. //         cleanBuffer();
  1023. //     }
  1024. //     conHideCursor();
  1025. //     for (int i = 12; i < consoleSize.columns; i++)
  1026. //     {
  1027. //         Console_setCursorPosition(11, i);
  1028. //         puts(" ");
  1029. //     }
  1030. //     conShowCursor();
  1031. //     return n;
  1032. // }
  1033.  
  1034. int countStructs(int length, struct Organisation *array[])
  1035. {
  1036.     int count = 0;
  1037.     for (int i = 0; i < length; i++)
  1038.     {
  1039.         if (array[i] == NULL)
  1040.         {
  1041.             break;
  1042.         }
  1043.         count++;
  1044.     }
  1045.     return count;
  1046. }
  1047.  
  1048. // bool addNewStruct(int length, struct Organisation *newOrganisation, struct Organisation *array[])
  1049. // {
  1050. //     int count = countStructs(length, array);
  1051. //     if (count == LENGTH)
  1052. //     {
  1053. //         return false;
  1054. //     }
  1055. //     else
  1056. //     {
  1057. //         array[count] = newOrganisation;
  1058. //         return true;
  1059. //     }
  1060. // }
  1061.  
  1062. // struct Organisation *removeOrganisation(int length, int index, struct Organisation *array[])
  1063. // {
  1064. //     int count = countStructs(length, array);
  1065. //     if (index < 0 || index >= count)
  1066. //     {
  1067. //         return NULL;
  1068. //     }
  1069. //     else
  1070. //     {
  1071. //         struct Organisation *organisationToRemove = array[index];
  1072. //         for (int i = index + 1; i < count; i++)
  1073. //         {
  1074. //             array[i - 1] = array[i];
  1075. //         }
  1076. //         array[count - 1] = NULL;
  1077. //         return organisationToRemove;
  1078. //     }
  1079. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement