Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void imprime (char ***mat, int n, int m) {
- int i, j;
- for (i = 0; i < n; i++) {
- for (j = 0; j < m; j++)
- printf("%s\t", mat[i][j]);
- printf("\n");
- }
- }
- void troca (char ***mat, int i1, int j1, int i2, int j2) {
- char * aux = (char *) malloc(128 * sizeof(char));
- strcpy(aux, mat[i2][j2]);
- strcpy(mat[i2][j2], mat[i1][j1]);
- strcpy(mat[i1][j1], aux);
- }
- void insere (char ***mat, int n, int m, char * nome) {
- int i, j, foi = 0;
- for (i = 0; i < n; i++)
- for (j = 0; j < m; j++) {
- if (strcmp(mat[i][j], "Vazio") == 0) {
- strcpy(mat[i][j], nome);
- foi = 1;
- break;
- }
- }
- if (foi == 0)
- printf("A sala está cheia.\n");
- }
- int main()
- {
- int n, m, i, j;
- printf("Digite o número de filas: ");
- scanf("%d", &n);
- printf("Digite o número de cadeiras: ");
- scanf("%d", &m);
- char ***mat = (char ***) malloc(n * sizeof(char **));
- for (i = 0; i < n; i++) {
- mat[i] = malloc(m * sizeof(char *));
- for (j = 0; j < m; j++)
- mat[i][j] = malloc(128 * sizeof(char));
- }
- for (i = 0; i < n; i++)
- for (j = 0; j < m; j++) {
- char * nome = (char *) malloc(128 * sizeof(char));
- printf("Digite o nome para a fila %d cadeira %d. 0 para vazio: ", i+1, j+1);
- scanf("%s", nome);
- if (strcmp(nome, "0") == 0)
- strcpy(mat[i][j], "Vazio");
- else
- strcpy(mat[i][j], nome);
- }
- int qq;
- do {
- printf("1- Inserir em local vazio.\n");
- printf("2- Trocar alunos de posicao.\n");
- printf("3- Imprimir alunos.\n");
- printf("4- Sair.\n");
- scanf("%d", &qq);
- switch (qq) {
- case 1: {
- char * nome = (char *) malloc(128 * sizeof(char));
- printf("Digite o nome do aluno: ");
- scanf("%s", nome);
- insere(mat, n, m, nome);
- break;
- }
- case 2: {
- int i1, j1, i2, j2;
- printf("Digite a linha e a coluna onde está o aluno a ser movido: ");
- scanf("%d %d", &i1, &j1);
- printf("Digite a linha e a coluna para onde o aluno irá: ");
- scanf("%d %d", &i2, &j2);
- i1--, j1--, i2--, j2--; // começando do 0
- troca(mat, i1, j1, i2, j2);
- break;
- }
- case 3: {
- imprime(mat, n, m);
- break;
- }
- }
- } while (qq != 4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement