چنتا باگه دیگه داشت که برطرف شد !
حالا تو ضرب اعداد دو رقم آخر رو اشتباه میده ! فقط هم دو رقم آخر !!!! هنوز نفهمیدم برا چی ؟!
اینم سورس جدید :
کد:// matris.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
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,temp=0,R[n],q=0;
char ch,opr=';',l,ans_CO,ans_RE;
// clrscr();
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=='-') // if char = -
{
A[0]=1;
temp=1;
}
else if(ch=='+') // if char = +
{
A[0]=0;
temp=1;
}
else if(ch>='0'&&ch<='9') // if char = number
{
for(j=0,l=48;l<58;l++)
{
if(ch==l) // check number
{
A[m]=j;
m++;
}
j++;
}
}
else if(ch=='\b') // if enter backspace
{
goto En_Num_1;
}
else // error
{
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++) // check it
{
if(ch==l)
{
A[i]=j;
i++;
}
j++;
}
}
else if(ch=='-'||ch=='+'||ch=='/'||ch=='*') // May be Oprator typed !
{
opr=ch;
printf("\b %c",opr);
break;
}
else if(ch=='\b') // if enter 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) // when Enter Backspace may be all of number deleted
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 : ; // Take Oprator
if(opr==';') // may oprator taked on number 1
{
opr=getche(); // Scan oprator
if(opr=='+'||opr=='-'||opr=='/'||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
printf(" (");
En_Num_2: ;
temp=0;
m=1;
ch=getche(); // enter - + or one number
if(ch=='-') // if enter -
{
B[0]=1;
temp=1; // for backspace
}
else if(ch=='+') // if enter +
{
B[0]=0;
temp=1; // for backspace
}
else if(ch>='0'&&ch<='9') //if enter number
{
for(j=0,l=48;l<58;l++)
{
if(ch==l) // check number
{
B[m]=j;
m++;
}
j++;
}
}
else if(ch=='\b') // if enter Backspace
{
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;
}
En_Num_2_2 : ;
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++) // check number
{
if(ch==l)
{
B[i]=j;
i++;
}
j++;
}
}
else if(ch=='\b') // if enter Backspace
{
i--;
if(i==0) // if all numbers deleted
{
if(temp==1) // check - or + typed or not !
{ // if - or + typed :
printf(" \b");
goto En_Num_2;
}
else if(temp==0) // if type only numbers
{
printf("(");
goto En_Num_2;
}
}
B[i]=0; // number > 0
printf(" ");
printf("\b");
}
else if(ch=='=') // when enter = type result
{
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==n) // if all of 20 numbers typed
{
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;
}
if(opr=='/') // if oprator = / check number B
{ // if number B = 0 type " is imposible ! "
for(i=1;i<n;i++)
{
if(B[i]==0)
{
if(i==n-1)
{
printf("\n\nIts Imposble !!!\nTry again !");
printf("\n\nPress any key to restart program :");
getch();
goto start;
}
continue;
}
else if(B[i]!=0)
break;
}
}
switch( opr ) // What is Oprator ?
{
case'+': // OPR = +
{
if(A[0]==0&&B[0]==0||A[0]==1&&B[0]==1) // if Numbers > 0 or < 0
{
for(i=n-1;i>0;i--)
{
if(A[i]+B[i]>=10)
{
if(A[1]+B[1]>9) // Maybe number larger than 20 numbers
{
C[1]=A[1]+B[1];
printf("\b\b \n\n\t\t * Number is too large *");
goto Ask ;
}
C[i]=(A[i]+B[i])%10;
A[i-1]++;
}
else
{
C[i]=A[i]+B[i];
}
}
if(A[0]==1&&B[0]==1) // if Numbers < 0 => Result number is -
{
C[0]=1;
}
}
if(A[0]==0&&B[0]==1||A[0]==1&&B[0]==0) // if Number 1 > 0 & Number 2 < 0
{
for(i=1;i<n;i++) // check A > B or A < B ???
{
if(A[i]==B[i])
continue;
else if(A[i]>B[i]) // if A > B
{
for(j=n-1;j>0;j--)
{
if(A[j]-B[j]<0) // if A[i] - B[i] > 10 => A[i-1]-- & A[i]+10
{
C[j]=A[j]+10-B[j];
k=1;
while(A[j-k]==0)
{
A[j-k]=9;
k++;
continue;
}
A[j-k]--;
}
else // if A - B < 10
{
C[i]=A[i]-B[i];
}
}// for(i=n-1;i>0;i--)
C[0]=A[0];
break;
}// else if(A[i]>B[i])
else // if B > A
{
for(i=n-1;i>0;i--)
{
if(B[i]<A[i]) // if B[i] - A[i] > 10 => B[i-1]-- & B[i]+10
{
C[i]=B[i]+10-A[i];
k=1;
while(B[i-k]==0)
{
B[i-k]=9;
k++;
continue;
}
B[i-k]--;
}
else // if A - B < 10
{
C[i]=B[i]-A[i];
}
}
C[0]=B[0];
break;
}// else
}// for(i=1;i<n;i++)
}
break;
}// Case '+'
case '-': // OPR = -
{
if(A[0]==0&&B[0]==1||A[0]==1&&B[0]==0) // if A > 0 & B < 0 or A < 0 & B > 0
{
for(i=n-1;i>0;i--)
{
if(A[i]+B[i]>=10) // if A + B > 9
{
if(A[1]+B[1]>9) // Maybe number larger than 20 numbers
{
C[1]=A[1]+B[1];
printf("\b\b \n\n\t\t * Number is too large *");
goto Ask ;
}
C[i]=(A[i]+B[i])%10;
A[i-1]++;
}
else // if A + B < 10
{
C[i]=A[i]+B[i];
}
}
if(A[0]==1&&B[0]==0) // if A < 0 & B > 0
{
C[0]=1;
}
}
if(A[0]==0&&B[0]==0||A[0]==1&&B[0]==1) // if 2 numbers < 0 or > 0
{
for(i=1;i<n;i++) // A > B or A < B ???
{
if(A[i]==B[i])
continue;
else if(A[i]>B[i]) // if A > B
{
for(j=n-1;j>0;j--)
{
if(A[j]-B[j]<0) // if A[j] < B[j] => A[i-1]-- & A[i]+10
{
C[j]=A[j]+10-B[j];
k=1;
while(A[j-k]==0)
{
A[j-k]=9;
k++;
continue;
}
A[j-k]--;
}
else // if A[j] - B[j] < 10
{
C[j]=A[j]-B[j];
}
}// for(i=n-1;i>0;i--)
C[0]=A[0];
break;
}// else if(A[i]>B[i])
else // if B > A
{
for(i=n-1;i>0;i--)
{
if(B[i]<A[i]) // if B[j] < A[j] => B[i-1]-- & B[i]+10
{
C[i]=B[i]+10-A[i];
k=1;
while(A[i-k]==0)
{
B[i-k]=9;
k++;
continue;
}
B[i-k]--;
}
else // if B[j] - A[j] < 10
{
C[i]=B[i]-A[i];
}
}
C[0]=B[0];
break;
}// else
}// for(i=1;i<n;i++)
}
break;
}
case '/' : // OPR = /
{
for(i=0;i<n;i++) // A -> R
{
R[i]=A[i];
}
Case : ;
i=1;
for(i=1;i<n;i++) // R > B or R < B ???
{
if(R[i]==B[i])
{ // if R = B & i = n-1
if(i==n-1)
{
for(j=n-1;j>0;j--)
{
if(R[j]-B[j]<0) // if R[j] < B[j] => R[j-1]-- & R[j]+10
{
R[j]=R[j]+10-B[j];
k=1;
while(R[j-k]==0)
{
R[j-k]=9;
k++;
continue;
}
R[j-k]--;
}
else // if R[j] - B[j] < 10
{
R[j]=R[j]-B[j];
}
}// for(j=n-1;j>0;j--)
q++;
// continue;
}
}
else if(R[i]>B[i]) // if R > B
{
for(j=n-1;j>0;j--)
{
if(R[j]-B[j]<0) // if A[j] < B[j] => A[j-1]-- & A[j]+10
{
R[j]=R[j]+10-B[j];
k=1;
while(R[j-k]==0)
{
R[j-k]=9;
k++;
continue;
}
R[j-k]--;
}
else if(R[j]-B[j]>=0) // if R[j] > B[j]
{
R[j]=R[j]-B[j];
}
}// for(j=n-1;j>0;j--)
q++;
}// else if(A[i]>B[i])
else // if B > R break !
{
break;
}// else
}// for(i=1;i<n;i++)
for(i=1;i<n;i++) // when - finished ? ( R < B )
{
if(R[i]==B[i]) // if R = B & i = n-1 ( R =B )
{
if(i==n-1)
goto Case;
continue;
}
else if(R[i]>B[i]) // if R > B => continue -
goto Case;
else if(R[i]<B[i]) // if R < B => Break
break;
}
break;
}
case '*': // OPR = *
{
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
// printf("\n\n%d * %d = %d",A[n-1],B[n-1],C[n-1]); //Preview
// printf("\n%d * %d + %d * %d = %d",A[n-2],B[n-1],A[n-1],B[n-2],C[n-2]); //Preview
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;
}
// printf("\nC[n-1] = %d \nC[n-2] = %d \nC[n-3] = %d",C[n-1],C[n-2],C[n-3]); //Preview
for(i=n-3;i>0;i--) // * numbers !
{
for(j=0;j<=n-i-1&&i-j>=0;j++)
{
// printf("\n%d + %d * %d = ",C[i],A[i+j],B[n-1-j]); //Preview
C[i]=C[i]+A[i+j]*B[n-1-j]; // *
// printf("%d",C[i]); //Preview
}
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
{
// printf("\n%d + %d/10 = ",C[n-1],C[i]); //Preview
C[i-1]=C[i-1]+C[i]/10;
// printf("%d",C[i-1]); //Preview
// printf("\n%d % 10 = ",C[i]); //Preview
C[i]=C[i]%10;
// printf("%d",C[i]); //Preview
}
// printf("\n");
}
if(A[0]==1&&B[0]==0||A[0]==0&&B[0]==1) // C + or - ?
{
C[0]=1;
}
break;
}
default : // if Oprator is not - + / or *
{
printf("\n\nYou can only use + & -");
printf("\n\nEnter any key to continue program from start it ");
getch();
goto start;
}
}
if(opr=='+'||opr=='-'||opr=='*') // Print Result for + & - & *
{
if(C[0]==0)
{
printf("+");
}
if(C[0]==1)
{
printf("-");
}
for(i=1;i<n;i++)
printf("%d",C[i]);
}
else if(opr=='/') // Print Result for /
{
printf("\n\nR is : ");
if(A[0]==0&&B[0]==1||A[0]==1&&B[0]==0) // Print + or - for R
printf("-");
else
printf("+");
for(i=1;i<n;i++) // Print R
printf("%d",R[i]);
printf("\nq is : %d",q); // Print Q
}
// * IF ANS IS NOT n & y ASK AGAIN *
Ask : ; // Ask for continue program
printf("\n\nDo you wnat to continue ? (y/n)");
ans_CO=getche();
if(ans_CO=='y'&&opr=='/') // if Ans for continue = y & OPR = / ( becouse / havent C )
{
goto start;
}
else if(ans_CO=='y'&&(C[1]<10||A[1]+B[1]<10)) // if Ans for continue = y & C not lorger than 20 numbers
{
printf("\n\nDo you want to continue with Result ? (y/n)");
ans_RE=getche();
if(ans_RE=='y') // if Ans for Result = y
{
opr=';';
system("cls");
printf("Enter Oprator and number and then enter = :\n\n");
for(i=0;i<n;i++)
{
A[i]=C[i];
if(i==0)
continue;
printf("%d",A[i]);
}
printf(" ");
goto Enter_OP;
}
else
{
goto start;
}
}
else if(ans_CO=='y'&&(A[1]+B[1]>9||C[1]>10)) // if Ans for continue = y & C lorger than 20 numbers
{
goto start;
}
else if(ans_CO=='n') // if Ans = n
{
goto end;
}
end : ;
// getch();
return 0;
}