|
| 1 | +/**************************************** |
| 2 | +技巧01:使用指针实现整数排序 |
| 3 | +****************************************/ |
| 4 | +/* |
| 5 | +#include <stdio.h> |
| 6 | +swap(int *p1,int *p2) |
| 7 | +{ |
| 8 | + int temp; |
| 9 | + temp=*p1; |
| 10 | + *p1=*p2; |
| 11 | + *p2=temp; |
| 12 | +} |
| 13 | +exchange(int *pt1,int *pt2,int *pt3) |
| 14 | +{ |
| 15 | + if(*pt1<*pt2) |
| 16 | + swap(pt1,pt2); |
| 17 | + if(*pt1<*pt3) |
| 18 | + swap(pt1,pt3); |
| 19 | + if(*pt2<*pt3) |
| 20 | + swap(pt2,pt3); |
| 21 | +} |
| 22 | +int main(int argc, char *argv[]) |
| 23 | +{ |
| 24 | + int a,b,c,*q1,*q2,*q3; |
| 25 | + puts("please input three key numbers you want to rank:"); |
| 26 | + scanf("%d%d%d",&a,&b,&c); |
| 27 | + q1=&a; |
| 28 | + q2=&b; |
| 29 | + q3=&c; |
| 30 | + exchange(q1,q2,q3); |
| 31 | + printf ("%d,%d,%d\n",a,b,c); |
| 32 | + puts("press any key to quit ..."); |
| 33 | + return 0; |
| 34 | +} |
| 35 | +*/ |
| 36 | +/**************************************** |
| 37 | +技巧02:使用指针实现数据交换 |
| 38 | +****************************************/ |
| 39 | +/* |
| 40 | +#include <stdio.h> |
| 41 | +swap(int *p1,int *p2) |
| 42 | +{ |
| 43 | + int temp; |
| 44 | + temp=*p1; |
| 45 | + *p1=*p2; |
| 46 | + *p2=temp; |
| 47 | +} |
| 48 | +int main(int argc, char *argv[]) |
| 49 | +{ |
| 50 | + int a,b; |
| 51 | + int *pointer1,*pointer2; |
| 52 | + printf ("please input two swap numbers:\n"); |
| 53 | + scanf("%d%d",&a,&b); |
| 54 | + pointer1=&a; |
| 55 | + pointer2=&b; |
| 56 | + swap(pointer1,pointer2); |
| 57 | + printf ("the result is :%d,%d\n",a,b); |
| 58 | + return 0; |
| 59 | +} |
| 60 | +*/ |
| 61 | +/**************************************** |
| 62 | +技巧03:指向结构体变量的指针 |
| 63 | +****************************************/ |
| 64 | +/* |
| 65 | +#include <stdio.h> |
| 66 | +struct student //自定义结构体 |
| 67 | +{ |
| 68 | + int num; //学生学号 |
| 69 | + char name[20]; //学生姓名,数组字符串 |
| 70 | + char sex; //学生性别,字符 |
| 71 | + int age; //学生年龄 |
| 72 | + float score; //学生成绩 |
| 73 | +}; |
| 74 | +int main(int argc, char *argv[]) |
| 75 | +{ |
| 76 | + //字符串用双引号,字符用单引号 |
| 77 | + struct student student1={1001,"liming",'M',20,92.5};//定义结构体变量 |
| 78 | + struct student *p; //定义指针变量指向结构体类型 |
| 79 | + p=&student1; |
| 80 | + printf ("number:%d\n",p->num); |
| 81 | + printf ("name:%s\n",p->name); |
| 82 | + printf ("sex:%c\n",p->sex); |
| 83 | + printf ("age:%d\n",p->age); |
| 84 | + printf ("score:%f\n",p->score); |
| 85 | + return 0; |
| 86 | +} |
| 87 | +*/ |
| 88 | +/**************************************** |
| 89 | +技巧04:使用指针输出数组元素 |
| 90 | +****************************************/ |
| 91 | +/* |
| 92 | +#include <stdio.h> |
| 93 | +int main(int argc, char *argv[]) |
| 94 | +{ |
| 95 | + int a[10]; |
| 96 | + int *p,i; |
| 97 | + puts("please input ten integer:\n"); |
| 98 | + for(i=1;i<=10;i++) |
| 99 | + scanf("%d",&a[i]); |
| 100 | + printf ("\n"); |
| 101 | + for(p=&a;p<(a+10);p++) |
| 102 | + printf ("%d ",*p); |
| 103 | + return 0; |
| 104 | +} |
| 105 | +*/ |
| 106 | +/**************************************** |
| 107 | +技巧05:用指针实现逆序存放数组元素值 |
| 108 | +****************************************/ |
| 109 | + /* |
| 110 | +#include <stdio.h> |
| 111 | +inverte(int *x,int n) |
| 112 | +{ |
| 113 | + int *p,temp,*i,*j,m=(n-1)/2; |
| 114 | + i=x; |
| 115 | + j=x+n-1; |
| 116 | + p=x+m; |
| 117 | + for (i=x;i<=p;i++,j--) |
| 118 | + { |
| 119 | + temp=*i; |
| 120 | + *i=*j; |
| 121 | + *j=temp; |
| 122 | + } |
| 123 | + return 0; |
| 124 | +} |
| 125 | +int main(int argc, char *argv[]) |
| 126 | +{ |
| 127 | + int i,a[10]= |
| 128 | + {1,2,3,4,5,6,7,8,9,0}; |
| 129 | + printf ("the elements of original array:\n"); |
| 130 | + for(i=0;i<10;i++) |
| 131 | + printf ("%d ",a[i]); |
| 132 | + printf ("\n"); |
| 133 | + inverte(a,10); |
| 134 | + printf ("the elements has been inverted:\n"); |
| 135 | + for(i=0;i<10;i++) |
| 136 | + printf ("%d ",a[i]); |
| 137 | + printf ("\n"); |
| 138 | + return 0; |
| 139 | +} |
| 140 | + */ |
| 141 | +/**************************************** |
| 142 | +技巧06:使用指针查找数列中最大值/最小值 |
| 143 | +****************************************/ |
| 144 | +/* |
| 145 | +#include <stdio.h> |
| 146 | +void max_min(int a[],int n,int *max,int *min) |
| 147 | +{ |
| 148 | + int *p; |
| 149 | + *max=*min=*a; |
| 150 | + for(p=a+1;p<a+n;p++) |
| 151 | + { |
| 152 | + if(*p>*max) |
| 153 | + *max=*p; |
| 154 | + else if(*p<*min) |
| 155 | + *min=*p; |
| 156 | + } |
| 157 | + return 0; |
| 158 | +} |
| 159 | +int main(int argc, char *argv[]) |
| 160 | +{ |
| 161 | + int i,a[10]; |
| 162 | + int max,min; |
| 163 | + printf ("input 10 integer numbers you want to operate:\n"); |
| 164 | + for(i=0;i<10;i++) |
| 165 | + scanf("%d",&a[i]); |
| 166 | + max_min(a,10,&max,&min); |
| 167 | + printf ("the maximun number is:%d\n",max); |
| 168 | + printf ("the maxinun number is:%d\n",min); |
| 169 | + return 0; |
| 170 | +} |
| 171 | +*/ |
| 172 | +/**************************************** |
| 173 | +技巧07:使用指针实现字符串复制(复制函数没有写成功) |
| 174 | +****************************************/ |
| 175 | +/* |
| 176 | +#include <stdio.h> |
| 177 | +copy(char *s,char *q) |
| 178 | +{ |
| 179 | + int i=0; |
| 180 | + for (;*s!='\0';) |
| 181 | + { |
| 182 | + *q=*s; |
| 183 | + s++; |
| 184 | + q++; |
| 185 | + } |
| 186 | + *q='\0'; |
| 187 | +} |
| 188 | +main() |
| 189 | +{ |
| 190 | + char *str,*p; |
| 191 | + str="hello world!"; |
| 192 | + copy(str,p); |
| 193 | + printf ("%s",p); |
| 194 | + return 0; |
| 195 | +} |
| 196 | +*/ |
| 197 | +/**************************************** |
| 198 | +技巧08:使用指针实现字符串的连接(段错误,未成功) |
| 199 | +****************************************/ |
| 200 | +/* |
| 201 | +#include <stdio.h> |
| 202 | +connect(char *s,char *t,char *q) |
| 203 | +{ |
| 204 | + int i=0; |
| 205 | + for ( ; *s!='\0'; ) |
| 206 | + { |
| 207 | + *q=*s; |
| 208 | + s++; |
| 209 | + q++; |
| 210 | + } |
| 211 | + for ( ; *t!='\0'; ) |
| 212 | + { |
| 213 | + *q=*t; |
| 214 | + t++; |
| 215 | + q++; |
| 216 | + } |
| 217 | + *q='\0'; |
| 218 | +} |
| 219 | +int main(int argc, char *argv[]) |
| 220 | +{ |
| 221 | + char *str,*t,*p; |
| 222 | + str="one world"; |
| 223 | + t="one dream"; |
| 224 | + printf ("the first string is:%s\n",str); |
| 225 | + printf ("the second string is:%s\n",t); |
| 226 | + connect(str,t,p); |
| 227 | + printf ("the connected string is:\n"); |
| 228 | + printf ("%s",p); |
| 229 | + return 0; |
| 230 | +} |
| 231 | +*/ |
| 232 | +/**************************************** |
| 233 | +技巧09:使用指针实现字符串插入(未成功) |
| 234 | +****************************************/ |
| 235 | +/* |
| 236 | +#include <stdio.h> |
| 237 | +#include <string.h> |
| 238 | +insert(char *s,char *q,int n) |
| 239 | +{ |
| 240 | + int i=0; |
| 241 | + char *str,strcp[50]; |
| 242 | + str=strcp; |
| 243 | + for (i=0; *s!='\0'; i++) |
| 244 | + { |
| 245 | + if (i==n-1) |
| 246 | + { |
| 247 | + for (; q!='\0'; ) |
| 248 | + { |
| 249 | + str[1]=*q; |
| 250 | + q++; |
| 251 | + i++; |
| 252 | + } |
| 253 | + } |
| 254 | + str[i]=*s; |
| 255 | + s++; |
| 256 | + } |
| 257 | + str[i+1]='\0'; |
| 258 | + return str; |
| 259 | +} |
| 260 | +int main(int argc, char *argv[]) |
| 261 | +{ |
| 262 | + char *strin,*str; |
| 263 | + int i; |
| 264 | + str="hello world!"; |
| 265 | + strin ="big"; |
| 266 | + printf ("please input the position you want to insert:"); |
| 267 | + scanf("%d",&i); |
| 268 | + str=insert(str,strin,i); |
| 269 | + printf ("%s\n",str); |
| 270 | + return 0; |
| 271 | +} |
| 272 | +*/ |
| 273 | +/**************************************** |
| 274 | +技巧10:使用指针实现字符串的匹配 |
| 275 | +****************************************/ |
| 276 | +/* |
| 277 | +#include <stdio.h> |
| 278 | +#include <string.h> |
| 279 | +int match(char *B,char *A) |
| 280 | +{ |
| 281 | + int i,j,start=0; |
| 282 | + int lastB=strlen(B)-1; |
| 283 | + int lastA=strlen(A)-1; |
| 284 | + int endmatch =lastA; |
| 285 | + for (j=0; endmatch<=lastB; endmatch++,start++) |
| 286 | + { |
| 287 | + if(B[endmatch]==A[lastA]) |
| 288 | + for(j=0,i=start;j<lastA&&B[i]==A[j];) |
| 289 | + i++,j++; |
| 290 | + if (j==lastA) |
| 291 | +{ |
| 292 | + return (start+1); |
| 293 | + } |
| 294 | + } |
| 295 | + if (endmatch>lastB) |
| 296 | + { |
| 297 | + printf ("the string is not matchable!\n"); |
| 298 | + return -1; |
| 299 | + } |
| 300 | +} |
| 301 | +int main(int argc, char *argv[]) |
| 302 | +{ |
| 303 | + char s[]="one world,one dream"; |
| 304 | + char t[]="world"; |
| 305 | + int p = match(s,t); |
| 306 | + if (p!=-1) |
| 307 | + { |
| 308 | + printf ("Matchable!\n"); |
| 309 | + printf ("the start position is %d\n",p); |
| 310 | + } |
| 311 | + printf ("\n"); |
| 312 | + return 0; |
| 313 | +} |
| 314 | +*/ |
| 315 | + |
0 commit comments