mfp吧 关注:778贴子:4,978
  • 18回复贴,共1

数组赋值问题

只看楼主收藏回复

function pqx()
variable fd = fopen("scripts/manual/练习/wb.txt", "r")
variable k
variable aa_k,ax_k,ay_q
k=1
while feof(fd)==false
aa_k=freadline(fd)
ax_k= clone(fscanf(aa_k,"%f%f%f%f%f%f%f"))
print(ax_k+"\n")
k=k+1
loop
for variable q=1 to k-1 step 1
ay_q=ax_k
print("输出"+ay_q+"\n")
next
fclose(fd)
endf
我想把ax_k(1~4)数组分别赋值给ay_1~ay_4参与后续的计算,但是现在ay_1~ay_4数组的值
都是默认ax_4数组的值,请问怎么修改代码。感谢!!


IP属地:广东1楼2022-06-25 11:38回复
    你首先要读取txt里面的行数,再用行数定义个二维数组,比如有n行,那就逐个输出ax_k[n]
    mfp数组的表示为中括号[] ,ax_k[0] 表示 ax_k这数组的第一个值,ax_k[1] 表示 ax_k这数组里的第二个值,以此类推


    IP属地:四川2楼2022-06-25 21:14
    回复
      function pqx()
      variable fd = fopen("scripts/manual/练习/wb.txt", "r")
      variable aa_k,ax_k,ay_q,aa,k
      k=1
      while feof(fd)==false
      aa_k=freadline(fd)
      aa=clone(fscanf(aa_k,"%s"))
      print(k+"\n")
      ax_k=set_array_elem(ax_k,[k-1],aa)
      print(ax_k[k-1]+"\n")
      k=k+1
      loop
      fclose(fd)
      ay_q=ax_k
      for variable q=0 to k-2 step 1
      print("输出"+ay_q[q]+"\n")
      next
      endf


      IP属地:四川5楼2022-06-25 22:17
      回复
        十分感谢大佬解惑,程序有一点错误,已经修改如图




        IP属地:广东来自Android客户端6楼2022-06-26 00:01
        回复
          function rd()//读内存卡数据转换成矩阵
          variable h=0,line,data
          variable fd=fopen("txt.txt","r")
          while !feof(fd)
          line=trim(freadline(fd))
          if strlen(line)==0
          continue
          endif
          line=evaluate("["+line+"]")
          data=set_array_elem(data,h,line)
          h=h+1
          loop
          fclose(fd)
          print(data)
          endf


          IP属地:四川7楼2022-07-15 19:05
          收起回复
            再请教一个问题
            function fsjs()
            variable fd = fopen("scripts/manual/复数/复数.txt", "r")
            variable aa_k,ax_k,ax_q,ay_p,ay_r,aa,k
            k=1
            while feof(fd)==false
            aa_k=freadline(fd)
            aa=clone(fscanf(aa_k,"%f%f%f"))
            ax_k=set_array_elem(ax_k,[k-1],aa)
            k=k+1
            loop
            ax_q=ax_k
            fclose(fd)
            print("数组"+ax_k+"\n")
            endf
            读取不了复数格式(见截图),把aa=clone(fscanf(aa_k,"%f%f%f")) 改为aa=clone(fscanf(aa_k,"%s%s%s")) ,读取出的式字符串(见截图),怎么把字符串转换为数据能参与运算。



            IP属地:广东8楼2022-07-16 12:06
            收起回复
              可以参照


              IP属地:山西来自Android客户端9楼2022-07-17 10:00
              收起回复
                function dfs()//读取复数数据示例
                variable data,h=0
                variable sjk4
                variable fd=fopen("scripts/manual/复数/复数.txt","r")
                while !feof(fd)
                variable sjk2=freadline(fd)
                if strlen(sjk2)==0
                continue
                endif
                variable sjk3=split(sjk2,",")
                variable g=size(sjk3)[0]
                for variable n=0 to g-1
                sjk4=set_array_elem(sjk4,[n],evaluate(sjk3[n]))
                next
                data=set_array_elem(data,[h],sjk4)
                h=h+1
                loop
                fclose(fd)
                print(data+"\n")
                endf


                IP属地:四川10楼2022-07-19 22:11
                回复