-2

所以你要读入整数 c 和 m。C 是案例数,m 是每个案例的行数。在 m 之后,有 m 行包含任意数量的字母,没有双精度 (az)。在这些行之后有一个 int r,如果我们对每行有一个字母的每个可能的结果进行递归,形成一个字符串,它就是我们想要的解数。

有一种方法可以跳过递归,直接进入我们想要的等级(int r)。通过尝试将我的结构发送到函数中,我的指针有问题。任何帮助都会很棒。这是我的代码示例

#include <stdio.h>
#include <stdlib.h>
#define MAX 26
//Struct to hold all information inside
struct passwordT{
char letters[MAX];
int charCount;
int possibleSkips;
int solutionArray;
};

//Functions. numOfSkips gets number of solutions to skip for each line, and getSol gets the array for the  
lines of chars.
passwordT* numOfSkips(passwordT *T, int *total, int m);
int getSol(int ps, int v, int m, int r);



int main(){
int i, j, k;
int c, m, r;
char p;
int total = 0;

//Read in Number of cases
scanf("%d", &c);

//Declaring a pointer to struct passwordT, then making mallocing for the size of the number of cases
passwordT* ptrToPass;
ptrToPass = malloc(c*sizeof(passwordT));

//Cycle through cases
for (i=0; i<c;i++){

    //Read in number of lines
    scanf("%d", &m);

    //Reading in the string on each line
    for(j=0;j<m;j++){
        scanf("%s", &ptrToPass[j].letters);
    }

    //Get number of characters for each string
    for(j=0; j<m; j++){
        ptrToPass[j].charCount = 0;
        k=0;
        p = ptrToPass[j].letters[0];
        while(p!='\0'){
            ptrToPass[j].charCount ++;
            k++;
            p = ptrToPass[j].letters[k];
        }
    }

    //Scan in solution number wanted
    scanf("%d", &r);

    //Get number of solutions to skip
    ptrToPass = numOfSkips(ptrToPass, &total, m);

    //Get solution set
    for(j=0;j<m;j++)
        ptrToPass[j].solutionArray = getSol(ptrToPass[j].possibleSkips, 1, m, r);

    //Print results
    for(j=0;j<m;j++)
        printf("%c", ptrToPass[j].letters[ptrToPass[j].solutionArray]);
}

return 0;
}

//This struct is for an algorithm used to skip to int r without recursion
passwordT* numOfSkips(passwordT *T, int *total, int m){

passwordT *skippers;
int i = 0;

//If i = 0, possible solution skips is 1. Else, possible solution skips for skippers[i] is total. Then total = 
total times current lines char count
for(i=0;i<m;i++){

    if(i==0){
        skippers[i].possibleSkips = 1;
        total = skippers[i].charCount;
    }

    else{
        skippers[i].possibleSkips = total;
        total = total * skippers[i].charCount;
    }
}
}


// Function used to get the array of the solution (int r) would be if we used recursion
int getSol(int ps, int v, int m,int r){
int sa = 0;
int i;
for(i=0; i<m; i++){
    while(v <= r - ps){
        sa ++;
        v += sa;
    }
}

return sa;

}

这些是我的错误:

12: syntax error before '*' token (Declaration of first function numOfSkips in header
13: warning, data definition has no type or storage class

IN MAIN

28: passwordT undeclared, first use in this function ( so when i have passwordT* ptrToPass;)
28: ptrtoPass undeclared

IN numOfSkips

73: syntax error before '*'--> error with passwordT* numOfskips(passwordT *T, int *total, int m){
79: m undeclared (first use in this function) --> First time I use m in function numOfSkips
83: total undeclared (same situation as line 79)
4

1 回答 1

1

您混合搭配使用passwordT.

有时,您将其称为struct passwordT,有时您将其称为 typedef。

我推荐pick-one-and-stick-with-it

您最新的错误消息表明您没有 C-99 编译器。
(哎呀,如果您在我第一次提出问题时提到您使用的是哪个编译器,那真的会有所帮助...)

相反,试试这个(旧式)声明:

typedef struct 
{
    char letters[MAX];
    int charCount;
    int possibleSkips;
    int solutionArray;
} passwordT;

passwordT* numOfSkips(passwordT *T, int *total, int m);

在这部分代码中,您total以不同的方式对待:

passwordT* numOfSkips(passwordT *T, int *total, int m){
[...]
total = skippers[i].charCount;
}

total被声明为 a pointer-to-int,但您将其视为 a int

你了解指针和它们指向的类型之间的区别吗?
你是什​​么类型的人total

我建议为变量名(尤其是指针)添加前缀,以帮助您跟踪它们的类型。
(现在这个名字可以帮助你记住它的指针)

passwordT* numOfSkips(passwordT *T, int *ptr_total, int m){
[...]
*ptr_total = skippers[i].charCount;
}
于 2013-09-19T14:40:54.430 回答