分享

VFP常见20道编程题

 王咸美 2018-02-13

1、求解AX^2 BX C=0的根、其中A、B、C三个参数由键盘输入。一元二次方程的求根公式是:X=-b±√b²-4ac/2a 

clear

text

一元二次方程求解ax^2 +bx+ c=0

endtext

input '请输入a的值:' to a

input '请输入b的值:' to b

input '请输入c的值:' to c

m=b*b-4*a*c

if m>=0

 x1=(-b sqrt(m))/(2*a) 

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

?'x1的值是:',x1 

?'x2的值是:',x2

else 

?'此方程无实根!'

endif

 2、编写程序将1-100之间所有能被7和3整除的整数输出 

clear

for i=1 to 100

 if i%3=0 and i%7=0 

??i 

endif

endfor

 3、编写程序计算e,e的近似值计算公式为:e=1 1/1! 1/2! 1/3! ... 1/n!,直到1/n!<0.000001为止 clear

e=1

n=1

do while .t.

k=1

for i=1 to n

k=k*i

endfor

m=1/k

e=e m

if m<0.000001

exit

endif

n=n 1

enddo

?'e=1 1/1! 1/2! 1/3! … 1/n!=',e

4、编写程序,计算1! 2! 3! .......N!=?

clear

input '请输入n的值:' to n

s=0

t=1

for i=1 to n

t=t*i

s=s t

endfor

?'1! 2! 3! .......N!=',s

5、从键盘输入十个数,将它们进行降序排列。

clear

dime a(10)

for i=1 to 10

input '请输入一个数:' to a(i)

endfor

?'降序排列为:'

for i=1 to 9

for j=i 1 to 10

if a(i)<a(j)

k=a(i)

a(i)=a(j)

a(j)=k

endif

endfor

??alltrim(str(a(i))) ' '

endfor

??alltrim(str(a(i)))

6、(1)输出有*号组成的图形:

*

***

*****

*******

*****

***

*

clear

for i=-3 to 3

?space(abs(i))

for j=1 to 7-abs(i)*2

??'*'

endfor

endfor

(2)

*

***

*****

*******

*********

clear

for i=1 to 5

?space(5-i)

for j=1 to 2*i-1

??'*'

endfor

endfor

7、编写一个程序产生一个有20项的Fibonacci数列并输出。注:Fibonacci数列的前两项为1,从第三项开始每一项是其前两项只和。

clear

a=1

b=1

??a,b

for i=1 to 18

c=a b

a=b

b=c

??c

endfor

8、九九乘法表

clear

for i=1 to 9

for j=1 to i

??alltrim(str(i)) '*' alltrim(str(j)) '=' alltrim(str(i*j)) space(3)

endfor

?

Endfor

9、显示1-100之间的所有素数,并求它们的和。

clear

s=0

??'1到100之间的素数为:'

for i=2 to 100

x=0

for j=2 to i-1

if i/j=int(i/j)

x=1

endif

endfor

if x=0

??alltrim(str(i)) ' '

s=s i

endif

endfor

?'它们的和是:',s

10、求100到999之间的水仙花数。

clear

?'100-999之间的水仙花数有:'

for i=100 to 999

k=int(i/100)

m=(int(i/10))%10

n=i%10

if k^3 m^3 n^3=i

??alltrim(str(i)) space(2)

endif

endfor

11、从键盘输入10个数,找出其中的最大数和最小数。

clear

input '请输入第1个数:' to a

k=a

for i=2 to 10

input '请输入第' alltrim(str(i)) '个数:' to b

if a<b

a=b

endif

if k>b

k=b

endif

endfor

?'其中最大的数是:',a

?'其中最小的数是:',k

12、从键盘输入一个字符串,统计其中各个字符的个数,包括数字,大小写英文字母和特殊字符。

clear

accept '请输入一串字符:' to x

store 0 to dyw,xyw,kg,sz,qt

m=len(x)

for i=1 to m

x1=substr(x,i,1)

k=asc(x1)

do case

case k=32

kg=kg 1

case k>=48 and k<=57

sz=sz 1

case k>=65 and k<=90

dyw=dyw 1

case k>=97 and k<=122

xyw=xyw 1

other

qt=qt 1

endcase

endfor

?'其中空格有: ' alltrim(str(kg)) '个'

?'大写字母有: ' alltrim(str(dyw)) '个'

?'小写字母有: ' alltrim(str(xyw)) '个'

?'数字有: ' alltrim(str(sz)) '个'

?'其它字符有: ' alltrim(str(qt)) '个'

13、输入一个十进制数,将它转换为二进制数并显示出来。

clears=''input '请输入一个十进制数:' to ado while a>0 b=a%2 a=int(a/2) s=alltrim(str(b)) senddo?'转换为二进制为:',s

14、VFP编程:有一个3×4的矩阵,要求编程求出其中值最大的那个元素的值,以及其所在的位置。

cleardime a(3,4)for i=1 to 3 for j=1 to 4 input '请输入一个值:' to a(i,j) endforendfork=a(1,1)for m=1 to 3 for n=1 to 4 r=a(m,n) if r>k k=r x1=m x2=n endif endforendfor?'其中最大的值是:',k?'它所在的位置是第' alltrim(str(x1)) '行,第' alltrim(str(x2)) '列'

15、求1000到10000之间的回文数(1001、3883、4554、7007、9999等),并求它们的和。

clear

s=0

?'1000到10000之间的回文数有:'

for i=1000 to 10000

m=alltrim(str(i))

zx=''

dx=''

for j=1 to 4

zx=zx substr(m,j,1)

dx=dx substr(m,5-j,1)

endfor

if zx=dx

??m space(3)

s=s i

endif

endfor

?'它们的和是:',s

16、(1)输入两个数,求它们的最大公约数。

clearinput '请输入第1个数:' to ainput '请输入第2个数:' to bc=min(a,b)do while c>0 if a%c=0 and b%c=0 s=c exit endif c=c-1enddo?'它们的最大公约数是:',s

(2)输入两个数,求它们的最小公倍数。

clearinput '请输入第1个数:' to ainput '请输入第2个数:' to bc=max(a,b)do while .t. if c%a=0 and c%b=0 s=c exit endif c=c 1enddo?'它们的最小公倍数是:',s

17、求s=a aa aaa aaaa... aaaaaaa...a (n个a) ,a是一个数字,n和a用键盘键入

clearinput '请输入a的值:' to ainput '请输入n的值:' to nk=as=afor i=2 to n k=a*10^(i-1) k s=s kendfor?'s=a aa aaa aaaa... aaaaaaa...a (n个a)=',s

18、用vf求100以内的所有完数

clear?'1到100内的所有完数为:'for i=1 to 100 s=0 for j=1 to int(i/2) if i%j=0 s=s j endif endfor if s=i ??i endifendfor

19、用vf求s=1 1/2 2/3 3/5 ......,前10项之和

clears=1a=1b=1for i=1 to 9 c=a b a=b b=c k=a/b s=s kendfor?'s=1 1/2 2/3 3/5 ......,前10项之和是:',s

20、1/2 2/3 3/5 5/8 ......,前20项之后

clear

a=1

b=2

s=0

for i=1 to 20

s=s a/b

c=a b

a=b

b=c

endfor

?'1/2 2/3 3/5 5/8 ......的前20项之后为:',s

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多