You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

指针值无法累加:计算车辆平均价格与里程的C语言程序问题

修复你的C语言车辆平均计算程序

嘿,我帮你找出了几个导致结果错误的关键问题,咱们一步步来搞定:

1. 踩了个经典的运算符顺序坑!

你在averagePrice和averageMiles函数里用了=+,这可不是累加的意思!=+其实是把右边的值直接赋值给total(相当于total = +xxx),每次都会覆盖之前的total值,而不是累加。比如在averagePrice里:

  • 第一步total是500.0(f1的价格)
  • 第二步total =+ 1800.0 → total变成1800.0
  • 第三步total =+1400.0 → total变成1400.0
  • 第四步total =+2600.0 → total变成2600.0
    最后除以4得到650.0,但后面还有格式符的问题导致输出0。

修复方法:把所有=+改成正确的累加运算符+=,比如:

total += ptr_car2->price.price;

2. printf格式符类型不匹配

你的avgprice是float类型,但你用了%d(整数格式符)来输出,这会导致内存解析错误,输出莫名其妙的数值(比如你看到的0)。而里程的计算虽然结果看似有一个正确值,但其实是巧合,因为用float计算最后转int会截断小数,存在潜在问题。

修复方法:

  • 输出价格用%.2f(保留两位小数,符合价格的显示习惯)
  • 里程计算改用int类型的total,避免不必要的类型转换:
printf("The average price of the vehicle is: %.2f.\n", avgprice);
printf("The average miles of the vehicle is: %d.\n", avgmile);

3. 结构体定义冗余(可选但重要的优化)

你的Car结构体里定义了两个Model类型的变量year和model,这完全是冗余的——因为Model结构体本身已经包含了year和model成员。这种嵌套会让你的代码变得混乱(比如f1.year.year这种奇怪的写法)。

优化后的结构体定义:

typedef struct {
 int year;
 char model[11];
} Model;
typedef struct {
 float price;
} Price;
typedef struct {
 int miles;
} Miles;
typedef struct {
 Model model_info; // 一个Model变量,包含年份和车型
 Price price;
 Miles miles;
 struct Car *next;
} Car;

对应的初始化代码也要改成:

f1.model_info.year = 56;
strcpy(f1.model_info.model, "Ford");

修复后的完整代码

#include <stdio.h>
#include <string.h>
#define SIZE 4

typedef struct {
 int year;
 char model[11];
} Model;
typedef struct {
 float price;
} Price;
typedef struct {
 int miles;
} Miles;
typedef struct {
 Model model_info;
 Price price;
 Miles miles;
 struct Car *next;
} Car;

float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4);
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4);

int main(void) {
 Car f1, im, f2, g;
 int avgmile;
 float avgprice;

 f1.model_info.year = 56;
 strcpy(f1.model_info.model, "Ford");
 f1.price.price = 500.00;
 f1.miles.miles = 23000;

 im.model_info.year = 64;
 strcpy(im.model_info.model, "Impala");
 im.price.price = 1800.00;
 im.miles.miles = 12000;

 f2.model_info.year = 57;
 strcpy(f2.model_info.model, "Ford");
 f2.price.price = 1400.00;
 f2.miles.miles = 22000;

 g.model_info.year = 65;
 strcpy(g.model_info.model, "Galaxy");
 g.price.price = 2600.00;
 g.miles.miles = 48000;

 avgprice = averagePrice(&f1, &im, &f2, &g);
 avgmile = averageMiles(&f1, &im, &f2, &g);

 printf("The average price of the vehicle is: %.2f.\n", avgprice);
 printf("The average miles of the vehicle is: %d.\n", avgmile);

 getchar();
 return 0;
}

float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4) {
 float total = 0.0;
 total += ptr_car->price.price;
 total += ptr_car2->price.price;
 total += ptr_car3->price.price;
 total += ptr_car4->price.price;
 return total / SIZE;
}

int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4) {
 int total = 0;
 total += ptr_car->miles.miles;
 total += ptr_car2->miles.miles;
 total += ptr_car3->miles.miles;
 total += ptr_car4->miles.miles;
 return total / SIZE;
}

运行这段代码后,你会得到正确的结果:

The average price of the vehicle is: 1575.00.
The average miles of the vehicle is: 26250.

内容的提问来源于stack exchange,提问作者Adam Perez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:14:10