Skip to content

Instantly share code, notes, and snippets.

@VitorDiToro
Last active November 23, 2016 17:28
Show Gist options
  • Select an option

  • Save VitorDiToro/9732b06a4e85f2af74d49d493a946db6 to your computer and use it in GitHub Desktop.

Select an option

Save VitorDiToro/9732b06a4e85f2af74d49d493a946db6 to your computer and use it in GitHub Desktop.
ac303
#include <iostream> /// Biblioteca padrão do C++
#include <cstdio> /// Para poder trabalhar com Arquivo
#include <iomanip> /// Para poder usar o setW
using namespace std;
FILE *pFile;
struct caracter{
int qnt;
char ch;
};
void abreArquivo(){
/*****************************************************
* Valida e abre o arquivo escolhido pelo usuário. *
*****************************************************/
char nomeArquivo[80];
while(true){
cout<<"Informe o nome do arquivo."<<endl;
cin>>nomeArquivo;
cout<<endl;
/// fopen(nomeArquivo,"r") => Open File in Read mode!
if ((pFile = fopen(nomeArquivo,"r"))==NULL){
perror ("Error opening file");
}else break;
cout<<endl;
}
}
void obtemCaracteres(int vetCaracteres[],int &totalCaracteres){
/*************************************************************
* Obtem e conta todos os diferetes caracteres do arquivo. *
*************************************************************/
char ch;
ch = fgetc(pFile);
while(ch != EOF){ /// EOF => End Of File
totalCaracteres++;
vetCaracteres[(int)ch] += 1;
ch = fgetc(pFile);
}
}
void exibeVetor(int vetCaracteres[], caracter vetB[], int &size){
/**********************************************************************
* Exibe o conteudo de vetCaracteres e copia este conteudo p/ vetB. *
**********************************************************************/
int i;
cout<<endl;
cout<<"+------------------------+"<<endl;
cout<<"| Vetor Lido |"<<endl;
cout<<"+-----------+------------+"<<endl;
cout<<"| Caracter | Quantidade |"<<endl;
cout<<"+-----------+------------+"<<endl;
size = 0;
for(i=32; i<128; i++){
if(vetCaracteres[i] != 0){
vetB[size].ch = i;
vetB[size].qnt = vetCaracteres[i];
cout << "| '" << vetB[size].ch << "' | " << setw(4) <<vetB[size].qnt <<" |" <<endl;
size++; /// setw() => Sets the field width to be used on output operations.
} /// http://www.cplusplus.com/reference/iomanip/setw/?kw=setw
}
cout<<"+------------------------+"<<endl;
}
void exibeOrdenado(caracter vetB[], int size){
/******************************************************************
* Exibe o conteudo de vetB em ordem Decrescente de quantidade. *
******************************************************************/
int i, j, maxQnt, posicao;
cout<<endl;
cout<<"+------------------------+"<<endl;
cout<<"| Vetor Ordenado |"<<endl;
cout<<"+-----------+------------+"<<endl;
cout<<"| Caracter | Quantidade |"<<endl;
cout<<"+-----------+------------+"<<endl;
for(i=0; i<size; i++){
maxQnt = -1;
for(j=0; j<size; j++){
if(vetB[j].qnt > maxQnt){
posicao = j;
maxQnt = vetB[j].qnt;
}
}
cout<<"| '"<<vetB[posicao].ch<<"' | "<<setw(4)<<vetB[posicao].qnt<<" |"<<endl;
vetB[posicao].qnt = -1;
}
cout<<"+------------------------+ "<<endl;
}
void exibeTotalCaracteres(int totalCaracteres){
cout<<endl;
cout<<"+-----------+------------+ "<<endl;
cout<<"| Total de | |"<<endl;
cout<<"| Caranteres| "<<setw(5)<<totalCaracteres<<" |"<<endl;
cout<<"+-----------+------------+ "<<endl;
}
int main(void){
int vetCaracteres[128]; //Tamanho arbitrado com base na Tabela ASCII (http://www.asciitable.com/index/asciifull.gif)
caracter vetB[128];
int i,sizeB, totalCaracteres;
/// Inicializa vetCaracteres com 0 em todas as suas posições
for(i=0; i<256; i++)
vetCaracteres[i]=0;
/// Abre e valida a existencia do arquivo
abreArquivo();
/// Lê e Conta tudo!
totalCaracteres = 0;
obtemCaracteres(vetCaracteres,totalCaracteres);
/// Exibe as quantidades lidas
exibeVetor(vetCaracteres, vetB, sizeB);
/// Exibe os Valores Ordenados
exibeOrdenado(vetB, sizeB);
/// Exibe total de caracteres no arquivo
exibeTotalCaracteres(totalCaracteres);
/// Fecha o arquivo;
fclose(pFile);
}
I N A I A V A T L U T E L U I Q L S T E H K J D
F Y U A N A P R O E L N O N A O L B N S N D T Y
A U O O M E J U H Z X I Z L K L M Y T Z X E R H
G L Y A R Q J A K A V O R P J G D F U I E U L C
8 8 7 9 7 8 1 2 3 4 5 6 7 8 9 0 1 1 1 1 1 1 1 8
123456789 01234567980
asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasda
qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@@@@#####$$$$$$%%%%%&&&&&&*****(((((()))))_______
------=======++++++++{{{{{{[[[[[[[]]]]]]}}}}}}}}}}
;;;;;;;;:::::::::::.......,,,,,<<<<<<<<>>>>>>>>>||
||||||\\\\\\\\\\\\\////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment