Aktualizovat du3/program.c
This commit is contained in:
parent
a70d3c2608
commit
bfc1f13969
114
du3/program.c
114
du3/program.c
@ -1,6 +1,118 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define SIZE 128
|
||||||
|
|
||||||
|
//kontrola, ze ci vycitane informacie su aritmeticky spravne
|
||||||
|
bool IsArithmeticallyCorrect(int num1, int num2, char* op)
|
||||||
|
{
|
||||||
|
if (num2 ==)
|
||||||
|
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case '-'
|
||||||
|
if ((num1-num2) == resultTemp) { return true;}
|
||||||
|
|
||||||
|
case '+'
|
||||||
|
if ((num1-num2) == resultTemp) { return true;}
|
||||||
|
|
||||||
|
case '*'
|
||||||
|
if ((num1*num2) == resultTemp) { return true;}
|
||||||
|
|
||||||
|
case '/'
|
||||||
|
if ((num1-num2) == resultTemp) { return true;}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
|
||||||
|
//ak dopasovany arit. operator NIE je platny (resp ide o systematicku chybu, nie aritmeticku), vrati 0
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
char line[SIZE];
|
||||||
|
line[0] = '\0';
|
||||||
|
char strBuffer[SIZE];
|
||||||
|
|
||||||
|
|
||||||
|
while (fgets(line, SIZE, stdin) != NULL)
|
||||||
|
{
|
||||||
|
if (sscantf(line, "%127[^\n]", strBuffer) == 1)
|
||||||
|
{
|
||||||
|
bool isLineInvalid = false;
|
||||||
|
int x = 0;
|
||||||
|
//docasne 'char' premeny
|
||||||
|
char num1Temp[10], num2Temp[10], resultTemp[10];
|
||||||
|
num1Temp[0] = '\0';
|
||||||
|
num2Temp[0] = '\0';
|
||||||
|
resultTemp[0] = '\0';
|
||||||
|
int writeIntoArrayNum = 1; // kontrolna premena na urcovanie toho, kde sa ma zapisovat obsah (1 == num1, 2 = num2, 3 = vysledok)
|
||||||
|
|
||||||
|
char op1 = 'N'; //N = 'nothing/nic'
|
||||||
|
|
||||||
|
for (int i = 0; i != '\0'; i++)
|
||||||
|
{
|
||||||
|
if (isdigit(i) != 0)
|
||||||
|
{
|
||||||
|
//skontroluj, ze ci char po nom je tiez cislo, AK NIE, povazuj buduce cisla v riadku ako ine cislo
|
||||||
|
// a inkrementuj kontrolnu citaciu premenu
|
||||||
|
if (isdigit(i+1) != 0)
|
||||||
|
{
|
||||||
|
writeIntoArrayNum += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (writeIntoArrayNum)
|
||||||
|
case 1:
|
||||||
|
strncat(num1Temp, i, 1);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
strncat(num2Temp, i, 2);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
strncat(resultTemp, i, 3);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (ispunct() != 0)
|
||||||
|
{
|
||||||
|
//ak je to nejaky printable symbol ('*/-,:./), skontroluj ci to nie je artimeticke znamienko,
|
||||||
|
//AK NIE, povazuj za riadok neplatny...
|
||||||
|
if (i == '+' || i == '-' || i == '/' i == '*')
|
||||||
|
{
|
||||||
|
op1 = i;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isLineInvalid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isalpha(i) != 0)
|
||||||
|
{
|
||||||
|
isLineInvalid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int vysledok = calcResult(atoi(num1Temp),atoi(num2Temp), op1);
|
||||||
|
|
||||||
|
|
||||||
|
if (isLineInvalid || vysledok == 0)
|
||||||
|
{
|
||||||
|
printf("CHYBA");
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user