submition8
This commit is contained in:
parent
52a3b7325d
commit
27d34610ae
106
a1/program.c
106
a1/program.c
@ -62,17 +62,19 @@ int reading_strings_and_storing_them_in_memory(link* pointer_to_start_of_chain){
|
|||||||
return counter;
|
return counter;
|
||||||
}
|
}
|
||||||
int analizing_string(char* pointer){
|
int analizing_string(char* pointer){
|
||||||
char token1[SIZE];
|
char first_number[SIZE];
|
||||||
char token2[SIZE];
|
char second_number[SIZE];
|
||||||
char token3[SIZE];
|
char result_nubmer[SIZE];
|
||||||
|
|
||||||
float number1, number2, result3;
|
|
||||||
char buffer_for_token[SIZE];
|
|
||||||
strcpy(buffer_for_token,pointer);
|
|
||||||
int counter=0;
|
|
||||||
char operant;
|
|
||||||
int i=0;
|
|
||||||
|
|
||||||
|
memset(first_number, 0, SIZE);
|
||||||
|
memset(second_number, 0, SIZE);
|
||||||
|
memset(result_nubmer, 0, SIZE);
|
||||||
|
|
||||||
|
float number1, number2, result3;
|
||||||
|
char operant;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
int counter=0;
|
||||||
while(pointer[i]!='\0'){
|
while(pointer[i]!='\0'){
|
||||||
if(pointer[i]=='+'||pointer[i]=='/'||pointer[i]=='*'||pointer[i]=='-'){
|
if(pointer[i]=='+'||pointer[i]=='/'||pointer[i]=='*'||pointer[i]=='-'){
|
||||||
operant=pointer[i];
|
operant=pointer[i];
|
||||||
@ -84,36 +86,51 @@ int analizing_string(char* pointer){
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
char delim[] =" +=-*/\n";
|
i = 0;
|
||||||
|
// Look for the start of the first number.
|
||||||
|
i = skip_spaces(i, pointer);
|
||||||
|
|
||||||
char *first_number_cucumber=strtok(buffer_for_token, delim);
|
// Copy first number to its destination.
|
||||||
if(first_number_cucumber==NULL) {//||is_number_question_mark(first_number_cucumber)==0){
|
i = copy_number(first_number, pointer, i);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
strcpy(token1, first_number_cucumber);
|
|
||||||
|
|
||||||
char *second_number=strtok(NULL, delim);
|
// Look for the start of the operation;
|
||||||
if(second_number==NULL) { //||is_number_question_mark(second_number)==0){
|
i = skip_spaces(i, pointer);
|
||||||
return -1;
|
// Processing operation.
|
||||||
}
|
operant = pointer[i];
|
||||||
strcpy(token2, second_number);
|
i++;
|
||||||
|
if (operant != '+' && operant != '-' && operant != '*' && operant != '/') {
|
||||||
|
|
||||||
char *result_token=strtok(NULL, delim);
|
|
||||||
if(result_token==NULL) { //||is_number_question_mark(result)==0){
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
strcpy(token3, result_token);
|
|
||||||
|
|
||||||
char *token = strtok(NULL, delim);
|
|
||||||
if(token!=NULL){
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Look for the start of the second number;
|
||||||
|
i = skip_spaces(i, pointer);
|
||||||
|
|
||||||
number1=atof(token1);
|
// Copy second number to its destination.
|
||||||
number2=atof(token2);
|
i = copy_number(second_number, pointer, i);
|
||||||
result3=atof(token3);
|
|
||||||
|
// Look for the start of the "=";
|
||||||
|
i = skip_spaces(i, pointer);
|
||||||
|
// Processing "="
|
||||||
|
if (pointer[i] != '=') {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
// Look for the start of the result number;
|
||||||
|
i = skip_spaces(i, pointer);
|
||||||
|
|
||||||
|
// Copy result number to its destination.
|
||||||
|
i = copy_number(result_nubmer, pointer, i);
|
||||||
|
|
||||||
|
// Look for the start of the "\n";
|
||||||
|
i = skip_spaces(i, pointer);
|
||||||
|
if ('\n' != pointer[i]) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
number1=atof(first_number);
|
||||||
|
number2=atof(second_number);
|
||||||
|
result3=atof(result_nubmer);
|
||||||
|
|
||||||
if(operant=='+' &&result3==number1+number2){
|
if(operant=='+' &&result3==number1+number2){
|
||||||
return 1;
|
return 1;
|
||||||
@ -130,6 +147,27 @@ int analizing_string(char* pointer){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int skip_spaces(int i, char *pointer) {
|
||||||
|
while(pointer[i]==' '){
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int copy_number(char number[], char *pointer, int i) {
|
||||||
|
int destination_i = 0;
|
||||||
|
while(pointer[i]!='\0'){
|
||||||
|
if (!isdigit(pointer[i])&&pointer[i]!='.'&&pointer[i]!=',') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
number[destination_i] = pointer[i];
|
||||||
|
destination_i++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
/*int is_number_question_mark(char *pointer){
|
/*int is_number_question_mark(char *pointer){
|
||||||
for(int i=0; pointer[i]!='\0'; i++){
|
for(int i=0; pointer[i]!='\0'; i++){
|
||||||
|
Loading…
Reference in New Issue
Block a user