#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
start: ;
int const n=21;
int A[n]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int B[n]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int C[n]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int m=1,i,j,k;
char ch,opr=';',l,ans_CO,ans_RE;
system("cls");
printf("\n\nEnter Number , Oprator , next number and then enter '=' :\n\n");
En_Num_1: ;
ch=getche(); // enter - + or one number
if(ch=='-')
A[0]=1;
else if(ch=='+')
A[0]=0;
else if(ch>='0'&&ch<='9')
{
for(j=0,l=48;l<58;l++)
{
if(ch==l)
{
A[m]=j;
m++;
}
j++;
}
}
else if(ch=='\b')
{
goto En_Num_1;
}
else
{
printf("\n\nNumber is not valid !!");
printf("\n\nEnter any key to start program again ");
getch();
goto start;
}
for(i=m;i<n;) // enter number 1
{
ch=getche();
if(ch>='0'&&ch<='9') // take it
{
for(j=0,l=48;l<58;l++)
{
if(ch==l)
{
A[i]=j;
i++;
}
j++;
}
}
else if(ch=='*')// May be Oprator typed !
{
opr=ch;
printf("\b %c",opr);
break;
}
else if(ch=='\b') // Backspace
{
printf(" ");
printf("\b");
i--;
A[i]=0;
}
else // error !
{
printf("\n\nNumber is not valid !!");
printf("\n\nEnter any key to start program again ");
getch();
goto start;
}
if(i==0)
goto En_Num_1;
}
if(i!=n) // clean Array A with number 1
{
for(j=i;j>0;j--)
A[j+(n-i)]=A[j];
for(j=1;j<n-i+1;j++)
A[j]=0;
}
Enter_OP : ;
if(opr==';')
{
opr=getche(); // Scan oprator
if(opr=='*')
{
printf("\b %c",opr);
}
else // Oprator not valid
{
printf("\n\nOprator is not valid !!");
printf("\n\nEnter any key to start program again ");
getch();
goto start;
}
}
// Number 2 start
m=1;
printf(" (");
En_Num_2: ;
ch=getche(); // enter - + or one number
if(ch=='-')
B[0]=1;
else if(ch=='+')
B[0]=0;
else if(ch>='0'&&ch<='9')
{
for(j=0,l=48;l<58;l++)
{
if(ch==l)
{
B[m]=j;
m++;
}
j++;
}
}
else if(ch=='\b')
{
printf("(");
goto En_Num_2 ;
}
else // error !
{
printf("\n\nNubmer is not valid !!!");
printf("\n\nEnter any key to start program again ");
getch();
goto start;
}
for(i=m;i<n;) // enter number 2
{
ch=getche();
if(ch>='0'&&ch<='9') // take it
{
for(j=0,l=48;l<58;l++)
{
if(ch==l)
{
B[i]=j;
i++;
}
j++;
}
}
else if(ch=='\b') // Backspace
{
i--;
if(i==0)
{
if(temp==1)
{
printf(" \b");
goto En_Num_2;
}
else if(temp==0)
{
printf("(");
goto En_Num_2;
}
}
B[i]=0;
printf(" ");
printf("\b");
}
else if(ch=='=')
{
printf("\b) = ");
break;
}
else // error !
{
printf("\n\nNubmer is not valid !!!");
printf("\n\nEnter any key to start program again ");
getch();
goto start;
}
if(i==0)
goto En_Num_2;
}
if(i==n)
{
printf(") = ");
}
if(i!=n) // clean Array B with number 1
{
for(j=i;j>0;j--)
B[j+(n-i)]=B[j];
for(j=1;j<n-i+1;j++)
B[j]=0;
}
switch( opr )
{
case '*': // Oprator = *
{
C[n-1]=A[n-1]*B[n-1]; // * number 1
C[n-2]=A[n-2]*B[n-1]+A[n-1]*B[n-2]; // * number 2
if(C[n-1]>9) // if number 1 > 9
{
C[n-2]=C[n-2]+C[n-1]/10;
C[n-1]=C[n-1]%10;
}
if(C[n-2]>9) // if number 2 > 9
{
C[n-3]=C[n-3]+C[n-2]/10;
C[n-2]=C[n-2]%10;
}
for(i=n-3;i>0;i--) // * numbers !
{
for(j=0;j<=n-i-1&&i-j>=0;j++)
{
C[i]=C[i]+A[i+j]*B[n-1-j]; // *
}
if(C[1]>9) // lorger than 20 number
{
printf("\n\n\t\tThe number is too large !");
goto Ask;
}
if(C[i]>=10) // if * > 9
{
C[i-1]=C[i-1]+C[i]/10;
C[i]=C[i]%10;
}
}
if(A[0]==1&&B[0]==0||A[0]==0&&B[0]==1) // C + or - ?
{
C[0]=1;
}
break;
}
default :
{
printf("\n\nYou can only use + & -");
printf("\n\nEnter any key to continue program from start it ");
getch();
goto start;
}
}
if(C[0]==0)
{
printf("+");
}
if(C[0]==1)
{
printf("-");
}
for(i=1;i<n;i++)
printf("%d",C[i]);
}