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* next;
- struct done* prev;
- int dia;
- int mes;
- int ano;
- }done;
- typedef struct user{
- char utilizador[128];
- char email[1024];
- 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*d){
- if(d->next!=NULL){
- printf("\nTarefas em Done:\n");
- for(d=d->next;d;d=d->next){
- strtok(d->tarefa, "\n");
- printf("Tarefa: '%s'. Utilizador: '%s'. Data conclusao:%d/%d/%d\n", d->tarefa, d->utilizador, d->dia, d->mes, d->ano);
- }
- }
- }
- void listar_user(user*h){
- if(h->next!=NULL){
- printf("\nUtilizadores:\n");
- for(h=h->next;h;h=h->next){
- printf("%d - %s - %s \n",h->id,h->utilizador,h->email);
- }
- }
- }
- 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);
- 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;
- }
- 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;
- }
- 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);
- temp->next=h->next;
- h->next=temp;
- strcpy(temp->tarefa, tarefa_input);
- temp->prioridade=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;
- }
- else
- remover_doing(b,tarefa_input);
- }
- void doing_para_done(doing*b, done*d){
- done*temp=malloc(sizeof(d));
- doing*tempo=b;
- listar_doing(b);
- int day; int month; int year;
- char tarefa_input[128];
- printf("Indique a tarefa concluida: ");
- fgets(tarefa_input, 128, stdin);strtok(tarefa_input, "\n");
- for(b=b->next;b;b=b->next){
- if(strcmp(b->tarefa, tarefa_input)==0){
- printf("Indique a data de conclusao:\n");
- printf("Dia: "); scanf("%d", &day); printf("Mes: "); scanf("%d", &month); printf("Ano: "); scanf("%d", &year);
- temp->next=d->next;
- d->next=temp;
- strcpy(temp->tarefa, tarefa_input);
- temp->dia=day; temp->mes=month; temp->ano=year;
- strcpy(temp->utilizador, b->utilizador);
- break;
- }
- }
- b=tempo;
- remover_doing(b, tarefa_input);
- }
- 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 = getc(fp); c != EOF; c = getc(fp))
- if (c == '\n')
- count = count + 1;
- fclose(fp);
- return count;
- }
- 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 inserir_user(user*h,char utilizador[128],char email[255],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 carregar_utilizadores(user*h) {
- FILE *fp;
- fp = fopen("user.txt", "r");
- char utilizador[128], email[1024];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 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;
- }
- }
- 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);
- }
- 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);
- 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:
- 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);
- return 0;
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement