我正在使用 C 进行编码。我试图让 pow 函数来解决这个问题。使用基数作为用户输入的变量。该程序要求用户计算一个简单的打开 jar 头的面积和成本。这是代码:
//Pre-processor Directives
#include <stdio.h>
#include <math.h>
#define PI 3.14159
//Start of function
int main(void)
{
//Declared variables
float base_area, height_area, total_area_per_container;
float radius, height, cost_per_container, total_cost, cost_per_sq_cm;
int containers;
//user input
//radius input
printf("Enter radius of base in cm: ");
scanf("%f", &radius);
//height input
printf ("Enter height of container in cm: ");
scanf("%f", &height);
//material cost
printf("Enter material cost per square cm: ");
scanf(" $%f", &cost_per_sq_cm);
//amount of containers
printf("Enter the number of containers to be produced: ");
scanf("%d", &containers);
//calcualtions of each container
base_area = PI * pow(radius,2);
height_area = 2 * PI * radius * height;
total_area_per_container = base_area + height_area;
//calculation of the cost of the material
cost_per_container = total_area_per_container * cost_per_sq_cm;
total_cost = containers * cost_per_container;
//Print results
printf("Surface area of container: %.2f cm\n", total_area_per_container);
printf("Cost per container: $%.2f\n", cost_per_container);
printf("Total production costs: $%.2f\n", total_cost);
//exit program
return (0);
}
如果我取出每个容器的注释计算下的 pow(radius,2) 并放入“radius * radius”,一切都会正常,我只是想测试一下 pow 函数是如何工作的。我觉得我做错了什么。我还使用 NetBeans IDE 8.0.2 来编写代码。
更新1:使用我的教练拥有的gcc编译器。在他的计算机上编译我的代码给了我这个错误的响应:
第一部分是一堆行话,说我正在将代码复制到他的计算机上,接下来的内容如下 - 存储我的东西的目录已被删除
In function `main':
undefined reference to `pow'
collect2: error: ld returned 1 exit status
gmake[2]: *** [dist/Debug/GNU-Linux-x86/hw5] Error 1
gmake[2]: Leaving directory
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory
gmake: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
最佳答案
编译失败,因为您没有链接到数学库。尝试通过以下方式编译:
gcc infile.c -lm
其次,您的代码中有一个错误。 The scanf()
calls are failing due to not "consuming/gobbling-up" the trailing newline character. Don't use scanf()
: use fgets()
and the atoX()
and sscanf()
functions if you must do string parsing like this.我的 getBuf()
函数中的 strtok()
调用只是为了将来您将此示例用于其他类型的字符串解析时使用。 fgets()
函数不像 scanf()
那样使用共享流缓冲区。
我已经更新了您的代码 list ,并且能够使用您建议的两种功率计算方法获得相同的结果。
代码列表
<小时/>/*******************************************************************************
* Pre-processor Directives
******************************************************************************/
#include <stdio.h> // printf()
#include <stdlib.h> // atof() will compile but return zero if this is missing
#include <math.h> // pow()
#include <stdbool.h> // bool
#define PI M_PI
#define BUF_LEN (256)
/*******************************************************************************
* Function prototypes
******************************************************************************/
bool getBuf(char* buf);
/*******************************************************************************
* Function definitions
******************************************************************************/
int main(void)
{
//Declared variables
float base_area, height_area, total_area_per_container;
float radius, height, cost_per_container, total_cost, cost_per_sq_cm;
int containers;
char buf[BUF_LEN] = { 0 };
// User input
//radius input
printf("Enter radius of base in cm: ");
if ( !getBuf(buf) ) { return (-1); }
radius = atof(buf);
//height input
printf ("Enter height of container in cm: ");
if ( !getBuf(buf) ) { return (-1); }
height = atof(buf);
//material cost
printf("Enter material cost per square cm: ");
if ( !getBuf(buf) ) { return (-1); }
cost_per_sq_cm = atof(buf);
//amount of containers
printf("Enter the number of containers to be produced: ");
if ( !getBuf(buf) ) { return (-1); }
containers = atoi(buf);
//calcualtions of each container
base_area = PI * pow(radius, 2.0);
//base_area = PI * radius * radius;
height_area = 2 * PI * radius * height;
total_area_per_container = base_area + height_area;
//calculation of the cost of the material
cost_per_container = total_area_per_container * cost_per_sq_cm;
total_cost = containers * cost_per_container;
//Print results
printf("Surface area of container: %.2f cm\n", total_area_per_container);
printf("Cost per container: $%.2f\n", cost_per_container);
printf("Total production costs: $%.2f\n", total_cost);
//exit program
return (0);
}
bool getBuf(char* buf)
{
if (!buf)
{
printf("Bad input.\n");
return false;
}
fgets(buf, BUF_LEN, stdin); // Get a string of data
strtok(buf, "\n"); // Clear out trailing newline
return true;
}
<小时/>
示例输出
<小时/>gcc test.c -lm && ./a.out
Enter radius of base in cm: 1
Enter height of container in cm: 2
Enter material cost per square cm: 3
Enter the number of containers to be produced: 4
Surface area of container: 15.71 cm
Cost per container: $47.12
Total production costs: $188.50
<小时/>
关于c - C 中的 pow(user_inputed_data, 2) 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32534277/