Advertisement
Guest User

Untitled

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