LinuxÒÁµéÔ°ÂÛ̳'s Archiver

ladan ·¢±íÓÚ 2008-1-3 17:28

°ïæ¿´Ò»ÏÂÕâ¸ö³ÌÐò¡£ÔËÐÐʱ×ÜÏÔʾ¡°¶Î´íÎó¡±

³ÌÐò×÷ÓãºÔÚ Linux Ï£¬±àд Shell ³ÌÐò£¬¼ÆËã´ÅÅÌÉÏËùÓÐĿ¼ÏÂÆ½¾ùÎļþ¸öÊý¡¢ËùÓÐĿ¼ƽ¾ù
!dzEuh e Éî¶È¡¢ËùÓÐÎļþÃûƽ¾ù³¤¶È J(N0yv5t;Y{
Ô´³ÌÐò£º
{SN{.Sn #include <sys/stat.h>:a0L s7fy!F,K-zc
#include <sys/types.h>)]0R'{mMnkG,`
#include <string.h>l+W^7J Kd
#include <stdio.h>UF1z$`ah;t,S"poZ
#include <stdlib.h> r8F X"LX-X
#include <dirent.h>
P$GT$kS rBvh4|W int func(char*path,int* AllFileNumber,int* AllDepthNumber,
,K9zHe1Qx                        int* AllFilenameNumber,int* AllFoldNumber) Qs].eWH{._2i
{ `"g x6s^S\
  struct stat statbuf;
i*qqJ/]3St`   struct dirent *dirp;
e#\e3X9b zrax   DIR *dp;
:p2W_7e3O Fw/?g   char *str1;?r7G}a _!P:l.O0]
  char *str2;0E%CFjNE
  int judge1;
\jL h!U!L'u   int judge2;zF!^V/Ke9X2jIF.GZ l
  int ret;M@5B0l6Z[
  judge1=0;a5p(Q t ^
  judge2=0;
"^Yha3K`                                  
I#Y&]0bX2MD5j'Wt   stat(path,&statbuf);
L5R%a^W^(R4} Tv0K{ _w_
switch(statbuf.st_mode&S_IFMT)
RU [An.{ {
7_Na7s ?%Vw case S_IFREG:                             
4tV&f-d0T                      *AllFilenameNumber=strlen(path)+1;
x4Yq$bJ'Q g                      *AllFileNumber=*AllFileNumber+1;\ D)e8rP.k du|(M
                     *AllDepthNumber=*AllDepthNumber+1;
F;|Fk:w7V2E#n%~9P                      break;
;W7\3^O*|0SV-x case S_IFDIR:     *AllDepthNumber=*AllDepthNumber+1;;}u$k/^ t,J9I@
                  *AllFoldNumber=*AllFoldNumber+1;2b3`o{L8jR:u
                  dp=opendir(path);(O"kM Q y.My
                  while((dirp=readdir(dp))!=NULL)
[E1h n'G)[$tya                                                {k!cv,Tv5Y1~&M gq
                  if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))E E]R+db[
                                                   
?J"vot3h                        if(judge1==0){
hfRFqwa-y"G(X                                              
zY+TK)?$qT6T5G+Nl                        strcpy(str1,path);0Z Y5f,yd:A6wo!`
                       judge1=1;}
Q{,zK/|,_.w                        if (judge2==0)I|X,z8t,O#F1a?*F
                                                            {
AD b$K%W&Upu                        str2=str1+strlen(str1);
/nj#?&Vwbx(t                        judge2=1;
)YW$|5fm-CN                                                             }.n&TX.n`/`|S:j_
                                               
w-N`1@1A~.lV                        *str2++='/';Jh6I6jL)^?$t4l
                       *str2=0;
} } EO&h3_                     strcpy(str2,dirp->d_name); l,S&p1\4B2v:d#|{:c
                    ret=func(str1, AllFileNumber,AllDepthNumber,I {^ Ny}H,Y*w
                                     AllFilenameNumber,AllFoldNumber);
4?p6U:|P7q&[ A b                     *str2--;
/aY4Y4p8b3kZ                     *str2=0;&q}-{l2@2s
                                                     }
v oz'i$I                     closedir(dp);               j3~M/\[?7^
                    break;
V)[ I#TqNYo default:        break;
K6B_!j[+Wa.i }Pw zO4q s3L*v3y
   return 0;'t1XM H4AOXR
}YqcoL`I v|
int main(int argc,char *argv[])u7sU;\ o
{d'gX'TJ E7hHD0C p
    int ret;D6qaBm__;{+N;C
    int AllFileNumber=0;#SEf+t(Yk
    int AllDepthNumber=0;
#y"J&Yc7q     int AllFilenameNumber=0;
n(LGph     int AllFoldNumber=0;
R^:?bY)q Fx     ret=func(argv[1],&AllFileNumber,&AllDepthNumber,&AllFilenameNumber,
;eR-R1Ad A              &AllFoldNumber);
,[5tE-P3o     printf("The average filenumber is %d\n",AllFileNumber/AllFoldNumber);
#yelj&u;fo     printf("The average fileDepthnumber is %d\n",P&P:QI!M
            AllDepthNumber/AllFoldNumber);.r#vk8d u7qiV
    printf("The average filenameNumber is %d\n",
p:iJ!G3B'n               AllFilenameNumber/AllFileNumber);w)W;N&eGn
    return 0;4xK x:R7u5t2M&B{+A._
}

ladan ·¢±íÓÚ 2008-1-4 17:59

ûÓÐÈ˰ïæ¿´Ò»ÏÂÂð£¿

ladan ·¢±íÓÚ 2008-1-5 12:39

»Ø¸´ #1 ladan µÄÌû×Ó

¾­¹ýŬÁ¦×îÖÕ½â¾öÁËÕâ¸öÎÊÌâ¡£ ÏְѸÄÍêµÄÔ´³ÌÐòÌùÉÏÀ´
Nu ]!B&hQD #include <sys/stat.h>!xc1ob(u.H
#include <sys/types.h>
'wu;Su6R"`Y5Y3w-]9Q_ #include <string.h>
.\3St+aJ4j*d #include <stdio.h> xe(g1@vQm&l-M
#include <stdlib.h> G F'M[h0N
#include <dirent.h>
xfr1J,|l3j #include<unistd.h>
-Byj%tV void func(const char* pathname,int* AllFileNumber,int* AllDepthNumber,int* AllFilenameNumber,int* AllFoldNumber)
7U x8Op-k+Z^ {%bN f:y)`$ZWk+[
   Y f$in8v!{8q.w
  struct stat statbuf;
/jf-]y@3w^q5x   struct dirent *dirp;
j|e^ih0q;e[ Q   DIR *dp;$C#gv}.E3~
  char str1[100];
%v,R%~:uf)|?   char str2[100];
9Z;N U;j:[|;Z   int judge1;];~1]nr
  int judge2;
w.b~n| r ?-R2n bQ   int m;ZKutH*MT
  judge1=0;5C"^R.^:k ~
  judge2=0; @'x8~(T6E0[
  stat(pathname,&statbuf);C2j$KO6F ?lp
  printf("%s",pathname);
j mY)@xFQ,U2C                                     //½«Â·¾¶½áµãµÄÐÅÏ¢·Åµ½ statbuf ½á¹¹ÌåÖÐ:Lw+{S!F/f!z$w!J
  printf("%d",statbuf.st_mode); sS \ZS Z
     //¼ì²é½ÚµãÎļþµÄÀàÐÍ
rY-Tmh&xI6Uy switch(statbuf.st_mode&S_IFMT)
7VB-Q|y9G9e {
E5~zAwd case S_IFREG:                              //µ±ÎªÆÕͨÎļþʱ
Ziv?|ln9X                      *AllFilenameNumber=strlen(pathname)+1;
b!^u+XJ9o,k[                      *AllFileNumber=*AllFileNumber+1;
2o5N\_7iFt:g(T                      *AllDepthNumber=*AllDepthNumber+1;
3W2??a%x'o                      printf("%d\n%d\n%d\n",*AllFilenameNumber,*AllFileNumber,*AllDepthNumber);9vg @y%];_
                     break;g#B#tV.\gTUe
case S_IFDIR:     *AllDepthNumber=*AllDepthNumber+1; //µ±ÎªÄ¿Â¼Îļþʱ,´ò¿ªÄ¿Â¼
3i`%rxV7Qd                   *AllFoldNumber=*AllFoldNumber+1;-A6~{}w5J
                  dp=opendir(pathname);
!G)`8m,bf1RV                   while((dirp=readdir(dp))!=NULL)9Q%T%KG ]1s
                                               {
BiF:X;[1M#o                                                          ,[3n2iQ/ws9a Gp
                  if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
v V}&N I8Ei                                             }ILX4P~1E0~
                       continue;
{y/uXq(q?R'N)~#h9Z&\                             if(judge1==0){
RwK$Kj*a*Ro },h                                                //¸´ÖÆÂ·¾¶µ½×Ö·û´® str1 ÉÏ8jH9Ph&uU*g7Ko%^L
                       strcpy(str1,pathname);
%s)N!l5T]H6a                        judge1=1;}
#f$M t-p OQaE ~1o                        if (judge2==0)j5r+L$C#R7A
                                                            {hM&|E&D:d3H(tH
                       //str2=str1+strlen(str1);|2RF5G\;qVO:f
                       strcpy(str2,str1);
%Z*yR r&{ d9\9Y                                                               
[k:EP"x                        judge2=1;
x?B*epU8U^                                                             }'E F7~&Dk5P QV#Z
                                                  //ÔÚ str2 µÄºóÃæ¼ÓÉÏ¡¯/¡¯
?@)_6p,D c                        m=strlen(str2);Ho\-GB"i [
                       str2[m]='/';
fn[;c{(DT{                        str2[m+1]='\0';
z%maX']uj2CDf                     strcpy(str2,dirp->d_name); //ÔÚ str2 µÄºóÃæ¼ÓÉÏij¸öÎļþÃû
(U`&oY-D O,[,l-S#h                                       
A/G;M2L^ {                     func(str2,AllFileNumber,AllDepthNumber,
M.D"H3KkNV(e\                                     AllFilenameNumber,AllFoldNumber);#d+F.C;[Mlx
                      str2[m]='\0';H1W5P%|H~)W/ml*SQ
                                                     }                                                
/~C5S nqwaV"J1G                     closedir(dp);Xt K%K/C
                                                               //¹Ø±ÕĿ¼8C!qJrU L
                    break;;Tz j6I0?-nEDs
default:        break;Ek? n5wir+`V*iK
}
'DA%iB%D/JN
U$V?7Y3d(kJ`7P M }
*Sot;AfQ;[t 0W3pL#Rz9h
int main(int argc,char *argv[])/pTFZX YN W
{Qi.e9}0Y
   
d'K9i\;] f(`ku;c     int AllFileNumber=0; F-[h1pF]!B\
    int AllDepthNumber=0;
6s[8m6} P YgB     int AllFilenameNumber=0;
? bf.je R:?:IL/G     int AllFoldNumber=0;
h9h'G[H8],tY
(N/io @Y     func(argv[1],&AllFileNumber,&AllDepthNumber,&AllFilenameNumber,1Awk M@ g-~
            &AllFoldNumber);Gt7Va%T5|dw)?;l
    printf("The average filenumber is %d\n",AllFileNumber+AllFoldNumber);U/Fv8}{7\bqt;s
    printf("The average fileDepthnumber is %d\n",3ia@R$k3N
            AllDepthNumber+AllFoldNumber);]Q YYi.[^
    printf("The average filenameNumber is %d\n",
+a _n!w\9z.p               AllFilenameNumber+AllFileNumber);
'J5UDrp[o,~,a1BK     return 0;0A8o#WDayZ
}

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.