需求:输入A数值范围映射出B数值范围,且A数值的中值不等于B数值的中值,也就是有比例系数偏移
已知:A数值范围,B数值范围,A数值的中值及其对应的B数值
#include <iostream>
#include <string>
using namespace std;
// 输入A数值范围映射出B数值范围
#define A_MIN 0
#define A_MAX 100
#define A_MID (A_MAX/2)
#define B_MIN 0
#define B_MAX 50
#define B_MID 30
// 比例系数1 (B_MID - (x - A_MID) / 10)
// 比例系数2 (B_MID + (A_MID - x) / 10)
// 此比例系数适用于B_MID值为20到30区间
int OutPutValue(int x)
{
if (x >= A_MID)
{
return (B_MID - (x - A_MID) / 10)*x / A_MID;
}
else
{
return (B_MID + (A_MID - x) / 10)*x / A_MID;
}
}
int main() {
int x_value,y_value;
printf("please input num:");
cin >> x_value; // 输入参数
y_value = OutPutValue(x_value);
printf("calculate output:");
cout << y_value; // 输出参数
return 0;
}
2022-11
实例:
显示的数值范围是0到100
实际的参数范围是0到50
并且显示的数值是50的时候,需要对应实际的参数34
输入数值范围0~100
输出数值范围5~100
int OutPut(int x)
{
if(x>=65)
// 输入65~100,输出46~100
return x/2*3-50;
else if((x>30)&&(x<65))
// 输入31~65,输出21~45
return (x-3)*3/4);
else
// 输入0~30,输出5~20
return (x+10)/2);
}
假设显示的数值是x参数,对应的实际参数是y参数
那么这个xy的数学模型与开平方根模型很相似
5√100=50
5√50=35
那么数学模型直接就是y=5√x
开平方根的代码如下(详细请看手动开平方数-2022):
求N位数字的开平方数(整数)
#include <iostream>
#include <string>
using namespace std;
unsigned int sqrtN(unsigned long M)
{
unsigned int N=0,Nbit=0,Nbit1,Nbit2, i;
unsigned int tmp,temp, ttp; // 结果、循环计数
temp = M;
if (M == 0) // 被开方数,开方结果也为0
return 0;
else if (M <= 3)
return 1;
if (M > 268435455)
Nbit1 = 30;
else if (M > 16777215)
Nbit1 = 26;
else if (M > 1048575)
Nbit1 = 22;
else if (M > 65535)
Nbit1 = 18;
else if (M > 4095)
Nbit1 = 14;
else if (M > 255)
Nbit1 = 10;
else if (M > 15)
Nbit1 = 6;
else if (M <= 15)
Nbit1 = 2;
Nbit2 = Nbit1 / 2 ;
tmp = (M >> Nbit1); // 获取最高位:B[m-1]
N = 0;
if (tmp >= 1) // 最高位为1
{
N++; // 结果当前位为1,否则为默认的0
tmp = tmp-1;
}
for (i = Nbit2; i > 0; i--) // 求剩余的分组
{
M = (M<<2);
tmp = (tmp << 2) ;
tmp += ((M >> Nbit1)&0x03); // 假设
ttp = N;
ttp = (ttp << 2) +1;
N <<= 1; // 左移一位
if (tmp >= ttp) // 假设成立
{
tmp -= ttp;
N++;
}
}
return N;
}
int main() {
long i;
for (i = 1; i <= 4096; i++)
{
cout << "i=" << i << endl;
cout << "output=" << sqrtN(i) << endl;
}
return 0;
}