Advertisement
Guest User

trab

a guest
Nov 17th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. /*X CL | Conta o número de letras
  6. X CC | Conta o número de caracteres
  7. X CD | Conta o número de dígitos
  8. X CN | Conta o número de linhas
  9. X CT | Conta o número incidências de determinado caracter informado pelo usuário: Exemplo -b Para contar o número de letras “A”: analyse CT teste.txt “A”
  10. X CO | Codifica seu arquivo*. Deve ser informado o nome do arquivo de saída. Exemplo de chamada: analyse CO teste.txt novo.txt (Inverte bit 2 e 7 || 1 p/0 e 0 p/ 1)
  11. X DO | Decodifica seu arquivo. O arquivo de entrada (codificado) e o de saída devem ser informados. Exemplo de chamada: analyse DO novo.txt novo2.txt */
  12.  
  13.  
  14.  
  15. /* SE USAR O CHAR X NO ARQUIVO E ELE DECODIFICAR, VAI DAR END OF FILE, POIS O VALOR NA TABELA ASCII DO CARACTERE CODIFICADO É 26, MESMO VALOR DE CTRL Z, QUE
  16. EQUIVALE A ENDOFILE*/
  17.  
  18.  
  19. int main(int argc, char *argv[]) {
  20. int ch;
  21. int i = 0;
  22. FILE *fp;
  23. FILE *fp2;
  24. FILE *fp3;
  25. FILE *fpbin;
  26. fp = fopen(argv[2], "r");
  27. if (argc == 3){
  28. if(strcmp(argv[1], "CL") == 0){
  29. while((ch = fgetc(fp)) != EOF){
  30. if(isalpha(ch)) {
  31. i++;
  32. }
  33. }
  34. printf("Numero de letras no arquivo: %d", i);
  35. }
  36. else if(strcmp(argv[1], "CC") == 0){
  37. while((ch = fgetc(fp)) != EOF){
  38. i++;
  39. }
  40. printf("Numero de caracteres no arquivo: %d", i);
  41. }
  42. else if(strcmp(argv[1], "CD") == 0){
  43. while((ch = fgetc(fp)) != EOF){
  44. if(isdigit(ch)) {
  45. i++;
  46. }
  47. }
  48. printf("Numero de digitos no arquivo: %d", i);
  49. }
  50. else if(strcmp(argv[1], "CN") == 0){
  51. while((ch = fgetc(fp)) != EOF){
  52. if(ch == '\n') {
  53. i++;
  54. }
  55. }
  56. printf("Numero de linhas no arquivo: %d", i+1);
  57. }
  58. else {
  59. printf("Parametro invalido");
  60. }
  61. }
  62. else if(argc == 4){
  63. if(strcmp(argv[1], "CT") == 0){
  64. char cha[40];
  65. while((ch = fgetc(fp)) != EOF){
  66. sprintf(cha, "%c", ch);
  67. if(strcmp(argv[3], cha) == 0)
  68. i++;
  69. }
  70. printf("Numero de caracteres %s no arquivo: %d",argv[3], i);
  71. }
  72. else if(strcmp(argv[1], "CO") == 0){
  73. fp2 = fopen(argv[3], "wb+");
  74. int convert = 66;
  75. int c;
  76. char novo;
  77. while((ch = fgetc(fp)) != EOF){
  78. c = ch ^ convert;
  79. fprintf(fp2, "%c", c);
  80. }
  81. fclose(fp2);
  82. }
  83. else if(strcmp(argv[1], "DO") == 0){
  84. fpbin = fopen(argv[2], "rb");
  85. fp3 = fopen(argv[3], "w+");
  86. int convert = 66;
  87. int c;
  88. while(1){
  89. if(feof(fpbin)) {
  90. break;
  91. }
  92. else {
  93. ch = fgetc(fpbin);
  94. c = ch ^convert;
  95. fputc(c, fp3);
  96. }
  97. }
  98. while((ch = fgetc(fpbin)) != EOF){
  99. fscanf(fpbin, "%c", &ch);
  100. printf("%c\n", ch);
  101. c = ch ^ convert;
  102. printf("%c", c);
  103. fputc(c, fp3);
  104. }
  105. fclose(fp3);
  106. fclose(fpbin);
  107. }
  108. else {
  109. printf("Parametro invalido");
  110. }
  111. }
  112. else {
  113. printf("Numero de parametros invalidos");
  114. }
  115.  
  116. fclose(fp);
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement