- UID
 - 23516
 
 注册时间2006-10-1
阅读权限10
最后登录1970-1-1
周游历练 
  
 
 
 
TA的每日心情  | 开心 2021-3-27 09:12 | 
|---|
 
  签到天数: 3 天 [LV.2]偶尔看看I  
 | 
 
1。数字排序 
=====================================================- #define  TOTAL 10
 
 - /*在指针p处,依次输入存放n个int型数*/
 
 - void InputNum(int *p,int n)
 
 - {
 
 -         int i=0;
 
 -         for(;i < n;i++)
 
 -         {
 
 -                 scanf("%d",p+i);
 
 -         }
 
 - }
 
 - /*把指针p指向的n个int型的数依次显示出来*/
 
 - void Show(int *p,int n)
 
 - {
 
 -         int i=0;
 
 -         for(;i<n;i++)
 
 -         {
 
 -                 printf("%d ",*(p+i));
 
 -         }
 
 -         printf("\n");
 
 - }
 
  
- /*比较两个数大小, a>b返回1 , a<b返回0 */
 
 - int NumCmp(int a,int b)
 
 - {
 
 -         return a>b ? 1:0;
 
 - }
 
 - /*交换两个数的位置*/
 
 - void Change(int *a,int *b)
 
 - {
 
 -         int t;
 
 -         t = *a;
 
 -         *a = *b;
 
 -         *b =t;
 
 - }
 
 - /*把指针p指向的n个int型的数进行从大到小排序*/
 
 - void LtoS(int *p, int n)
 
 - {
 
 -         int i,j;
 
 -         for(j=0;j<n-1;j++)
 
 -         {
 
 -                 for(i=0;i<n-1-j;i++)
 
 -                 {
 
 -                         if(!(NumCmp(*(p+i),*(p+i+1))))
 
 -                         {
 
 -                                 Change(p+i,p+i+1);
 
 -                         }
 
 -                 }
 
 -         }        
 
 - }
 
 - /*把指针p指向的n个int型的数进行从小到大排序*/
 
 - void StoL(int *p, int n)
 
 - {
 
 -         int i,j;
 
 -         for(j=0;j<n-1;j++)
 
 -         {
 
 -                 for(i=0;i<n-1-j;i++)
 
 -                 {
 
 -                         if(NumCmp(*(p+i),*(p+i+1)))
 
 -                         {
 
 -                                 Change(p+i,p+i+1);
 
 -                         }
 
 -                 }
 
 -         }        
 
 - }
 
 - /*把指针p指向的n个int型的数进行乱序排序*/
 
 - void LX(int *p, int n)
 
 - {
 
 -         int i,j,t=1;
 
 -         for(j=0;j<n-1;j++)
 
 -         {
 
 -                 for(i=n-2;i>=0;i--)
 
 -                 {
 
 -                         if(t == 1)
 
 -                         {                        
 
 -                                 if(NumCmp(*(p+i),*(p+i+1)))
 
 -                                 {
 
 -                                         Change(p+i,p+i+1);
 
 -                                 }
 
 -                         }
 
 -                         else
 
 -                         {
 
 -                                 if(!(NumCmp(*(p+i),*(p+i+1))))
 
 -                                 {
 
 -                                         Change(p+i,p+i+1);
 
 -                                 }
 
 -                         }
 
 -                         
 
 -                 }
 
 -                 t=t*(-1);
 
 -         }        
 
 - }
 
 - /*        比较两个指针指向的字符串是否相等,相等返回1,不等返回0   */
 
 - int MyStrCmp(char *str1,char *str2)
 
 - {
 
 -         int i=0;
 
 -         while(*(str1+i))
 
 -         {
 
 -                 if(*(str1+i) != *(str2+i))
 
 -                 {
 
 -                         return 0;
 
 -                 }
 
 -                 i++;
 
 -         }
 
 -         return 1;
 
 - }
 
 - /*根据指针type指向的字符串,判断用哪种排序方法,对指针p指向的n个int型的数进行排序*/
 
 - void SortIntList(int *p,int n,char *type)
 
 - {
 
 -         char fun1[] = {"LtoS"};
 
 -         char fun2[] = {"StoL"};
 
 -         char fun3[] = {"LX"};
 
 -         if(MyStrCmp(fun1,type))
 
 -         {
 
 -                 LtoS(p,n);        
 
 -         }
 
 -         else if(MyStrCmp(fun2,type))
 
 -         {
 
 -                 StoL(p,n);
 
 -         }
 
 -         else if(MyStrCmp(fun3,type))
 
 -         {
 
 -                 LX(p,n);
 
 -         }
 
  
- }
 
  
- int main()
 
 - {
 
 -         int *p = (int *)malloc(10 * sizeof(int));
 
 -         printf("Please Input %d Number:\n",TOTAL);
 
 -         InputNum(p,TOTAL);
 
 -         SortIntList(p,TOTAL,"LtoS");
 
 -         printf("The Large to Small Number:\n");
 
 -         Show(p,TOTAL);
 
 -         SortIntList(p,TOTAL,"StoL");
 
 -         printf("The Small to Large Number:\n");
 
 -         Show(p,TOTAL);
 
 -         SortIntList(p,TOTAL,"LX");
 
 -         printf("The Luan Xu Number:\n");
 
 -         Show(p,TOTAL);
 
 -         free(p);
 
  
-         return 0;
 
 - }
 
 
  复制代码 ================================================== 
2。字符串处理- #define N 20
 
 - /* 指针p指向的字符串小写转大写 */
 
 - void MyStrUpper(char *p)
 
 - {
 
 -         int i=0;
 
 -         while(*(p+i))
 
 -         {
 
 -                 if(*(p+i) >= 'a' && *(p+i) <= 'z')
 
 -                 {
 
 -                         *(p+i)-=0x20;
 
 -                 }
 
 -                 i++;
 
 -         }
 
 - }
 
 - /* 指针p指向的字符串大写转小写 */
 
 - void MyStrLower(char *p)
 
 - {
 
 -         int i=0;
 
 -         while(*(p+i))
 
 -         {
 
 -                 if(*(p+i) >= 'A' && *(p+i) <= 'Z')
 
 -                 {
 
 -                         *(p+i)+=0x20;
 
 -                 }
 
 -                 i++;
 
 -         }
 
 - }
 
 - void MyStrPJ(char *str1,char *str2)
 
 - {
 
 -         int i=0,si=0;
 
  
-         if(strlen(str1) + strlen(str2) > N)
 
 -         {
 
 -                 printf("Don't PJ!\n");
 
 -         }
 
 -         else
 
 -         {
 
 -                 si=strlen(str1);
 
 -                 while(*(str2+i))
 
 -                 {
 
 -                         *(str1+si+i) = *(str2+i);
 
 -                         i++;
 
 -                 }
 
 -                         *(str1+si+i) =0;
 
 -                 puts(str1);
 
 -                 printf("\n");
 
 -         }
 
 - }
 
  
