我对此很新,所以请耐心等待。对于编码C类,我们总共使用九个函数和一个头文件(我们称之为my.h)来收集房间的长度和宽度(int),折扣百分比(也是一个int) ),和地毯的单价(双倍)。我们将使用输入来计算安装地毯的总成本。我可以得到打印的长度,宽度和面积,但不是单价。它从Read_Data.c和Calc_Value.c函数中打印frin,但不在Install_Calice.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中。再次感谢您的帮助!
my.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;
}
答案 0 :(得分:8)
我没有读完整个问题但是,我已经发现了一个错误:
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
甚至不能解除引用。
答案 1 :(得分:2)
您的问题是在Install_Price
函数声明中,unit_price被声明为int
,但在所有其他函数定义中,它被声明为double
或*double
。当您将该值传递给double
时,C会自动将int
转换为Install_Price
。问题在于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);
解决这个问题。