大王, 捞不捞?# Stock
s*1
1 楼
去面试被要求用纯C写程序,不能有C++ syntax,我写的代码如下:
//Assume ids[] is to get specific_id in aliasID
//num is used to fix the length of ids[]
//alias is used to get the aliasID array
//aliasNum is used to get the length of array alias
/*
successFind = ture means return ids[] with length of num
successFind = false means return ids[] is null
or the length of ids[] is aliasNum (num > aliasNum)
*/
int* GetSomeIDs(int num, aliasID* &alias, int &aliasNum, bool& successFind)
{
successFind = true;
aliasNum = GetNumberOfAliases();
int idsNum = num;
//when num > aliasNum we try to return ids with the length of aliasNum
if(num > aliasNum)
{
printf("There are not enough alias was find and the found alias is %d n",
aliasNum);
successFind = false;
int* ids = (int*) malloc(aliasNum * sizeof(int));
idsNum = aliasNum;
if(aliasNum != 0 && ids == NULL)
{
//when there are not enough memory space for num of int (ids)
printf("There are not enough memory space for ids n");
idsNum = 0;
}
}
else
{
int* ids = (int*) malloc(num * sizeof(int));
}
alias = (aliasID*) malloc(aliasNum * sizeof(aliasID));
if(aliasNum != 0 && alias == NULL)
{
//when there are not enough momery space for aliasNum of aliasID
printf("There are not enough memory space for alias n");
free(ids);
successFind = false;
}
else
{
for(int i=0; i {
alias[i] = GetNextAlias();
}
}
if(alias)
{
for(int i=0; i ids[i] = alias[i] -> specific_id;
}
return ids;
}
被告知有C++ syntax, 但是我真的不知道哪些部分用到了C++ syntax,还有我程序有不
足的地方有大神来给指点一下吗?
//Assume ids[] is to get specific_id in aliasID
//num is used to fix the length of ids[]
//alias is used to get the aliasID array
//aliasNum is used to get the length of array alias
/*
successFind = ture means return ids[] with length of num
successFind = false means return ids[] is null
or the length of ids[] is aliasNum (num > aliasNum)
*/
int* GetSomeIDs(int num, aliasID* &alias, int &aliasNum, bool& successFind)
{
successFind = true;
aliasNum = GetNumberOfAliases();
int idsNum = num;
//when num > aliasNum we try to return ids with the length of aliasNum
if(num > aliasNum)
{
printf("There are not enough alias was find and the found alias is %d n",
aliasNum);
successFind = false;
int* ids = (int*) malloc(aliasNum * sizeof(int));
idsNum = aliasNum;
if(aliasNum != 0 && ids == NULL)
{
//when there are not enough memory space for num of int (ids)
printf("There are not enough memory space for ids n");
idsNum = 0;
}
}
else
{
int* ids = (int*) malloc(num * sizeof(int));
}
alias = (aliasID*) malloc(aliasNum * sizeof(aliasID));
if(aliasNum != 0 && alias == NULL)
{
//when there are not enough momery space for aliasNum of aliasID
printf("There are not enough memory space for alias n");
free(ids);
successFind = false;
}
else
{
for(int i=0; i
alias[i] = GetNextAlias();
}
}
if(alias)
{
for(int i=0; i
}
return ids;
}
被告知有C++ syntax, 但是我真的不知道哪些部分用到了C++ syntax,还有我程序有不
足的地方有大神来给指点一下吗?