Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- struct tpAluno {
- int Matricula;
- char Nome[15];
- float Nota1, Nota2, Nota3, Media;
- };
- int main(){
- struct tpAluno Aluno;
- FILE *arq;
- if( (arq=fopen("CADASTRO.txt","a"))==NULL) {
- printf("Nao foi possivel abrir arquivo.\n");
- return 1;
- }
- int opcao;
- while (1) {
- printf("\n");
- printf("1 - Inserir alunos\n");
- printf("2 - Listar alunos\n");
- printf("3 - Pesquisar alunos pelo nome\n");
- printf("4 - Pesquisar alunos pela media\n");
- printf("5 - Sair\n");
- printf("Sua opcao: ");
- scanf("%d", &opcao);
- switch (opcao) {
- case 1:{
- AdicionaAluno ();
- break;
- }
- case 2:{
- ListaAluno ();
- break;
- }
- case 3:{
- PesquisaNome ();
- break;
- }
- case 4:{
- PesquisaMedia ();
- break;
- }
- case 5:{
- Sair ();
- break;
- }
- default: {
- printf("\nOpcao invalida. Tente novamente.\n");
- break;
- }
- }
- }
- return 0;
- }
- int AdicionaAluno () {
- struct tpAluno Aluno;
- FILE *arq;
- if( (arq=fopen("CADASTRO.txt","a"))==NULL) {
- printf("Nao foi possivel abrir arquivo.\n");
- return 1;
- }
- else {
- while (1){
- printf("Matricula (0 para voltar ao menu): ");
- scanf("%d", &Aluno.Matricula);
- if(Aluno.Matricula == 0)
- break;
- else {
- printf("Nome: ");
- scanf("%s", Aluno.Nome);
- printf("Nota1: ");
- scanf("%f", &Aluno.Nota1);
- printf("Nota2: ");
- scanf("%f", &Aluno.Nota2);
- printf("Nota3: ");
- scanf("%f", &Aluno.Nota3);
- Aluno.Media = (Aluno.Nota1 + Aluno.Nota2 + Aluno.Nota3)/3;
- fprintf(arq,"\n%d %s %.1f %.1f %.1f %.1f\n",
- Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
- }
- }
- }
- fclose(arq);
- }
- int ListaAluno () {
- float TNota1, TNota2, TNota3, TMedia;
- struct tpAluno Aluno;
- FILE *arq;
- if( (arq=fopen("CADASTRO.txt","r"))==NULL) {
- printf("Nao foi possivel abrir arquivo.\n");
- return 1;
- }
- else {
- printf("\n------ ---------- ------- ------- ------- -------\n");
- printf("\n Matr Nome Nota1 Nota2 Nota3 Media\n");
- printf("\n------ ---------- ------- ------- ------- -------\n");
- while ((fscanf(arq, "%d %s %f %f %f %f\n", &Aluno.Matricula, Aluno.Nome, &Aluno.Nota1, &Aluno.Nota2, &Aluno.Nota3, &Aluno.Media))!=EOF){
- printf("\n %d %s %.1f %.1f %.1f %.1f\n",
- Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
- }
- TMedia = (TNota1 + TNota2 + TNota3)/3;
- printf("\n------------------ ------- ------- ------- -------\n");
- printf("\n Media Geral %.1f %.1f %.1f %.1f\n", TNota1, TNota2, TNota3, TMedia);
- printf("\n------------------ ------- ------- ------- -------\n");
- }
- fclose(arq);
- }
- int PesquisaNome () {
- int i;
- struct tpAluno Aluno;
- FILE *arq;
- if( (arq=fopen("CADASTRO.txt","r"))==NULL) {
- printf("Nao foi possivel abrir arquivo.\n");
- return 1;
- }
- else{
- while(1){
- printf("\nDigite o nome do aluno (""voltar"" para voltar ao menu): ");
- char nom[15];
- scanf("%s", nom);
- if (strcmp(Aluno.Nome, nom) == 0){
- break;
- }
- else{
- while ((fscanf(arq, "%d %s %f %f %f %f\n", &Aluno.Matricula, Aluno.Nome, &Aluno.Nota1, &Aluno.Nota2, &Aluno.Nota3, &Aluno.Media))!=EOF){
- if (strcmp(Aluno.Nome, nom) == 0){
- printf("\nMatricula: %d\nNome: %s\nNota1: %.1f\nNota2: %.1f\nNota3: %.1f\nMedia: %.1f\n",
- Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
- }
- }
- }
- }
- }
- fclose(arq);
- }
- int PesquisaMedia () {
- int med;
- int i;
- struct tpAluno Aluno;
- FILE *arq;
- if( (arq=fopen("CADASTRO.txt","r"))==NULL) {
- printf("Nao foi possivel abrir arquivo.\n");
- return 1;
- }
- else{
- while(1){
- printf("\nDigite a media a pesquisar (0 para voltar ao menu): ");
- scanf("%d", med);
- if (med == 0){
- break;
- }
- else{
- while ((fscanf(arq, "%d %s %f %f %f %f\n", &Aluno.Matricula, Aluno.Nome, &Aluno.Nota1, &Aluno.Nota2, &Aluno.Nota3, &Aluno.Media))!=EOF){
- if(Aluno.Media == med){
- printf("\nMatricula: %d\nNome: %s\nNota1: %.1f\nNota2: %.1f\nNota3: %.1f\nMedia: %.1f\n",
- Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
- }
- }
- }
- }
- }
- fclose(arq);
- }
- int Sair (){
- printf("\nPressione ENTER para sair.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement