Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. bool chooseInOutType(char messageType) {
  8.     bool isInvalidInput = true;
  9.     bool answer;
  10.     if (messageType == 'i') {
  11.         puts("В случае, если вы хотите ввести строку из файла, введите F. Если же вы хотите ввести строку из строку, введите С");
  12.     }
  13.     else if (messageType == 'o') {
  14.         puts("В случае, если вы хотите произвести вывод в файл, введите F. Если же вы хотите произвести вывод только на экран, введите C");
  15.         getchar();
  16.     }
  17.     do {
  18.         char typedLetter;
  19.         scanf("%c", &typedLetter);
  20.         typedLetter = toupper(typedLetter);
  21.         if (typedLetter == 'F') {
  22.             answer = true;
  23.             isInvalidInput = false;
  24.         }
  25.         else if (typedLetter == 'C') {
  26.             answer = false;
  27.             isInvalidInput = false;
  28.         }
  29.         else {
  30.             puts("Ошибка. Был введён некорректный ответ. Повторите попытку.");
  31.             getchar();
  32.         }
  33.     } while (isInvalidInput);
  34.     return answer;
  35. }
  36.  
  37. char* readStrFromFile(char* fileName) {
  38.     FILE* inputSource;
  39.     inputSource = fopen(fileName, "r");
  40.     char* inputStr = (char*)malloc(sizeof(char) * 100);
  41.     fgets(inputStr, 21, inputSource);
  42.     inputStr = (char*)realloc(inputStr, (strlen(inputStr) + 1) * sizeof(char));
  43.     fclose(inputSource);
  44.     return inputStr;
  45. }
  46.  
  47.  
  48. char* readFileName() {
  49.     char* fileName = (char*)malloc(sizeof(char) * _MAX_FNAME);
  50.     scanf("%s", fileName);
  51.     strcat(fileName, ".txt");
  52.     fileName = (char*)realloc(fileName, (strlen(fileName) + 1) * sizeof(char));
  53.     return fileName;
  54. }
  55.  
  56. bool isFileExists(char* fileForCheckName) {
  57.     FILE* testFile;
  58.     testFile = fopen(fileForCheckName, "r");
  59.     if (testFile == NULL) {
  60.         free(fileForCheckName);
  61.         printf("Ошибка. Указанный файл не найден \n");
  62.         return true;
  63.     }
  64.     fclose(testFile);
  65.     return false;
  66. }
  67.  
  68. void clearFile(char* outputFileName) {
  69.     FILE* outputFile;
  70.     outputFile = fopen(outputFileName, "w");
  71.     fclose(outputFile);
  72. }
  73.  
  74. char* readString(){
  75.     char* inputFileName;
  76.     bool fileInput = chooseInOutType('i');
  77.     char* fullStr = (char*) malloc (32 * sizeof(char));
  78.     if (fileInput) {
  79.         do {
  80.             printf("Укажите название файла, из которого хотите считать матрицу:");
  81.             inputFileName = readFileName();
  82.         } while (isFileExists(inputFileName));
  83.         fullStr = readStrFromFile(inputFileName);
  84.     }
  85.     else{
  86.         getchar();
  87.         gets(fullStr);
  88.     }
  89.     fullStr = (char*)realloc(fullStr, (strlen(fullStr) + 1) * sizeof(char));
  90.     return fullStr;
  91. }
  92.  
  93. int countStrNums(char* inputStr){
  94.     int numsCount = 1;
  95.     for (int i = 0; i < strlen(inputStr); i++){
  96.         if (inputStr[i] == ' '){
  97.             numsCount++;
  98.         }
  99.     }
  100.     return numsCount;
  101. }
  102.  
  103. int* breakStringIntoArray(char* inputStr, int numsCount){
  104.     int* fillingArray = (int*) malloc (sizeof(int) * numsCount);
  105.     int high = (numsCount - 1);
  106.     char* end;
  107.     for (int i = 0; i < high; i++){
  108.         fillingArray[i] = atoi(strtok(inputStr, " "));
  109.     }
  110.     return fillingArray;
  111. }
  112.  
  113. char* decToRoman(int decNumber){
  114.     int* decDigits = {1, 4, 5, 9, 10, 40, 50, 90, 100, 500, 900, 1000};
  115.     char* romanDigits = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
  116.     int i = 12;
  117.     char* strResult = "";
  118.     while (decNumber > 0){
  119.         if (decDigits[i] > decNumber) {
  120.             i--;
  121.         } else {
  122.             char* bufStr;
  123.             decNumber -= decDigits[i];
  124.             strcat(strResult, itoa(bufStr, decNumber, 10));
  125.         }
  126.     }
  127.     return strResult;
  128. }
  129.  
  130. char* convertString(char* decStr){
  131.     int numsCount = countStrNums(decStr);
  132.     int* decNumbers = breakStringIntoArray(decStr, numsCount);
  133.     char* romanStr = "";
  134.     char* bufStr = "";
  135.     for (int i = 0; i < numsCount; i++) {
  136.         sprintf(bufStr, "%d", decNumbers[i]);
  137.         strcat(romanStr, bufStr);
  138.     }
  139.     return romanStr;
  140. }
  141.  
  142. char* chooseOutputFile() {
  143.     char* outputFileName;
  144.     bool isIncorrectFile = true;
  145.     do {
  146.         puts("Укажите имя файла, в который хотите произвести запись");
  147.         outputFileName = readFileName();
  148.         FILE* inFile;
  149.         inFile = fopen(outputFileName, "r");
  150.         if (inFile == NULL) {
  151.             puts("Файл с указанным названием не существует. Если вы желаете создать файл с указанным именем, введите Y, в обратном случае введите N");
  152.             bool isIncorrectAnswer = true;
  153.             do {
  154.                 char answer;
  155.                 getchar();
  156.                 scanf("%c", &answer);
  157.                 answer = toupper(answer);
  158.                 if (answer == 'Y') {
  159.                     clearFile(outputFileName);
  160.                     isIncorrectFile = false;
  161.                     isIncorrectAnswer = false;
  162.                 }
  163.                 else if (answer == 'N') {
  164.                     isIncorrectAnswer = false;
  165.                 }
  166.                 else if ((answer != 'Y') && (answer != 'N')) {
  167.                     puts("Ошибка. Был введён некорректный ответ. Повторите попытку");
  168.                 }
  169.             } while (isIncorrectAnswer);
  170.         }
  171.         else {
  172.             clearFile(outputFileName);
  173.             isIncorrectFile = false;
  174.         }
  175.     } while (isIncorrectFile);
  176.     return outputFileName;
  177. }
  178.  
  179. void writeStrInFile(char* outputFileName, char* str) {
  180.     FILE* outputFile;
  181.     outputFile = fopen(outputFileName, "a");
  182.     fprintf(outputFile, "%s", str);
  183.     fclose(outputFile);
  184. }
  185.  
  186. int main() {
  187.     SetConsoleOutputCP(CP_UTF8);
  188.     puts("Данная программа переводит до 4 введённых чисел, представленных арабскими цифрами, в римскую систему счисления");
  189.     char* fullDecStr = readString();
  190.     printf("Числа, представленные арабскими цифрами: %s \n", fullDecStr);
  191.     char* fullRomanStr = convertString(fullDecStr);
  192.     printf("Числа, представленные римскими цифрами: \n");
  193.     bool fileOutput = chooseInOutType('o');
  194.     if (fileOutput) {
  195.         char* outputFileName = chooseOutputFile();
  196.         writeStrInFile(outputFileName,"Числа, представленные арабскими цифрами: ");
  197.         writeStrInFile(outputFileName, fullDecStr);
  198.         puts("Запись в файл успешно произведена");
  199.     }
  200.     getchar();
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement