【问题标题】:Finding a set of characters [closed]查找一组字符[关闭]
【发布时间】:2013-09-19 14:21:05
【问题描述】:

所以你要读入整数 c 和 m。 C 是案例数,m 是每个案例的行数。在 m 之后,有 m 行包含任意数量的字母,没有双精度 (a-z)。在这些行之后有一个 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)

【问题讨论】:

  • “我的指针有问题,我试图将我的结构发送到函数中。” 这不是什么问题描述。您是否收到编译时错误?运行时崩溃?意外的输出(什么输入?)? “有关您编写的代码问题的问题必须描述具体问题。”
  • 它甚至没有编译。我很好地评论我的错误
  • 第 54 行:从不兼容的指针类型传递 'numOfSkips' 的 arg 1 第 54 行:从函数 NumOfSkips 中不兼容的指针类型赋值 第 75 行:无效使用未定义类型 'struct passwordT' 75:取消引用指向的指针不完整类型 76:无效使用未定义类型“struct passwordT”76:取消引用指向不完整类型的指针 80:无效使用未定义类型“struct passwordT”80:取消引用指向不完整类型的指针 81:无效使用未定义类型“struct passwordT”81 : 解除指向不完整类型的指针
  • 我会这样做,但我自己数到 54?不...说真的,将 cmets 放入出现问题的代码中。逐行调试代码也是一个问题。似乎您希望您的代码按照您编写的方式完美,但事实并非如此。因此,只需将所有代码与所有警告放在一起,甚至无需尝试自己调试。我强烈反对这种行为。
  • 问题出在我的结构和函数上。我所有的错误都是将我的结构传递给我的函数

标签: c recursion computer-science


【解决方案1】:

您可以混合使用passwordT

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

我推荐挑选一个并坚持使用它

您最新的错误消息表明您没有 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 被声明为pointer-to-int,但您将其视为int

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

我建议在变量名前加上前缀(尤其是指针),以帮助您跟踪它们的类型。
(现在该名称可以帮助您记住它的指针)

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

【讨论】:

  • 我只想在函数中访问结构的内容,并在完成该函数后保留这些值
  • 而那个 m 应该是一个 c。我修好了
猜你喜欢
  • 1970-01-01
  • 2012-12-08
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
相关资源
最近更新 更多