我制作了一个程序,我可以输入三个变量,长度,外径和内径。输入内径后,程序冻结,弹出一个窗口,提示程序已停止响应。该程序的目的是在给定多个可变输入的情况下计算圆柱形管道的面积,体积,质量,重量等。 任何帮助将不胜感激。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159
#define ACC_GRAVITY 9.81 /* metres/sec^2 */
#define FALSE 0
#define TRUE !FALSE
int main(void)
{
/* define the required variables */
float length;
float ext_diam, int_diam;
float ext_rad, int_rad;
float volume;
float weight;
float area;
float mass;
float width;
float flag_1, flag_2, thickness, percent;
double no_sheets;
/* define some constants - could use #define */
const float density_convert = 1.0E3;
const float mm_to_metres = 1.0E-3;
const float density = 8.03; /* grams per cm^3 */
/* prompt and get values */
printf("input the length of pipe in metres: ");
scanf("%f", &length);
do
{
flag_1 = FALSE;
flag_2 = FALSE;
printf("intput the external diameter of the pipe in milimeters: ");
scanf("%f", &ext_diam);
printf("ext_diam: %f\n", ext_diam);
printf("intput the internal diameter of the pipe in milimeters: ");
scanf("%f", int_diam);
printf("int_diam: %f\n", int_diam);
if (ext_diam < int_diam)
{
printf("external diameter must be greater than the internal diameter\n");
flag_1 = TRUE;
}
else
{
percent = thickness / ext_diam * 100.0;
if(percent > 2.5 )
{
/* do calculations - conversions, area of pipe cross-section, volume
** of pipe, mass and weight of pipe */
ext_diam = ext_diam * mm_to_metres;
int_diam = int_diam * mm_to_metres;
ext_rad = ext_diam / 2.0;
int_rad = int_diam / 2.0;
area = (PI * ext_rad * ext_rad) - (PI * int_rad * int_rad);
volume = area * length;
mass = volume * density * density_convert;
weight = mass * ACC_GRAVITY;
}
else
{
printf("Width of pipe too small\n");
flag_2 = TRUE;
}
}
} while(ext_diam > 0.0);
/* output the results */
printf("area of cylinder: %f m^2\n", area);
printf("volume of steel needed: %f m^3\n", volume);
printf("mass of steel needed: %f kg\n", mass);
printf("weight of steel needed: %f newtons\n", weight);
/* compute number of sheets of steel needed - 10m is max length of a
** sheet */
no_sheets = trunc (length / 10.0) + 1;
printf("number of 10m long sheets needed: %d\n", no_sheets);
/* assume width of sheets is based on the average of the internal and
** external diameters */
width = 2.0 * PI * (ext_rad + int_rad) / 2.0;
printf("width of sheets: %f m\n",width);
system("pause");
return 0;
}
答案 0 :(得分:0)
将scanf("%f", int_diam);
更改为scanf("%f", &int_diam);
此外,您的do while
循环似乎进入无限循环。由于它取决于外径,要么改变回路条件,要么确保外径在回路内达到<0°的值。只有当用户输入do while
的非正值时,您的ext_diam
循环才会终止。
此外,假设thickness
指的是管道的厚度,请在使用此变量thickness=ext_diam-int_diam;
或您用于计算厚度的任何其他公式之前添加此等式。
答案 1 :(得分:0)
您的代码存在一些问题:
scanf("%f", int_diam);
,scanf需要一个指向变量的指针,这将起作用
scanf("%f", &int_diam);
在
行中percent = thickness / ext_diam * 100.0;
第一次使用时,厚度是不初始化(你忘了一段吗?)
不确定当ext_diam
是&lt; = 0时是否终止。
最后,有多个双向浮点转换(例如区域微积分)可能导致数据丢失