Advertisement
x0Henr1Must47d

TP2-To_Do

Oct 26th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "To-Do.h"
  5. #include "fila.h"
  6. #include "lista.h"
  7. #include "pilha.h"
  8.  
  9. int main(){
  10.  
  11.     Atividade activity;
  12.     Fila f;
  13.     Lista l;
  14.     Pilha p;
  15.  
  16.     int op, n_tarefa=0;
  17.     char ativi[65];
  18.  
  19.     printf("Hello to To-Do Pendulares!\n");
  20.  
  21.     printf("1 - Criar uma atividade\n"
  22.            "2 - Vizualizar uma atividade");
  23.     scanf("%d", &op);
  24.  
  25.     switch(op){
  26.         case 1:
  27.             nova_tarefa(n_tarefa);
  28.             criar_atividade(&l, activity, n_tarefa);
  29.             n_tarefa++;
  30.         break;
  31.  
  32.         case 2:
  33.             printf("Digite o titulo da tarefa que deseja vizualizar:\n");
  34.             scanf("%s", ativi);
  35.             vizualizar_atividade(&l, ativi);
  36.     }
  37. }
  38.  
  39. =============================To_do.h====================================
  40. #ifndef TO-DO_H_INCLUDED
  41. #define TO-DO_H_INCLUDED
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include "lista.h"
  45. #include "fila.h"
  46. #include "pilha.h"
  47.  
  48. typedef struct{
  49.     char titulo [65]; //titulo da tarefa;
  50.     char descricao [257]; //descrição da tarefa;
  51.     char data [11]; //data limite para realização da tarefa, no formato DDMMAAAA; D-dia, M-mês, A-ano;
  52.     int prioridade; // 1 - baixa, 2 - média, 3 - alta;
  53.     int status; //0 - concluida, 1 - em andamento, 2 - pendente, 3 atrazada;
  54. }Atividade;
  55.  
  56. typedef struct Celula{
  57.     Atividade dado; //dado passa a ser do tipo Atividade, contendo todos os seus atribuitos;
  58.     struct Celula *prox; //ponteiro para a proxima celula;
  59. }Celula;
  60.  
  61. void nova_tarefa(int n){
  62.     printf("Titulo da tarefa:\n");
  63.     scanf("%s", Atividade[n].titulo);
  64.     printf("Descricao da Tarefa:\n");
  65.     scanf("%s", Atividade[n].descricao);
  66.     printf("Data da Tarefa:\n");
  67.     scanf("%s", Atividade[n].data);
  68.     printf("Prioridade:\n"
  69.            "1 para baixa;\n"
  70.            "2 para media;\n"
  71.            "3 para alta;\n");
  72.     scanf("%d", Atividade[n].prioridade);
  73.     printf("Status\n"
  74.            "0 - concluida;\n"
  75.            "1 - em andamento;\n"
  76.            "2 - pendente;\n"
  77.            "3 - atrazada;\n");
  78. }
  79.  
  80. void criar_atividade(Lista *l, Atividade activity, int n){
  81.     enqueue(l, activity[n]);
  82. }
  83.  
  84. void vizualizar_atividade(Lista *l, char ativi){
  85.     vizualizar(l, ativi);
  86. }
  87.  
  88. #endif // TO-DO_H_INCLUDED
  89. ====================================lista.h=============================
  90. #ifndef LISTA_H_INCLUDED
  91. #define LISTA_H_INCLUDED
  92. #include <stdlib.h>
  93. #include <stdio.h>
  94. #include <stdbool.h>
  95. #include "To-Do.h"
  96.  
  97. typedef struct Celula{
  98.     Atividade dado; //O dado da Celula é do tipo Atividade;
  99.     struct Celula *prox;
  100. }Celula;
  101.  
  102. typedef struct Lista{
  103.     Celula *inicio, *fim;
  104.     int tam;
  105. }Lista;
  106.  
  107. void create_lista(Lista *l){
  108.     Celula *temp = (Celula*)malloc(sizeof(Celula));
  109.     temp->prox = NULL;
  110.     l->inicio = l->fim = temp;
  111.     l->tam = 0;
  112. }
  113.  
  114. void add(Lista *l, Atividade dado){
  115.     Celula *temp = (Celula*)malloc(sizeof(Celula));
  116.     temp->prox = NULL;
  117.     temp->dado = dado;
  118.  
  119.     l->fim->prox = temp;
  120.     l->fim = temp;
  121.     l->tam++;
  122. }
  123.  
  124. int remove_at(Lista *l, int pos){
  125.  
  126.     if(pos < 0 || pos >= l->tam )
  127.         return -1;
  128.  
  129.     Celula *ant = l->inicio;
  130.     for(int i=0; i<pos;i++)
  131.         ant = ant->prox;
  132.  
  133.     Celula *temp = ant->prox;
  134.     Atividade dado = temp->dado;
  135.  
  136.     ant->prox = temp->prox;
  137.  
  138.     if(ant->prox == NULL)
  139.         l->fim = ant;
  140.  
  141.     free(temp);
  142.  
  143.     l->tam--;
  144.  
  145.     return dado;
  146. }
  147.  
  148. void print_lista(Lista l){
  149.     printf("\nSize: %d\n", l.tam);
  150.     Celula *temp = l.inicio->prox;
  151.     int i=0;
  152.     while(temp!=NULL){
  153.         printf("|%s|\n", temp->dado.titulo);
  154.         printf("%s\n", temp->dado.descricao);
  155.         printf("%s\n", temp->dado.data);
  156.         printf("%d\n", temp->dado.prioridade);
  157.         printf("%s\n", temp->dado.status);
  158.         temp = temp->prox;
  159.         i++;
  160.     }
  161. }
  162.  
  163. void vizualizar(Lista *l, char ativi){
  164.     Celula *temp = l.inicio->prox;
  165.     while(strcmp(l->dado.titulo, ativi) !=0){ //irá comparar o titulo da atividade que o usuarios quer
  166.         temp = temp->prox; //vizualizar e enquanto for diferente ele passa aiante para a proxima celula
  167.     }
  168.     printf("|%s|\n", temp->dado.titulo);
  169.     printf("%s\n", temp->dado.descricao);
  170.     printf("%s\n", temp->dado.data);
  171.     printf("%d\n", temp->dado.prioridade);
  172.     printf("%s\n", temp->dado.status);
  173. }
  174.  
  175. void destroy_lista(Lista *l){
  176.     while(l->inicio != l->fim)
  177.         remove_at(l, 0);
  178.     free(l->inicio);
  179. }
  180.  
  181. #endif // LISTA_H_INCLUDED
  182.  
  183. ========================================fila.h===================================
  184. #ifndef FILA_H_INCLUDED
  185. #define FILA_H_INCLUDED
  186. #include <stdlib.h>
  187. #include <stdio.h>
  188. #include <stdbool.h>
  189. #include "To-do.h"
  190.  
  191. typedef struct Celula{
  192.     Atividade dado; //O dado da Celula é do tipo Atividade;
  193.     struct Celula *prox;
  194. }Celula;
  195.  
  196. typedef struct{
  197.     Celula *inicio, *fim;
  198.     int tam;
  199. }Fila;
  200.  
  201. void create_fila(Fila *f){
  202.     f->inicio = f->fim = (Celula *)malloc(sizeof(Celula));
  203.     f->tam = 0;
  204. }
  205.  
  206. bool is_empty_fila(Fila f){
  207.     return f.inicio == f.fim;
  208. }
  209.  
  210. bool enqueue(Fila *f, Atividade dado){
  211.  
  212.     Celula *temp = (Celula *)malloc(sizeof(Celula));
  213.  
  214.     if(temp == NULL)
  215.         return false;
  216.  
  217.     temp->prox = NULL;
  218.     temp->dado = dado;
  219.  
  220.     f->fim->prox = temp;
  221.     f->fim =  f->fim->prox;
  222.  
  223.     f->tam++;
  224.  
  225.     return true;
  226. }
  227.  
  228. int dequeue(Fila *f){
  229.  
  230.     if(is_empty_fila(*f))
  231.         return -1;
  232.  
  233.     Celula *temp = f->inicio;
  234.  
  235.     f->inicio = f->inicio->prox;
  236.  
  237.     free(temp);
  238.  
  239.     f->tam--;
  240.  
  241.     return f->inicio->dado;
  242. }
  243.  
  244. int peek(Fila f){
  245.     if(is_empty_fila(f))
  246.         return -1;
  247.  
  248.     return f.inicio->prox->dado;
  249. }
  250.  
  251. void print_fila(Fila f){
  252.     printf("\nSize: %d\n", f.tam);
  253.     Celula *temp = f.inicio->prox;
  254.     while(temp!=NULL){
  255.         printf("|%s|\n", temp->dado.titulo);
  256.         printf("%s\n", temp->dado.descricao);
  257.         printf("%s\n", temp->dado.data);
  258.         printf("%d\n", temp->dado.prioridade);
  259.         printf("%s\n", temp->dado.status);
  260.         temp = temp->prox;
  261.     }
  262.     printf("\n");
  263. }
  264.  
  265. void destroy_fila(Fila *f){
  266.     while(!is_empty_fila(*f))
  267.         dequeue(f);
  268.     free(f->inicio);
  269. }
  270.  
  271. #endif // FILA_H_INCLUDED
  272.  
  273. =======================================pilha.h================================
  274. #ifndef PILHA_H_INCLUDED
  275. #define PILHA_H_INCLUDED
  276. #include <stdlib.h>
  277. #include <stdio.h>
  278. #include <stdbool.h>
  279. #include "To-Do.h"
  280.  
  281. typedef struct Celula{
  282.     Atividade dado; //O dado da Celula é do tipo Atividade;
  283.     struct Celula *prox;
  284. }Celula;
  285.  
  286. typedef struct{
  287.     Celula *topo;
  288.     int tam;
  289. }Pilha;
  290.  
  291.  
  292. void create_pilha(Pilha *p){
  293.     p->topo = NULL;
  294.     p->tam = 0;
  295. }
  296.  
  297. void push(Pilha *p, Atividade dado){
  298.  
  299.     Celula *temp =  (Celula*)malloc(sizeof(Celula));
  300.  
  301.     temp->prox = p->topo;
  302.     temp->dado = dado;
  303.  
  304.     p->topo = temp;
  305.     p->tam++;
  306. }
  307.  
  308. void print_pilha(Pilha p){
  309.  
  310.     Celula *temp = p.topo;
  311.  
  312.     printf("\nSize:%d\n", p.tam);
  313.     while(temp!=NULL){
  314.         printf("|%s|\n", temp->dado.titulo);
  315.         printf("%s\n", temp->dado.descricao);
  316.         printf("%s\n", temp->dado.data);
  317.         printf("%d\n", temp->dado.prioridade);
  318.         printf("%s\n", temp->dado.status);
  319.         temp = temp->prox;
  320.     }
  321. }
  322.  
  323. bool is_empty_pilha(Pilha p){
  324.     return p.topo == NULL;
  325. }
  326.  
  327. int pop(Pilha *p){
  328.  
  329.     if(is_empty_pilha(*p))
  330.         return -1;
  331.  
  332.     Celula *temp = p->topo;
  333.  
  334.     p->topo = p->topo->prox;
  335.  
  336.     Atividade dado = temp->dado;
  337.     free(temp);
  338.  
  339.     p->tam--;
  340.  
  341.     return dado;
  342. }
  343.  
  344. void destroy_pilha(Pilha *p){
  345.  
  346.     while(!is_empty_pilha(*p))
  347.         pop(p);
  348. }
  349.  
  350. #endif // PILHA_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement