所以我们在学校里学习FoxPro
并且这个程序我不理解,也不能真正让它起作用(老师做了)。
有一张桌子:Food C(15); Food_Cost N(3)
我需要计算哪种食物最能说“受欢迎”并写下来(或者如果有更多相同数字的食物,则需要这些食物)。
以下是该计划:(error in IF B(j)>max line - operand/operator type mismatch
)
如果有人可以重写或更正它并向我解释它是如何工作的,我将不胜感激!
USE table1
COUNT TO n
DIMENSION A(n)
GO 1
i=1
A(i)=food
SCAN
R=.F.
FOR j=1 TO i
IF A(i)=food
R=.T.
ENDIF
IF R=.F.
i=i+1
A(i)=food
ENDIF
ENDFOR
ENDSCAN
FOR j=1 TO i
DIMENSION B(i)
COUNT TO B(i) FOR food=A(i)
ENDFOR
max=B(1)
FOR j=1 TO i
IF B(j)>max
max=B(j)
ENDIF
ENDFOR
@15,1 say 'Most popular food:'
FOR j=1 TO i
IF max=B(j)
@15+j,2 say A(j)
ENDIF
ENDFOR
答案 0 :(得分:1)
首先,VFP允许正常的sql查询,就像你在MySQL,SQL-Server等中可以做的那样有一些注意事项和一些细微差别,所以习惯于从其他人那里读取查询,即使它们在SQL中-Server,MySQL等.PRINCIPALS和基本语法是一样的。它们之间的最大区别是其他引擎使用“;”分号为END语法。 VFP使用“;”表明该声明在下一行继续......这也适用于所有其他VFP语法。
我首先要做一个简单的基于SQL的查询
*/ Pre-close the cursor in case previously opened...
*/ You will get to something like this later..
USE IN SELECT( "C_PopularFoods" )
*/ Run a simple SQL query and have the engine do the counting on a per-food basis
SELECT ;
food, ;
COUNT(*) as FoodCount;
FROM ;
Table1;
GROUP BY ;
food ;
ORDER BY ;
FoodCount DESC;
INTO ;
CURSOR C_PopularFoods READWRITE
“INTO CURSOR”基本上创建了另一个结果表IN MEMORY而不是a 永久表,您必须在完成后手动擦除事后。 现在,工作区/表别名“C_PopularFoods”是当前的“工作区”并且是 坐在结果集中的第一条记录。您现在可以获取食物计数栏 从那个结果集算起最受欢迎的食物的基础。
*/ Preserve the first value in case there are MULTIPLE by this same count
lnPopularFoodCount = C_PopularFoods.FoodCount
*/ Announce the popular food count... but could also be done with @say...
*/ using "?" is like a simple output to the screen.
? "The most popular food by count is: " + ALLTRIM( STR( lnPopularFoodCount ))
*/ NOW, you can do with either a do/while or scan loop. One of the difference between
*/ do/while and scan is that scan is always based on a table (or cursor) and will go
*/ through each record, and at the end of the loop will automatically skip to the next
*/ record and exit the loop once the end of the file is reached.
*/ Do/While loops can be done for any other condition, such as prompting user for
*/ something or until some condition is reached.
*/
*/ In this case, since we KNOW what the most popular food IS, just scan through those
*/ records based on a simple FOR condition
? "Most Popular Food(s) is(are):"
SCAN FOR FoodCount = lnPopularFoodCount
? C_PopularFood.Food
ENDSCAN
*/ Done, close the temp cursor we are done with it.
USE IN SELECT( "C_PopularFood" )
你的其他代码没有利用处理表,查询等的任何真正的优势。但是,如果我不得不手动执行代码,这就是我要做的。
use Table1
dimension PopFoods( reccount(), 2 )
*/ Prime the array that we have one food item, but
*/ leave it's count (array element ,2) to zero until
*/ we are IN the scan loop and actually cycle through the rows
numFoods = 1
PopFoods( 1, 1 ) = Table1.Food
PopFoods( 1, 2 ) = 0
maxFoodCount = 0
scan
*/ prep flag that it WILL BE ASSUMED a new food
isANewFood = .T.
*/ Take the current record and go through all KNOWN foods
for int i = 1 to numFoods
*/ is the current food record the same as the food already
*/ in the array list?
if( Table1.Food = PopFoods( i, 1 )
*/ Yes, already in the list... clear the isANewFood flag
isANewFood = .F.
*/ Now, just increment the counter for this food
PopFoods( i,2 ) = PopFoods( i,2 ) + 1
*/ Did this food become the new Most popular food?
if PopFoods( i, 2 ) > maxFoodCount
*/ Yes, it is a new maximum most popular food.
*/ set the maxFoodCount to whatever this count just hit
maxFoodCount = PopFoods( i, 2 )
endif
endif
endfor
*/ If we went through the entire list of KNOWN foods, and the flag
*/ for IsANewFood is still TRUE, then it was not in the list and
*/ we need to add it and set it's initial count value to 1
if isANewFood
*/ We have a new food item not already in the list
numFoods = numFoods +1
PopFoods( numFoods, 1 ) = Table1.Food
PopFoods( numFoods, 2 ) = 1
endif
endscan
*/ Ok, we are DONE with all rows in the table being put into the array
*/ dump the results.
? "The most popular food count is: " + allt( str( maxFoodCount ))
*/ Now, go through the array for all items that match the count
for int i = 1 to numFoods
if PopFoods( i, 2 ) = maxFoodCount
? PopFoods( i, 1 )
endif
endfor