Created
September 22, 2010 07:33
-
-
Save kaldas/591298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************** | |
P2 - Engenharia - Instituto Infnet - Prof. Cesar Teixeira | |
Aluno: Rodrigo Caldas de Moura Duarte | |
Nota: ler arquivo de entrada, sendo um numero inteiro por | |
linha, calcular porcentagem de multiplos de 3 e 5 e depois | |
exportar estatistica para arquivo de saida... o professor | |
ainda pediu que os numeros lidos fossem passados para uma | |
array do tipo int. ~rcaldasmd~ */ | |
#include<stdio.h> | |
#include<stdlib.h> | |
/* @abrirArq(char "nome do arq", "perms do arq") | |
* Abre o arquivo e o transfere para *dados */ | |
void abrirArq(const char *file, const char perm[]); | |
/* contarLinhas(char "nome do arq", "perms do arq") | |
* O arq. de entrada segue o formato <inteiro><newline>.. | |
* então o numero de linhas reflete o numero de entradas */ | |
int contarLinhas(const char *fname, const char nperm[]); | |
//Globals | |
FILE *dados; | |
int *buff; | |
//Functions | |
void abrirArq(const char *file, const char perm[]) | |
{ | |
dados = fopen(file, perm); | |
if(!dados) | |
{ | |
printf("Impossivel abrir [%s].\n",file); | |
exit(0); | |
} | |
} | |
int contarLinhas(const char *fname, const char nperm[]) | |
{ //esta func foi criada puramente por motivos academicos | |
char ch; //eu queria demonstrar leitura de arqs com while(fgetc) | |
int cnt=0; | |
abrirArq(fname, nperm); | |
while((ch = fgetc(dados)) != EOF) | |
{ | |
if(ch == 10) | |
cnt++; | |
} | |
if(cnt == 0 || cnt > 1000) //nao aceitamos entradas nulas ou maiores que mil | |
{ | |
printf("Estrutura do Arq. de Entrada deve ser:\n<int><newline><int><newline>\n"); | |
exit(0); | |
} | |
fclose(dados); | |
return cnt; | |
} | |
int main(int argc, char **argv) | |
{ | |
int entradas=0, //numero de entradas | |
numero=0, | |
cnt=0; | |
float tpor=0, | |
mcinco=0, //multiplos de cinco | |
mtres=0, //m. de tres; //valor de cada elemento da porcentagem | |
outro=0; | |
char linha[8]; | |
if(argc != 3) | |
{ | |
printf("Uso incorreto..\nExemplo de argumentos: %s entrada.txt saida.txt\n",argv[0]); | |
return 0; | |
} | |
entradas = contarLinhas(argv[1],"r"); | |
buff = (int *) malloc(entradas * sizeof(int)); | |
if(!buff) | |
{ | |
printf("Impossivel alocar memoria...\n"); | |
exit(0); | |
} | |
abrirArq(argv[1],"r"); | |
printf("%i",entradas); | |
while(fgets(linha, sizeof(linha), dados) != NULL) | |
{ | |
numero = atoi(linha); | |
*(buff+cnt) = numero; | |
cnt++; | |
if(numero % 3 == 0) | |
mtres++; | |
if(numero % 5 == 0) | |
mcinco++; | |
if(numero % 5 != 0 && numero % 3 != 0) | |
outro++; | |
} | |
fclose(dados); | |
///////////////////////////////////////////// | |
tpor = 100 / (mcinco + mtres + outro); | |
mcinco = mcinco * tpor; | |
mtres = mtres * tpor; | |
///////////////////////////////////////////// | |
abrirArq(argv[2],"w"); | |
fprintf(dados,"Total: %i | Multiplos 3: %3.2f% | Multiplos 5: %3.2f%\n",entradas, mtres, mcinco); | |
fprintf(dados,"Int Array ="); | |
for(cnt=0;cnt != entradas;cnt++) | |
{ | |
fprintf(dados," %i ",*(buff+cnt)); | |
} | |
free(buff); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment