加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Res_calculate.c 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
沉默的云 提交于 2024-05-08 14:25 . 第一次提交
#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);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化