代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
float Res_cal(float a, float b, float c, float d);
float Res_cal2(float a, float b, float c);
int float_equal(float x, float y, float epsilon);
void screen1(float res);
void screen2(float res);
float arr1[32] = {0.1, 0.5, 1, 1.5, 2, 2.5, 3, 3.3, 4, 5, 6, 8, 10, 12, 15, 20, 22, 25, 30, 33, 39, 47, 50, 68, 75, 100, 120, 150, 200, 300, 330, 500};
void main() {
float res, mistake;
int num, flag;
//printf("%f\n", arr1[0]);
flag = 0;
while (true) {
printf("请输入需要计算的阻值(范围0.1-100):");
scanf("%f", &res);
printf("请输入并联电阻个数(3或者4):");
scanf("%d", &num);
for (int i = 0; i < 32; i++) {
if (float_equal(res, arr1[i], 0.0001)) {
printf("arr1[%d]:%g\r\n", i, arr1[i]);
flag = 1;
break;
}
}
if ( !flag) {
if (3 == num)
screen1(res);
else
screen2(res);
}
flag = 0;
printf("***********************************************************\r\n");
}
}
float Res_cal(float a, float b, float c, float d) {
float result;
result = 1 / (1 / a + 1 / b + 1 / c + 1 / d);
return result;
}
float Res_cal2(float a, float b, float c) {
float result;
result = 1 / (1 / a + 1 / b + 1 / c );
return result;
}
int float_equal(float x, float y, float epsilon) {
if (fabs(x - y) < epsilon)
return 1;
else
return 0;
}
void screen2(float res) {
int arr[4];
float result, flag = 1;
for (int i = 0; i < 32; i++) {
for (int j = i; j < 32; j++) {
for (int k = i; k < 32; k++) {
for (int l = i; l < 32; l++) {
if (flag > fabs(Res_cal(arr1[i], arr1[j], arr1[k], arr1[l]) - res)) {
flag = fabs(Res_cal(arr1[i], arr1[j], arr1[k], arr1[l]) - res);
result = Res_cal(arr1[i], arr1[j], arr1[k], arr1[l]);
arr[0] = i;
arr[1] = j;
arr[2] = k;
arr[3] = l;
}
}
}
}
}
printf("arr1[%d]:%g arr1[%d]:%g arr1[%d]:%g arr1[%d]:%g 阻值:%g\r\n", arr[0], arr1[arr[0]], arr[1], arr1[arr[1]],
arr[2],
arr1[arr[2]], arr[3], arr1[arr[3]], result);
}
void screen1(float res) {
int arr[3];
float result, flag = 1;
for (int i = 0; i < 32; i++) {
for (int j = i; j < 32; j++) {
for (int k = i; k < 32; k++) {
if (flag > fabs(Res_cal2(arr1[i], arr1[j], arr1[k]) - res)) {
flag = fabs(Res_cal2(arr1[i], arr1[j], arr1[k]) - res);
result = Res_cal2(arr1[i], arr1[j], arr1[k]);
arr[0] = i;
arr[1] = j;
arr[2] = k;
}
}
}
}
printf("arr1[%d]:%g arr1[%d]:%g arr1[%d]:%g 阻值:%g\r\n", arr[0], arr1[arr[0]], arr[1], arr1[arr[1]], arr[2],
arr1[arr[2]], result);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。