回 帖 发 新 帖 刷新版面

主题:两个程序,总不太对

1.以循环来计算1/1!+1/2!+1/3!+....+1/10!
问题:计算到n=3时就不对了

program ch6_4
implicit none
integer :: i,j,n
real :: s,k
s=0.0
write(*,*)'please input n'
read(*,*)n
do i=1,n,1
  do j=1,i,1
    k=1
    k=k*j
  end do
  s=s+1./k
end do
write(*,*)s
end


2.把输入的字符串中的空格去掉重新输出

问题:
当输入字符串:Happy new year后
输出却只有Happy

program ch6_5
implicit none
character(len=30) :: string
integer :: i,j,strlen
write(*,*)'please input a string'
read(*,*)string
strlen=len_trim(string)
do i=1,strlen
 if(string(i:i)==' ')then
   do j=i+1,strlen
    string(i:i)=string(j:j)
   end do
 end if
end do
write(*,"(a30)")string
end


求解???
Many thanks!

回复列表 (共10个回复)

沙发

1.
  do j=1,i,1
    k=1
    k=k*j
  end do
你这个k是不是用错了啊, 循环到最后 k=i, 不是你期望的哦.
  k=1
  do j=1,i,1
    k=k*j
  end do
吧.

2. 
 不明白你为什么这么做, if似乎有点废, i不变而不断改变j, 这样做没什么意义吧.

program ch6_5
implicit none
character(len=30) :: string
integer :: i,j,k, strlen
write(*,*)'please input a string'
read(*,*)string
strlen=len_trim(string)
do i=1,strlen
 if(string(i:i)==' ')then
    k=i
 end if
end do
write(*,"(a30)") string(1:k-1)
end
这样试试行不行?

板凳


the first one is solved according to your suggestion, thanks
But the second one is still not correct, no any output according to your advices.

3 楼

第二个我测试了

C:\protest>a.exe
 please input a string
we are children.
                            we

C:\protest>

我不知道你究竟是想要什么, 我的修改是揣摩你的意图而已. 如果不对的话先自己想清楚想要什么吧.

4 楼


Just as what I mentioned in first floor
I want to re-output the string after deleting the blanks inside of the original string. for example, if you input a string like: "we are children", the correct or expected output is "wearechildren", have I stated obviously?
Thanks

5 楼

program ch6_5
implicit none
character(len=30) :: string, stringoutput
integer :: i,j,k, strlen
write(*,*)'please input a string'
read(*,'(a30)')string
strlen=len_trim(string)

k=0
do i=1,strlen
 if(string(i:i)==' ')then
    cycle
 else
    stringoutput(k+1:k+1) = string(i:i)
    k=k+1
 end if
end do

if(k==0) then
  write(*, *) "all of your input are blanks."
else
  write(*,"(a30)") stringoutput(1:k)
end if
end

你讲得对, 我没认真看. 就好像24号的帖到昨天才有回复一样.
C:\protest>a.exe
 please input a string
we are children
                 wearechildren

C:\protest>

6 楼


Now you are right by switching to another train of thought, this line is completely right without doubt, Many thanks really. but I want to know the right point where my original program is wrong?

Besides, concerning about the sentence"the post of 24th just received its reply yeasterday", is it for spuring yourself or mutual encouragement?

Thanks again and happy every day.

7 楼

At first floor, i had pointed out one of the errors in your program. Indeed, I didn't find out all error until I reprogram your codes this morning. Such as " [color=FF0000]read(*,'(a30)')string[/color] "

I said "就好像24号的帖到昨天才有回复一样". I want you to know that it is too long to remember what had you said. And that should be replied in one or two days.

Read through you codes again, finally, I understood your main idea. (I think you should tell us the function of the [color=FF0000]j loop[/color]. That was pointed out at first floor.) Maybe the following is helpful for you:

program ch6_5
implicit none
character(len=30) :: string
integer :: i,j,strlen
write(*,*)'please input a string'
read(*,'(a30)')string
strlen=len_trim(string)
do i=1,strlen
 if(string(i:i)==' ')then
    [color=FF0000]string(i:strlen)=string(i+1:strlen)[/color]
 end if
end do
write(*,"(a30)")string
end

-----------
C:\protest>a.exe
 please input a string
we are children
wearechildren

C:\protest>
------------

This program is not as good as fifth floor.

8 楼

Thank you.
The problem can be substantially solved now, except a little bug as the following:

inside the if statement:
 if(string(i:i)==' ')then
    string(i:strlen-1)=string(i+1:strlen)
 end if

Question: when the content of parenthesis is (i:strlen-1), the output is wearechildrennn
and only when (i:strlen-1), wearechildren
why?

looking forward to your reply

9 楼

Yes, you are right. I just run it on gfortran without any debug setting, so the program give me the result as i typed.. In fact, my sixth floor codes has defect.

We must make sure that two sides of string argument have the same lenght.
(i:strlen-1)=(i+1:strlen) seems right. But, it lead to another problem as you typed. 
string(strlen:strlen) should be empty(or use blank to replace). you can modify it a little.

 if(string(i:i)==' ')then
    string(i:strlen-1)=string(i+1:strlen)
    string(strlen:strlen) = ' '
 end if

it has been debug in cvf6.6c. it should be fine.

I still think the code in 5th floor is better.

10 楼

the salary is in the other post

我来回复

您尚未登录,请登录后再回复。点此登录或注册