EDIT:在print_tables函数中,我有for循环,相应填充所有值。有什么问题是,我试图计算每个元素的值,但是有错误的输入填充。var的弹性和numOfDimples是正确的,但是var距离和它的计算是错误的。对于计算
dimpleFactor = 120 - numOfDimples;
distance = elasticity * (800 - (dimpleFactor * dimpleFactor));
它是在乘以弹性的最终结果,而不是相应地乘以它。例如:
假设elasticity = 0.14, numOfDimples = 102。所以当for循环第一次执行的时候弹性= 1.12(弹性的最后值),所以这几乎会破坏方程。
这是完整的代码,任何修复它的建议都将非常感激。就像如果我应该使用结构体或联合(因为我对C真的很陌生)。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
const float MIN = 0.0;
const float MAX_ELAS = 1.0;
const float MAX_DIFFERENCE = 10.0;
void get_data(float *minDimples, float *maxDimples, float *elasticity);
void print_tables(float minDimples, float maxDimples, float elasticity);
void get_data(float *minDimples, float *maxDimples, float *elasticity)
{
float minMaxDifference;
bool minBool, maxBool, elasBool = false;
minBool = maxBool = elasBool; //set booleans to false
/* Prompt the user for data using a do-while loop
* Exit the loop when data is valid
*/
do {
/* Get MINIMUM number of dimples data */
printf("Enter the minimum number of dimples:n");
scanf("%f", minDimples);
/* Check if it's valid */
if (*minDimples < MIN) {
//invalid data entered
minBool = false;
}
else {
minBool = true;
}
/* Get MAXIMUM number of dimples data */
printf("Enter the maximum number of dimples:n");
scanf("%f", maxDimples);
/* Check if it's valid */
if (*maxDimples < MIN || *maxDimples < *minDimples) {
maxBool = false;
}
else {
maxBool = true;
}
minMaxDifference = *maxDimples - *minDimples;
if (minMaxDifference > 10) {
minBool = false;
}
/* Get elasticity step size data */
printf("Enter the elasticity step size:n");
scanf("%f", elasticity);
/* Check if it's valid */
if (*elasticity < MIN || *elasticity > MAX_ELAS) {
elasBool = false;
}
else {
elasBool = true;
}
/* Print error message if something is invalid */
if (elasBool == false || minBool == false || maxBool == false) {
printf("Sorry, but your data is invalidn");
}
else {
/* Do nothing */
printf("");
}
} while (elasBool == false || minBool == false || maxBool == false);
}
void print_tables(float minDimples, float maxDimples, float elasticity)
{
float elasticityLength, minMaxDifference, setElasticity, dimpleFactor, distance, numOfDimples;
float table[10][10]; //elasticity step size's and distance's (rows cannot be longer than 10 & columns cannot be > 100)
int rows, columns, i, j;
setElasticity = elasticity;
numOfDimples = minDimples;
elasticityLength = MAX_ELAS / elasticity; //How many times does elasticity step size go into 1? /Columns
minMaxDifference = maxDimples - minDimples; //How many times should rows go along? /Rows
/* This loop is for filling the table 2D array */
for (rows = 0; rows <= minMaxDifference; rows++) {
elasticity = setElasticity;
for (columns = 0; columns < elasticityLength; columns++) {
/* Setting the dimple size's. table[0][i] */
if (columns == 0 && rows >= 1) {
printf("%.0ft", numOfDimples);
table[columns][rows] = numOfDimples;
if (numOfDimples <= maxDimples) {
numOfDimples++;
}
}
if (rows != 0 && columns != 0) {
dimpleFactor = 120 - numOfDimples;
distance = elasticity * (800 - (dimpleFactor * dimpleFactor));
// <<< THIS calculation is wrong
printf("%.1ft", distance);
table[columns][rows] = distance;
}
/* Setting the elasticity along column 1. table[i][0] */
if (rows == 0 && columns >= 1) {
printf("%.02fn", elasticity);
table[columns][rows] = elasticity;
elasticity += setElasticity;
}
}
}
printf("n-------------------n");
/* Print the table */
for (i = 0; i <= minMaxDifference; i++) {
for (j = 0; j < elasticityLength; j++) {
printf("%0.2ft", table[i][j]);
}
printf("n");
}
}
int main(int argc, char *argv[])
{
float minDimples, maxDimples, elasticity;
get_data(&minDimples, &maxDimples, &elasticity);
print_tables(minDimples, maxDimples, elasticity);
return 0;
}
我试过把列循环外,但仍然没有运气。我想让它看起来像http://www.homeandlearn.co.uk/powerpoint/powerpoint_p3s21.html(距离有正确的值)。
任何建议或解决方案将不胜感激!另外,使用二维数组适合吗?
至于代码结构,考虑到整个2D数组只是print_tables
的局部数组,并且唯一的目的是打印数组,您不妨完全删除数组并直接打印值,而不是暂时存储它们,只是为了在打印它们的数组上重复。
我也处理"标题"行和列从数据行分开;这通过删除if
来简化代码,因此更容易跟踪正在发生的事情。例如:
#include <stdio.h>
#include <math.h>
const static float MAX_ELAS = 1.0;
void print_tables(unsigned minDimples, unsigned maxDimples, float minElasticity) {
const unsigned elasticityLength = (unsigned) ceilf(MAX_ELAS / minElasticity);
float elasticity = minElasticity;
// Header row
(void) printf(" "); // Empty (0,0) cell
for (unsigned col = 0; col < elasticityLength; ++col) {
(void) printf("t%.02f", elasticity);
elasticity += minElasticity;
}
for (unsigned dimples = minDimples; dimples <= maxDimples; ++dimples) {
float dimpleMultiplier = 120.0f - dimples;
dimpleMultiplier = 800.0f - (dimpleMultiplier * dimpleMultiplier);
elasticity = minElasticity;
(void) printf("n%u", dimples); // Header column
for (unsigned col = 0; col < elasticityLength; ++col) {
float distance = elasticity * dimpleMultiplier;
(void) printf("t%.1f", distance);
elasticity += minElasticity;
}
}
(void) printf("n"); // Terminate last row
}
int main (void) {
// TODO: Read input instead of hard-coded values (or use command-line?)
print_tables(121, 125, 0.14);
return 0;
}
输出:
0.14 0.28 0.42 0.56 0.70 0.84 0.98 1.12
121 111.9 223.7 335.6 447.4 559.3 671.2 783.0 894.9
122 111.4 222.9 334.3 445.8 557.2 668.6 780.1 891.5
123 110.7 221.5 332.2 443.0 553.7 664.4 775.2 885.9
124 109.8 219.5 329.3 439.0 548.8 658.6 768.3 878.1
125 108.5 217.0 325.5 434.0 542.5 651.0 759.5 868.0
作为进一步清理的建议,可以指定elasticityStep
,即elasticity
在列之间的增量,并将minElasticity
, maxElasticity
分开,而不是elasticityLength
。那么列循环就像这样:
for (elasticity = minElasticity; elasticity <= maxElasticity; elasticity += step)