编辑:在print_tables函数中,我有一个for-循环,它相应地填充所有的值。它的问题是,我试图计算每个元素的值,但是有错误的输入填充。vars弹性和numOfDimples是正确的,但对于var距离和它的计算是错误的。为计算
dimpleFactor = 120 - numOfDimples;
distance = elasticity * (800 - (dimpleFactor * dimpleFactor));它是将弹性的最终结果乘以,而不是相应地乘以。例如:
假设弹性= 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 invalid\n");
}
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("%.0f\t", 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("%.1f\t", distance);
table[columns][rows] = distance;
}
/* Setting the elasticity along column 1. table[i][0] */
if (rows == 0 && columns >= 1) {
printf("%.02f\n", 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.2f\t", 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;
}我试过把它放在列循环之外,但还是没有结果。我希望它看起来有点像p3s21.html (距离有正确的值)。
如有任何建议或解决方案,将不胜感激!也是使用一个2D数组适合这一点吗?
发布于 2015-07-26 05:34:30
至于代码结构,考虑到整个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)https://stackoverflow.com/questions/31633760
复制相似问题