gpt4 book ai didi

c - 错误 : request for member in something not a structure or union

转载 作者:太空宇宙 更新时间:2023-11-04 05:39:19 27 4
gpt4 key购买 nike

<分区>

我的代码有问题。我的程序是一个简化分数的程序。所以我的问题是:

我声明了结构 Fraction。然后我在主函数中声明结构 Fraction f。

但是当我尝试使用结构部分的任何成员(即 f.num、f.den)时,它说它不是结构或 union 的成员。我有 10 个错误都对我的程序说同样的话。

错误(逐字记录):错误:在非 union 结构中请求成员“num”

#include <stio.h>

struct Fraction{
int num;
int den;
int lownum;//lownum = lowest numerator.
int lowden;//lowden = lowest denominator
int error;
};

void enter(struct Fraction *f);
void simplify(struct Fraction *f);
void display(const struct Fraction *f);


int main(void){
struct Fraction f;

printf("Fraction Simplifier\n");
printf("===================\n");

enter(&f);
simplify(&f);
display(&f);
}

void enter(struct Fraction *f){
printf("Please enter numerator : \n");
scanf("%d", &f.num);

printf("please enter denominator : \n");
scanf("%d", &f.den);

printf("%d %d", f.num, f.den);
}

void simplify(struct Fraction *f){
int a;
int b;
int c;
int negative; //is fraction positive?

a = f.num;
b = f.den;

if (a/b < 0){
negative = 1;
}

if(b == 0){
f.error = 1;
}

if(a < 0){
a = a * -1;
}

if(b < 0){
b = b * -1;
}

//euclids method
if(a < b){
c = a;
a = b;
b = c;
}
while(b != 0){
c = a % b;
a = b;
b = c;
}

f.lownum = f.num / a;
f.lowden = f.den / a;

if(negative = 1){
f.lownum = f.lownum * -1;
}
}

void display (const struct Fraction *f){
if (f.error != 1){
printf("%d / %d", f.lownum, f.lowden);
}else{
printf("error");
}
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com