c的问题
今天敲代码的时候发现的问题,代码要求是写一段代码,可以找出给定的两个自然数的最大公因数和最小公倍数,然后我写了下面这段代码:#include <stdio.h>
#include <string.h>
#define MAX 1024
int compare(int x[], int a, int y[], int b);
int count(int x);
int find(int x);
int count(int x)
{
int i;
int j = 0;
for(i = 1; i <= x; i++)
{
if(x % i == 0)
{
j++;
}
}
return j;
}/*确认x的公因数个数*/
int find(int x)
{
int i;
int j = 0;
int p;
for(i = 0; i <= x; i++)
{
if(x % i == 0)
{
p = i;
j++;
}
}
return p;
}
int compare(int x[], int a, int y[], int b)
{
int i, j;
int temp = 0;
for(i = 0; i< a; i++)
{
for(j = 0; j < b ; j++)
{
if(x == y)
{
temp = x;
}
}
}
return temp;
}
int main(void)
{
int aa, bb;
int temp = 0;
printf("请输入两个自然数:");
scanf("%d %d", &aa, &bb);
if(aa < bb)
{
temp = aa;
aa = bb;
bb = temp;
}
int ca, cb;
ca = count(aa);
cb = count(bb);
int p, q;
strcpy(p, find(aa));
strcpy(q, find(bb));
int yin;
yin = compare(p, ca, q, cb);
printf("最大公因数为:%d", yin);
printf("最小公倍数为:%d", aa*bb/yin);
return 0;
}
他的报错是这样子的:
:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2057: expected constant expression
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2466: cannot allocate an array of constant size 0
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2133: 'p' : unknown size
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2057: expected constant expression
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2466: cannot allocate an array of constant size 0
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2133: 'q' : unknown size
E:\VC6.0green\MyProjects\1117\17.cpp(77) : error C2664: 'strcpy' : cannot convert parameter 1 from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\VC6.0green\MyProjects\1117\17.cpp(78) : error C2664: 'strcpy' : cannot convert parameter 1 from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\VC6.0green\MyProjects\1117\17.cpp(81) : error C2664: 'compare' : cannot convert parameter 1 from 'int' to 'int []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
这个问题是出在什么地方了,怎么能改进呢 :\VC6.0green\MyProjects\1117\17.cpp(76):错误C2057:应为常量表达式
E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2466:无法分配大小为0的常量数组
E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2133:“p”:未知大小
E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2057:需要常量表达式
E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2466:无法分配大小为0的常量数组
E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2133:“q”:未知大小
E:\VC6.0green\MyProjects\1117\17.cpp(77):错误C2664:“strcpy”:无法将参数1从“int”转换为“char*”
从整数类型转换为指针类型需要重新解释强制转换、C样式强制转换或函数样式强制转换
E:\VC6.0green\MyProjects\1117\17.cpp(78):错误C2664:“strcpy”:无法将参数1从“int”转换为“char*”
从整数类型转换为指针类型需要重新解释强制转换、C样式强制转换或函数样式强制转换
E:\VC6.0green\MyProjects\1117\17.cpp(81):错误C2664:“compare”:无法将参数1从“int”转换为“int[]
从整数类型转换为指针类型需要重新解释强制转换、C样式强制转换或函数样式强制转换
百度翻译一下不难吧{:10_249:} 本帖最后由 jackz007 于 2019-11-17 18:49 编辑
求两个数的最大公约数和最小公倍数,87 行代码,整太复杂了,试试这个代码吧:
#include <stdio.h>
int main(void)
{
int m , n , t , x , y ;
printf("输入两个整数 m , n : ") ;
scanf("%d%d", & m , & n) ;
if(m > 0 && n > 0) for(x = m , y = n ; y > 0 ; t = x % y , x = y , y = t) ;
if (x > 1) printf("最大公约数为 : %d\n" , x) ;
else printf("m , n 没有最大公约数\n") ;
printf("最小公倍数为 : %d\n" , m * n / x) ;
return 0 ;
} 本帖最后由 bin554385863 于 2019-11-18 02:38 编辑
#include <stdio.h>
#include <limits.h>
typedef struct
{
int MAXCommonDivisor;
int MinCommonMultiple;
} num;
num func1(const int n, const int m)
{
num res = {-1, -1};
int tmax = m > n ? m : n;
int tmin = m < n ? m : n;
if (tmax >= __INT32_MAX__ )
{
return res;
}
else
{
for (size_t i = tmax;; i++)//最小公倍数一定大于等于较大的数
{
if (i % m == 0 && i % n == 0)
{
res.MinCommonMultiple = i;
break;
}
}
for (size_t i = tmin; i > 0; i--)//最小公约数一定小于等于较小的数,且1是所有数的公约数
{
if (m % i == 0 && n % i == 0)
{
res.MAXCommonDivisor = i;
break;
}
}
}
return res;
}
int main(int argc, char const *argv[])
{
int a = 24, b = 288;
int m = 37, n = 15;
printf("%d & %d--(%d , %d)\n", a, b, func1(a, b).MAXCommonDivisor, func1(a, b).MinCommonMultiple);
printf("%d & %d--(%d , %d)\n", m, n, func1(m, n).MAXCommonDivisor, func1(n, m).MinCommonMultiple);
return 0;
}
------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。
E:\Users\admin\Documents\VScode>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-uzatlvzi.lj2 --stdout=Microsoft-MIEngine-Out-rjc5cw52.bqv --stderr=Microsoft-MIEngine-Error-r3ancrla.4xx --pid=Microsoft-MIEngine-Pid-ic40r14d.fft --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
24 & 288--(24 , 288)
37 & 15--(1 , 555)
E:\Users\admin\Documents\VScode>
页:
[1]