123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <math.h>
- int read_in_file(float* coefficient, FILE* file) {
- char** buffer = malloc(sizeof(char) * 3);
- for (size_t i = 0; i < 3; i++)
- {
- buffer[i] = malloc(sizeof(char) * 100);
- }
- if (fscanf(file, "%s %s %s", buffer[0], buffer[1], buffer[2]) == 3) {
- for (size_t i = 0; i < 3; i++)
- {
- if (!check_str(buffer[i])) {
- coefficient[i] = atof(buffer[i]);
- }
- else {
- printf("Íåêêîðåêòíûé ââîä");
- return 1;
- }
- }
- }
- else {
- printf("Íåêêîðåêòíûé ââîä. ôàéëå äîëæíî áûòü 3 êîýôôèöèåíòà");
- return 1;
- }
- return 0;
- }
- int check_str(char* str) {
- int i = 0;
- int count_point = 0;
- if (str[0] == '.') {
- return 1;
- }
- if (str[0] == '-') {
- i++;
- }
- while (str[i] != '\0') {
- if ((str[i] >= 0x410 && str[i] <= 0x419) || !isdigit(str[i])) {
- if (str[i] == '.' && count_point <= 0) {
- count_point++;
- }
- else {
- return 1;
- }
- }
- i++;
- }
- return 0;
- }
- int count_coefficient(float* coefficient, float** coefficient_answer) {
- if (coefficient[1] == 0 && coefficient[2] == 0) {
- coefficient_answer[0] = 0;
- coefficient_answer[1] = 0;
- return 0;
- }
- else if (coefficient[1] == 0) {
- if (coefficient[2] * -1 / coefficient[0] < 0) {
- return 1;
- }
- else {
- (*coefficient_answer)[0] = sqrt(coefficient[2] * -1 / coefficient[0]);
- (*coefficient_answer)[1] = sqrt(coefficient[2] * -1 / coefficient[0]) * -1;
- return 0;
- }
- }
- else if (coefficient[2] == 0) {
- (*coefficient_answer)[0] = 0;
- (*coefficient_answer)[1] = coefficient[1] * -1 / coefficient[0];
- return 0;
- }
- else if (coefficient[0] == 0) {
- return 1;
- }
- else {
- float* temp_array = realloc((*coefficient_answer), sizeof(float) * 3);
- float D = pow(coefficient[1], 2) - 4 * coefficient[0] * coefficient[2];
- temp_array[0] = D;
- if (D < 0) {
- return 1;
- }
- else if (D == 0) {
- temp_array[1] = (coefficient[1] * -1) / (2 * coefficient[0]);
- *coefficient_answer = temp_array;
- return 2;
- }
- else {
- temp_array[1] = ((coefficient[1] * -1) + sqrt(D)) / (2 * coefficient[0]);
- temp_array[2] = ((coefficient[1] * -1) - sqrt(D)) / (2 * coefficient[0]);
- *coefficient_answer = temp_array;
- return 3;
- }
- }
- }
- int write_in_file(int result_code, float* coefficient_answer) {
- FILE* file_with_answer = fopen("answers.txt", "w");
- switch (result_code)
- {
- case 0:
- fprintf(file_with_answer, "Äàííîå óðàâíåíèå áûëî ðåøåíî áåç äèñêðèìèíàíòà\n");
- fprintf(file_with_answer, "Ïåðâûé êîðåíü - %.4f\nÂòîðîé êîðåíü - %.4f\n", coefficient_answer[0], coefficient_answer[1]);
- fclose(file_with_answer);
- break;
- case 1:
- fprintf(file_with_answer, "Óðàâíåíèå íå èìååò êîðíåé");
- fclose(file_with_answer);
- break;
- case 2:
- fprintf(file_with_answer, "äèñêðèìèíàíòà ðàâåí %.4f, ïîýòîìó ó óðàâíåíèÿ îäèí êîðåíü\nÊîðåíü - %.4f", coefficient_answer[0], coefficient_answer[1]);
- fclose(file_with_answer);
- break;
- case 3:
- fprintf(file_with_answer, "äèñêðèìèíàíòà ðàâåí %.4f\nÏåðâûé êîðåíü - %.4f\nÂòîðîé êîðåíü - %.4f", coefficient_answer[0], coefficient_answer[1],coefficient_answer[2]);
- fclose(file_with_answer);
- break;
- default:
- break;
- }
- }
- int main() {
- system("chcp 1251>null");
- FILE* file = fopen("coefficient.txt", "r");
- float* coefficient = malloc(sizeof(float) * 3);
- if (read_in_file(coefficient, file)) {
- return 1;
- }
- fclose(file);
- float* coefficient_answer = malloc(sizeof(float) * 2);
- int code = count_coefficient(coefficient, &coefficient_answer);
- write_in_file(code, coefficient_answer);
- }
|