所以我正在编写一个汇编语言程序来确定封闭圆柱形罐的最佳尺寸,例如用于罐装食品的那些。有三个输入变量,我已经在汇编语言代码中创建了微积分部分:
最终材料的成本以美元/cm2 为单位。
边料成本以美元/cm2计。
罐的体积,以毫升为单位。
给定这三个输入变量,我已经确定了罐子的尺寸(高度和直径),以使罐子的成本最小化。do
我再次提出了解决这个程序的微积分部分,但很好奇使用orwhile
循环的蛮力会是什么样子。如何创建一种蛮力,产生与微积分答案几乎相同的输出,例如:
Enter the cost of end material per square cm:
.001
Enter the cost of the side material per square cm:
.003
Enter the desired volume in milliliters:
100
Calculus Answer:
Can cost: 0.24
Diameter: 7.25
Height: 2.41
Brute Force Answer:
Can cost: 0.24
Diameter: 7.25
Height: 2.41
我提出的导致微积分答案输出的微积分部分是:
********** CONSTANTS **********
TWO: EQU $40000000
PI: EQU $40490FDA
ONE_THIRD: EQU $3EAAAAAb
START_R: EQU $3C23D70A
*******************************
start: initIO * Initialize (required for I/O)
initF
setEVT
lineout p1
floatin buffer
cvtaf buffer,D5 * END cost
lineout p2
floatin buffer
cvtaf buffer,D6 * SIDE cost
lineout p3
floatin buffer
cvtaf buffer,D7 * VOLUME
**********************************************************************
** Calculus Answer
** Formula for the radius of the optimum can:
** radius = (((volume*side_cost)/(2*PI*end_cost))^(1/3)
** numerator, volume*side_cost:
move.l D7,D1 * VOLUME
fmul D6,D1 * VOLUME*SIDE_COST
** denominator, 2*PI*end_cost
move.l D5,D2 * END_COST
fmul #TWO,D2 * END_COST * 2.0
fmul #PI,D2 * END_COST * 2.0 * PI
** now take result to 1/3 power
fdiv D2,D1 * numerator/denominator
move.l #ONE_THIRD,D0
fpow D1,D0 *(numerator/denominator) ^ (1/3)
** Calulus answer done, now calculate diameter, height, cost
move.l D0,D1 * D1 has radius
fmul #TWO,D0 * D0 has diameter
cvtfa diameter,#2
** calculate height = (volume / PI*r^2)
move.l D1,D2 * radius
fmul D2,D2 * radius^2
fmul #PI,D2 * radius^2*PI
move.l D7,D3 * copy of volume
fdiv D2,D3 * vol / PI*radius^2 HEIGHT --> D3
move.l D3,D0
cvtfa height,#2
** calculate cost = SIDE_COST*SIDE_SURFACE + 2*END_COST*END_SURFACE
*** side cost:
move.l #PI,D2
fmul #TWO,D2 * 2*PI
fmul D1,D2 * 2*PI*radius
fmul D3,D2 * 2*PI*radius*height = side surface area
fmul D6,D2 * side surface area * SIDE_COST
*** end cost:
move.l #PI,D0
fmul #TWO,D0 * 2*PI
fmul D1,D0 * 2*PI*radius
fmul D1,D0 * 2*PI*radius*radius
fmul D5,D0 * 2*PI*radius*radius*END_COST
fadd D2,D0
cvtfa cost,#2
** DONE, print the calculus answer
lineout ans1
lineout ans2
lineout ans3
如果有人想使用如下所示的“do”或“while”循环为该程序创建一个蛮力,那会怎样。有人能帮我吗。
radius = 0.01
lastCost = Calculate
do:
radius = radius+0.01
newCost = Calculate
if(newCost lastCost)
goto print
lastCost = newCost
goto loop
print lastcost
只是好奇这种蛮力方法可能是什么样子,我很确定它基本上是相同的代码,只是添加了几行代码。我只想知道我可以在哪里添加这些代码行。