pvjc22/du4/program.c

188 lines
5.5 KiB
C
Raw Normal View History

2022-03-29 10:25:20 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2022-03-31 06:02:11 +00:00
#include <math.h>
2022-03-31 08:05:03 +00:00
#include <ctype.h>
2022-03-29 10:25:20 +00:00
2022-03-29 10:30:01 +00:00
2022-03-31 08:20:05 +00:00
int main() {
int i,j;
2022-03-31 08:07:17 +00:00
double number1, number2, summary,msum;
2022-03-31 08:05:03 +00:00
char operand, equ;
2022-03-31 08:07:17 +00:00
float rsum=0;
2022-03-31 08:20:05 +00:00
/* char input[1000];
2022-03-31 08:05:03 +00:00
char num_str[30] = {0};
float numbers[3];
fgets(input, 999, stdin);
for (int i = 0, j =0; i < strlen(input); i++) {
char c = input[i];
if (c==' ') continue;
if (c=='+') operand='+';
if (c=='-') operand='-';
if (c=='*') operand='*';
if (c=='/') operand='/';
if (c=='\n') break;
if (isdigit(c)) {num_str[j++] = c - '0';}
else if (j > 0) {
j = 0;
numbers[...] = atof(num_str);
}
}
}
// scanf("%lf", &number1);
//scanf("%lf", &number2);
//scanf("%lf",&msum);
2022-03-31 06:02:11 +00:00
2022-03-31 08:05:03 +00:00
*/
char* input = calloc(1000, 1);
while (1) {
2022-03-31 08:20:05 +00:00
2022-03-31 08:05:03 +00:00
input = fgets(input, 1000, stdin);
if (input == NULL) break;
fflush(stdin);
if (input[0] == '\n' || input[0] == EOF || strlen(input) <= 0)
break;
2022-03-31 08:20:05 +00:00
float values[3] = {0};
2022-03-31 08:05:03 +00:00
char operation = 0;
2022-03-31 08:20:05 +00:00
2022-03-31 08:05:03 +00:00
int k = 0;
int num = strlen(input);
for (int i = 0; i < num; i++) {
if (input[i] == ' ')
continue;
if (isalpha(input[i]))
break;
2022-03-31 08:20:05 +00:00
2022-03-31 08:05:03 +00:00
if (i < strlen(input) && (isdigit(input[i]) || input[i] == '.' || (input[i] == '-' && isdigit(input[i + 1])))) {
char temp[100] = {0};
for (int j = 0; i < strlen(input) && (isdigit(input[i]) || input[i] == '.' || input[i] == '-'); j++) {
temp[j] = input[i++];
}
i--;
values[k++] = (float)atof(temp);
} else if (isdigit(input[i]) == 0 && input[i] != ' ' && input[i] != '\n' && input[i] != '=') {
operation = input[i];
}
}
2022-03-31 08:20:05 +00:00
2022-03-31 08:05:03 +00:00
if (k != 3) {
printf("CHYBA\n");
continue;
}
if (operation == 0 && values[1] < 0) {
operation = '-';
values[1] = abs(values[1]);
}
2022-03-31 08:20:05 +00:00
float result = 0;
switch (operation) {
case '+': {
result = values[0] + values[1];
break;
}
case '-': {
result = values[0] - values[1];
break;
}
case '/': {
result = values[0] / values[1];
break;
}
case '*': {
result = values[0] * values[1];
break;
}
default: {
printf("CHYBA\n");
continue;
}
}
char result_string[50];
sprintf(result_string, "%.2f", result);
result = atof(result_string);
if (result == values[2])
printf("OK\n");
else
printf("ZLE\n");
}
if((operand == '/') && (number2 == 0)){
2022-03-31 06:02:11 +00:00
printf("Nulou sa ned<65> deli<6C>");
2022-03-29 10:30:01 +00:00
}
2022-03-31 06:02:11 +00:00
if(operand !='+' && operand !='-' && operand !='*' && operand !='/'){
return 1;
printf("\n");
2022-03-31 08:05:03 +00:00
}
2022-03-31 08:20:05 +00:00
//msum=values[2];
if(operand=='+'){
// rsum=values[0]+values[1];
2022-03-31 08:10:11 +00:00
char rmsum[50];
sprintf(rmsum, "%.2f", msum);
msum = atof(rmsum);
2022-03-31 06:02:11 +00:00
if(msum==rsum){
2022-03-31 08:20:05 +00:00
//printf("OK\n");
2022-03-29 10:30:01 +00:00
}
2022-03-31 06:02:11 +00:00
else{
2022-03-31 08:20:05 +00:00
//printf("ZLE\n");
2022-03-31 06:02:11 +00:00
}
2022-03-31 08:05:03 +00:00
//printf("%lf\n",rsum);
2022-03-31 06:02:11 +00:00
}
2022-03-31 08:20:05 +00:00
else if(operand=='-'){
// rsum=values[0]-values[1];
2022-03-31 08:10:11 +00:00
char rmsum[50];
sprintf(rmsum, "%.2f", msum);
msum = atof(rmsum);
2022-03-31 06:02:11 +00:00
if(msum==rsum){
2022-03-31 08:20:05 +00:00
//printf("OK\n");
2022-03-31 06:02:11 +00:00
}
else{
2022-03-31 08:20:05 +00:00
//printf("ZLE\n");
2022-03-31 06:02:11 +00:00
}
2022-03-31 08:05:03 +00:00
//printf("%lf\n",rsum);
2022-03-31 06:02:11 +00:00
}
2022-03-31 08:20:05 +00:00
else if(operand=='*'){
// rsum=values[0]*values[1];
2022-03-31 08:10:11 +00:00
char rmsum[50];
sprintf(rmsum, "%.2f", msum);
msum = atof(rmsum);
2022-03-31 06:02:11 +00:00
if(msum==rsum){
2022-03-31 08:20:05 +00:00
//printf("OK\n");
2022-03-31 06:02:11 +00:00
}
else{
2022-03-31 08:20:05 +00:00
//printf("ZLE\n");
2022-03-31 06:02:11 +00:00
}
2022-03-31 08:05:03 +00:00
//printf("%lf\n",rsum);
2022-03-31 06:02:11 +00:00
}
2022-03-31 08:20:05 +00:00
else if(operand=='/'){
// rsum=values[0]/values[1];
2022-03-31 08:10:11 +00:00
char rmsum[50];
sprintf(rmsum, "%.2f", msum);
msum = atof(rmsum);
2022-03-31 06:02:11 +00:00
if(msum==rsum){
2022-03-31 08:20:05 +00:00
//printf("OK\n");
2022-03-31 06:02:11 +00:00
}
else{
2022-03-31 08:20:05 +00:00
//printf("ZLE\n");
2022-03-31 06:02:11 +00:00
}
2022-03-31 08:05:03 +00:00
//printf("%lf\n",rsum);
}
2022-03-31 08:20:05 +00:00
free(input);
return 0;
}