Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
int64_t值在C中的结构中溢出_C_Structure_Overflow_Int64 - Fatal编程技术网

int64_t值在C中的结构中溢出

int64_t值在C中的结构中溢出,c,structure,overflow,int64,C,Structure,Overflow,Int64,我想创建一个只有一个项的多项式:6/5并显示它。 它应该打印以下内容:6/5X^0 poly_t polyFromRatioArray(ratio_t *c, unsigned int degree){ poly_t p = (struct __poly_struct_t*)malloc(sizeof(struct __poly_struct_t)); p->deg = degree; p->coeffs = c; return p; } poly_

我想创建一个只有一个项的多项式:6/5并显示它。 它应该打印以下内容:6/5X^0

poly_t polyFromRatioArray(ratio_t *c, unsigned int degree){
    poly_t p = (struct __poly_struct_t*)malloc(sizeof(struct __poly_struct_t));
    p->deg = degree;
    p->coeffs = c;
    return p;
}
poly_t polyFromRatio(ratio_t c){
    return polyFromRatioArray(&c, 0);
}
多项式的结构是:

typedef struct __poly_struct_t *poly_t;
struct __poly_struct_t{
    unsigned int deg;
    ratio_t *coeffs;
};
如果ratio_t是有理数数组,则其结构为:

typedef struct __ratio_struct_t{
    int64_t num;
    int64_t den;
}ratio_t;
我用了两个函数来构造这个多项式。polyFromRatioArray作品:打印6/5X^0

poly_t polyFromRatioArray(ratio_t *c, unsigned int degree){
    poly_t p = (struct __poly_struct_t*)malloc(sizeof(struct __poly_struct_t));
    p->deg = degree;
    p->coeffs = c;
    return p;
}
poly_t polyFromRatio(ratio_t c){
    return polyFromRatioArray(&c, 0);
}
另一个使分母溢出:polyFromRatio打印6/140218959144480X^0

poly_t polyFromRatioArray(ratio_t *c, unsigned int degree){
    poly_t p = (struct __poly_struct_t*)malloc(sizeof(struct __poly_struct_t));
    p->deg = degree;
    p->coeffs = c;
    return p;
}
poly_t polyFromRatio(ratio_t c){
    return polyFromRatioArray(&c, 0);
}
主要功能:

int main(){
    ratio_t ra = createRatio((int64_t)6,(int64_t)5);
    poly_t p1 = polyFromRatioArray(&ra, 0);  // one that works
    polyPrint(p1);
    poly_t p2 = polyFromRatio(ra);  // this doesn't
    polyPrint(p2);
    free(p1);
    free(p2);
    return 0;
}
涉及的其他事项:

ratio_t createRatio(int64_t a, int64_t b){
    if(b == 0){
        printf("Error : a divise by 0 \n");
        exit(1);
    }
    ratio_t r;
    int64_t pgcd = gcd(a, b); // gcd(int64_t a, int64_t b) is a function that finds pgcd using Euclid.
    r.num = a/pgcd;
    r.den = b/pgcd;
    return r;
}

int64_t gcd(int64_t a, int64_t b){
    int64_t u, v, g;
    ext_eucl_div(&u, &v, &g, llabs(a), llabs(b));
    return g;
}

void ext_eucl_div(int64_t *u, int64_t *v, int64_t *g, int64_t a, int64_t b){ // this function stocks pgcd of a and b in g
    int64_t u1, u2, u3 , v1, v2, v3, q, t1, t2, t3;
    int tour = 0;
    do{
        if(tour == 0){
             u1 = 1; u2 = 0; u3 = a; v1 = 0; v2 = 1; v3 = b;
        }
        else{
            u1 = v1; u2 = v2; u3 = v3; v1 = t1; v2 = t2; v3 = t3;
        }
        q = u3/v3;
        t1 = u1 - q*v1;
        t2 = u2 - q*v2;
        t3 = u3%v3;
        tour++;
    } while(t3>=1);
    *u = v1;
    *v = v2;
    *g = v3;
}

void polyPrint(poly_t p){
    unsigned int i;
    for(i=0; i<= p->deg; i++){
        if(p->coeffs[i].num != 0){
            printRatio(p->coeffs[i]);
            if(i != p->deg) printf("X^%u + ", i);
            else printf("X^%u\n", i);
        }else printf("0\n");
    }
}

void printRatio(ratio_t a){
    printf("%" PRId64, a.num);
    printf("/%" PRId64, a.den);
}
ratio\u t createRatio(int64\u t a,int64\u t b){
如果(b==0){
printf(“错误:被0除数”\n”);
出口(1);
}
比率r;
int64_t pgcd=gcd(a,b);//gcd(int64_t a,int64_t b)是一个使用欧几里德查找pgcd的函数。
r、 num=a/pgcd;
r、 den=b/pgcd;
返回r;
}
int64_t gcd(int64_t a,int64_t b){
int64_t u,v,g;
分界区(&u、v、g、LLAB(a)、LLAB(b));
返回g;
}
void ext_eucl_div(int64_t*u,int64_t*v,int64_t*g,int64_t a,int64_t b){//此函数在g中存储a和b的pgcd
int64_t u1、u2、u3、v1、v2、v3、q、t1、t2、t3;
int-tour=0;
做{
如果(巡更==0){
u1=1;u2=0;u3=a;v1=0;v2=1;v3=b;
}
否则{
u1=v1;u2=v2;u3=v3;v1=t1;v2=t2;v3=t3;
}
q=u3/v3;
t1=u1-q*v1;
t2=u2-q*v2;
t3=u3%v3;
旅游++;
}而(t3>=1);
*u=v1;
*v=v2;
*g=v3;
}
无效多打印(多打印){
无符号整数i;
对于(i=0;ideg;i++){
如果(p->coefs[i].num!=0){
打印比率(p->coefs[i]);
如果(i!=p->deg)printf(“X^%u+”,i);
else printf(“X^%u\n”,i);
}else printf(“0\n”);
}
}
空隙率(比率a){
printf(“%”PRId64,a.num);
printf(“/%”第64页,a.den);
}

这很奇怪,polyFromRatioArray和polyFromRatio似乎在做同样的事情,但是没有。

这段代码没有编译。即使猜测缺少的include,当没有命名为
d
时,也会出现多个错误,如
p->deg=d
。请创建一个。在
polyFromRatio
中,参数
c
是结构的本地副本。该本地副本在函数返回后不再存在,因此
&c
在函数返回后指向垃圾。@abarnet我将参数列表中的d更改为degree,以使其更清楚地表示degree,然后我忘记更改正文中的d,现在已更正。
printRatio()
缺失<代码>gcd(a,b)缺失。两个
typedef
s,一个是指针,另一个是
struct
,混淆并隐藏了@Jamie Yes指定的错误ID,在这一点上没有理由使用
polyFromRatio
。您可以使用宏定义polyFromRatio(x)polyFromRatioArray((x),0)