我编写了一个 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;