比如数组:
data[120]={12.45,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.3
4,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.
38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12
.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,1
2.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,
12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.21},
要找出其中最大、最小值,该用何命令?
另外,我想把这120个数据分别在120个数据显示元件中显示,有什么简便指令?
谢谢!!
回答:
这是一个很精典的需求,对应有一个很精典的冒泡排序算法。
可以使用威纶触摸屏宏指令循环语句来实现对数组升序或降序冒泡排序后,
提取数组的首尾元素就为最大,最小数据。
这里编写一个实例,提供两种方法来回答。
界面如下:
威纶触摸屏中数组中最大最小值判断的宏指令函数是什么
点击两个按钮,会分别调用两个宏指令,使用两种方法对上面的数组进取最大,最小值。
两个宏指令代码如下:
- macro_command main()
- float data[120]={12.45,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,
- 12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,
- 12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,
- 12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,
- 12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,
- 12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.21}
- //假设已知最小值不超过0.0,最大值不超过1000.0
- float fMax=0.0,fMin=1000.0
- short i=0
- for i=0 to 119 step 1
- if data[i]>fMax then
- fMax = data[i]
- end if
- //
- if data[i]<fMin then
- fMin = data[i]
- end if
- next
- SetData(fMax, "Local HMI", LW, 0, 1)
- SetData(fMin, "Local HMI", LW, 2, 1)
- end macro_command
复制代码
上面这一种方法相对运算快,理解起来也简单。
前提是要知道数值的大小范围。
下面一种方法是冒泡排序算法,将数组元素从左到右升序排序。
用到了双重循环语句,模拟运行测试,速度明显慢很多。
可以根据自己需要选择使用。
宏指令代码如下:
- macro_command main()
- float data[120]={12.45,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,
- 12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,
- 12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,
- 12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,
- 12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,
- 12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,
- 12.29,12.35,12.33,12.34,12.36,12.37,12.32,12.33,12.38,12.35,12.34,12.31,12.21}
- int nLen=120//数组元素个数
- //从左到右升序冒泡排序
- int x=0,y=0,y1=0
- for x=0 to nLen-2 step 1//要排序几趟
- for y=0 to nLen-2-x step 1//每趟比较几次
- y1=y+1
- if data[y]>data[y1] then
- float b=0.0
- b=data[y1]
- data[y1]=data[y]
- data[y]=b
- end if
- next
- next
- SetData(data[119], "Local HMI", LW, 0, 1)
- SetData(data[0], "Local HMI", LW, 2, 1)
复制代码
//要将数组元素都显示,可以直接用下面SetData函数.
//要注意的是数组元素为浮点数,占用两个寄存器。
//如data[0]占用RW0,RW1。
//120表示120个浮点数值,也就是说会用到RW0--RW240共240个配方寄存器。
SetData(data[0], "Local HMI", RW, 0, 120)
end macro_command
下面为例程下载地址:
如果不熟悉宏指令,也可以看看我录制的两套威纶通视频教程。
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|