Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. struct tpAluno {
  7. int Matricula;
  8. char Nome[15];
  9. float Nota1, Nota2, Nota3, Media;
  10. };
  11.  
  12. int main(){
  13.  
  14. struct tpAluno Aluno;
  15. FILE *arq;
  16. if( (arq=fopen("CADASTRO.txt","a"))==NULL) {
  17. printf("Nao foi possivel abrir arquivo.\n");
  18. return 1;
  19. }
  20. int opcao;
  21.  
  22. while (1) {
  23. printf("\n");
  24. printf("1 - Inserir alunos\n");
  25. printf("2 - Listar alunos\n");
  26. printf("3 - Pesquisar alunos pelo nome\n");
  27. printf("4 - Pesquisar alunos pela media\n");
  28. printf("5 - Sair\n");
  29.  
  30. printf("Sua opcao: ");
  31. scanf("%d", &opcao);
  32.  
  33. switch (opcao) {
  34. case 1:{
  35. AdicionaAluno ();
  36. break;
  37. }
  38. case 2:{
  39. ListaAluno ();
  40. break;
  41. }
  42. case 3:{
  43. PesquisaNome ();
  44. break;
  45. }
  46. case 4:{
  47. PesquisaMedia ();
  48. break;
  49. }
  50. case 5:{
  51. Sair ();
  52. break;
  53. }
  54. default: {
  55. printf("\nOpcao invalida. Tente novamente.\n");
  56. break;
  57. }
  58. }
  59.  
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. int AdicionaAluno () {
  66. struct tpAluno Aluno;
  67. FILE *arq;
  68.  
  69. if( (arq=fopen("CADASTRO.txt","a"))==NULL) {
  70. printf("Nao foi possivel abrir arquivo.\n");
  71. return 1;
  72. }
  73. else {
  74. while (1){
  75. printf("Matricula (0 para voltar ao menu): ");
  76. scanf("%d", &Aluno.Matricula);
  77. if(Aluno.Matricula == 0)
  78. break;
  79. else {
  80. printf("Nome: ");
  81. scanf("%s", Aluno.Nome);
  82. printf("Nota1: ");
  83. scanf("%f", &Aluno.Nota1);
  84. printf("Nota2: ");
  85. scanf("%f", &Aluno.Nota2);
  86. printf("Nota3: ");
  87. scanf("%f", &Aluno.Nota3);
  88. Aluno.Media = (Aluno.Nota1 + Aluno.Nota2 + Aluno.Nota3)/3;
  89. fprintf(arq,"\n%d %s %.1f %.1f %.1f %.1f\n",
  90. Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
  91.  
  92. }
  93.  
  94. }
  95. }
  96.  
  97. fclose(arq);
  98. }
  99.  
  100. int ListaAluno () {
  101. float TNota1, TNota2, TNota3, TMedia;
  102. struct tpAluno Aluno;
  103. FILE *arq;
  104. if( (arq=fopen("CADASTRO.txt","r"))==NULL) {
  105. printf("Nao foi possivel abrir arquivo.\n");
  106. return 1;
  107. }
  108. else {
  109. printf("\n------ ---------- ------- ------- ------- -------\n");
  110. printf("\n Matr Nome Nota1 Nota2 Nota3 Media\n");
  111. printf("\n------ ---------- ------- ------- ------- -------\n");
  112. while ((fscanf(arq, "%d %s %f %f %f %f\n", &Aluno.Matricula, Aluno.Nome, &Aluno.Nota1, &Aluno.Nota2, &Aluno.Nota3, &Aluno.Media))!=EOF){
  113. printf("\n %d %s %.1f %.1f %.1f %.1f\n",
  114. Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
  115. }
  116. TMedia = (TNota1 + TNota2 + TNota3)/3;
  117. printf("\n------------------ ------- ------- ------- -------\n");
  118. printf("\n Media Geral %.1f %.1f %.1f %.1f\n", TNota1, TNota2, TNota3, TMedia);
  119. printf("\n------------------ ------- ------- ------- -------\n");
  120. }
  121.  
  122.  
  123. fclose(arq);
  124. }
  125.  
  126. int PesquisaNome () {
  127.  
  128. int i;
  129. struct tpAluno Aluno;
  130.  
  131. FILE *arq;
  132. if( (arq=fopen("CADASTRO.txt","r"))==NULL) {
  133. printf("Nao foi possivel abrir arquivo.\n");
  134. return 1;
  135. }
  136. else{
  137. while(1){
  138. printf("\nDigite o nome do aluno (""voltar"" para voltar ao menu): ");
  139. char nom[15];
  140. scanf("%s", nom);
  141. if (strcmp(Aluno.Nome, nom) == 0){
  142. break;
  143. }
  144.  
  145. else{
  146. while ((fscanf(arq, "%d %s %f %f %f %f\n", &Aluno.Matricula, Aluno.Nome, &Aluno.Nota1, &Aluno.Nota2, &Aluno.Nota3, &Aluno.Media))!=EOF){
  147. if (strcmp(Aluno.Nome, nom) == 0){
  148. printf("\nMatricula: %d\nNome: %s\nNota1: %.1f\nNota2: %.1f\nNota3: %.1f\nMedia: %.1f\n",
  149. Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
  150. }
  151.  
  152. }
  153. }
  154.  
  155. }
  156. }
  157.  
  158. fclose(arq);
  159.  
  160. }
  161.  
  162. int PesquisaMedia () {
  163. int med;
  164. int i;
  165. struct tpAluno Aluno;
  166.  
  167. FILE *arq;
  168. if( (arq=fopen("CADASTRO.txt","r"))==NULL) {
  169. printf("Nao foi possivel abrir arquivo.\n");
  170. return 1;
  171. }
  172. else{
  173. while(1){
  174. printf("\nDigite a media a pesquisar (0 para voltar ao menu): ");
  175. scanf("%d", med);
  176.  
  177. if (med == 0){
  178. break;
  179. }
  180. else{
  181. while ((fscanf(arq, "%d %s %f %f %f %f\n", &Aluno.Matricula, Aluno.Nome, &Aluno.Nota1, &Aluno.Nota2, &Aluno.Nota3, &Aluno.Media))!=EOF){
  182.  
  183. if(Aluno.Media == med){
  184. printf("\nMatricula: %d\nNome: %s\nNota1: %.1f\nNota2: %.1f\nNota3: %.1f\nMedia: %.1f\n",
  185. Aluno.Matricula, Aluno.Nome, Aluno.Nota1, Aluno.Nota2, Aluno.Nota3, Aluno.Media);
  186. }
  187.  
  188. }
  189. }
  190.  
  191. }
  192. }
  193.  
  194. fclose(arq);
  195.  
  196. }
  197.  
  198. int Sair (){
  199. printf("\nPressione ENTER para sair.\n");
  200. return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement