Advertisement
joaoantoniodornelas

Prática 9 - Questão 2

Apr 29th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define MAX 80
  7.  
  8. char opcao_menu()
  9. {
  10. system("cls");
  11. printf(" (L)istar notas\n");
  12. printf(" (I)nserir aluno\n");
  13. printf(" (F)im\n");
  14. printf("> ");
  15. return (toupper(getche()));
  16. }
  17.  
  18. void listar_notas(){
  19.  
  20.     int num,notas;
  21.     float n1,n2,media;
  22.     char *nome;
  23.     char buf[MAX];
  24.     FILE *arq;
  25.     arq = fopen("dados.txt","r");
  26.     if (arq == NULL){
  27.         printf("Erro ao abrir arquivo\n");
  28.         return;
  29.     }
  30.     printf("\n");
  31.     printf("NUM | NOME | N1 | N2\n");
  32.     printf("----+----------------------+------+------\n");
  33.     notas = 0;
  34.     media = 0;
  35.     fgets(buf,MAX,arq);
  36.     while (!feof(arq))  {
  37.         num = atoi(strtok(buf,","));
  38.         nome = strtok(NULL,",");
  39.         n1 = atof(strtok(NULL,","));
  40.         n2 = atof(strtok(NULL,","));
  41.         printf("%03d | %-20s | %4.1f |%4.1f\n",num,nome,n1,n2);
  42.         notas = notas + 2;
  43.         media = media + n1 + n2;
  44.         fgets(buf,MAX,arq);
  45.     }
  46.  
  47.     printf("----+----------------------+------+------\n");
  48.     media = media/notas;
  49.     printf("Media das notas = %4.1f\n",media);
  50.     fclose(arq);
  51.  
  52. }
  53.  
  54. void novo_aluno(){
  55.  
  56.     char nome[40];
  57.     int numero;
  58.     float n1, n2;
  59.  
  60.     scanf("%d", &numero);
  61.     gets(nome);
  62.     scanf("%f", &n1);
  63.     scanf("%f", &n2);
  64.  
  65.     FILE *arq = fopen("dados.txt", "a");
  66.  
  67.     fprintf(arq , "%d,%s,%f,%f", numero,nome, n1, n2);
  68.  
  69.     fclose(arq);
  70.  
  71. }
  72.  
  73. int main(int args, char * arg[]){
  74.     char op;
  75.  
  76.     do{
  77.         op = opcao_menu();
  78.         if (op == 'L'){
  79.             listar_notas();
  80.         }
  81.  
  82.         else if (op == 'I'){
  83.             novo_aluno();
  84.         }
  85.  
  86.         printf("\n");
  87.         system("pause");
  88.     }while (op !='F');
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement