Quantcast
Channel: Tópicos
Viewing all articles
Browse latest Browse all 11336

Programa não faz o esperado! Arrays

$
0
0
bem ... cheguei a pensar e quando eficiente iria fazer o código, mas acho que se o fizesse, não irias pescar nada .. por isso simplesmente alterei o teu para ficar "mais bonito" #include <stdlib.h> #include <stdio.h> #include <string.h> // CONSTANTS #define MAX_NAME_SIZE 30 #define MAX_ALUNOS_POR_TURMA 30 // TYPEDEFS typedef struct { size_t id; unsigned int idade; char nome[MAX_NAME_SIZE]; int valid:1; } Aluno; typedef struct { size_t id; char prof[MAX_NAME_SIZE]; struct { Aluno list[MAX_ALUNOS_POR_TURMA]; size_t count; } alunos; } Turma; Aluno criarAluno(size_t id, unsigned int idade, char * nome); int imprimeAluno(Aluno * aluno); Turma criarTurma(size_t id, char * prof); int inscreverAluno(Turma * turma, Aluno aluno); int inscreverAlunoEx(Turma * turma, size_t id, unsigned int idade, char * nome); int desinscreverAluno(Turma * turma, size_t id); int contarAlunos(Turma * turma); int imprimeTurma(Turma * turma); Aluno criarAluno(size_t id, unsigned int idade, char * nome) { Aluno aluno; aluno.id = id; aluno.idade = idade; strncpy(aluno.nome, nome, MAX_NAME_SIZE); aluno.valid = 1; return aluno; } int imprimeAluno(Aluno * aluno) { if (!aluno) return -1; printf("[Nome=%s][Numero=%lu][Idade=%u]\n", aluno->nome, aluno->id, aluno->idade); return 0; } Turma criarTurma(size_t id, char * prof) { Turma turma; turma.id = id; strncpy(turma.prof, prof, MAX_NAME_SIZE); memset(turma.alunos.list, 0, sizeof(Aluno) * MAX_ALUNOS_POR_TURMA); turma.alunos.count = 0; return turma; } int inscreverAluno(Turma * turma, Aluno aluno) { if (!turma) return -1; if(turma->alunos.count == MAX_ALUNOS_POR_TURMA) { printf("A turma ja esta cheia!\n"); return -2; } else { for (size_t index = 0; index < MAX_ALUNOS_POR_TURMA; ++index) { if (! turma->alunos.list[index].valid) { turma->alunos.list[index] = aluno; turma->alunos.list[index].valid = 1; turma->alunos.count++; return 0; } } } return -3; } int inscreverAlunoEx(Turma * turma, size_t id, unsigned int idade, char * nome) { return inscreverAluno(turma, criarAluno(id, idade, nome)); } int desinscreverAluno(Turma * turma, size_t id) { if (!turma) return -1; for (size_t index = 0; index < MAX_ALUNOS_POR_TURMA; ++index) { if (turma->alunos.list[index].id == id) { turma->alunos.list[index].valid = 0; turma->alunos.count--; return 0; } } printf("O aluno em causa nao se encontra inscrito!\n"); return -2; } int contarAlunos(Turma * turma) { if (!turma) return -1; return turma->alunos.count; } int imprimeTurma(Turma * turma) { if (!turma) return -1; printf("[Turma: %d][Professor: %s]\n", turma->id, turma->prof); for (size_t index = 0; index < MAX_ALUNOS_POR_TURMA; ++index) { if (turma->alunos.list[index].valid != 0) imprimeAluno(& turma->alunos.list[index]); } } int main(void) { Turma turma = criarTurma(2, "Julia Juliano"); inscreverAlunoEx(& turma, 160221011, 18, "Francisco Leal"); inscreverAlunoEx(& turma, 160221039, 19, "Hugo Ferreira"); inscreverAlunoEx(& turma, 160221013, 13, "Gabriel Ambrosio"); inscreverAlunoEx(& turma, 160221058, 19, "Diogo Silva"); inscreverAlunoEx(& turma, 160221050, 21, "Bruno Dias"); printf("Numero de alunos: %d\n", contarAlunos(& turma)); imprimeTurma(& turma); return (EXIT_SUCCESS); }

Viewing all articles
Browse latest Browse all 11336

Trending Articles


FLASHBACK WITH SIRASA FM AT GALGAMUWA 2022


Mp3 Download: Mdu - Mazola


Imitation gun was fired at motorist in Leicester road-rage incident


Ndebele names


MCKINNEY EMALINE “EMMA” OF WES...


Okra & Motia — The Workshop (Prod by Hammer)


Skint TV teen to be sentenced


Moondru Mudichu 19-09-2017 – Polimer tv Serial


YOSVANI JAMES Arrested by Miami-Dade County Corrections on Jan 10, 2017


Stories • Goddess Stepmom