结构体运算
struct Student
{
char name;
doublegrade;
doublegoal;
double score;
}a;
int i,j,k;
double total
for(j=0;j<5;j++)
{
total=0.3 * (a.goal) + 0.5 * (a.grade)+0.2 * (a.score);
}
为什么会报错? invalid operands to binary * (have 'double' and 'double *') 结构体里面的double为啥是数组???狠不理解
而且错误也和这个有关 wp231957 发表于 2022-11-10 13:54
结构体里面的double为啥是数组???狠不理解
而且错误也和这个有关
对啊,最近干啥都想到数组,脑子懵逼了。{:10_277:} total=0.3 * (a.goal) + 0.5 * (a.grade)+0.2 * (a.score);
goal, grade, score 都是数组,用数组名参加计算肯定不行啊
示例: total=0.3 * (a.goal) + 0.5 * (a.grade)+0.2 * (a.score); #include <stdio.h>
struct Student
{
char name ;
doublegrade ;
doublegoal ;
double score ;
} a = {{"abc" , 85 , 95 , 98} , {"bcd" , 65 , 91 , 88} , {"cde" , 85 , 89 , 93} , {"def" , 92 , 98 , 99} , {"efg" , 97 , 93 , 90}} ;
int main(void)
{
int i , j , k ;
double total ;
for(j = 0 ; j < 5 ; j ++) total = 0.3 * a . goal + 0.5 * a . grade + 0.2 * a . score ;
printf("%.2f" , total) ;
for(j = 1 ; j < 5 ; j ++) printf(", %.2f" , total) ;
printf("\n") ;
}
编译运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
90.60, 77.40, 87.80, 95.20, 94.40
D:\\C>
页:
[1]