作为一个业余项目,我一直在为Chef(一种深奥的编程语言)开发IDE。在Chef中编写各种测试程序时,我意识到实现一种简单的排序算法,甚至比较两个整数以查看哪个更大是一个难点,这是该语言中唯一的“比较分支”语句是一个循环,当数字非零时将重复。例如:
Dissolve the sugar. <-- execute loop if value of 'sugar' is non zero
Add flour to mixing bowl. <-- add value of 'flour' into mixing bowl
Set aside. <-- break out of the loop
Stir until dissolved. <-- mark the end of the loop
我确实有一个可行的解决方案来比较Chef中的两个整数,但是它长40行!
这里相当于我在Java中的方法,大多数方法都比我编写的Chef代码更具可读性:-)
public static void main(String[] args) {
int first = 100;
int second = 200;
int looper = 1;
int tester;
int difference = first - second;
int inverse = difference * -1;
while (looper != 0) {
difference -= 1;
inverse -= 1;
tester = 1;
while (difference != 0) {
tester--;
break;
}
while (tester != 0) {
System.out.println("First is bigger");
exit(1);
}
tester = 1;
while (inverse != 0) {
tester--;
break;
}
while (tester != 0) {
System.out.println("Second is bigger");
exit(1);
}
}
}
我的问题是,当我只有一个非零循环时,比较两个数字的最佳方法是什么?