Advertisement
Guest User

PROJETO DO CARALHO

a guest
May 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5.  
  6. typedef struct to_do{
  7. int prioridade;
  8. char tarefa[128];
  9. struct to_do* next;
  10. struct to_do* prev;
  11. }to_do;
  12. typedef struct doing{
  13. char tarefa[128];
  14. char utilizador[128];
  15. struct doing* next;
  16. struct doing* prev;
  17. }doing;
  18. typedef struct done{
  19. char tarefa[128];
  20. char utilizador[128];
  21. struct done* prev;
  22. struct done* next;
  23. char dia[128];
  24. char mes[128];
  25. char ano[128];
  26.  
  27.  
  28. }done;
  29. typedef struct user{
  30. char utilizador[128];
  31. char email[128];
  32. int id;
  33. struct user *next;
  34. struct done* prev;
  35. }user;
  36.  
  37. int menu(){
  38. int opcao=10;
  39. printf("\nInserir tarefas na lista To Do\t\t\t\t (1)\n");
  40. printf("Mover tarefas de To do para Doing\t\t\t (2)\n");
  41. printf("Mover tarefas de Doing para To do\t\t\t (3)\n");
  42. printf("Mover tarefas de Done para To do\t\t\t (4)\n");
  43. printf("Mover tarefa para Done\t\t\t\t\t (5)\n");
  44. printf("Alterar utilizador de uma tarefa em Doing\t\t (6)\n");
  45. printf("Ver as tarefas atribuidas por utilizador\t\t (7)\n");
  46. printf("Listar utilizadores \t\t\t\t\t (8)\n");
  47. printf("Listar tarefas \t\t\t\t\t\t (9)\n");
  48. printf("Sair\t\t\t\t\t\t\t (0)\n\n");
  49. printf("Escolha uma opçao 0-9: ");
  50. while (1) {
  51. if (scanf("%d",&opcao) == 1 && opcao<10 && opcao>=0)
  52. break;
  53. printf("Insira uma opção válida entre 0-9: ");
  54. fseek(stdin,0,SEEK_END);
  55. }return opcao;
  56. }
  57.  
  58. to_do* cria_lista_todo(){
  59. to_do*aux = (to_do*) malloc (sizeof (to_do));
  60. assert(aux);
  61. aux -> next = NULL;
  62. return aux;
  63. }
  64. doing* cria_lista_doing(){
  65. doing*aux = (doing*) malloc (sizeof (doing));
  66. assert(aux);
  67. aux -> next = NULL;
  68. return aux;
  69. }
  70. done* cria_lista_done(){
  71. done*aux = (done*) malloc (sizeof (done));
  72. assert(aux);
  73. aux -> next = NULL;
  74. return aux;
  75. }
  76. user* cria_lista_user(){
  77. user*aux = (user*) malloc (sizeof (user));
  78. assert(aux);
  79. aux -> next = NULL;
  80. return aux;
  81. }
  82.  
  83. void listar_doing(doing*b){
  84. if(b->next!=NULL){
  85. printf("\nTarefas em Doing:\n");
  86. for(b=b->next;b;b=b->next){
  87. strtok(b->tarefa, "\n");
  88. printf("Tarefa: '%s'. Utilizador: %s\n", b->tarefa, b->utilizador);
  89. }
  90. }
  91. }
  92. void listar_tarefas(to_do*h){
  93. if(h->next!=NULL){
  94. printf("\nTarefas em To Do:\n");
  95. for(h=h->next;h;h=h->next){
  96. strtok(h->tarefa,"\n");
  97. printf("Tarefa: '%s'. Prioridade: %d \n",h->tarefa,h->prioridade);
  98. }
  99. }
  100. }
  101. void listar_done(done*h){
  102. if(h->next!=NULL){
  103. printf("\nTarefas em Done:\n");
  104. for(h=h->next;h;h=h->next){
  105. printf("Tarefa: '%s'. Utilizador: '%s'. Data conclusao: %s/%s/%s\n", h->tarefa, h->utilizador, h->dia, h->mes, h->ano);
  106. }
  107. }
  108. }
  109. void listar_user(user*h){
  110. if(h->next!=NULL){
  111. printf("\nUtilizadores:\n");
  112. for(h=h->next;h;h=h->next){
  113. printf("%d\t%s\t %s \n",h->id,h->utilizador,h->email);
  114. }
  115. }
  116. }
  117. void listar_tarefas_por_user(doing*b,done*d,user*u){
  118. doing*temp_doing=b;done*temp_done=d;
  119. for(u=u->next;u;u=u->next){
  120. printf("\nTarefas do user '%s' com o ID %d:\n",u->utilizador,u->id);
  121. printf("\tTarefas em Doing:\n");
  122. for(b=b->next;b;b=b->next){
  123. if(strcmp(b->utilizador,u->utilizador)==0){
  124. printf("\t\t%s\n",b->tarefa);
  125. }
  126. }
  127. printf("\tTarefas em Done:\n");
  128. for(d=d->next;d;d=d->next){
  129. if(strcmp(d->utilizador,u->utilizador)==0){
  130. printf("\t\t%s\n",d->tarefa);
  131. }
  132. }
  133. d=temp_done;
  134. b=temp_doing;
  135. }
  136. }
  137.  
  138. int remover_todo(to_do*h, char*key){
  139. int teste=0;
  140. while(h->next!=NULL){
  141. if((strcmp(h->next->tarefa,key)==0) || h->next==NULL){
  142. teste=1;
  143. break;
  144. }
  145. if((strcmp(h->tarefa,key)==0) || h->next==NULL){
  146. teste=2;
  147. break;
  148. }
  149. h=h->next;
  150. }
  151. if(teste==1){
  152. to_do*aux=h->next;
  153. h->next=h->next->next;
  154. if(h->next!=NULL)
  155. h->next->prev=h;
  156. free(aux);
  157. }
  158. if(teste==2){
  159. to_do*temp;
  160. temp=h;
  161. free(temp);
  162. }
  163. return 0;
  164. }
  165. int remover_doing(doing*b, char*key){
  166. int teste=0;
  167. while(b->next!=NULL){
  168. if((strcmp(b->next->tarefa,key)==0) || b->next==NULL){
  169. teste=1;
  170. break;
  171. }
  172. if((strcmp(b->tarefa,key)==0) || b->next==NULL){
  173. teste=2;
  174. break;
  175. }
  176. b=b->next;
  177. }
  178. if(teste==1){
  179. doing*aux=b->next;
  180. b->next=b->next->next;
  181. if(b->next!=NULL)
  182. b->next->prev=b;
  183. free(aux);
  184. }
  185. if(teste==2){
  186. doing*temp;
  187. temp=b;
  188. free(temp);
  189. }
  190. return 0;
  191. }
  192. int remover_done(done*d, char key[128]){
  193. int teste=0;
  194. while(d->next!=NULL){
  195. if((strcmp(d->next->tarefa,key)==0) || d->next==NULL){
  196. teste=1;
  197. break;
  198. }
  199. if((strcmp(d->tarefa,key)==0) || d->next==NULL){
  200. teste=2;
  201. break;
  202. }
  203. d=d->next;
  204. }
  205. if(teste==1){
  206. done*aux=d->next;
  207. d->next=d->next->next;
  208. if(d->next!=NULL)
  209. d->next->prev=d;
  210. free(aux);
  211. }
  212. if(teste==2){
  213. done*temp;
  214. temp=d;
  215. free(temp);
  216. }
  217. return 0;
  218. }
  219.  
  220. void altera_user(doing*b,user*u) {
  221. char tarefa_input[128];int teste=0,teste2=0;int id;
  222. doing*temp_doing=b;user*temp_user=u;
  223. listar_doing(b);
  224. printf("\nIndique a tarefa a alterar o utilizador: ");
  225. while(teste==0){
  226. fgets(tarefa_input, 128, stdin);strtok(tarefa_input, "\n");
  227. for(b=b->next;b;b=b->next){
  228. if(strcmp(b->tarefa,tarefa_input)==0){
  229. teste=1;
  230. break;
  231. }
  232. }
  233. if(teste==0){
  234. b=temp_doing;
  235. printf("Indique uma tarefa válida: ");
  236. }
  237. }
  238. listar_user(u);
  239. printf("\nIndique o ID do utilizador: ");
  240. while(teste2==0) {
  241. scanf("%d", &id);
  242. for (u = u->next; u;u= u->next) {
  243. if (u->id == id) {
  244. teste2 = 1;
  245. break;
  246. }
  247. if(u->next==NULL)
  248. break;
  249. }
  250. if (teste2 == 0) {
  251. u = temp_user;
  252. printf("Indique um ID válido: ");
  253. }
  254. }
  255. strcpy(b->utilizador,u->utilizador);
  256. }
  257. int countlines(char*filename) {
  258. FILE *fp;
  259. int count = 1;
  260. char c;
  261. fp = fopen(filename, "r");
  262. for (c = (char) getc(fp); c != EOF; c = (char) getc(fp))
  263. if (c == '\n')
  264. count = count + 1;
  265. fclose(fp);
  266. return count;
  267. }
  268.  
  269. void inserir_doing(doing*h,char tarefa_input[128],char utilizador_input[128]){
  270. doing*temp=malloc(sizeof(h));
  271. temp->next=h->next;
  272. h->next=temp;
  273. strcpy(temp->utilizador, utilizador_input);
  274. strcpy(temp->tarefa, tarefa_input);
  275. }
  276. void inserir_done(done*h,char tarefa[128],char utilizador[128],char day[128],char month[128], char year[128]){
  277. done*temp=malloc(sizeof(h));
  278. temp->next=h->next;
  279. h->next=temp;
  280. strcpy(temp->tarefa, tarefa);
  281. strcpy(temp->utilizador, utilizador);
  282. strcpy(temp->dia,day );
  283. strcpy(temp->mes,month );
  284. strcpy(temp->ano,year );
  285.  
  286. }
  287. void inserir_user(user*h,char utilizador[128],char email[128],int id){
  288. user*temp=malloc(sizeof(h));
  289. while(h->next!=NULL && h->next->id<id)
  290. h=h->next;
  291. temp->next=h->next;
  292. h->next=temp;
  293. strcpy(temp->utilizador,utilizador);
  294. strcpy(temp->email,email);
  295. temp->id = id;
  296. }
  297. void inserir_tarefa(to_do*h){
  298. to_do*temp=malloc(sizeof(h));
  299. char tarefa_input[128];int prioridade_input;
  300. printf("Tarefa a inserir: ");fgets(tarefa_input,128,stdin);strtok("\n",tarefa_input);
  301. printf("Insira a sua prioridade: ");
  302. while (1) {
  303. if (scanf("%d",&prioridade_input) == 1 && prioridade_input<11 && prioridade_input>0)
  304. break;
  305. printf("Insira uma opção válida entre 0-10: ");
  306. fseek(stdin,0,SEEK_END);
  307. }
  308. while(h->next!=NULL && h->next->prioridade>prioridade_input)
  309. h=h->next;
  310.  
  311. temp->next=h->next;
  312. h->next=temp;
  313. strcpy(temp->tarefa,tarefa_input);strtok(temp->tarefa,"\n");
  314. temp->prioridade = prioridade_input;
  315.  
  316. }
  317. void inserir_tarefa2(to_do*h,char tarefa_input[128],int prioridade_input){
  318. to_do*temp=malloc(sizeof(h));
  319. while(h->next!=NULL && h->next->prioridade>prioridade_input)
  320. h=h->next;
  321.  
  322. temp->next=h->next;
  323. h->next=temp;
  324. strcpy(temp->tarefa,tarefa_input);
  325. temp->prioridade = prioridade_input;
  326.  
  327. }
  328.  
  329. void todo_para_doing(to_do*h , doing*b, user*u){
  330. int teste=0,teste2=0;
  331. to_do*temporario_todo=h;
  332. user*temporario_user=u;
  333. doing*temp=malloc(sizeof(b));
  334. listar_tarefas(h);
  335. char tarefa_input[128];int id;
  336. printf("\nIndique a tarefa que deseja passar para Doing: ");
  337. fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
  338.  
  339. todo_para_doing:
  340. for(h=h->next;h;h=h->next){
  341. if (strcmp(h->tarefa, tarefa_input)==0){
  342. listar_user(u);
  343. printf("Digite o ID do utilizador: ");
  344.  
  345. while(teste2!=3) {
  346.  
  347. scanf("%d", &id);
  348. for(u=u->next;u;u=u->next){
  349. if (u->id == id) {
  350. teste2 = 1;
  351. break;
  352. }
  353. }
  354. if (teste2 == 0) {
  355. u = temporario_user;
  356. printf("Digite um ID válido: ");
  357. } else
  358. teste2 = 3;
  359. }
  360.  
  361. temp->next=b->next;
  362. b->next=temp;
  363. strcpy(temp->utilizador, u->utilizador);
  364. strcpy(temp->tarefa, tarefa_input);
  365. teste=1;
  366. break;
  367. }
  368. }
  369. h=temporario_todo;
  370.  
  371. if(teste==0){
  372. printf("Indique uma tarefa válida: ");
  373. fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
  374. goto todo_para_doing;
  375. }
  376. else
  377. remover_todo(h,tarefa_input);
  378. }
  379. void doing_para_todo(doing*b, to_do*h){
  380. to_do*temp=malloc(sizeof(h));
  381. int prioridade_input;
  382. int teste=0;
  383. doing*temporario=b;
  384. listar_doing(b);
  385. char tarefa_input[128];
  386. printf("\nIndique a tarefa que deseja passar para To Do:");
  387. fgets(tarefa_input, 100, stdin);strtok(tarefa_input, "\n");
  388. doing_para_todo:
  389. for(b=b->next;b;b=b->next){
  390. if (strcmp(b->tarefa, tarefa_input)==0){
  391. printf("Digite a nova prioridade da tarefa: ");
  392. scanf("%d", &prioridade_input);
  393. teste=1;
  394. break;
  395. }
  396. }
  397. b=temporario;
  398.  
  399. if(teste==0){
  400. printf("Indique uma tarefa válida: ");
  401. fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
  402. goto doing_para_todo;
  403. }
  404. if(prioridade_input>10 || prioridade_input<1){
  405. printf("A prioridade deve ser de 0 a 10. ");
  406. goto doing_para_todo;
  407. }
  408. else{
  409. temp->next=h->next;
  410. h->next=temp;
  411. strcpy(temp->tarefa, tarefa_input);
  412. temp->prioridade=prioridade_input;
  413. remover_doing(b,tarefa_input);
  414. }
  415.  
  416. }
  417. void doing_para_done(doing*b, done*d){
  418. done*temp=malloc(sizeof(d));
  419. doing*tempo=b;
  420. int teste=0;
  421. listar_doing(b);
  422. char day[128],month[128],year[128];
  423. char tarefa_input[128];
  424. printf("\nIndique a tarefa concluida: ");
  425. fgets(tarefa_input, 128, stdin);strtok(tarefa_input, "\n");
  426. doing_para_done:
  427. for(b=b->next;b;b=b->next){
  428. if(strcmp(b->tarefa, tarefa_input)==0){
  429. printf("Indique a data de conclusao:\n");
  430. printf("Dia: ");fgets(day, 128, stdin);strtok(day, "\n");
  431. printf("Mes: ");fgets(month, 128, stdin);strtok(month, "\n");
  432. printf("Ano: ");fgets(year, 128, stdin);strtok(year, "\n");
  433. temp->next=d->next;
  434. d->next=temp;
  435. strcpy(temp->tarefa, tarefa_input);
  436. strcpy(temp->dia, day);
  437. strcpy(temp->mes, month);
  438. strcpy(temp->ano, year);
  439. strcpy(temp->utilizador, b->utilizador);
  440. teste=1;
  441. break;
  442.  
  443. }
  444. }
  445. b=tempo;
  446. if(teste==0){
  447. printf("Indique uma tarefa válida: ");
  448. fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
  449. goto doing_para_done;
  450. }
  451. else
  452. remover_doing(b,tarefa_input);
  453. }
  454. void done_para_todo(done*d, to_do*b){
  455. done*tempo=d;
  456. listar_done(d);
  457. int prioridade_input;
  458. int teste=0;
  459. char tarefa_input[128];
  460. printf("\nIndique a tarefa a passar para To Do: ");
  461. fgets(tarefa_input, 128, stdin);strtok(tarefa_input, "\n");
  462. dpt:
  463. for(d=d->next;d;d=d->next){
  464. if(strcmp(d->tarefa, tarefa_input)==0){
  465. printf("Indique a nova prioridade da tarefa: ");
  466. scanf("%d", &prioridade_input);
  467. teste=1;
  468. break;
  469. }
  470. }
  471. d=tempo;
  472. if(teste==0){
  473. printf("Indique uma tarefa válida: ");
  474. fgets(tarefa_input, 128, stdin);strtok(tarefa_input,"\n");
  475. goto dpt;
  476. }
  477. if(prioridade_input>10 || prioridade_input<1){
  478. printf("A prioridade deve ser de 0 a 10. ");
  479. goto dpt;
  480. }
  481. else{
  482. inserir_tarefa2(b,tarefa_input,prioridade_input);
  483. remover_done(d,tarefa_input);
  484. }
  485. }
  486.  
  487. void carregar_todo(to_do*h) {
  488. FILE *fp;
  489. fp = fopen("todo.txt", "r");
  490. char tarefa_input[128];int prioridade_input;
  491. int linhas = countlines("todo.txt");
  492. for (int i = 0; i < (linhas / 2); i++) {
  493. fgets(tarefa_input,sizeof(tarefa_input),fp);strtok(tarefa_input,"\n");
  494. fscanf(fp,"%d",&prioridade_input);
  495. fgetc(fp);
  496. inserir_tarefa2(h,tarefa_input,prioridade_input);
  497. }
  498. fclose(fp);
  499. }
  500. void carregar_doing(doing*h) {
  501. FILE *fp;
  502. fp = fopen("doing.txt", "r");
  503. char tarefa_input[128], utilizador_input[128];
  504. int linhas = countlines("doing.txt");
  505. for (int i = 0; i < (linhas / 2); i++) {
  506. fgets(tarefa_input,sizeof(tarefa_input),fp);strtok(tarefa_input,"\n");
  507. fgets(utilizador_input,sizeof(utilizador_input),fp);strtok(utilizador_input,"\n");
  508. inserir_doing(h,tarefa_input,utilizador_input);
  509. }
  510. fclose(fp);
  511. }
  512. void carregar_done(done*h) {
  513. FILE *fp;
  514. fp = fopen("done.txt", "r");
  515. char tarefa_input[128], utilizador_input[128],day[128],month[128],year[128];
  516. int linhas = countlines("done.txt");
  517. for (int i = 0; i < (linhas / 5); i++) {
  518. fgets(tarefa_input,sizeof(tarefa_input),fp);strtok(tarefa_input,"\n");
  519. fgets(utilizador_input,sizeof(utilizador_input),fp);strtok(utilizador_input,"\n");
  520. fgets(day,sizeof(day),fp);strtok(day,"\n");
  521. fgets(month,sizeof(month),fp);strtok(month,"\n");
  522. fgets(year,sizeof(year),fp);strtok(year,"\n");
  523. inserir_done(h,tarefa_input,utilizador_input,day,month,year);
  524. }
  525. fclose(fp);
  526. }
  527. void carregar_utilizadores(user*h) {
  528. FILE *fp;
  529. fp = fopen("user.txt", "r");
  530. char utilizador[128], email[128];int id;
  531. int linhas = countlines("user.txt");
  532. for (int i = 0; i < (linhas / 3); i++) {
  533. fgets(utilizador,sizeof(utilizador),fp);strtok(utilizador,"\n");
  534. fgets(email,sizeof(email),fp);strtok(email,"\n");
  535. fscanf(fp,"%d",&id);
  536. fgetc(fp);
  537. inserir_user(h,utilizador,email,id);
  538. }
  539. fclose(fp);
  540. }
  541.  
  542. void guardar_todo(to_do*h){
  543. FILE *fp;
  544. fp = fopen("todo.txt", "w");
  545. for(h=h->next;h;h=h->next){
  546. fprintf(fp,"%s\n",h->tarefa);
  547. fprintf(fp,"%d\n",h->prioridade);
  548. }
  549. fclose(fp);
  550. }
  551. void guardar_doing(doing*h){
  552. FILE *fp;
  553. fp = fopen("doing.txt", "w");
  554. for(h=h->next;h;h=h->next){
  555. fprintf(fp,"%s\n",h->tarefa);
  556. fprintf(fp,"%s\n",h->utilizador);
  557. }
  558. fclose(fp);
  559. }
  560. void guardar_done(done*h){
  561. FILE *fp;
  562. fp = fopen("done.txt", "w");
  563. for(h=h->next;h;h=h->next){
  564. fprintf(fp,"%s\n",h->tarefa);
  565. fprintf(fp,"%s\n",h->utilizador);
  566. strtok(h->dia,"\n");fprintf(fp,"%s\n",h->dia);
  567. strtok(h->mes,"\n");fprintf(fp,"%s\n",h->mes);
  568. strtok(h->ano,"\n");fprintf(fp,"%s\n",h->ano);
  569. }
  570. fclose(fp);
  571. }
  572.  
  573. int main(){
  574. to_do*lista_todo;doing*lista_doing;done*lista_done;user*lista_user;
  575. lista_todo=cria_lista_todo();
  576. lista_doing=cria_lista_doing();
  577. lista_done=cria_lista_done();
  578. lista_user=cria_lista_user();
  579.  
  580. int opcao;
  581.  
  582. carregar_todo(lista_todo);
  583. carregar_utilizadores(lista_user);
  584. carregar_doing(lista_doing);
  585. carregar_done(lista_done);
  586.  
  587. MENU:
  588. opcao = menu();
  589. char input[128];
  590. fgets(input, 100, stdin);
  591. switch (opcao) {
  592. case 1:
  593. inserir_tarefa(lista_todo);
  594. goto MENU;
  595. case 2:
  596. todo_para_doing(lista_todo, lista_doing,lista_user);
  597. goto MENU;
  598. case 3:
  599. doing_para_todo(lista_doing, lista_todo);
  600. goto MENU;
  601. case 4:
  602. done_para_todo(lista_done,lista_todo);
  603. goto MENU;
  604. case 5:
  605. doing_para_done(lista_doing,lista_done);
  606. goto MENU;
  607. case 6:
  608. altera_user(lista_doing,lista_user);
  609. goto MENU;
  610. case 7:
  611. listar_tarefas_por_user(lista_doing,lista_done,lista_user);
  612. goto MENU;
  613. case 8:
  614. listar_user(lista_user);
  615. goto MENU;
  616. case 9:
  617. listar_tarefas(lista_todo);
  618. listar_doing(lista_doing);
  619. listar_done(lista_done);
  620. goto MENU;
  621. case 0:
  622. guardar_todo(lista_todo);
  623. guardar_doing(lista_doing);
  624. guardar_done(lista_done);
  625. return 0;
  626. default:break;
  627. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement