Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6.  
  7.  
  8. int numberOfOperators(char *input);
  9. int numberOfOperations(char *input);
  10. int readFromFileAndWrite(char *textfileName,char *codeFileName);
  11. int readFromFileAndPrint(char *fileName);
  12. int readFromKeyboardAndSaveInFile(char *filename, char *input);
  13. int readFromeKeyboardAndOutput(char *input);
  14.  
  15.  
  16. int main()
  17. {
  18.  
  19. int i=0;
  20. int n;
  21.  
  22. printf("\nMenu with different options!\n");
  23.  
  24. printf("\nPress 1) for reading a program from a fail and save the result in a file.\n");
  25. printf("\nPress 2) for reading a program from a fail and output the result on the screen.\n");
  26. printf("\nPress 3) for reading a program from the keyboard and output in a file entered from the user.\n");
  27. printf("\nPress 4) for reading a program from the keyboard and output on the screen.\n");
  28.  
  29. scanf("%d", &n);
  30.  
  31.  
  32. while(i==0)
  33. {
  34.  
  35. if(n==1)
  36.  
  37. {
  38. printf("\nPress 1) for reading a program from a fail and save the result in a file.\n");
  39.  
  40. char textFileName[50];
  41. char codeFileName[50];
  42.  
  43. printf("Enter file name for txt file: ");
  44. scanf("%s", textFileName);
  45.  
  46. printf("File name added \n");
  47.  
  48. printf("Enter file name for code file: ");
  49. scanf("%s", codeFileName);
  50.  
  51. printf("File name added \n");
  52.  
  53. readFromFileAndWrite(textFileName, codeFileName);
  54.  
  55. return;
  56.  
  57. }
  58.  
  59. if(n==2)
  60. {
  61. printf("\nPress 2) for reading a program from a fail and output the result on the screen.\n");
  62.  
  63. char fileName[50];
  64.  
  65. printf("Enter filename: ");
  66. scanf("%s", fileName);
  67.  
  68. readFromFileAndPrint(fileName);
  69.  
  70. return;
  71.  
  72. }
  73.  
  74. if(n==3)
  75. {
  76.  
  77. printf("\nPress 3) for reading a program from the keyboard and output in a file entered from the user.\n");
  78.  
  79. char fileName[50];
  80. char programInput[500];
  81.  
  82. printf("File name: ");
  83. scanf("%s", fileName);
  84.  
  85. printf("File name added \n");
  86.  
  87. printf("Program input: ");
  88.  
  89. scanf(" %s", programInput);
  90. printf("ProgramInputAdded \n");
  91.  
  92. readFromKeyboardAndSaveInFile(fileName, programInput);
  93.  
  94. return;
  95.  
  96. }
  97.  
  98. if(n==4)
  99. {
  100. printf("\nPress 4) for reading a program from the keyboard and output on the screen.\n");
  101.  
  102. char programInput[500];
  103.  
  104. scanf(" %s", programInput);
  105. readFromeKeyboardAndOutput(programInput);
  106. }
  107. else
  108. {
  109. printf("Please enter a valid option!");
  110. scanf("%d", &n);
  111. }
  112.  
  113.  
  114. }
  115. return 0;
  116. }
  117.  
  118. int numberOfOperators(char *input) // number of operators
  119. {
  120. int operators=0;
  121.  
  122. size_t inputlenght=strlen(input);
  123.  
  124. while(*input<inputlenght)
  125. {
  126. if(*input == '>' || *input == '<' || *input == '==' || *input =='!=' || *input == '>=' || *input == '<=')
  127. {
  128.  
  129. operators++;
  130. }
  131.  
  132. input++;
  133.  
  134. }
  135.  
  136. return operators;
  137. }
  138.  
  139.  
  140. int numberOfOperations(char *input) // Брой входно/изходни операции
  141. {
  142. int operations=0;
  143.  
  144.  
  145. size_t inputlenght=strlen(input);
  146.  
  147. while(*input<inputlenght)
  148. {
  149. if(*input == 'printf' || *input == 'scanf' || *input =='gets' || *input=='puts' || *input=='fgets' || *input=='fputs')
  150. {
  151. operations++;
  152. }
  153.  
  154. input++;
  155. }
  156.  
  157. return operations;
  158.  
  159. }
  160.  
  161. int readFromFileAndWrite(char *textfileName,char *codeFileName) // 1
  162. {
  163. FILE *fp;
  164. FILE *newfile;
  165.  
  166. int operators=0;
  167. int operations=0;
  168.  
  169.  
  170.  
  171. char newFile_name[1000];
  172.  
  173. fp=fopen(textfileName,"r");
  174.  
  175. newfile=fopen(codeFileName, "w");
  176.  
  177. if(fp==NULL)
  178. {
  179. perror("Invalid directory for your file!");
  180. return 1;
  181. }
  182.  
  183.  
  184. while(fgets(newFile_name, 1000, newfile) != NULL)
  185. {
  186. fputs(newFile_name, newfile);
  187.  
  188. operators=numberOfOperators(newFile_name);
  189. operations=numberOfOperations(newFile_name);
  190.  
  191.  
  192. }
  193.  
  194. printf("\nThe number of operators is %d\n", operators);
  195. printf("\nThe number of operations is %d\n", operations);
  196.  
  197.  
  198. return 0;
  199.  
  200. }
  201.  
  202. int readFromFileAndPrint(char *fileName) // 2
  203. {
  204. FILE *fp;
  205.  
  206. int operators=0;
  207. int operations=0;
  208.  
  209.  
  210. char buf[1000];
  211.  
  212. fp=fopen(fileName, "r");
  213.  
  214. if(fp==NULL)
  215. {
  216. perror("Invalid directory for your file!");
  217. return 1;
  218. }
  219.  
  220. while(fgets(buf, 1000, fp) != NULL)
  221. {
  222. printf("%s", buf);
  223.  
  224. operators = numberOfOperators(buf);
  225.  
  226. operations=numberOfOperations(buf);
  227.  
  228.  
  229.  
  230. }
  231.  
  232. printf("\nThe number of operators is %d\n", operators);
  233. printf("\nThe number of operations is %d\n", operations);
  234.  
  235.  
  236.  
  237. return 0;
  238.  
  239. }
  240. int readFromKeyboardAndSaveInFile(char *filename, char *input) // 3
  241. {
  242. FILE *fp;
  243.  
  244. fp=fopen(filename, "w");
  245.  
  246. int result=fputs(input, fp);
  247.  
  248.  
  249. fclose(fp);
  250.  
  251. printf("The name of the file is %s", filename);
  252.  
  253. int operators=numberOfOperators(input);
  254.  
  255. int operations=numberOfOperations(input);
  256.  
  257. printf("\nThe number of operators is %d\n", operators);
  258. printf("\nThe number of operations is %d\n", operations);
  259.  
  260. return 0;
  261.  
  262. }
  263. int readFromeKeyboardAndOutput(char *input) // 4
  264. {
  265. printf("%s", input);
  266.  
  267. int operators=numberOfOperators(input);
  268. int operations=numberOfOperations(input);
  269.  
  270. printf("\nThe number of operators is %d\n", operators);
  271. printf("\nThe number of operations is %d\n", operations);
  272.  
  273. return 0;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement