【发布时间】: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