【问题标题】:Value printed in one function works fine, but printed in the next function is wrong (returns 0.00)在一个函数中打印的值可以正常工作,但在下一个函数中打印的值是错误的(返回 0.00)
【发布时间】:2011-09-21 02:06:39
【问题描述】:

我对此很陌生,所以请多多包涵。对于一个编码 C 类,我们将使用总共 9 个函数和一个头文件(我们称之为 my.h)来收集房间的长度和宽度(ints),百分比折扣(也是一个 int ),以及地毯的单价(一倍)。我们将使用输入来计算安装地毯的总成本。我可以得到要打印的长度、宽度和面积,但不能得到单价。它从 Read_Data.c 和 Calc_Value.c 函数打印 frin,但不在 Install_Price.c 中打印,后者在 Calc_Value.c 中调用。 unit_price 与长度和宽度同时读取,但不知何故,它没有正确传递。我不确定为什么。也许它确实需要一个不同的指针。任何帮助将不胜感激。我读过我的书,和我的教授和同学交谈过,也搜索过互联网,但我没有找到任何有用的东西。 Install_Price 的代码是:

/*This function calculates the cost of the carpet and the labor cost in order to    
calculate the installed price.*/

#include "my.h"

void Install_Price (int length, int width, int unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

{

printf("\nThe unit price is %7.2f.\n", *unit_price);

*area = length * width;
*carpet_cost = (*area) * unit_price;

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);

*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);

return;
}

请注意,这里的 printf 语句只是为了找出我在 unit_price 方面出了什么问题。下面,我包含了 my.h 头代码 main.c 以及它调用到 Install_Price.c 的函数。再次感谢您的帮助!

我的.h

#include <stdio.h>
void Read_Data(int* length, int* width, int* percent_discount, double* 
unit_price);

void Calc_Values(int length, int width, int percent_discount, double 
unit_price, int* area, double* carpet_cost, double* labor_cost, double* 
installed_price, double* discount, double* subtotal, double* tax, 
double* total);

void Install_Price(int length, int width, int unit_price, int* area, double*      
carpet_cost, double* labor_cost, 
double* installed_price);

void Subtotal (int percent_discount, double installed_price, double* discount, 
double* subtotal);

void Total (double subtotal, double* tax, double* total);

void Print (int length, int width, int area, double unit_price, double carpet_cost,    
double labor_cost, double 
installed_price, int percent_discount, double discount, double subtotal, double tax,   
double total);

void Print_Measurements (int length, int width, int area);

void Print_Charges (double unit_price, double carpet_cost, double labor_cost, double   
installed_price, int percent_discount, double discount, double subtotal, double tax, 
double total);

#define LABOR_RATE 0.35
#define TAX_RATE 0.085

main.c

/*  This function calls three subfuctions to calculate the costs of installing a    
carpet and prints an invoice.  */

#include "my.h"

int main (void)

{
        int length;
        int width;
        int percent_discount;
        double unit_price;
        int area;
        double carpet_cost;
        double labor_cost;
        double installed_price;
        double discount;
        double subtotal;
        double tax;
        double total;

Read_Data(&length, &width, &percent_discount, &unit_price);

Calc_Values(length, width, percent_discount, unit_price, &area, &carpet_cost,  
&labor_cost,&installed_price, &discount, &subtotal, &tax, &total);

Print(length, width, area, unit_price, carpet_cost, labor_cost,
installed_price, percent_discount, discount, subtotal, tax, total);

return 0;
}

Read_Data.c

/*This function asks the user for the length and width of a room to be carpeted, the  
percent discount and the unit price of the carpet. */

#include "my.h"

void Read_Data (int* length, int* width, int* percent_discount, double* unit_price)

{
printf("What is the length, in feet, of the room?\n");
scanf("%d", length);

printf("What is the width, in feet, of the room?\n"); 
scanf("%d", width);

printf("What is the percent discount?\n");
scanf("%d", percent_discount);

printf("What is the unit price of the carpet?\n");
scanf("%lf", unit_price);

printf("\nThe length is %6d.\n", *length);   //These printf statements work properly.
printf("The width is %6d.\n", *width);
printf("The percent discount is %3d%.\n", *percent_discount);
printf("The unit price is $%7.2f.\n", *unit_price);

return;
}

Calc_Value.c

/*This function calls three subfuctions that calculate all required quantities.  */

#include "my.h"

void Calc_Values (int length, int width, int percent_discount, double unit_price, 
int* area, double* carpet_cost, double* labor_cost, double* installed_price, double*  
discount, double* subtotal, double* tax, double* total)

{

printf("\nUnit Price:  %7.2f.\n", unit_price);  //This printf statement works properly.

Install_Price (length, width, unit_price, area, carpet_cost, labor_cost, 
installed_price);

Subtotal (percent_discount, *installed_price, discount, subtotal);

Total (*subtotal, tax, total);

return;
}

Install_Price.c(为方便用户重复)

/*This function calculates the cost of the carpet and the labor cost in order to    
calculate the installed price.*/

#include "my.h"

void Install_Price (int length, int width, int unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

{

printf("\nThe unit price is %7.2f.\n", *unit_price);  //THIS DOES NOT WORK

*area = length * width;
*carpet_cost = (*area) * unit_price;

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, 
*carpet_cost);  //THIS DOES NOT WORK

*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);

return;
}

【问题讨论】:

  • 带有最小、完整示例的问题通常最受关注。

标签: c pointers printf function-pointers


【解决方案1】:

我没有阅读整个问题,但是我已经在这里发现了一个错误:

printf("\nThe unit price is %7.2f.\n", *unit_price);

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);

变量unit_price 的类型为int,但您将其打印为浮点数。

我猜第一条语句根本不应该编译,因为 unit_price 甚至不能解除引用。

【讨论】:

    【解决方案2】:

    您的问题是,在 Install_Price 函数声明中,unit_price 被声明为 int,但在所有其他函数定义中,它被声明为 double*double。当您将该值传递给Install_Price 时,C 会自动将double 转换为int。问题在于Install_price 中的这两行代码:

    printf("\nThe unit price is %7.2f.\n", *unit_price);
    

    printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, 
           unit_price, *carpet_cost);
    

    您正在使用%f 打印已转换为int 的单价。

    Install_Price的定义改为

    void Install_Price (int length, int width, double unit_price, int* area, 
    double* carpet_cost, double* labor_cost, double* installed_price)
    

    应该可以解决您的一个问题。

    另一个问题也在这一行:

    printf("\nThe unit price is %7.2f.\n", *unit_price);
    

    unit_price 不应在此行中取消引用。将其更改为:

    printf("\nThe unit price is %7.2f.\n", unit_price);
    

    解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 2013-03-17
      • 2021-05-16
      • 2013-12-06
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多