任何人都可以检查我的乘法函数并帮助我修复“运行时检查失败#2-变量'operation'周围的堆栈已损坏。”
#include<conio.h>
#include<stdio.h>
#include<Windows.h>
#include<string.h>
#include<math.h>
#define MAX 1000
int checkinput(char element[])
{
int check = 1;
if (strlen(element) >= 51)
{
check = 0;
printf("\nVariable can not contain more than 50 character!");
return check;
}
for (int i = 0; i <= strlen(element) - 1; i++)
{
if (element[i] <= 47 || element[i] >= 58)
{
check = 0;
printf("\nThe entered variable contain character(s) different from set (0...9)!");
return check;
}
}
return check;
}
int checkoperation(char OPERATION[])
{
int check = 0;
if (strlen(OPERATION) >= 2)
{
printf("\nThe entered operation is not accepted!");
return check;
}
char op = OPERATION[0];
if (op == '+' || op == '-' || op == '*' || op == '/')
{
check = 1;
}
else
{
check = 0;
printf("\nThe entered operation is not accepted!");
}
return check;
}
void getinput(char value1[], char value2[], char Operation[])
{
int checkvalue1;
do
{
printf("\nEnter the first variable: ");
gets(value1);
checkvalue1 = checkinput(value1);
} while (checkvalue1 == 0);
int check_operation;
do
{
printf("\nEnter the operation: ");
gets(Operation);
check_operation = checkoperation(Operation);
} while (check_operation == 0);
int checkvalue2;
do
{
printf("\nEnter the second variable: ");
gets(value2);
checkvalue2 = checkinput(value2);
} while (checkvalue2 == 0);
}
void addelement(char element[], int position, char add)
{
for (int i = strlen(element); i >= 0; i--)
{
element[i + 1] = element[i];
}
element[position] = add;
element[strlen(element) + 1] = '\0';
}
void synchronizeinputs(char varia1[], char varia2[])
{
int check;
strlen(varia1) > strlen(varia2) ? check = 1 : check = 0;
if (check == 1)
{
int lenght1 = strlen(varia1) - strlen(varia2);
for (int i = 1; i <= lenght1 ; i++)
{
addelement(varia2, 0, '0');
}
}
else
{
int lenght2 = strlen(varia2) - strlen(varia1);
for (int i = 1; i <= lenght2 ; i++)
{
addelement(varia1, 0, '0');
}
}
}
void plus( char number1[], char number2[], char Result[])
{
int remain = 0;
for (int i = strlen(number1) - 1; i >= 0; i--)
{
if (remain == 1)
{
remain = 0;
Result[strlen(number1) - 1 - i] = ((number1[i] - 48) + (number2[i] - 48) + 1) % 10 + 48;
if ((((number1[i] - 48) + (number2[i] - 48) + 1) / 10) == 1)
{
remain = 1;
}
continue;
}
Result[strlen(number1) - 1 - i] = ((number1[i] - 48) + (number2[i] - 48)) % 10 + 48;
if((((number1[i] - 48) + (number2[i] - 48)) / 10) == 1)
{
remain = 1;
}
}
if (remain == 1)
{
Result[strlen(number1)] = '1';
Result[strlen(number1) + 1] = '\0';
}
else
{
Result[strlen(number1)] = '\0';
}
strrev(Result);
}
void minusinside(char number1[], char number2[], char Result[])
{
int remain = 0;
for (int i = strlen(number1) - 1; i >= 0; i--)
{
if (remain == 0)
{
// if variable 1 smaller than variable 2, then add variable 1 10 units.
if (number1[i] < number2[i])
{
number1[i] += 10;
remain = 1;
}
Result[strlen(number1) - 1 - i] = (number1[i] - 48) - (number2[i] - 48) + 48;
}
else
{
// if variable 1 smaller than variable 2 plus 1 unit, then add variable 1 10 units.
if (number1[i] < number2[i] + 1)
{
number1[i] += 10;
remain = 1;
}
else
remain = 0;
Result[strlen(number1) - 1 - i] = (number1[i] - 48) - (number2[i] - 47) + 48;
}
}
Result[strlen(number1)] = '\0';
strrev(Result);
}
void minus(char number1[], char number2[], char* Result)
{
int check = 1;
int i = 0;
// compare to consider which variable is greater than the other.
do
{
if (number1[i] > number2[i])
{
check = 1;
break;
}
else if (number1[i] < number2[i])
{
check = 0;
break;
}
i++;
} while (number1[i] == number2[i] && i <= strlen(number1));
// if check = 1, then execute "minusinside" regularly
if (check == 1)
minusinside(number1, number2, Result);
// if check = 0, then execute "minusinside" and insert a "-" in the firt posstion of "Result"
else
{
minusinside(number2, number1, Result);
addelement(Result, 0, '-');
}
}
void multiplicate(char number1[], char number2[], char Result[])
{
char re[MAX];
int count0 = 0;
for (int i = strlen(number1) - 1; i >= 0; i--)
{
int remain = 0;
for (int j = strlen(number1) - 1; j >= 0; j--)
{
re[strlen(number1) - 1 - j] = ((number1[i] - 48) * (number2[j] - 48) + remain) % 10 + 48;
remain = ((number1[i] - 48) * (number2[j] - 48) + remain) / 10;
}
re[strlen(number1)] = remain + 48;
re[strlen(number1) + 1] = '\0';
for (int k = 1; k <= count0; k++)
{
addelement(re, 0, '0');
}
char re2[MAX];
for (int u = strlen(re) - 1; u >= 0; u--)
{
re2[strlen(re) - 1 - u] = re[u];
}
re2[strlen(re)] = '\0';
char re3[MAX];
for (int q = 0; q <= strlen(Result) - 1; q++)
{
re3[q] = Result[q];
}
re3[strlen(Result)] = '\0';
synchronizeinputs(re2, re3);
plus(re3, re2, Result);
count0++;
}
}
int main()
{
char variable1[MAX], variable2[MAX], operation[MAX];
char RESULT[] = "0";
getinput(variable1, variable2, operation);
synchronizeinputs(variable1, variable2);
//plus(variable1, variable2, RESULT);
//minus(variable1, variable2, RESULT);
multiplicate(variable1, variable2, RESULT);
printf("\nResult is: %s", RESULT);
getch();
return 0;
}
答案 0 :(得分:1)
使用
char RESULT[] = "0";
您将结果定义为最多能够容纳长度为1的字符串的数组。与
相同char RESULT[2] = "0";
也许您希望数组能够容纳足够大的字符串,以使2个50位数字相乘得到位数?
char RESULT[50+50+1] = "0";