Aktualizovat „du4/program.c“

This commit is contained in:
Bohdan Yanchyk 2020-04-02 23:23:43 +00:00
parent e8f15a894b
commit e93f2bcae5

View File

@ -1,5 +1,14 @@
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h> #include <stdio.h>
#include<string.h> #include<string.h>
#include<math.h>
#include<stdlib.h> #include<stdlib.h>
char* compactString(char *src,int leng) { char* compactString(char *src,int leng) {
char *new_str = (char*)calloc(leng+1,sizeof(char)); char *new_str = (char*)calloc(leng+1,sizeof(char));
@ -16,16 +25,34 @@ char* compactString(char *src,int leng) {
double operation(double first,double second,char c){ double operation(double first,double second,char c){
double x =0;
if(c=='*'){ if(c=='*'){
return first*second; x=first*second;
return x;
} }
else if(c =='-'){ else if(c =='-'){
return first-second; x=first-second;
return x;
} }
else if(c=='/'){ else if(c=='/'){
return first/second; x=first/second;
return x;
} }
return first+second; x= first+second;
return x;
}
double round_to(double value, double eps)
{
return floor(value/eps + 0.5) * eps;
}
double compare_percent(double a, double b, double eps)
{
double diff = round_to( (a - b) * 2 / (a + b), eps);
return diff < 0 ? -1 : diff > 0 ? +1 : 0;
} }
@ -36,13 +63,9 @@ int main()
memset(str,0,100); memset(str,0,100);
fgets(str,100,stdin); fgets(str,100,stdin);
char *new_str = compactString(str,strlen(str)); char *new_str = compactString(str,strlen(str));
//printf("%s\n",new_str);
double first =-5,second =-5,res=-5; double first =-5,second =-5,res=-5;
char c='E'; char c='E';
sscanf(new_str,"%lf%c%lf%*[=]%lf",&first,&c,&second,&res); sscanf(new_str,"%lf%c%lf%*[=]%lf",&first,&c,&second,&res);
// printf("%lf %lf %lf",first,second,res);
if(c=='E'||first==-5||second==-5||res==-5){ if(c=='E'||first==-5||second==-5||res==-5){
@ -50,7 +73,7 @@ int main()
return 0; return 0;
} }
double my_res = operation(first,second,c); double my_res = operation(first,second,c);
if(my_res==res){ if(compare_percent(res,my_res,0.01)==0){
printf("OK\n"); printf("OK\n");
} }
else{ else{