13 de março de 2009

Projeto I

Possível solução da Atividade 2 por Leandro Hossaki
Clique Aqui para Baixar


Lembrando que no arquivo de leitura nas colunas em que houverem espaços em branco, (quando não tem número) , em vez dos espaços em branco o professor deixou colocar "-" então, coloquem "-" nos espaços em branco para facilitar um pouco.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main(){
FILE *Arq, *ArqNew;

char Str1[10], Str2[10], Str3[5], Str4[5];
char Str5[5], Str6[5], Str7[5], a[50];
int i, count_hpF2 = 0 , count_hF = 0 , count_foF2 = 0;
float hpF2, hF, foF2, Soma_hF = 0, Soma_foF2 = 0, Soma_hpF2 = 0, Soma_Sub = 0, Sub;
clrscr();
Arq = fopen("C:\\TC\\ARQUIVO\\ARQ.SJC","r");
ArqNew = fopen("C:\\TC\\ARQUIVO\\ARQNEW.SJC","w");

if(!Arq){
printf("Erro ao abrir o arquivo.");
getch();
exit(1);
}
if(!ArqNew){
printf("Erro ao criar o arquivo.");
getch();
exit(1);
}
// Copia o Cabeçalho
for(i=1;i<=11;i++){
fscanf(Arq,"%s",&a);
fprintf(ArqNew,"%s\t",a);
if((i==2) || (i==4))
fprintf(ArqNew,"\n");
}
fprintf(ArqNew,"hpF2-h'F");
fprintf(ArqNew,"\n");

// Começa a leitura do arquivo
while(!feof(Arq)){

if(getc(Arq) == EOF){
break;
}

fscanf(Arq,"%s %s %s %s %s %s %s",&Str1, &Str2, &Str3, &Str4, &Str5, &Str6, &Str7);
fprintf(ArqNew,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t",&Str1, &Str2, &Str3, &Str4, &Str5, &Str6, &Str7);
hF = atof(Str3);

hpF2 = atof(Str5);
if(hF){

Soma_hF = Soma_hF + hF;
count_hF = count_hF + 1;
if(hpF2){
Soma_hpF2 = Soma_hpF2 + hpF2;
count_hpF2 = count_hpF2 + 1;
Sub = hpF2 - hF;
Soma_Sub = Soma_Sub + Sub;
}

}
if(atof(Str4)){
Soma_foF2 = Soma_foF2 + atof(Str4);
count_foF2 = count_foF2 + 1;
}

if(Sub){
fprintf(ArqNew,"%.0f\n",Sub);
}

else{
fprintf(ArqNew,"-\n");
}

hF = NULL;
hpF2 = NULL;
Sub = NULL;
}

fprintf(ArqNew,"Medias \t \t%.2f",Soma_hF/count_hF);
fprintf(ArqNew,"\t%.2f",Soma_foF2/count_foF2);
fprintf(ArqNew,"\t%.2f",Soma_hpF2/count_hpF2);
fprintf(ArqNew,"\t \t \t%.2f",Soma_Sub/count_hpF2);

fclose(Arq);
getch();
return 0;

}