0

我正在编写一个矩阵,其条目是具有有理系数的多项式。任何帮助将不胜感激。
我声明了有理数和有理多项式:
rational_number.h

struct long_rational{
    long p;
    long q;
    };

typedef struct long_rational rational;

多项式.h

#define MAX_DEGREE 200

struct rational_polynomial{
    long degree;
    rational coef[MAX_DEGREE]; //rational coefficients in increase power.
};

typedef struct rational_polynomial polynomial;

完整的 poly_mat.c

#include "poly_mat.h"

#define NR_END 1
#define FREE_ARG char*

polynomial **poly_matrix( long nrl, long nrh, long ncl, long nch )
/* allocates a matrix with polynomial entries in the range m[nrl..nrh][ncl..nch] */
{
    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
    polynomial **m;
    /* allocate pointers to rows */
    m=( polynomial ** ) malloc( ( size_t )( ( nrow+NR_END )*sizeof( polynomial* ) ) );
    if ( !m ) nrerror( "allocation failure 1 in matrix()" );
    m += NR_END;
    m -= nrl;
    /* allocate rows and set pointers to them */
    m[nrl]=( polynomial * ) malloc( ( size_t )( ( nrow*ncol+NR_END )*sizeof( polynomial ) ) );
    if ( !m[nrl] ) nrerror( "allocation failure 2 in matrix()" );
    m[nrl] += NR_END;
    m[nrl] -= ncl;
    for ( i=nrl+1; i<=nrh; i++ ) m[i]=m[i-1]+ncol;
    /* return pointer to array of pointers to rows */
    return m;
}

void **free_poly_matrix( polynomial **m, long nrl, long nrh, long ncl, long nch )
/* free a polynomial matrix allocated by poly_matrix() */
{
    free( ( FREE_ARG ) ( m[nrl]+ncl-NR_END ) );
    free( ( FREE_ARG ) ( m+nrl-NR_END ) );
}

void init_random_poly_matrix( int **m, long nrl, long nrh, long ncl, long nch )
/* initialize a random polynomial matrix with coefficient <=100*/
{
    long i,j;
    long iseed = ( long )time( NULL );
    srand ( iseed );
    for ( i=nrl; i<=nrh; i++ )
    {
        for ( j=ncl; j<=nch; j++ )
        {
            m[i][j].degree=( rand()%MAX_DEGREE );
            for ( k=0;k<=MAX_DEGREE;k++ )
            {
                m[i][j].coef[k].p = (rand()%100 );
                m[i][j].coef[k].q = (1+rand()%100 );
            }
        }
    }
}

这是神秘的错误消息:

gcc -Wall -c -o poly_mat.o poly_mat.c
poly_mat.c:在函数“init_random_poly_matrix”中:
poly_mat.c:6:错误:“(”标记之前的预期声明说明符
poly_mat.c:28:错误:在 '{' 标记之前应为 '='、','、';'、'asm' 或 '__attribute__'
poly_mat.c:35:错误:在 '{' 标记之前应为 '='、','、';'、'asm' 或 '__attribute__'
poly_mat.h:14:错误:原型函数定义中的旧式参数声明
poly_mat.c:51:错误:输入结束时应为“{”
制作:*** [poly_mat.o] 错误 1

poly_mat.h 用缺少的分号填充。

#ifndef POLY_MAT_H
#define POLY_MAT_H

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "nrutil.h"
#include "rational_number.h"
#include "polynomial.h"

/* matrix with polynomial entries */
polynomial **poly_matrix( long nrl, long nrh, long ncl, long nch );
void init_random_poly_matrix( int **m, long nrl, long nrh, long ncl, long nch );
void **free_poly_matrix( polynomial **m, long nrl, long nrh, long ncl, long nch );

#endif

现在我无法使用点运算符访问数组中的多项式成员。
新的错误信息:

gcc -Wall -c -o poly_mat.o poly_mat.c
poly_mat.c:在函数“init_random_poly_matrix”中:
poly_mat.c:43:错误:请求成员“学位”不是结构或联合
poly_mat.c:46:错误:在不是结构或联合的东西中请求成员“coef”
poly_mat.c:47:错误:在不是结构或联合的东西中请求成员“coef”
制作:*** [poly_mat.o] 错误 1

编辑2:发现错误。将其声明为 int** 而不是多项式**。

4

1 回答 1

0

不知道 poly_mat.h,但我理解错误消息,poly_mat.h 中存在语法错误。我相信在声明 init_random_poly_matrix() 的原型时,在第 14 行或之前缺少括号/大括号或分号。

于 2012-04-28T09:45:48.880 回答