Advertisement
rdsedmundo

canela3.c

Oct 31st, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void imprime (char ***mat, int n, int m) {
  6.     int i, j;
  7.     for (i = 0; i < n; i++) {
  8.         for (j = 0; j < m; j++)
  9.             printf("%s\t", mat[i][j]);
  10.  
  11.         printf("\n");
  12.     }
  13. }
  14.  
  15. void troca (char ***mat, int i1, int j1, int i2, int j2) {
  16.     char * aux = (char *) malloc(128 * sizeof(char));
  17.  
  18.     strcpy(aux, mat[i2][j2]);
  19.     strcpy(mat[i2][j2], mat[i1][j1]);
  20.     strcpy(mat[i1][j1], aux);
  21. }
  22.  
  23. void insere (char ***mat, int n, int m, char * nome) {
  24.     int i, j, foi = 0;
  25.  
  26.     for (i = 0; i < n; i++)
  27.         for (j = 0; j < m; j++) {
  28.             if (strcmp(mat[i][j], "Vazio") == 0) {
  29.                 strcpy(mat[i][j], nome);
  30.                 foi = 1;
  31.                 break;
  32.             }
  33.         }
  34.  
  35.     if (foi == 0)
  36.         printf("A sala está cheia.\n");
  37. }
  38.  
  39. int main()
  40. {
  41.     int n, m, i, j;
  42.  
  43.     printf("Digite o número de filas: ");
  44.     scanf("%d", &n);
  45.  
  46.     printf("Digite o número de cadeiras: ");
  47.     scanf("%d", &m);
  48.  
  49.     char ***mat = (char ***) malloc(n * sizeof(char **));
  50.  
  51.     for (i = 0; i < n; i++) {
  52.         mat[i] = malloc(m * sizeof(char *));
  53.  
  54.         for (j = 0; j < m; j++)
  55.             mat[i][j] = malloc(128 * sizeof(char));
  56.     }
  57.  
  58.     for (i = 0; i < n; i++)
  59.         for (j = 0; j < m; j++) {
  60.             char * nome = (char *) malloc(128 * sizeof(char));
  61.             printf("Digite o nome para a fila %d cadeira %d. 0 para vazio: ", i+1, j+1);
  62.             scanf("%s", nome);
  63.  
  64.             if (strcmp(nome, "0") == 0)
  65.                 strcpy(mat[i][j], "Vazio");
  66.             else
  67.                 strcpy(mat[i][j], nome);
  68.         }
  69.  
  70.     int qq;
  71.     do {
  72.         printf("1- Inserir em local vazio.\n");
  73.         printf("2- Trocar alunos de posicao.\n");
  74.         printf("3- Imprimir alunos.\n");
  75.         printf("4- Sair.\n");
  76.  
  77.         scanf("%d", &qq);
  78.  
  79.         switch (qq) {
  80.             case 1: {
  81.                 char * nome = (char *) malloc(128 * sizeof(char));
  82.  
  83.                 printf("Digite o nome do aluno: ");
  84.                 scanf("%s", nome);
  85.  
  86.                 insere(mat, n, m, nome);
  87.  
  88.                 break;
  89.             }
  90.  
  91.             case 2: {
  92.                 int i1, j1, i2, j2;
  93.                 printf("Digite a linha e a coluna onde está o aluno a ser movido: ");
  94.                 scanf("%d %d", &i1, &j1);
  95.  
  96.                 printf("Digite a linha e a coluna para onde o aluno irá: ");
  97.                 scanf("%d %d", &i2, &j2);
  98.  
  99.                 i1--, j1--, i2--, j2--; // começando do 0
  100.                 troca(mat, i1, j1, i2, j2);
  101.  
  102.                 break;
  103.             }
  104.  
  105.             case 3: {
  106.                 imprime(mat, n, m);
  107.                 break;
  108.             }
  109.         }
  110.     } while (qq != 4);
  111.  
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement