pvjc23/du4/program.c

93 lines
2.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int Program() {
static void Main(string[] args) {
int ch = Console.Read();
string priklad = "";
int count = 0;
while (true) {
priklad += (char)ch;
if (ch == '\n') {
count++;
}
ch = Console.Read();
if ((priklad[priklad.Length - 1] == '\n' && ch == '\n') || ch == -1) {
break;
}
}
string start = priklad;
string 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;
}
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) {
continue;
}
if (z == 0) {
while (start[0] != '\n') {
start = start.Substring(1);
}
start = start.Substring(1);
Console.WriteLine("CHYBA");
continue;
}
float num1 = float.Parse(start, out end);
if (end == start) {
Console.WriteLine("KONIEC");
}
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");
}
}
}
}