lunes, 16 de enero de 2012

Ficheros 3


#include <stdio.h>
#include <stdlib.h>


int main(){

  FILE *f, *g;
  char nomFich[50], nomFich2[50];
  char c;
  double res, n;

  printf("Introduce el nombre del fichero: ");
  scanf("%s", nomFich);
  f=fopen(nomFich, "r");

  if (f==NULL)
    printf("El fichero \'%s\' no se ha podido abrir.", nomFich);
  else {
    printf("Introduce el nombre del fichero resultado: ");
    scanf("%s", nomFich2);
    g=fopen(nomFich2, "w");
    if (g==NULL) {
      printf("El fichero \'%s\' no se ha podido abrir.", nomFich2);
      fclose(f);
    }
    else {
      fscanf(f, "%lf%c", &res, &c);                  /*Leer primer número y espacio o \n */
      while (!feof(f)){
        fprintf(g,"%7.2f ", res);
        while (!feof(f) && (c!='\n')){
          fscanf(f, "%c%lf",&c,&n);                  /*Leer operación y número */
          fprintf(g,"%c %7.2f ",c, n);
          switch (c) {
            case '+': res=res+n;break;
            case '-': res=res-n;break;
            case '*': res=res*n;break;
            case '/': res=res/n;break;
          }
          fscanf(f,"%c",&c);                        /* Leer \n */
        }
        fprintf(g, "= %7.2f\n", res);
        fscanf(f, "%lf%c", &res, &c);               /*Otra vez lo mismo con la siguiente línea */
      }
    fclose(f);fclose(g);
    }
  }
  system("pause");
  return 0;
}

No hay comentarios:

Publicar un comentario