Commit 7f8273ed by viakul

Update 2.c

parent b6d171c3
Showing with 150 additions and 23 deletions
......@@ -5,61 +5,190 @@
int findA(char* equation);
int findB(char* equation);
int findC(char* equation);
int findDiscriminant(int B, int C);
int result(int D,int B, int A);
int findDiscriminant(int B, int C, int A);
int userInput(char* string);
int IsNumber(char* string, int i);
int main()
{
char string[100];
int a;
int b;
int c;
int a,b,c,d;
int Bool = 1;
printf("Write your equation:\n");
while(Bool){
scanf("%s", string);
a= findA(string);
Bool = userInput(string);
if (Bool==1){
printf("please repeat make sure that your code meets the next form Ax^2+Bx+c, where a,b and c are integers\n");
}
}
a = findA(string);
printf("A value %d\n", a);
b= findB(string);
b = findB(string);
printf("B value %d\n", b);
c= findC(string);
printf("C value %d", c);
c = findC(string);
printf("C value %d\n", c);
d = findDiscriminant(b,c,a);
printf("the discriminant %d\n", d);
Bool = result(d, b, a);
return 0;
}
int result(int D,int B, int A){
double outpt;
if ( D < 0 ) {
printf("There is no solution, the D<0");
}else if (D == 0){
printf("there is only one solution\n");
outpt = -B/(2*A);
printf(" x = %fl", outpt);
}else{
printf("There are two solutions\n");
printf("The first:\n");
outpt = (-B-sqrt(D))/(2*A);
printf(" x = %f\n", outpt);
printf("The second:\n");
outpt = (-B+sqrt(D))/(2*A);
printf(" x = %f", outpt);
}
return(1);
}
int findDiscriminant(int B, int C, int A){
int D;
D = B*B-4*A*C;
return(D);
}
int findA(char* equation){
int findA(char* equation){
int i=0;
char A[100]={""};
char one[100]={"1"};
while(1){
if (((equation[i]>47)&&(equation[i]<58))||(equation[i]=='x')){
if (equation[i]=='x'){
break;
}
else
{
strcat(A, &equation[i]);
strncat(A, &equation[i], 1);
i++;
}
A[i]='\0';
}
if(((equation[0]=='-')&&(equation[1]=='x'))||(equation[0]=='x'))
{
strcat(A,&one[0]);
}
return atoi(A);
}
int IsNumber(char* string, int i){
int m;
m = 1;
if ((string[i]<58)&&(string[i]>47)){
m = 0;
}
return (m);
}
int userInput(char* string){
int i, bool;
bool = 0;
i = 0;
if ( (string[i] == '-') || ((string[i] < 58) && (string[i]) > 48) || (string[i] == 'x')) {
//printf("the First value is okay\n");
}else{
printf("you have a mistake in user input. ");
// printf("the first element is wrong\n");
bool=1;
}
if (string[i] == '-'){
i++;
//printf("the first sign is -\n");
}
while(i < strlen(string)-5){
if(string[i]=='x'){
break;
}
else if(IsNumber(string, i))
{
bool=1;
break;
}
}
i++;
}
if ((string[i+1] == '^')&&(string[i+2] == '2')&&((string[i+3] == '-')||(string[i+3] == '+')))
{
//printf("the first argument is okay\n");
}else{
//printf("the firstargument is wrong\n");
bool=1;
}
i = i+4;
while(i < string[i] - 2){
if(string[i]=='x'){
break;
}
else if(IsNumber(string, i))
{
// printf("the second argument is not good\n");
bool=1;
break;
}
i++;
}
if ((string[i+1] == '-')||(string[i+1] == '+')){
i++;
//printf("the sign is correct after x^2\n");
}else{
//printf("the sign is wrong\n");
bool=1;
}
i++;
//printf("%c\n", string[i]);
while(i<strlen(string)){
if (IsNumber(string, i)){
// printf("the third value is not good\n");
bool=1;
break;
}else{
//printf("%c", string[i]);
i++;
}
}
return(bool);
}
int findB(char* equation){
int i=0;
char one[100]={"1"};
char B[100]={""};
while(1){
if (equation[i]=='x'){
......@@ -78,6 +207,9 @@ int findB(char* equation){
i++;
}
i++;
if (equation[i]=='x'){
strncat(B, &one[0], 1);
}
while(1){
if (equation[i]=='x'){
break;
......@@ -109,11 +241,6 @@ int findC(char* equation){
}
i=i+1;
strcat(C, &equation[i]);
C[i]='\0';
return atoi(C);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment