我将C程序编码如下: -
#include <stdio.h>
#include <conio.h>
#include <string.h>
char getPositions(int randNo, int guessNo);
void main()
{
char positions[6];
clrscr();
positions = getPositions(5242, 5243);
printf(positions);
getchar();
}
char getPositions(int randNo, int guessNo)
{
char outPut[6];
int randNoArr[4], guessNoArr[4];
int c, w, p;
for(int i=4; i>0; i--){
randNoArr[i] = randNo%10;
randNo /= 10;
guessNoArr[i] = guessNo%10;
guessNo /= 10;
//If Number Possitioned Right Place Incress variable c
if(randNoArr[i] == guessNoArr[i]){
c++;
}
}
for(int j=1; j<=4; j++){
if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
w++;
}
}
if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
p++;
}
if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
p++;
}
if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
p++;
}
if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
p++;
}
sprintf(outPut, "%dC%dW%dP", c,w,p);
return outPut;
}
我有两个错误:
Error prog1.CPP 10: Lvalue required //positions = getPositions(5242, 5243);
Error prog1.cpp 50: Cannot convert 'char *' to 'char' //return outPut;
答案 0 :(得分:2)
首先,你犯了一个非常可怕的错误:
尝试将函数中本地声明的数组返回给另一个函数。返回数组的正确方法是返回指向它的指针。并确保指针指向您拥有的内存。当函数返回时,局部变量将失去焦点,因此当函数返回时你不拥有它们。
快速解决方案: 将position数组作为第3个参数传递给函数返回void是最简单的事情。
答案 1 :(得分:1)
问题是你试图将指针放入数组中。在C中,你不能通过使用简单的等号来复制指针内容。
这是解决方案。
#include <stdio.h>
#include <conio.h>
#include <string.h>
void getPositions(char *outPut, int randNo, int guessNo);
void main()
{
char positions[6];
clrscr();
getPositions(positions, 5242, 5243);
printf(positions);
getchar();
}
void getPositions(char *outPut, int randNo, int guessNo)
{
int randNoArr[4], guessNoArr[4];
int c, w, p;
for(int i=4; i>0; i--){
randNoArr[i] = randNo%10;
randNo /= 10;
guessNoArr[i] = guessNo%10;
guessNo /= 10;
//If Number Possitioned Right Place Incress variable c
if(randNoArr[i] == guessNoArr[i]){
c++;
}
}
for(int j=1; j<=4; j++){
if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
w++;
}
}
if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
p++;
}
if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
p++;
}
if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
p++;
}
if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
p++;
}
sprintf(outPut, "%dC%dW%dP", c,w,p);
}
那是您的代码,但我将编译并运行以下内容(我删除了#include和clrscr();):
#include <stdio.h>
#include <string.h>
void getPositions(char *outPut, int randNo, int guessNo);
void main()
{
char positions[6];
getPositions(positions, 5242, 5243);
printf(positions);
getchar();
}
void getPositions(char *outPut, int randNo, int guessNo)
{
int randNoArr[4], guessNoArr[4];
int c, w, p, i, j;
for(i=4; i>0; i--){
randNoArr[i] = randNo%10;
randNo /= 10;
guessNoArr[i] = guessNo%10;
guessNo /= 10;
//If Number Possitioned Right Place Incress variable c
if(randNoArr[i] == guessNoArr[i]){
c++;
}
}
for(j=1; j<=4; j++){
if(guessNoArr[j] != randNoArr[1] && guessNoArr[j] != randNoArr[2] && guessNoArr[j] && randNoArr[3] || guessNoArr[j] && randNoArr[4]){
w++;
}
}
if(guessNoArr[1] == randNoArr[2] || guessNoArr[1] == randNoArr[3] || guessNoArr[1] == randNoArr[4]){
p++;
}
if(guessNoArr[2] == randNoArr[1] || guessNoArr[2] == randNoArr[3] || guessNoArr[2] == randNoArr[4]){
p++;
}
if(guessNoArr[3] == randNoArr[1] || guessNoArr[3] == randNoArr[2] || guessNoArr[3] == randNoArr[4]){
p++;
}
if(guessNoArr[4] == randNoArr[1] || guessNoArr[2] == randNoArr[2] || guessNoArr[2] == randNoArr[3]){
p++;
}
sprintf(outPut, "%dC%dW%dP", c,w,p);
}
输出:
T0109059@P90b11c603564 ~/tmp/test
$ ./a.exe
759583832C2272364W2282526P