- /* 记录指针str指向的字符串中字符a的个数js,返回js */
 
 - int MyStrJS(char *str,char a)
 
 - {
 
 -         int i=0,js=0;
 
 -         while(*(str+i))
 
 -         {
 
 -                 if(*(str+i) == a)
 
 -                 {
 
 -                         js++;
 
 -                 }
 
 -                 i++;
 
 -         }
 
 -         return js;
 
 - }
 
 - /*记录指针str指向的字符串中字符串word的个数js,返回js*/
 
 - int MyStrWord(char *str,char *word)
 
 - {
 
 -         int i=0,j,js=0;
 
 -         while(*(str+i))
 
 -         {
 
 -                 if(*(str+i) == *word)
 
 -                 {
 
 -                         j=0;
 
 -                         while(*(word+j))
 
 -                         {
 
 -                                 if(*(str+i) != *(word+j))
 
 -                                 {
 
 -                                         break;
 
 -                                 }
 
 -                                 j++;
 
 -                                 i++;
 
 -                         }
 
 -                         i--;
 
 -                         if(j == strlen(word))
 
 -                         {
 
 -                                 js+=1;
 
 -                         }
 
 -                 }
 
 -                 i++;
 
 -         }
 
 -         return js;
 
 - }
 
 - /* 查找字符串str是否是字符串b开头,是返回1,不是返回0 */
 
 - int MyStrBeginCmp(char *str,char *b)
 
 - {
 
 -         int i=0;
 
 -         while(*(b+i))
 
 -         {
 
 -                 if(*(b+i) != *(str+i))
 
 -                 {
 
 -                         return 0;
 
 -                 }
 
 -                 
 
 -                 i++;
 
 -         }
 
 -         return 1;
 
 - }
 
 - /* 查找字符串str是否是字符串e结尾,是返回1,不是返回0 */
 
 - int MyStrEndCmp(char *str,char *e)
 
 - {
 
 -         int i=0,len;
 
 -         len = strlen(str)- strlen(e);
 
 -         while(*(e+i))
 
 -         {
 
 -                 if(*(e+i) != *(str+i+len))
 
 -                 {
 
 -                         return 0;
 
 -                 }
 
 -                 
 
 -                 i++;
 
 -         }
 
 -         return 1;
 
 - }
 
 - main()
 
 - {
 
 -         char *str1 = (char *)malloc(N * sizeof(char));
 
 -         char *str2 = (char *)malloc(N * sizeof(char));
 
 -         
 
 -         printf("Plsase Input str1: ");
 
 -         scanf("%s",str1);                                                                                                /*输入一个字符串*/        
 
 -         
 
 -         MyStrUpper(str1);                                                                                                /*字符串小写转大写*/
 
 -         printf("The Upper str1 : ");
 
 -         puts(str1);
 
 -         
 
 -         printf("Plsase Input str2: ");                        
 
 -         scanf("%s",str2);                                                                                                /*输入一个字符串*/        
 
  
-         MyStrLower(str2);                                                                                                /*字符串小写转大写*/
 
 -         printf("The Lower str2 : ");
 
 -         puts(str2);
 
 -         
 
 -         printf("The str1 and str2 PJ(把2个字符串拼接起来) :");
 
 -         MyStrPJ(str1,str2);                                                                                                /* 把字符串str1,str2拼接到str1 */        
 
  
-         printf("Plsase Input str(输入一个字符串查看其中'a'个数): ");
 
 -         scanf("%s",str1);                                                                                                /*输入一个字符串*/
 
 -         printf(" %d 'a' in The str!\n",MyStrJS(str1,'a'));                                /*查找字符串中'a'的个数*/
 
  
-         printf("Plsase Input str(输入一个字符串查看其中'hello'个数): ");
 
 -         scanf("%s",str1);                                                                                                /*输入一个字符串*/
 
 -         printf("%d 'hello' in The str\n",MyStrWord(str1,"hello"));                /*查找字符串中'hello'的个数*/
 
  
-         printf("Plsase Input str(输入一个字符串查看是否已'begin'开头): ");
 
 -         scanf("%s",str1);                                                                                                /*输入一个字符串*/
 
 -         
 
 -         if(MyStrBeginCmp(str1,"begin"))                                                                        /*查找字符串是否是'begin'开头*/
 
 -         {
 
 -                 printf("Yes!\n");
 
 -         }
 
 -         else
 
 -         {
 
 -                 printf("No!\n");
 
 -         }
 
  
-         printf("Plsase Input str(输入一个字符串查看是否已'end'结尾): ");
 
 -         scanf("%s",str1);                                                                                                /* 输入一个字符串 */
 
 -         
 
 -         if(MyStrEndCmp(str1,"end"))                                                                                /* 查找字符串是否是'end'结尾 */
 
 -         {
 
 -                 printf("Yes!\n");
 
 -         }
 
 -         else
 
 -         {
 
 -                 printf("No!\n");
 
 -         }
 
 -         
 
 -         
 
 -         free(str2);
 
 -         free(str1);
 
 - }
 
  复制代码 |   
 
 
 
 |