Update 'du4/program.c'

This commit is contained in:
Anzhelika Nikolaieva 2023-03-17 20:45:13 +00:00
parent 7eebc85a6b
commit b21597ee93

View File

@ -1,93 +1,91 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <math.h> #include <math.h>
int Program() { int main() {
static void Main(string[] args) { int ch = getchar();
int ch = Console.Read(); char priklad[300];
string priklad = ""; int count = 0;
int count = 0;
while (true) { for (int idx = 0; 1; idx++) {
priklad += (char)ch; priklad[idx] = ch;
if (ch == '\n') { if (ch == '\n') {
count++; count++;
}
ch = getchar();
if ((priklad[idx] == '\n' && ch == '\n') || ch == EOF) {
break;
}
}
char *start = priklad, *end = NULL;
for (int c = 0; c < count; c++) {
int skip = 0, z = 0;
for (int idx = 0; start[idx] != '\n'; idx++) {
if (start[idx] == '+' || start[idx] == '-' || start[idx] == '*' || start[idx] == '/') {
z = 1;
} }
ch = Console.Read(); if ((start[idx] < '0' || start[idx] > '9') && start[idx] != ' ' && start[idx] != '=' && start[idx] != '+' && start[idx] != '-' && start[idx] != '*' && start[idx] != '/' && start[idx] != '.') {
if ((priklad[priklad.Length - 1] == '\n' && ch == '\n') || ch == -1) { skip = 1;
printf("CHYBA\n");
while ((*start) != '\n') {
start++;
}
start++;
break; break;
} }
} }
string start = priklad; if (skip == 1) {
string end = null; continue;
}
if (z == 0) {
while ((*start) != '\n') {
start++;
}
start++;
printf("CHYBA\n");
continue;
}
float num1 = strtof(start, &end);
if (end == start) {
printf("KONIEC\n");
}
while ((*start) != '+' && (*start) != '-' && (*start) != '/' && (*start) != '*') {
start++;
}
char znak = (*start);
start++;
for (int c = 0; c < count; c++) { float num2 = strtof(start, &end);
int skip = 0, z = 0; start = end;
while (((*start) < '0' || (*start) > '9') && (*start) != '-') {
start++;
}
for (int idx = 0; start[idx] != '\n'; idx++) { float vysledok2 = strtof(start, &end);
if (start[idx] == '+' || start[idx] == '-' || start[idx] == '*' || start[idx] == '/') { start = end;
z = 1; start++;
} float vysledok = 0;
if ((start[idx] < '0' || start[idx] > '9') && start[idx] != ' ' && start[idx] != '=' && start[idx] != '+' && start[idx] != '-' && start[idx] != '*' && start[idx] != '/' && start[idx] != '.') {
skip = 1;
Console.WriteLine("CHYBA");
while (start[0] != '\n') {
start = start.Substring(1);
}
start = start.Substring(1);
break;
}
}
if (skip == 1) { if (znak == '-') {
continue; vysledok = num1 - num2;
} } else if (znak == '+') {
if (z == 0) { vysledok = num1 + num2;
while (start[0] != '\n') { } else if (znak == '*') {
start = start.Substring(1); vysledok = num1 * num2;
} } else if (znak == '/') {
start = start.Substring(1); vysledok = num1 / num2;
Console.WriteLine("CHYBA"); }
continue; vysledok = (round(vysledok * 100)) / 100;
} if (vysledok2 - vysledok < 0.001 && vysledok2 - vysledok > -0.001) {
float num1 = float.Parse(start, out end); printf("OK\n");
if (end == start) { } else {
Console.WriteLine("KONIEC"); printf("ZLE\n");
}
while (start[0] != '+' && start[0] != '-' && start[0] != '/' && start[0] != '*') {
start = start.Substring(1);
}
char znak = start[0];
start = start.Substring(1);
float num2 = float.Parse(start, out end);
start = end;
while ((start[0] < '0' || start[0] > '9') && start[0] != '-') {
start = start.Substring(1);
}
float vysledok2 = float.Parse(start, out end);
start = end;
start = start.Substring(1);
float vysledok = 0;
if (znak == '-') {
vysledok = num1 - num2;
} else if (znak == '+') {
vysledok = num1 + num2;
} else if (znak == '*') {
vysledok = num1 * num2;
} else if (znak == '/') {
vysledok = num1 / num2;
}
vysledok = (float)Math.Round(vysledok * 100) / 100;
if (Math.Abs(vysledok2 - vysledok) < 0.001) {
Console.WriteLine("OK");
} else {
Console.WriteLine("ZLE");
}
} }
} }
return EXIT_SUCCESS;
} }