Advertisement
Guest User

Untitled

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