构造并将它们引用到函数中 [英] Structs and referencing them into functions

查看:91
本文介绍了构造并将它们引用到函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以你要用整数c和m读。 C是个案数,m是每个案例的行数。在m之后,有m行包含任意数量的字母,没有双打(a-z)。在那些行之后有一个int r,这是我们想要的解决方案号,如果我们为每个可能的结果递归,每行有一个字母,形成一个字符串。



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



So you are to read in integers c and m. C is the number of cases, m is the number of lines per case. After m, there are m lines containing any number of letters, no doubles (a-z). After those lines there is an int r, which is the solution number we want if we did recursion for every possible result having one letter from each line, forming a string.

There is a way to skip recursion, and go straight to the rank (int r) that we want. I am having a problem with my pointers by trying to send my struct into functions. all my errors pertain to my struct and sending my struct into my functions. Any help would be great. Here is a sample of my code.

#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 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;

}







这是我的错误行






Here are my error lines

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)

推荐答案

正如Jochen已经提到的那样,你忘了在一些注释行前放置注释字符。



第28行中的错误是由于在指定ptrToPass后声明指针可执行的声明。请记住,在C语言中,所有声明必须位于函数的顶部!



numOfSkips 函数中,您还可以忘记评论一些线条。另外:指针 skippers 已声明并使用,但从未分配给。这肯定会导致崩溃。



您的 numOfSkips 函数不会返回值,尽管它已声明返回一个 passworT 指针。



最后有几条建议:



- 在代码中使用适当的缩进;因为它很难读,因为你的函数里面的所有内容都是齐平的。



- 阅读你的程序并让自己扮演处理器的角色。然后逐步完成代码,看看它是否能达到您的预期。



- 第一次运行程序时,使用调试器并逐步执行程序并再次将变量的状态与您期望它们包含的内容进行比较。这可能是最重要的建议:学习使用调试器!



祝你好运!
As Jochen already mentioned you forgot to put comment characters in front of some of you comment lines.

The error in line 28 result from declaring the pointer ptrToPass after an executable statement. Remember, in C all declarations must be at the top of a function!

In your numOfSkips function you also forgot to comment out some lines. Also: Pointer skippers is declared and used, but never assigned to. That will definitely lead to a crash.

Your numOfSkips function does not return a value, although it is declared to return a passworT pointer.

And finally a couple of recommendations:

- Use proper indentation in your code; as it stands it is hard to read as everything inside your functions is flush left.

- Read your program and put yourself in the role of the processor. Then go through your code step by step and see if it will do what you expect.

- When running the program the first time, use a debugger and step through your program and again compare the state of variables with what you expect them to contain. This is probably the most important advice: Learn to use the debugger!

Good luck!


这篇关于构造并将它们引用到函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