Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <Windows.h>
- #include <stdbool.h>
- #include <stdlib.h>
- bool chooseInOutType(char messageType) {
- bool isInvalidInput = true;
- bool answer;
- if (messageType == 'i') {
- puts("В случае, если вы хотите ввести строку из файла, введите F. Если же вы хотите ввести строку из строку, введите С");
- }
- else if (messageType == 'o') {
- puts("В случае, если вы хотите произвести вывод в файл, введите F. Если же вы хотите произвести вывод только на экран, введите C");
- getchar();
- }
- do {
- char typedLetter;
- scanf("%c", &typedLetter);
- typedLetter = toupper(typedLetter);
- if (typedLetter == 'F') {
- answer = true;
- isInvalidInput = false;
- }
- else if (typedLetter == 'C') {
- answer = false;
- isInvalidInput = false;
- }
- else {
- puts("Ошибка. Был введён некорректный ответ. Повторите попытку.");
- getchar();
- }
- } while (isInvalidInput);
- return answer;
- }
- char* readStrFromFile(char* fileName) {
- FILE* inputSource;
- inputSource = fopen(fileName, "r");
- char* inputStr = (char*)malloc(sizeof(char) * 100);
- fgets(inputStr, 21, inputSource);
- inputStr = (char*)realloc(inputStr, (strlen(inputStr) + 1) * sizeof(char));
- fclose(inputSource);
- return inputStr;
- }
- char* readFileName() {
- char* fileName = (char*)malloc(sizeof(char) * _MAX_FNAME);
- scanf("%s", fileName);
- strcat(fileName, ".txt");
- fileName = (char*)realloc(fileName, (strlen(fileName) + 1) * sizeof(char));
- return fileName;
- }
- bool isFileExists(char* fileForCheckName) {
- FILE* testFile;
- testFile = fopen(fileForCheckName, "r");
- if (testFile == NULL) {
- free(fileForCheckName);
- printf("Ошибка. Указанный файл не найден \n");
- return true;
- }
- fclose(testFile);
- return false;
- }
- void clearFile(char* outputFileName) {
- FILE* outputFile;
- outputFile = fopen(outputFileName, "w");
- fclose(outputFile);
- }
- char* readString(){
- char* inputFileName;
- bool fileInput = chooseInOutType('i');
- char* fullStr = (char*) malloc (32 * sizeof(char));
- if (fileInput) {
- do {
- printf("Укажите название файла, из которого хотите считать матрицу:");
- inputFileName = readFileName();
- } while (isFileExists(inputFileName));
- fullStr = readStrFromFile(inputFileName);
- }
- else{
- getchar();
- gets(fullStr);
- }
- fullStr = (char*)realloc(fullStr, (strlen(fullStr) + 1) * sizeof(char));
- return fullStr;
- }
- int countStrNums(char* inputStr){
- int numsCount = 1;
- for (int i = 0; i < strlen(inputStr); i++){
- if (inputStr[i] == ' '){
- numsCount++;
- }
- }
- return numsCount;
- }
- int* breakStringIntoArray(char* inputStr, int numsCount){
- int* fillingArray = (int*) malloc (sizeof(int) * numsCount);
- int high = (numsCount - 1);
- char* end;
- for (int i = 0; i < high; i++){
- fillingArray[i] = atoi(strtok(inputStr, " "));
- }
- return fillingArray;
- }
- char* decToRoman(int decNumber){
- int* decDigits = {1, 4, 5, 9, 10, 40, 50, 90, 100, 500, 900, 1000};
- char* romanDigits = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
- int i = 12;
- char* strResult = "";
- while (decNumber > 0){
- if (decDigits[i] > decNumber) {
- i--;
- } else {
- char* bufStr;
- decNumber -= decDigits[i];
- strcat(strResult, itoa(bufStr, decNumber, 10));
- }
- }
- return strResult;
- }
- char* convertString(char* decStr){
- int numsCount = countStrNums(decStr);
- int* decNumbers = breakStringIntoArray(decStr, numsCount);
- char* romanStr = "";
- char* bufStr = "";
- for (int i = 0; i < numsCount; i++) {
- sprintf(bufStr, "%d", decNumbers[i]);
- strcat(romanStr, bufStr);
- }
- return romanStr;
- }
- char* chooseOutputFile() {
- char* outputFileName;
- bool isIncorrectFile = true;
- do {
- puts("Укажите имя файла, в который хотите произвести запись");
- outputFileName = readFileName();
- FILE* inFile;
- inFile = fopen(outputFileName, "r");
- if (inFile == NULL) {
- puts("Файл с указанным названием не существует. Если вы желаете создать файл с указанным именем, введите Y, в обратном случае введите N");
- bool isIncorrectAnswer = true;
- do {
- char answer;
- getchar();
- scanf("%c", &answer);
- answer = toupper(answer);
- if (answer == 'Y') {
- clearFile(outputFileName);
- isIncorrectFile = false;
- isIncorrectAnswer = false;
- }
- else if (answer == 'N') {
- isIncorrectAnswer = false;
- }
- else if ((answer != 'Y') && (answer != 'N')) {
- puts("Ошибка. Был введён некорректный ответ. Повторите попытку");
- }
- } while (isIncorrectAnswer);
- }
- else {
- clearFile(outputFileName);
- isIncorrectFile = false;
- }
- } while (isIncorrectFile);
- return outputFileName;
- }
- void writeStrInFile(char* outputFileName, char* str) {
- FILE* outputFile;
- outputFile = fopen(outputFileName, "a");
- fprintf(outputFile, "%s", str);
- fclose(outputFile);
- }
- int main() {
- SetConsoleOutputCP(CP_UTF8);
- puts("Данная программа переводит до 4 введённых чисел, представленных арабскими цифрами, в римскую систему счисления");
- char* fullDecStr = readString();
- printf("Числа, представленные арабскими цифрами: %s \n", fullDecStr);
- char* fullRomanStr = convertString(fullDecStr);
- printf("Числа, представленные римскими цифрами: \n");
- bool fileOutput = chooseInOutType('o');
- if (fileOutput) {
- char* outputFileName = chooseOutputFile();
- writeStrInFile(outputFileName,"Числа, представленные арабскими цифрами: ");
- writeStrInFile(outputFileName, fullDecStr);
- puts("Запись в файл успешно произведена");
- }
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement