Advertisement
tegusta

contatore vocali, lettere, numeri (file binari)

May 13th, 2012
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. /*
  2. Studente : Scia Massimiliano
  3. Classe : 3IC
  4. Data : 13/05/12 18:25
  5. Nome del file : controlla vocali, lettere, numeri
  6. */
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <ctime>
  11. #include <cstdlib>
  12. #include <cctype>
  13. #include <windows.h>
  14. #include <fstream>
  15. #include <time.h>
  16. #include <stdio.h>
  17. #define N 100
  18.  
  19. using namespace std;
  20.  
  21. struct code{
  22. char c[N];
  23. };//struct
  24.  
  25. void end(void){
  26. fflush(stdin);
  27. cout<<"\n\nPremere Invio per continuare.";
  28. getchar();
  29. }//end
  30.  
  31. bool scrivi(char* nomefile){
  32. FILE* f;
  33. f=fopen(nomefile,"ab+");
  34. if(f==NULL)
  35. return false;
  36. int n;
  37. cout<<"\nQuanti codici vuoi inserire?\n- ";
  38. cin>>n;
  39. code cd;
  40. for(int i=0;i<n;i++){
  41. cout<<"\nCodice "<<i+1<<": ";
  42. fflush(stdin);
  43. cin.get(cd.c,N,'\n');
  44. fflush(stdin);
  45. fwrite(&cd,sizeof(code),1,f);
  46. }//for
  47. fclose(f);
  48. return true;
  49. }//scrivi
  50.  
  51. bool controllo(char* nomefile, int a[]){
  52. FILE* f;
  53. f=fopen(nomefile,"rb");
  54. code cd;
  55. if(f==NULL)
  56. return false;
  57. for(int i=0;i<3;i++)
  58. a[i]=0;
  59. fread(&cd,sizeof(code),1,f);
  60. while(!feof(f)){
  61. for(int j=0;j<strlen(cd.c);j++){
  62. if(isalpha(cd.c[j])){
  63. a[0]++;
  64. if(tolower(cd.c[j])=='a' || tolower(cd.c[j])=='e' || tolower(cd.c[j])=='i' || tolower(cd.c[j])=='o' || tolower(cd.c[j])=='u')
  65. a[1]++;
  66. }//if
  67. if(isdigit(cd.c[j]))
  68. a[2]++;
  69. }//for
  70. fread(&cd,sizeof(code),1,f);
  71. }//while
  72. fclose(f);
  73. return true;
  74. }//controllo
  75.  
  76. bool visualizza(char* nomefile, int a[]){
  77. FILE* f;
  78. f=fopen(nomefile,"rb");
  79. if(f==NULL)
  80. return 0;
  81. code cd;
  82. fread(&cd,sizeof(code),1,f);
  83. while(!feof(f)){
  84. cout<<"\n\nLettere: "<<a[0]<<"\nVocali: "<<a[1]<<"\nNumeri: "<<a[2];
  85. fread(&cd,sizeof(code),1,f);
  86. }//while
  87. fclose(f);
  88. return 1;
  89. }//visualize
  90.  
  91.  
  92. int main(){
  93. char nomefile[N];
  94. int a[N];
  95. cout<<"\nIl nome del file e`: ";
  96. fflush(stdin);
  97. cin.get(nomefile,N,'\n');
  98. fflush(stdin);
  99. bool s,c,v;
  100. s=scrivi(nomefile);
  101. c=controllo(nomefile,a);
  102. v=visualizza(nomefile,a);
  103. end();
  104. return 0;
  105. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement