文档库 最新最全的文档下载
当前位置:文档库 › VF程序设计经典例题

VF程序设计经典例题

VF程序设计经典例题
VF程序设计经典例题

1. 求园的面积(要求判断半径是否合理)

Clea

Inpu ‘r=’ to r

If r>0

S=3.14*r*r

?s

Else

?’半径错误!’

Endif

2. 求分段函数Y 的值

Clea Inpu ‘x=’If x>0

Y=2*x+5

Else

If x=0

Y=x

Else

Y=abs(x)

Endif

Endif

?y

3. 输入一个百分制成绩判断其等级(优/良/中/及格/不及格)

Clea

Inpu ‘cj=’ to cj

Do case

Case cj>=90

?’优’

Case cj>=80

?’良’

Case cj>=70

?’中’

Case cj>=60

?’及格’

Orth

?’不及格’

Endcase

4. 输入若干个(个数不定)百分制成绩判断其等级(优/良/中/及格/不及格)

Clea

Inpu ‘请输入处理几个人的成绩:’ to n

For i=1 to n

Inpu ‘cj=’ to cj

Do case

2x +5 x>0 x x=0 | x | x<0

Case cj>=90

?’优’

Case cj>=80

?’良’

Case cj>=70

?’中’

Case cj>=60

?’及格’

Orth

?’不及格’

Endcase

endfor

5.求S=1+2+3. . . . . .+100

C

lea

S=0

For i=1 to 100

S=s+i

Endfor

?s

6.求S=1×2×3. . . . . .×100

Clea

p=1

For i=1 to 100

p=p*i

Endfor

?p

7.求S=1+3+5.. . . . .+99

Clea

S=0

For i=1 to 99 step 2

S=s+i

Endfor

?s

8.求S=1-2+3-4. . . . . .-100

Clea

S=0

For i=1 to 100

S=s+(-1)^(i+1)*i

Endfor

?s

9.求S=1+1/2+2/3+3/5. . . . . .前10项之和Clea

S=0

A=1

B=1

For i=1 to 10

S=s+a/b

T=a

A=b

B=t+b

Endfor

?s

10. 求S =1!+2!+3!. . . . . .+10!

Clea

S=0

P=1

For i=1 to 10

P=p*i

S=s+p

Endfor

?s

11. 对学生表中所有入学成绩≥650分的学生免去贷款

Clea

Use 学生

Scan for 入学成绩>=650 .and. 贷款否=.t.

Repl 贷款否 with .f.

Endscan

Use

12. 输出图形

Clea For i=1 to 4

For j=1 to i

??’*’

Endfor

?

Endfor

Clea

For i=1 to 4

For j=1 to 4-i

??’ ’ &&有一个空格

Endfor

For j=1 to 2*i-1

??’*’

* **** * *******

* **** ** *** *** *****

** *** *** ** ***** ***

*** ** **** * ******* *

**** *

?

Endfor

Clea

For i=1 to 4

For j=1 to 4-i

??’’&&有一个空格Endfor

For j=1 to i

??’*’

Endfor

?

Endfor

13.判断一个整数是否素数Clea

Inpu ‘x=’ to x

For i=2 to x-1

If mod(x,i)<>0

Loop

Else

Exit

Endif

Endfor

If i>x-1

?x,’是素数’

Else

?x,’不是素数’

Endif

14.判断十个整数是否素数Clea

For j=1 to 10

Inpu ‘x=’ to x

For i=2 to x-1

If mod(x,i)<>0

Loop

Else

Exit

Endif

Endfor

If i>x-1

?x,’是素数’

Else

?x,’不是素数’

Endif

15.找出两个数的大数和小数Clea

Inpu ‘x=’ to x

Inpu ‘y’to y

If x>y

?x,’大’,y,’小’

Else

?y,’大’,x,’小’

Endif

16.找出三个数的最大数和最小数Clea

Inpu ‘x=’ to x

Inpu ‘y’to y

Inpu ‘z’to z

If x

t=x

x=y

y=t

Else

If x

t=x

x=z

z=t

endif

endif

if y

t=y

y=z

z=t

endif

?x,’是最大数’,z,’是最小数’

17.找出十个数的最大数和最小数Clea

Dime a(10)

For i=1 to 10

Inpu to a(i)

Endfor

Max=a(1)

Min=a(1)

For i=2 to 10

If max

Max=a(i)

Else

If min>a(i)

.

Min=a(i)

Endif

Endif

Endfor

?max,min

18.找出2×3矩阵中的最大数和最小数

clea

dime a(2,3)

for i=1 to 2

for j=1 to 3

input ‘a(‘+str(I,2)+’,’+str(j,2)+’)=’to a(I,j) endfor

endfor

max=a(1,1)

min=a(1,1)

for i=1 to 2

for j=1 to 3

if max

max= a(I,j)

else

if min> a(I,j)

min= a(I,j)

endif

endif

endfor

endfor

?’max=’,max,’min=’,min

19.对三个整数从大到小排序

Clear

Input ‘a=’ to a

Input ‘b=’ to b

Input ‘c=’ to c

If a

T=a

A=b

B=t

Else

If a

t=a

A=c

c=t

endif

endif

If b

T=b

A=c

c=t

endif

?a,b,c

20.对十个整数从大到小排序(用选择法和起泡法两种方法)选择法:

Clear

Dime a(10)

For i=1 to 10

Input to a(i)

Endfor

For i=1 to 9

Max=a(i)

Num=i

For j=i+1 to 10

If max

max=a(j)

Num=j

Endif

Endfor

If i<>num

t=A(i)

a(i)=a(num)

a(num)=t

Endif

Endfor

For i=1 to 10

??a(i),' '

Endfor

起泡法:

Clear

Dime a(10)

For i=1 to 10

Input to a(i)

Endfor

For i=1 to 9

For j=1 to 10-i

If a(j)

t=A(j)

a(j)=a(j+1)

a(j+1)=t

Endif

endfor

Endfor

Endfor

21.输出Fibonacci(斐波那契)数列的前十项Clear

Dime a(10)

a(1)=1

a(2)=1

For i=3 to 10

a(i)=a(i-1)+a(i-2)

Endfor

For i=1 to 10

??A(i)

Endfor

22.输出杨辉三角的前十行

Clear

Dime a(10,10)

For i=1 to 10

A(I,1)=1

A(I,i)=1

Endfor

For i=3 to 10

For j=2 to i-1

A(I,j)=a(i-1,j)+a(i-1,j-1)

Endfor

Endfor

For i=1 to 10

For j=1 to i

??A(I,j)

Endfor

?

Endfor

23.对2×3矩阵转置

Clea

Dime a(2,3),b(3,2)

for i=1 to 2

for j=1 to 3

input to a(I,j)

endfor

endfor

for i=1 to 3

for j=1 to 2

b(I,j)=a(j,i)

endfor

endfor

??b(I,j)

Endfor

?

endfor

24.求三位数中的所有水仙花数(即指一个三位数,其各位数字立方和等于该数本身)Clea

for x=100 to 999

a=int(x/100)

b=mod(int(x/10),10)

c=mod(x,10)

if x==a*a*a+b*b*b+c*c*c

?x

Endif

endfor

25.求100以内的所有完数(即一个数恰好等于除它本身外的所有因子之和)

Clea

for i=3 to 100

s=0

for j=1 to i-1

if mod(i,j)==0

s=s+j

endif

endfor

if i==s

?i

endif

26.已知三角形的三边(从键盘输入),求其面积(S^2=p(p-a)(p-b)(p-c), p=(a+b+c)/2)

Clear

input 'a=' to a

input 'b=' to b

input 'c=' to c

if a+b>c and a+c>b and b+c>a

p=(a+b+c)/2

s=sqrt(p*(p-a)*(p-b)*(p-c))

?s

else

?'三边不能组成三角形'

Endif

27.求二元方程的根(分三种情况:两个不等实根,两个相等实根,无实根)

clea

inpu 'a=' to a &&a<>0

inpu 'b=' to b &&b<>0

inpu 'c=' to c

i=b*b-4*a*c

if i<0

?"方程无实根!"

else

if i==0

r=(-b)/(2*a)

?"方程有两个相等实数根:",r

else

x1=(-b+sqrt(i))/(2*a)

x2=(-b-sqrt(i))/(2*a)

?"方程有两个不相等实数根:",x1,x2

endif

endif

28.输入任意一个五位整数,前后对应位置上的数据进行交换重新排列(即逆序排列)(例:25984→

48952)

clea

dime a(5)

inpu to b

a(1)=int(b/10000)

a(2)=mod(int(b/1000),10)

a(3)=mod(int(b/100),10)

a(4)=mod(int(b/10),10)

a(5)=mod(b,10)

for i=1 to int(5/2)

t=a(i)

a(i)=a(6-i)

a(6-i)=t

endfor

c=a(1)*10000+a(2)*1000+a(3)*100+a(4)*10+a(5)

?b,c

29.找出一个3x3矩阵的“鞍点”,即该位置上的元素在该行上最大,在该列上最小(也有可能没有鞍点)clea

dime a(3,3)

flag=.t.

for i=1 to 3

for j=1 to 3

input 'a('+str(I,2)+','+str(j,2)+')=' to a(i,j)

endfor

endfor

for i=1 to 3

max=a(i,1)

col=1

for j=2 to 3

if max

max=a(i,j)

col=j

endif

endfor

min=a(1,col)

row=1

for k=2 to 3

if min>a(k,col)

min=a(k,col)

row=k

endif

endfor

if max==min

?a(row,col),'是鞍点,在',row,'行',col,'列'

flag=.f.

endif

endfor

if flag==.t.

?'无鞍点'

endif

30.求S(n)=a+aa+aaa+...+aaa....aaa(其中有n个a)之值,a是一个数字,n和a由键盘键入(例如:

2+22+222+22222+22222,此时n=5)

clea

inpu 'a=' to a

inpu 'n=' to n

s=0

t=a

for i=1 to n

s=s+t

t=a+t*10

endfor

?s

31.把一张一元钞票,换成一分、二分和五分硬币,每种至少11枚,问有多少种方案? 13

clea

s=0

for a=11 to 100

for b=11 to 50

for c=11 to 20

if a+2*b+5*c=100

s=s+1

endif

endfor

endfor

next

?s

32.一只猴子一天从山上摘来一袋桃子,从这天开始,它每天都要把袋中的桃子平分为二堆,吃掉其中的一堆,然后再从剩下的桃中拿出一个解谗,等到第10天,它发现袋中只有一只桃可吃啦,问猴子总共摘了多少桃。1534

clea

dime f(10)

f(1)=1

f(2)=4

f(3)=10

s=0

for n=4 to 10

f(n)=2*f(n-1)+2

s=f(n)

endfor

?s

33.已知S1=1, S2=1+2, S3=1+2+4, S4=1+2+4+8,S5=1+2+4+8+16,…,编制一个程序求S=S1+S2+S3+S4+S5+…+S20的值。2097130

clea

dime f(20)

f(1)=1

f(2)=2

f(3)=4

s=0

q=0

for n=1 to 20

f(n)=2^(n-1)

s=s+f(n)

q=q+s

endfor

?q

相关文档