Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- typedef struct to_do{
- int prioridade;
- char tarefa[128];
- struct to_do* next;
- struct to_do* prev;
- }to_do;
- typedef struct doing{
- char tarefa[128];
- char utilizador[128];
- struct doing* next;
- struct doing* prev;
- }doing;
- typedef struct done{
- char tarefa[128];
- char utilizador[128];
- struct done* prev;
- struct done* next;
- char dia[128];
- char mes[128];
- char ano[128];
- }done;
- typedef struct user{
- char utilizador[128];
- char email[128];
- int id;
- struct user *next;
- struct done* prev;
- }user;
- int menu(){
- int opcao=10;
- printf("\nInserir tarefas na lista To Do\t\t\t\t (1)\n");
- printf("Mover tarefas de To do para Doing\t\t\t (2)\n");
- printf("Mover tarefas de Doing para To do\t\t\t (3)\n");
- printf("Mover tarefas de Done para To do\t\t\t (4)\n");
- printf("Mover tarefa para Done\t\t\t\t\t (5)\n");
- printf("Alterar utilizador de uma tarefa em Doing\t\t (6)\n");
- printf("Ver as tarefas atribuidas por utilizador\t\t (7)\n");
- printf("Listar utilizadores \t\t\t\t\t (8)\n");
- printf("Listar tarefas \t\t\t\t\t\t (9)\n");
- printf("Sair\t\t\t\t\t\t\t (0)\n\n");
- printf("Escolha uma opçao 0-9: ");
- while (1) {
- if (scanf("%d",&opcao) == 1 && opcao<10 && opcao>=0)
- break;
- printf("Insira uma opção válida entre 0-9: ");
- fseek(stdin,0,SEEK_END);
- }return opcao;
- }
- to_do* cria_lista_todo(){
- to_do*aux = (to_do*) malloc (sizeof (to_do));
- assert(aux);
- aux -> next = NULL;
- return aux;
- }
- doing* cria_lista_doing(){
- doing*aux = (doing*) malloc (sizeof (doing));
- assert(aux);
- aux -> next = NULL;
- return aux;
- }
- done* cria_lista_done(){
- done*aux = (done*) malloc (sizeof (done));
- assert(aux);
- aux -> next = NULL;
- return aux;
- }
- user* cria_lista_user(){
- user*aux = (user*) malloc (sizeof (user));
- assert(aux);
- aux -> next = NULL;
- return aux;
- }
- void listar_doing(doing*b){
- if(b->next!=NULL){
- printf("\nTarefas em Doing:\n");
- for(b=b->next;b;b=b->next){
- strtok(b->tarefa, "\n");
- printf("Tarefa: '%s'. Utilizador: %s\n", b->tarefa, b->utilizador);
- }
- }
- }
- void listar_tarefas(to_do*h){
- if(h->next!=NULL){
- printf("\nTarefas em To Do:\n");
- for(h=h->next;h;h=h->next){
- strtok(h->tarefa,"\n");
- printf("Tarefa: '%s'. Prioridade: %d \n",h->tarefa,h->prioridade);
- }
- }
- }
- void listar_done(done*h){
- if(h->next!=NULL){
- printf("\nTarefas em Done:\n");
- for(h=h->next;h;h=h->next){
- printf("Tarefa: '%s'. Utilizador: '%s'. Data conclusao: %s/%s/%s\n", h->tarefa, h->utilizador, h->dia, h->mes, h->ano);
- }
- }
- }
- void listar_user(user*h){
- if(h->next!=NULL){
- printf("\nUtilizadores:\n");
- for(h=h->next;h;h=h->next){
- printf("%d\t%s\t %s \n",h->id,h->utilizador,h->email);
- }
- }
- }
- void listar_tarefas_por_user(doing*b,done*d,user*u){
- doing*temp_doing=b;done*temp_done=d;
- for(u=u->next;u;u=u->next){
- printf("\nTarefas do user '%s' com o ID %d:\n",u->utilizador,u->id);
- printf("\tTarefas em Doing:\n");
- for(b=b->next;b;b=b->next){
- if(strcmp(b->utilizador,u->utilizador)==0){
- printf("\t\t%s\n",b->tarefa);
- }
- }
- printf("\tTarefas em Done:\n");
- for(d=d->next;d;d=d->next){
- if(strcmp(d->utilizador,u->utilizador)==0){
- printf("\t\t%s\n",d->tarefa);
- }
- }
- d=temp_done;
- b=temp_doing;
- }
- }
- int remover_todo(to_do*h, char*key){
- int teste=0;
- while(h->next!=NULL){
- if((strcmp(h->next->tarefa,key)==0) || h->next==NULL){
- teste=1;
- break;
- }
- if((strcmp(h->tarefa,key)==0) || h->next==NULL){
- teste=2;
- break;
- }
- h=h->next;
- }
- if(teste==1){
- to_do*aux=h->next;
- h->next=h->next->next;
- if(h->next!=NULL)
- h->next->prev=h;
- free(aux);
- }
- if(teste==2){
- to_do*temp;
- temp=h;
- free(temp);
- }
- return 0;
- }
- int remover_doing(doing*b, char*key){
- int teste=0;
- while(b->next!=NULL){
- if((strcmp(b->next->tarefa,key)==0) || b->next==NULL){
- teste=1;
- break;
- }
- if((strcmp(b->tarefa,key)==0) || b->next==NULL){
- teste=2;
- break;
- }
- b=b->next;
- }
- if(teste==1){
- doing*aux=b->next;
- b->next=b->next->next;
- if(b->next!=NULL)
- b->next->prev=b;
- free(aux);
- }
- if(teste==2){
- doing*temp;
- temp=b;
- free(temp);
- }
- return 0;
- }
- int remover_done(done*d, char key[128]){
- int teste=0;
- while(d->next!=NULL){
- if((strcmp(d->next->tarefa,key)==0) || d->next==NULL){
- teste=1;
- break;
- }
- if((strcmp(d->tarefa,key)==0) || d->next==NULL){
- teste=2;
- break;
- }
- d=d->next;
- }
- if(teste==1){
- done*aux=d->next;
- d->next=d->next->next;
- if(d->next!=NULL)
- d->next->prev=d;
- free(aux);
- }
- if(teste==2){
- done*temp;
- temp=d;
- free(temp);
- }
- return 0;
- }
- void altera_user(doing*b,user*u) {
- char tarefa_input[128];int teste=0,teste2=0;int id;
- doing*temp_doing=b;user*temp_user=u;
- listar_doing(b);
- printf("\nIndique a tarefa a alterar o utilizador: ");
- while(teste==0){
- fgets(tarefa_input, 128, stdin);strtok(tarefa_input, "\n");
- for(b=b->next;b;b=b->next){
- if(strcmp(b->tarefa,tarefa_input)==0){
- teste=1;
- break;
- }
- }
- if(teste==0){
- b=temp_doing;
- printf("Indique uma tarefa válida: ");
- }
- }
- listar_user(u);
- printf("\nIndique o ID do utilizador: ");
- while(teste2==0) {
- scanf("%d", &id);
- for (u = u->next; u;u= u->next) {
- if (u->id == id) {
- teste2 = 1;
- break;
- }
- if(u->next==NULL)
- break;
- }
- if (teste2 == 0) {
- u = temp_user;
- printf("Indique um ID válido: ");
- }
- }
- strcpy(b->utilizador,u->utilizador);
- }
- int countlines(char*filename) {
- FILE *fp;
- int count = 1;
- char c;
- fp = fopen(filename, "r");
- for (c = (char) getc(fp); c != EOF; c = (char) getc(fp))
- if (c == '\n')
- count = count + 1;
- fclose(fp);
- return count;
- }
- void inserir_doing(doing*h,char tarefa_input[128],char utilizador_input[128]){
- doing*temp=malloc(sizeof(h));
- temp->next=h->next;
- h->next=temp;
- strcpy(temp->utilizador, utilizador_input);
- strcpy(temp->tarefa, tarefa_input);
- }
- void inserir_done(done*h,char tarefa[128],char utilizador[128],char day[128],char month[128], char year[128]){
- done*temp=malloc(sizeof(h));
- temp->next=h->next;
- h->next=temp;
- strcpy(temp->tarefa, tarefa);
- strcpy(temp->utilizador, utilizador);
- strcpy(temp->dia,day );
- strcpy(temp->mes,month );
- strcpy(temp->ano,year );
- }
- void inserir_user(user*h,char utilizador[128],char email[128],int id){
- user*temp=malloc(sizeof(h));
- while(h->next!=NULL && h->next->id<id)
- h=h->next;
- temp->next=h->next;
- h->next=temp;
- strcpy(temp->utilizador,utilizador);
- strcpy(temp->email,email);
- temp->id = id;
- }
- void inserir_tarefa(to_do*h){
- to_do*temp=malloc(sizeof(h));
- char tarefa_input[128];int prioridade_input;
- printf("Tarefa a inserir: ");fgets(tarefa_input,128,stdin);strtok("\n",tarefa_input);
- printf("Insira a sua prioridade: ");
- while (1) {
- if (scanf("%d",&prioridade_input) == 1 && prioridade_input<11 && prioridade_input>0)
- break;
- printf("Insira uma opção válida entre 0-10: ");
- fseek(stdin,0,SEEK_END);
- }
- while(h->next!=NULL && h->next->prioridade>prioridade_input)
- h=h->next;
- temp->next=h->next;
- h->next=temp;
- strcpy(temp->tarefa,tarefa_input);strtok(temp->tarefa,"\n");
- temp->prioridade = prioridade_input;
- }
- void inserir_tarefa2(to_do*h,char tarefa_input[128],int prioridade_input){
- to_do*temp=malloc(sizeof(h));
- while(h->next!=NULL && h->next->prioridade>prioridade_input)
- h=h->next;
- temp->next=h->next;
- h->next=temp;
- strcpy(temp->tarefa,tarefa_input);
- temp->prioridade = prioridade_input;
- }
- void todo_para_doing(to_do*h , doing*b, user*u){
- int teste=0,teste2=0;
- to_do*temporario_todo=h;
- user*temporario_user=u;
- doing*temp=malloc(sizeof(b));
- listar_tarefas(h);
- char tarefa_input[128];int id;
- printf("\nIndique a tarefa que deseja passar para Doing: ");
- fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
- todo_para_doing:
- for(h=h->next;h;h=h->next){
- if (strcmp(h->tarefa, tarefa_input)==0){
- listar_user(u);
- printf("Digite o ID do utilizador: ");
- while(teste2!=3) {
- scanf("%d", &id);
- for(u=u->next;u;u=u->next){
- if (u->id == id) {
- teste2 = 1;
- break;
- }
- }
- if (teste2 == 0) {
- u = temporario_user;
- printf("Digite um ID válido: ");
- } else
- teste2 = 3;
- }
- temp->next=b->next;
- b->next=temp;
- strcpy(temp->utilizador, u->utilizador);
- strcpy(temp->tarefa, tarefa_input);
- teste=1;
- break;
- }
- }
- h=temporario_todo;
- if(teste==0){
- printf("Indique uma tarefa válida: ");
- fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
- goto todo_para_doing;
- }
- else
- remover_todo(h,tarefa_input);
- }
- void doing_para_todo(doing*b, to_do*h){
- to_do*temp=malloc(sizeof(h));
- int prioridade_input;
- int teste=0;
- doing*temporario=b;
- listar_doing(b);
- char tarefa_input[128];
- printf("\nIndique a tarefa que deseja passar para To Do:");
- fgets(tarefa_input, 100, stdin);strtok(tarefa_input, "\n");
- doing_para_todo:
- for(b=b->next;b;b=b->next){
- if (strcmp(b->tarefa, tarefa_input)==0){
- printf("Digite a nova prioridade da tarefa: ");
- scanf("%d", &prioridade_input);
- teste=1;
- break;
- }
- }
- b=temporario;
- if(teste==0){
- printf("Indique uma tarefa válida: ");
- fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
- goto doing_para_todo;
- }
- if(prioridade_input>10 || prioridade_input<1){
- printf("A prioridade deve ser de 0 a 10. ");
- goto doing_para_todo;
- }
- else{
- temp->next=h->next;
- h->next=temp;
- strcpy(temp->tarefa, tarefa_input);
- temp->prioridade=prioridade_input;
- remover_doing(b,tarefa_input);
- }
- }
- void doing_para_done(doing*b, done*d){
- done*temp=malloc(sizeof(d));
- doing*tempo=b;
- int teste=0;
- listar_doing(b);
- char day[128],month[128],year[128];
- char tarefa_input[128];
- printf("\nIndique a tarefa concluida: ");
- fgets(tarefa_input, 128, stdin);strtok(tarefa_input, "\n");
- doing_para_done:
- for(b=b->next;b;b=b->next){
- if(strcmp(b->tarefa, tarefa_input)==0){
- printf("Indique a data de conclusao:\n");
- printf("Dia: ");fgets(day, 128, stdin);strtok(day, "\n");
- printf("Mes: ");fgets(month, 128, stdin);strtok(month, "\n");
- printf("Ano: ");fgets(year, 128, stdin);strtok(year, "\n");
- temp->next=d->next;
- d->next=temp;
- strcpy(temp->tarefa, tarefa_input);
- strcpy(temp->dia, day);
- strcpy(temp->mes, month);
- strcpy(temp->ano, year);
- strcpy(temp->utilizador, b->utilizador);
- teste=1;
- break;
- }
- }
- b=tempo;
- if(teste==0){
- printf("Indique uma tarefa válida: ");
- fgets(tarefa_input,100,stdin);strtok(tarefa_input,"\n");
- goto doing_para_done;
- }
- else
- remover_doing(b,tarefa_input);
- }
- void done_para_todo(done*d, to_do*b){
- done*tempo=d;
- listar_done(d);
- int prioridade_input;
- int teste=0;
- char tarefa_input[128];
- printf("\nIndique a tarefa a passar para To Do: ");
- fgets(tarefa_input, 128, stdin);strtok(tarefa_input, "\n");
- dpt:
- for(d=d->next;d;d=d->next){
- if(strcmp(d->tarefa, tarefa_input)==0){
- printf("Indique a nova prioridade da tarefa: ");
- scanf("%d", &prioridade_input);
- teste=1;
- break;
- }
- }
- d=tempo;
- if(teste==0){
- printf("Indique uma tarefa válida: ");
- fgets(tarefa_input, 128, stdin);strtok(tarefa_input,"\n");
- goto dpt;
- }
- if(prioridade_input>10 || prioridade_input<1){
- printf("A prioridade deve ser de 0 a 10. ");
- goto dpt;
- }
- else{
- inserir_tarefa2(b,tarefa_input,prioridade_input);
- remover_done(d,tarefa_input);
- }
- }
- void carregar_todo(to_do*h) {
- FILE *fp;
- fp = fopen("todo.txt", "r");
- char tarefa_input[128];int prioridade_input;
- int linhas = countlines("todo.txt");
- for (int i = 0; i < (linhas / 2); i++) {
- fgets(tarefa_input,sizeof(tarefa_input),fp);strtok(tarefa_input,"\n");
- fscanf(fp,"%d",&prioridade_input);
- fgetc(fp);
- inserir_tarefa2(h,tarefa_input,prioridade_input);
- }
- fclose(fp);
- }
- void carregar_doing(doing*h) {
- FILE *fp;
- fp = fopen("doing.txt", "r");
- char tarefa_input[128], utilizador_input[128];
- int linhas = countlines("doing.txt");
- for (int i = 0; i < (linhas / 2); i++) {
- fgets(tarefa_input,sizeof(tarefa_input),fp);strtok(tarefa_input,"\n");
- fgets(utilizador_input,sizeof(utilizador_input),fp);strtok(utilizador_input,"\n");
- inserir_doing(h,tarefa_input,utilizador_input);
- }
- fclose(fp);
- }
- void carregar_done(done*h) {
- FILE *fp;
- fp = fopen("done.txt", "r");
- char tarefa_input[128], utilizador_input[128],day[128],month[128],year[128];
- int linhas = countlines("done.txt");
- for (int i = 0; i < (linhas / 5); i++) {
- fgets(tarefa_input,sizeof(tarefa_input),fp);strtok(tarefa_input,"\n");
- fgets(utilizador_input,sizeof(utilizador_input),fp);strtok(utilizador_input,"\n");
- fgets(day,sizeof(day),fp);strtok(day,"\n");
- fgets(month,sizeof(month),fp);strtok(month,"\n");
- fgets(year,sizeof(year),fp);strtok(year,"\n");
- inserir_done(h,tarefa_input,utilizador_input,day,month,year);
- }
- fclose(fp);
- }
- void carregar_utilizadores(user*h) {
- FILE *fp;
- fp = fopen("user.txt", "r");
- char utilizador[128], email[128];int id;
- int linhas = countlines("user.txt");
- for (int i = 0; i < (linhas / 3); i++) {
- fgets(utilizador,sizeof(utilizador),fp);strtok(utilizador,"\n");
- fgets(email,sizeof(email),fp);strtok(email,"\n");
- fscanf(fp,"%d",&id);
- fgetc(fp);
- inserir_user(h,utilizador,email,id);
- }
- fclose(fp);
- }
- void guardar_todo(to_do*h){
- FILE *fp;
- fp = fopen("todo.txt", "w");
- for(h=h->next;h;h=h->next){
- fprintf(fp,"%s\n",h->tarefa);
- fprintf(fp,"%d\n",h->prioridade);
- }
- fclose(fp);
- }
- void guardar_doing(doing*h){
- FILE *fp;
- fp = fopen("doing.txt", "w");
- for(h=h->next;h;h=h->next){
- fprintf(fp,"%s\n",h->tarefa);
- fprintf(fp,"%s\n",h->utilizador);
- }
- fclose(fp);
- }
- void guardar_done(done*h){
- FILE *fp;
- fp = fopen("done.txt", "w");
- for(h=h->next;h;h=h->next){
- fprintf(fp,"%s\n",h->tarefa);
- fprintf(fp,"%s\n",h->utilizador);
- strtok(h->dia,"\n");fprintf(fp,"%s\n",h->dia);
- strtok(h->mes,"\n");fprintf(fp,"%s\n",h->mes);
- strtok(h->ano,"\n");fprintf(fp,"%s\n",h->ano);
- }
- fclose(fp);
- }
- int main(){
- to_do*lista_todo;doing*lista_doing;done*lista_done;user*lista_user;
- lista_todo=cria_lista_todo();
- lista_doing=cria_lista_doing();
- lista_done=cria_lista_done();
- lista_user=cria_lista_user();
- int opcao;
- carregar_todo(lista_todo);
- carregar_utilizadores(lista_user);
- carregar_doing(lista_doing);
- carregar_done(lista_done);
- MENU:
- opcao = menu();
- char input[128];
- fgets(input, 100, stdin);
- switch (opcao) {
- case 1:
- inserir_tarefa(lista_todo);
- goto MENU;
- case 2:
- todo_para_doing(lista_todo, lista_doing,lista_user);
- goto MENU;
- case 3:
- doing_para_todo(lista_doing, lista_todo);
- goto MENU;
- case 4:
- done_para_todo(lista_done,lista_todo);
- goto MENU;
- case 5:
- doing_para_done(lista_doing,lista_done);
- goto MENU;
- case 6:
- altera_user(lista_doing,lista_user);
- goto MENU;
- case 7:
- listar_tarefas_por_user(lista_doing,lista_done,lista_user);
- goto MENU;
- case 8:
- listar_user(lista_user);
- goto MENU;
- case 9:
- listar_tarefas(lista_todo);
- listar_doing(lista_doing);
- listar_done(lista_done);
- goto MENU;
- case 0:
- guardar_todo(lista_todo);
- guardar_doing(lista_doing);
- guardar_done(lista_done);
- return 0;
- default:break;
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement