刘征瀛吧 关注:20贴子:5,281

一些有用的程序

只看楼主收藏回复

把自己用C语言编的一些有用的程序贴到这


1楼2008-10-17 20:41回复
    #include <stdio.h>
    #include <math.h>
    #define B 2
    void main()
    {
    int i,a,b,result;
    scanf("%d",&a);
    i=0;
    result=0;
    while (a>0)
    {
    b=a%B;
    result+=b*(int)pow(10,i);
     a=a/B;
    i++;
    }
    printf("%d\n",result);
    }


    2楼2008-10-17 20:42
    回复
      #include <stdio.h>
      #include <math.h>
      void main()
      {
      int x,u,i=1,result=0;
      scanf("%d",&x);
      u=(int)log10(x)+1;
      while (i<=u)
      {
      result+=(int)(x%(int)pow(10,i)/pow(10,i-1))*pow(10,u-i);
      i++;
      }
      printf("%d\n",result);
      }


      3楼2008-10-17 20:45
      回复
        我确定我不学C++……
        要不然我把我的Matlab语言也贴到这里,看哪个容易…


        4楼2008-10-17 23:11
        回复
          %what can be changed to fit different computers:
          %clockmax,dt,step
          clear;
          figure(1);
          clf;
          G=1;
          N=500;%the whole body consist of 500 bodies
          k=2500;%a parameter 
          clockmax=5000;% that is too small, not acurate but fast..recommand:>2000
          dt=1/2000 ;% cannot be larger, so this program cannot run faster
          time=clockmax*dt;
          r0=1;
          r1=N^(1/3)*r0;% so N bodies have the same volume as the whole body
          L=3*r1;
          m0=1000;
          W=sqrt(G*m0*N/r1^3);% W must be large enough so that it can be seen, so m0 have to be large
          %r(1)=0;a(1)=0;b(1)=0;x(1)=0;y(1)=0;z(1)=0;u(1)=0;v(1)=0;w(1)=0;m(1)=m0;
          %it's first designed as a massive center, but finnally I decided not use this
          clock=1;record=1;
          step=5;%based on computer's memory,too small will make computer very slow.
          while clock<=N % to make them in random place, but have to be symatric so that the mass center is the origin.
           x1=(rand-0.5)*r1*2;
           y1=(rand-0.5)*r1*2;
           z1=(rand-0.5)*r1*2;
           if x1^2+y1^2+z1^2<=r1^2;%in the sphere
           x(clock)=x1;
           y(clock)=y1;
           z(clock)=z1;
           x(clock+1)=-x1;
           y(clock+1)=-y1;
           z(clock+1)=-z1;
           %r(clock)=sqrt(x1^2+y1^2+z1^2); useless
           %r(clock+1)=sqrt(x1^2+y1^2+z1^2);
           clock=clock+2;
           end%else nothing happend.
          end
          %save them, new part.
          xsave=x;
          ysave=y;
          zsave=z;
          for i=2:N%I first use spherical coordinate, but that means center have much more bodies, much higher dencity
           %r(i)=r1*rand;
           %a(i)=2*pi*rand;
           %b(i)=2*pi*rand;
           %x(i)=r(i)*cos(b(i))*cos(a(i));
           %y(i)=r(i)*cos(b(i))*sin(a(i));
           %z(i)=r(i)*sin(b(i)); 
           u(i)=-W*y(i);%keep this part to determine velocity and mass
           v(i)=W*x(i);
           w(i)=0;
           m(i)=m0;
          end
          %2*2 subplot, x-y,y-z,3D and a graph to show the radius.
          set(gcf,'double','on');
          subplot(2,2,1),h1=plot(x,y,'ro');
          axis([-L,L,-L,L]);
          title('x-y')
          xlabel('x'),ylabel('y')
          set(gcf,'double','on');
          subplot(2,2,2),h2=plot(y,z,'ro');
          axis([-L,L,-L,L])
          title('z-y')
          xlabel('z'),ylabel('y')
          set(gcf,'double','on');
          subplot(2,2,4),h=plot3(x,y,z,'ro');
          axis([-L,L,-L,L,-L,L]);
          title('3D plot')
          %I didn't set a hook,but use "hold on" and plot many dot on the graph tohave a line
          subplot(2,2,3),hold on
          axis([0,time,.5*r1,3*r1])
          xlabel('time'),ylabel('maxium on each axis')
          plot (0,max(abs(x)),'b.')
          plot (0,max(abs(x)),'y.')
          plot (0,max(abs(x)),'g.')
          legend('X','Y','Z')
          drawnow
          for clock=1:clockmax
           t(clock)=clock*dt;
           for i=1:N
           for j=i+1:N % a way to make computer compute less
           r=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2+(z(i)-z(j))^2);
           if(r>2*r0)% not touched
           du=G*dt*m0*(x(j)-x(i))/r^3;
           dv=G*dt*m0*(y(j)-y(i))/r^3;
           dw=G*dt*m0*(z(j)-z(i))/r^3;
           u(i)=u(i)+du;
           v(i)=v(i)+dv;
           w(i)=w(i)+dw;
           u(j)=u(j)-du;
           v(j)=v(j)-dv;
           w(j)=w(j)-dw;
           end
           if(r<2*r0)% touched
           du=-dt*k*(2*r0-r)*(x(j)-x(i))/r;
           dv=-dt*k*(2*r0-r)*(y(j)-y(i))/r;
           dw=-dt*k*(2*r0-r)*(z(j)-z(i))/r;
           u(i)=u(i)+du;
           v(i)=v(i)+dv;
           w(i)=w(i)+dw;
           u(j)=u(j)-du;
           v(j)=v(j)-dv;
           w(j)=w(j)-dw;
           end
           end
           end
           for i=1:N
           x(i)=x(i)+dt*u(i);
           y(i)=y(i)+dt*v(i);
           z(i)=z(i)+dt*w(i);
           end
           figure(1)
           set(h,'xdata',x,'ydata',y,'zdata',z);
           set(h1,'xdata',x,'ydata',y);
           set(h2,'xdata',z,'ydata',y);
           drawnow
           %find radius on each axis
           xm(clock)=max(abs(x));
           ym(clock)=max(abs(y));
           zm(clock)=max(abs(z));
           subplot(2,2,3)%here I use avrage so that it won't change so dramatically.
           plot (t(clock),mean(xm),'b.')
           plot (t(clock),mean(ym),'y.')
           plot (t(clock),mean(zm),'g.')
           drawnow
           %new part, to save and run it faster later
           %careful! use a lot of memory!!
           if (step*record<clock)
           xsave=[xsave;x];
           ysave=[ysave;y];
           zsave=[zsave;z];
           record=record+1
           end
          end
          pause
          for i=1:record
           figure(2)
           subplot(2,2,1)
           plot3(xsave(i,:),ysave(i,:),zsave(i,:),'ro')
           axis([-L,L,-L,L,-L,L]);
           subplot(2,2,2)
           plot(xsave(i,:),zsave(i,:),'ro')
           axis([-L,L,-L,L]);
           title('x-z')
           xlabel('x'),ylabel('z')
           subplot(2,2,3)
           plot(ysave(i,:),zsave(i,:),'ro')
           axis([-L,L,-L,L]);
           title('z-y')
           xlabel('z'),ylabel('y')
           subplot(2,2,4)
           plot(xsave(i,:),ysave(i,:),'ro')
           axis([-L,L,-L,L]);
           title('x-y')
           xlabel('x'),ylabel('y')
           pause (0.1)
          end


          5楼2008-10-17 23:13
          回复
            #include<stdio.h>
            void main()
            {
            int a,b;
            while(0<1)
            {

            scanf("%d",&a);
            void qwm(int x,int y);
            for(b=1;b<=94;b++)
            {
            qwm(a,b);
            if(b%10==0)
            {
            putchar(10);
            }
            }
            putchar(10);
            }
            }

            void qwm(int x,int y)
            {
            printf("%c%c",x+160,y+160);
            }


            6楼2008-10-31 21:07
            回复
              上面是
              显示区位


              7楼2008-10-31 21:08
              回复
                #include<stdio.h>
                #include<math.h>
                void main() 
                {
                int a[100],i,wei,d,f,result=0,num=0;
                for(i=0;i<100;i++)
                {
                a[i]=getchar();
                if(a[i]==' ')break;
                }
                wei=i;
                scanf("%d %d",&d,&f);
                if(f==1)
                {
                for(i=0;i<wei;i++)
                {
                if(a[i]>='0'&&a[i]<='9')result+=(a[i]-48)*(int)pow(d,wei-i-1);
                else if(a[i]>='A'&&a[i]<='Z')result+=(a[i]-55)*(int)pow(d,wei-i-1);
                else if(a[i]>='a'&&a[i]<='z')result+=(a[i]-87)*(int)pow(d,wei-i-1);
                }
                printf("%d\n",result);
                }
                else if(f==0)
                {
                for(i=0;i<wei;i++)
                {
                num+=(a[i]-48)*(int)pow(10,wei-i-1);
                }
                for(i=0;num!=0;i++)
                {
                a[i]=num%d;
                if(a[i]<=9&&a[i]>=0)a[i]+=48;
                else if(a[i]>=10)a[i]+=55;
                num=num/d;
                }
                wei=i;
                for(i=wei-1;i>=0;i--)printf("%c",a[i]);
                putchar(10);
                }
                }


                9楼2008-11-14 21:20
                回复
                  #include <stdio.h>
                  #include <math.h>
                  void main()
                  {
                  int shu,yinzi=1,i,j,b;
                  scanf("%d",&shu);
                  printf("%d=",shu);
                  for(i=1;;i++)
                  {
                  b=(int)sqrt(shu);
                  for(j=2;j<=b;j++)
                  {
                  if(shu%j==0)break;
                  }
                  if(j!=(b+1)){yinzi=j,shu=shu/yinzi;printf("%d*",yinzi);continue;}
                  else{printf("%d",shu);putchar(10);break;}
                  }
                  }


                  10楼2008-11-16 22:28
                  回复
                    int zhishu(int x)
                    {
                    int b,j;
                    b=(int)sqrt(x);
                    for(j=2;j<=b;j++)
                    {
                    if(x%j==0)break;
                    }
                    if(j!=(b+1))return(0);
                    else return(1);
                    }


                    11楼2008-11-16 22:48
                    回复
                      找点有共同语言的吧……大家谁都看不懂谁…


                      12楼2008-11-22 05:54
                      回复
                        VB?
                        可我们现在在学C,VB都忘了…


                        13楼2008-11-22 13:36
                        回复
                          估计我大学是要学C语言的,computer science的东西,挺好玩的…


                          14楼2008-11-24 04:34
                          回复
                            就是为了赚学分的…


                            IP属地:北京15楼2008-11-24 10:02
                            回复
                              有可能我要double major,然后就不只赚学分了,赚个专业~


                              16楼2008-11-28 01:13
                              回复