Advertisement
salron3

KursovaNew

Jan 8th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.09 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define SIZE 256
  7. #define NAMES 128
  8.  
  9. // Прототипи на функциите използвани по долу
  10. int menu();
  11. void getFileName(char* fileName, int mode);
  12. void GetNumberOfOperators(FILE *inputFile, FILE *outputFile);
  13.  
  14. int FileToFile();
  15. int FileToScreen();
  16. int ScreenToFile();
  17. int ScreenToScreen();
  18.  
  19. int main()
  20. {
  21.     while (1)
  22.     {
  23.         switch (menu())
  24.         {
  25.         case 1:
  26.             FileToFile();
  27.             break;
  28.         case 2:
  29.             FileToScreen();
  30.             break;
  31.         case 3:
  32.             ScreenToFile();
  33.             break;
  34.         case 4:
  35.             ScreenToScreen();
  36.             break;
  37.         case 0:
  38.             exit(0);
  39.         default:
  40.             break;
  41.         }
  42.  
  43.         system("pause");
  44.         system("cls");
  45.     }
  46.     return 0;
  47. }
  48.  
  49. int menu()
  50. {
  51.     int i;
  52.  
  53.     printf(" ________________M A I N   M E N U_________________\n");
  54.     printf("|                                                  |\n");
  55.     printf("| 1. Open a .txt/.c file and write to a file       |\n");
  56.     printf("| 2. Open a .txt/.c file and write to the screen   |\n");
  57.     printf("| 3. Enter text and write to a file                |\n");
  58.     printf("| 4. Enter text and write to the screen            |\n");
  59.     printf("| 0. Exit                                          |\n");
  60.     printf("|__________________________________________________|\n");
  61.  
  62.     do //while (i<0 || i>4);
  63.     {
  64.         fflush(stdin);// Чисти буфера
  65.         printf("Select Option: ");
  66.         scanf("%d", &i);
  67.     } while (i<0 || i>4);// Позволява въвеждане на числа само между 0 и 4
  68.     return i;// Връщаме избраната стойност
  69. }
  70.  
  71. void getFileName(char* fileName, int mode) /// 1 TO OPEN A FILE TO READ /// 2 TO OPEN A FILE TO WRITE ///
  72. {
  73.     while (1)// Безкраен цикъл
  74.     {
  75.         fflush(stdin);
  76.         if (mode == 1) // Ако е подадено 1 ще се изисква потребителя да въведе име на файл за четене
  77.         {
  78.             printf("Enter File Name To Read (.c/.txt): ");
  79.             scanf("%s", fileName);      ///gets(fileName);
  80.  
  81.             if (fileName[strlen(fileName) - 4] == '.' && toupper(fileName[strlen(fileName) - 3]) == 'T' && toupper(fileName[strlen(fileName) - 2]) == 'X' && toupper(fileName[strlen(fileName) - 1]) == 'T')
  82.             {
  83.                 return;
  84.             }
  85.  
  86.             if (fileName[strlen(fileName) - 2] == '.' && toupper(fileName[strlen(fileName) - 1]) == 'C')// Проверка дали файла завършва на .С
  87.             {
  88.                 return;// Ако завършва се излиза от функцията
  89.             }
  90.         }
  91.  
  92.         if (mode == 2)// При мод 2 се въвежда име на файла за запис
  93.         {
  94.             printf("Enter File Name To Write (.c/.txt): ");
  95.             scanf("%s", fileName);      ///gets(fileName);
  96.  
  97.             if (fileName[strlen(fileName) - 4] == '.' && toupper(fileName[strlen(fileName) - 3]) == 'T' && toupper(fileName[strlen(fileName) - 2]) == 'X' && toupper(fileName[strlen(fileName) - 1]) == 'T')
  98.             {
  99.                 return;
  100.             }
  101.  
  102.             if (fileName[strlen(fileName) - 2] == '.' && toupper(fileName[strlen(fileName) - 1]) == 'C')
  103.             {
  104.                 return;
  105.             }
  106.         }
  107.     }
  108. }
  109.  
  110. void GetNumberOfOperators(FILE *inputFile, FILE *outputFile)
  111. {
  112.     int countOfAssignments = 0, flag = 0;
  113.     int countOfNot = 0;
  114.     int countOfOperators = 0;
  115.     char line[SIZE];
  116.     while (fgets(line, sizeof(line), inputFile) != NULL) //Четене ред по ред
  117.     {
  118.         for (size_t i = 0; i < strlen(line); i++)
  119.         {
  120.  
  121.             if (line[i] == '\"' && !flag) flag = 1; // не търсим в къвичките
  122.             else if (line[i] == '\"' && line[i - 1] != '\\' && flag == 1) flag = 0;
  123.  
  124.             if (!flag && line[i] == '/' && line[i + 1] == '/') break; // при наличието на коментар спираме да четем реда
  125.  
  126.             if (!flag && line[i] == '/' && line[i + 1] == '*') flag = 2;// не търсим в големите коментари / *
  127.             else if (flag == 2 && line[i] == '*' && line[i + 1] == '/') flag = 0;
  128.  
  129.             if (line[i] == '\'' && !flag) flag = 3; // не търсим в апострофите;
  130.             else if (line[i] == '\'' && flag == 3) flag = 0;
  131.  
  132.             if (!flag)
  133.             {
  134.                 if (line[i] == '=' && line[i + 1] != '=' && line[i - 1] != '=' && line[i - 1] != '!' &&
  135.                     line[i - 1] != '+' && line[i - 1] != '-' && line[i - 1] != '*' && line[i - 1] != '/' &&
  136.                     line[i - 1] != '%' && line[i - 1] != '<' && line[i - 1] != '>') //Tърсим само оператор '=', без +=, -= и др.
  137.                 {
  138.                     countOfAssignments++;
  139.                     countOfOperators++;
  140.                 }
  141.                 else if (line[i] == '!' && line[i + 1] != '=')// Проверка за оператор !
  142.                 {
  143.                     countOfNot++;
  144.                     countOfOperators++;
  145.                 }
  146.                 else if (line[i] == '+' && line[i + 1] != '+') countOfOperators++; // Проверка само за оператора "+", а не "++"
  147.                 else if (line[i] == '-' && line[i + 1] != '-') countOfOperators++;// Проверка само за оператора "-", а не "--"
  148.                 else if (line[i] == '&' && line[i + 1] == '&') countOfOperators++;// Проверка само за оператора &&
  149.                 else if (line[i] == '|' && line[i + 1] == '|') countOfOperators++;// Проверка само за оператора ||
  150.  
  151.                 else if (line[i] == '*') countOfOperators++;
  152.                 else if (line[i] == '/') countOfOperators++;
  153.                 else if (line[i] == '%') countOfOperators++;
  154.                 else if (line[i] == '?') countOfOperators++;
  155.  
  156.                 else if (line[i] == '~') countOfOperators++;
  157.                 else if (line[i] == '>') countOfOperators++;
  158.                 else if (line[i] == '<') countOfOperators++;
  159.             }
  160.         }
  161.     }
  162.  
  163.     fprintf(outputFile, "The number of operators '=' is: %d\n", countOfAssignments);// Запис на информацията
  164.     fprintf(outputFile, "The number of all operators is: %d\n", countOfOperators);// Запис на информацията
  165.     fprintf(outputFile, "The number of operators '!' is: %d\n", countOfNot);
  166. }
  167.  
  168. int FileToFile()
  169. {
  170.     char readFile[NAMES] = { 0 }, writeFile[NAMES] = { 0 }; // Имената на файловете
  171.     FILE* inputFile, *outputFile; // Деклариране на файлов поинтър
  172.     getFileName(readFile, 1);// Въвеждане на името на файла за четене
  173.  
  174.     inputFile = fopen(readFile, "r"); // Отваряме файла за четене. "r" за четене
  175.     if (inputFile == NULL)// Проверка дали съществува файла
  176.     {
  177.         fprintf(stderr, "ERROR! CANNOT OPEN FILE!!!");
  178.         return; // Връщаме се към main-а
  179.     }
  180.  
  181.     getFileName(writeFile, 2);// Въвеждане на файла за писане
  182.     outputFile = fopen(writeFile, "w");// Проверка дали е отворен/създанен файл
  183.  
  184.     if (outputFile == NULL)
  185.     {
  186.         fprintf(stderr, "ERROR! CANNOT OPEN FILE!!!");
  187.         return;// Връщаме се към main-а
  188.     }
  189.  
  190.     GetNumberOfOperators(inputFile, outputFile);//Викане на функцията брояща циклите
  191.     rewind(inputFile);// Връща файловия поинтър към началото на файла(за да може пак да се прочете)
  192.     printf("Information saved to \"%s\"\n", writeFile);
  193.  
  194.     fclose(inputFile);//затваряне на файла
  195.     fclose(outputFile);
  196.  
  197.     return 0;
  198. }
  199.  
  200. int FileToScreen()
  201. {
  202.     char readFile[NAMES] = { 0 };
  203.     FILE* inputFile;
  204.     getFileName(readFile, 1);
  205.  
  206.     inputFile = fopen(readFile, "r");// Отваряне на файла за четене
  207.     if (inputFile == NULL)
  208.     {
  209.         fprintf(stderr, "CANNOT OPEN FILE!");
  210.         return;
  211.     }
  212.  
  213.     GetNumberOfOperators(inputFile, stdout);// stdout e изходен поток за данни. Той превръща fprintf-a в обикновен printf
  214.     rewind(inputFile);// Връща файловия поинтър към началото на файла(за да може пак да се прочете)
  215.  
  216.     fclose(inputFile);
  217.  
  218.     return 0;
  219. }
  220.  
  221. int ScreenToFile()
  222. {
  223.     char writeFile[NAMES] = { 0 }, manage[SIZE], c = 0;
  224.     FILE *outputFile, *tempBuffer;
  225.  
  226.     getFileName(writeFile, 2);// Въвеждане на името на файла за запис
  227.  
  228.     outputFile = fopen(writeFile, "w");//Отваря файла за писане
  229.     if (outputFile == NULL)// Проверка за дали файла е отворен(създаден)
  230.     {
  231.         fprintf(stderr, "CANNOT OPEN FILE!");// stderr поток за грешки
  232.         return;
  233.     }
  234.  
  235.     tempBuffer = fopen("TempChanges.tmp", "w");// Отваряне на временен файл за писане
  236.  
  237.     printf("\nWrite text under the line and type \"exit\" on a new line to save the information\n");
  238.     printf("________________________________________________________________________________\n");
  239.     fflush(stdin);
  240.  
  241.     while (1)
  242.     {
  243.         gets(manage);// Взима низ от клавиетурата
  244.         if (!strcmp(manage, "exit"))// Проверка дали се среща 'exit' във въведения от потребителя низ
  245.         {
  246.             fclose(tempBuffer);//ако се среща файла се затваря
  247.             tempBuffer = fopen("TempChanges.tmp", "r");// след това се отваря за четене
  248.             break;//излизане от безкрайният цикъл
  249.         }
  250.         fprintf(tempBuffer, "%s\n", manage);// Записване на низа въведен от потребителя
  251.     }
  252.  
  253.     GetNumberOfOperators(tempBuffer, outputFile);
  254.     rewind(tempBuffer);
  255.  
  256.     printf("Information saved to \"%s\"\n", writeFile);
  257.  
  258.     fclose(outputFile);
  259.     fclose(tempBuffer);
  260.  
  261.     return 0;
  262. }
  263.  
  264. int ScreenToScreen()// Тук нещата са като по-горе
  265. {
  266.     char  manage[SIZE], c = 0;
  267.     int counter = 0;
  268.     FILE *tempBuffer;
  269.  
  270.     tempBuffer = fopen("TempChanges.tmp", "w");
  271.  
  272.     printf("\nWrite text under the line and type \"exit\" on a new line to save the information\n");
  273.     printf("________________________________________________________________________________\n");
  274.     fflush(stdin);
  275.  
  276.     while (1)
  277.     {
  278.         gets(manage);
  279.         if (!strcmp(manage, "exit"))
  280.         {
  281.             fclose(tempBuffer);
  282.             tempBuffer = fopen("TempChanges.tmp", "r");
  283.             break;
  284.         }
  285.         fprintf(tempBuffer, "%s\n", manage);
  286.     }
  287.  
  288.     system("CLS");
  289.     printf("Information:\n");
  290.  
  291.     GetNumberOfOperators(tempBuffer, stdout);
  292.     rewind(tempBuffer);
  293.  
  294.     fclose(tempBuffer);
  295.  
  296.     return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement