C语言练习题【1】
1 #define M 10 2 #include<math.h> 3 #include<stdio.h> 4 #include<string.h> 5 int main1234(){ 6 #if( 0 ) 7 double x,s; 8 printf("input number:\n"); 9 scanf("%lf",&x); 10 s=sin(x); 11 printf("sine of %lf is %lf\n",x,s); 12 13 char s[80],c='a'; 14 int i=0; 15 //scanf("%s",s); //gets可以读入空格,遇到回车才会停止;而scanf遇到空格、回车和Tab键都会认为输入结束 16 gets(s); 17 while(s[i]!='\0') 18 {if(s[i]==c) s[i]=s[i]-32; 19 else if(s[i]==c-32) s[i]=s[i]+32; 20 i++;} 21 puts(s); //ahAMa 22 23 #endif //输入输出 24 25 #if( 0 ) 26 printf("12345678901234567890\n"); 27 printf("\\abc\n\'abd\bc\"\n"); 28 printf("erg\r"); 29 printf("\110\x49\tijk\n"); 30 #endif //转义字符 字符串的输出 31 32 33 #if( 0 ) 34 int a,b=32; 35 float x,y=8.88; 36 char cl='k',c2; 37 a=y; 38 x=b; 39 a=cl; 40 c2=b; 41 printf("%d,%f,%d,%c",a,x,a,c2); 42 #endif //类型转化 43 44 #if( 0 ) 45 //int i=8; 46 //printf("%d\n",++i); 47 //printf("%d\n",--i); 48 //printf("%d\n",i++); 49 //printf("%d\n",i--); 50 //printf("%d\n",-i++); 51 //printf("%d\n",-i--); 52 //??? 53 //int i=5,j=5,p,q; 54 //p=(i++)+(i++)+(i++); 55 //q=(++j)+(++j)+(++j); 56 //printf("%d,%d,%d,%d",p,q,i,j); 57 #endif//自增自减运算 58 59 #if( 0 ) 60 //char a='B',b='o',c='k'; 61 //putchar(a);putchar(b);putchar(b);putchar(c);putchar('\t'); 62 //putchar(a);putchar(b); 63 //putchar('\n'); 64 //putchar(b);putchar(c); 65 #endif //使用putchar 输出字符 66 67 #if( 0 ) 68 //int a=15; 69 //float b=123.1234567; 70 //double c=12345678.1234567; 71 //char d='p'; 72 //printf("a=%d,%5d,%o,%x\n",a,a,a,a); 73 //printf("b=%f,%lf,%5.14f,%e\n",b,b,b,b); 74 //printf("c=%lf,%f,%8.41lf\n",c,c,c); 75 //printf("d=%c,%8c\n",d,d); 76 #endif //格式输出 77 78 #if( 0 ) 79 //int i=8; 80 //printf("%d\n",++i); 81 //printf("%d\n",--i); 82 //printf("%d\n",i++); 83 //printf("%d\n",i--); 84 //printf("%d\n",i++); 85 //printf("%d\n",i--); 86 //int i=8; 87 //printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,i++,i--); 88 #endif //格式输出 89 90 #if( 0 ) 91 char str[30]; 92 char ch; 93 printf("请输入字符串:"); 94 //gets(str); //可接受回车键之前输入的所有字符,并用’\0’替代 ‘\n’.回车键不会留在输入缓冲区中 95 scanf("%s",&str);//当遇到回车,空格和tab键会自动在字符串后面添加’\0’,但是回车,空格和tab键仍会留在输入的缓冲区中。 96 //printf("%s",str); 97 puts(str); //输出字符串时会将’\0’自动转换成’\n’进行输出 98 getchar(); //接受缓冲区的回车 99 ch = getchar(); //接受用户输入的一个字符 100 //scanf("%c",&ch); 101 printf("ch = %c\n",ch); 102 #endif //gets puts getchar() 的用法 103 104 #if( 0 ) 105 int a; 106 printf("input a number\n"); 107 scanf("%d",&a); 108 printf("%ld\n",a); 109 110 long a; 111 printf("input a long integer"); 112 scanf("%ld\n",&a); 113 printf("%ld\n",a); 114 115 char a,b,c; 116 printf("input char a,b,c\n"); 117 scanf("%c %c %c",&a,&b,&c); 118 printf("%d,%d,%d\n%c,%c,%c\n",a,b,c,a-32,b-32,c-32); 119 120 char ch; 121 ch=getchar(); 122 putchar(ch-32); 123 putchar('\n'); 124 125 int num; 126 scanf("%x",&num); 127 printf("%d",num); 128 #endif //格式的输入与输出 129 130 #if( 0 ) 131 int a=1; 132 a=(int)((double)(3/2)+0.5+(int)1.99*2); 133 printf("%d\n",a); 134 #endif //优先级运算 135 136 #if( 0 ) 137 char c='k'; 138 int i=1,j=2,k=3; 139 float x=3e+5,y=0.85; 140 printf("%d,%d\n",!x*!y,!!!x); 141 printf("%d,%d\n",x||i&&j-3,i<j&&x<y); 142 printf("%d,%d\n",i==5&&c&&(j=8),x+y||i+j+k); 143 #endif //逻辑符的使用 144 145 #if( 0 ) 146 int a,b,c,temp; 147 printf("请输入三个数\n"); 148 scanf("%d%d%d",&a,&b,&c); 149 if( a > b ) 150 { 151 temp = a; 152 a = b; 153 b = temp; 154 } 155 if( a > c ) 156 { 157 temp = a; 158 a = c; 159 c = temp; 160 } 161 if( b > c ) 162 { 163 temp = b; 164 b = c; 165 c = temp; 166 } 167 printf("%d %d %d\n" , a,b,c); 168 #endif //从大到小排列 169 170 #if( 0 ) 171 char c1; 172 printf("请输入一个字符\n"); 173 scanf("%c",&c1); 174 //if(c1 >= 'A'&& c1 <= 'Z') 175 //{ 176 // c1+=32; 177 // printf("%c %d\n",c1,c1); 178 //} 179 //else if (c1 >= 'a'&& c1 <= 'z') 180 //{ 181 // c1-=32; 182 // printf("%c %d\n",c1,c1); 183 //} 184 //else 185 //{ 186 // printf("%c %d\n",c1,c1); 187 //} 188 c1=(c1 >= 'A'&& c1 <= 'Z') ? c1+32 : (c1 >= 'a'&& c1 <= 'z') ? c1-32 : c1; 189 printf("%c %d\n",c1,c1); 190 #endif //转换大小写 191 192 #if( 0 ) 193 int i,j,k; 194 for (i = 1; i < 5; i++) 195 { 196 for (j = 1; j < 5; j++) 197 { 198 for (k = 1; k < 5; k++) 199 { 200 if(i!=j&&i!=k&&j!=k) 201 { 202 printf("%d,%d,%d\n",i,j,k); 203 204 } 205 } 206 } 207 } 208 #endif //1,2,3,4个数字组成互不相同且无重复数字的三位数 209 210 #if( 0 ) 211 //方法① 212 //int s=1; 213 //double i,pi=0; //pi=0一定要赋值 214 //for (i = 1; fabs(s/i) >1e-6;) //fabs()函数求浮点数的绝对值 需要引用头文件#include<math.h> 215 //{ 216 // pi+=s/i; 217 // i=i+2; 218 // s=-s; 219 //} 220 //pi*=4; 221 //printf("pi=%g\n",pi); 222 223 //方法② 224 //double i,pi=0,s=1; //pi=0一定要赋值 225 //for (i = 1; fabs(pow(-1,s+1)/i) >1e-6;) //fabs()函数求浮点数的绝对值 pow(x,y) 表示x的y次方 需要引用头文件#include<math.h> 226 //{ 227 // pi+=pow(-1,s+1)/i; 228 // i=i+2; 229 // s++; 230 //} 231 //pi*=4; 232 //printf("pi=%g\n",pi); 233 234 //方法③ 235 //int s=1; double n=1.0,t=1,pi=0; 236 //while (fabs(t)>1e-6) //绝对值小于10^-6为止 所以大于10^-6进行循环 237 //{ 238 // pi=pi+t; 239 // n=n+2; 240 // s=-s; 241 // t=s/n; 242 //} 243 //pi=pi*4; 244 //printf("pi=%10.6lf\n",pi); 245 #endif //用π/4≈1-1/3+1/5-1/7+...公式求π的近似值直到某一项的绝对值小于10^-6为止 246 247 #if( 0 ) 248 //①my method 249 //long a=1,b=1,i; 250 //printf("%d\n%d\n",a,b); 251 //for (i = 1; i <= 19; i++) //两个数循环19次 一共输出38个数 252 //{ 253 // a=a+b; 254 // b=a+b; 255 // printf("%d\n%d\n",a,b); 256 //} 257 258 //②reference 259 //long f1,f2; 260 //int i; 261 //f1=1; f2=1; 262 //for (i = 1; i <=20; i++) 263 //{ 264 // printf("%12ld %12ld",f1,f2); 265 // if(i%2==0) 266 // { 267 // printf("\n"); 268 // } 269 // f1=f1+f2; 270 // f2=f2+f1; 271 //} 272 #endif //求斐波拉列数列前40个数。斐波拉列数特点 (n=1)时 F(1)=1 (n=2)时 F(2)=1 (n>=3)时 F(n)=F(n-1)+F(n-2) 273 274 #if( 0 ) 275 long a[20] = {1,1}; 276 int i; 277 for ( i = 0; i < 20; i++) 278 { 279 if(i>=2) 280 { 281 a[i] = a[i-1] + a[i-2]; 282 } 283 if(i % 5 == 0) 284 { 285 printf("\n"); 286 } 287 printf("%12d",a[i]); 288 } 289 printf("\n"); 290 #endif //用数组求斐波拉列数列前20个数 291 292 #if ( 0 ) 293 int m , n ,temp, i; 294 printf("input m and n"); 295 scanf("%d %d",&m,&n); 296 if( m < ng ) 297 { 298 temp = m; 299 m = n; 300 n = temp; 301 } 302 //for (i = m ; i > 0 ;i++) //从大数开始寻找满足条件的自然数 303 //{ 304 // if(i % m== 0 && i % n == 0 ) 305 // { 306 // printf("%d and %d LVW is %d",m,n,i); 307 // break; 308 // } 309 //} 310 for ( i = n; i > 0; i--) //从大到小寻找满足条件的自然数 311 { 312 if( m % i == 0 && n % i == 0 ) 313 { 314 printf("%d and %d GCD is %d",m,n,i); 315 break; 316 } 317 } 318 #endif //求最小公倍数和最大公约数 319 320 #if( 0 ) 321 int a[10]; 322 int i,j,temp; 323 for (i = 0; i < 10; i++) 324 { 325 scanf_s("%d",&a[i]); 326 } 327 //外层控制循环多少趟,内层控制每一趟的循环次数 328 for (j = 0; j < 9; j++) //N个数字要排序完成 总共进行N-1趟排序 329 { 330 bool flag = false; //假如没有元素交换 331 for (i = 0; i < 10-j-1; i++) //每i趟的排序次数为(N-i)// -1防止a[i+1]越界 332 { 333 if(a[i] > a[i+1]) 334 { 335 temp = a[i+1]; 336 a[i+1] = a[i]; 337 a[i] = temp; 338 flag = true; 339 } 340 } 341 if(!flag) //说明数组已经有序不需要再进行比较 342 { 343 break; 344 } 345 } 346 for (i = 0; i < 10; i++) 347 { 348 printf("%5d\n",a[i]); 349 } 350 #endif //冒泡排序 351 352 #if( 0 ) 353 int i, j ,k = 0 , temp, a[10]; 354 for ( i = 0; i < 10; i++) 355 { 356 scanf("%d",&a[i]); 357 } 358 for ( i = 0; i < 9; i++) 359 { 360 k = i ; 361 for ( j = i + 1; j < 10; j++) 362 { 363 if(a[k] < a[j] ) 364 { 365 k = j ; 366 temp = a[i]; 367 a[i] = a[k]; 368 a[k] = temp; 369 } 370 } 371 } 372 for (j = 0; j < 10; j++) 373 { 374 printf("%5d\n",a[j]); 375 } 376 #endif //选择排序 377 378 #if( 0 ) 379 int a[2][3]; 380 int i,j,k,b[6]; 381 int temp; 382 int y,z; //一维数组转换成二维数组时候用到的 383 k = 0; //初始化k的值 384 385 //输入二维数组每个元素的值 386 for ( i = 0; i < 2; i++) 387 { 388 for ( j = 0; j < 3; j++) 389 { 390 scanf("%d",&a[i][j]); 391 } 392 } 393 394 //转换一维数组 395 for ( i = 0; i < 2; i++) 396 { 397 for ( j = 0; j < 3; j++) 398 { 399 //b[k] = a[i][j]; 400 //k++; 401 b[i*3+j] = a[i][j]; 402 } 403 } 404 405 //对一维数组b[6]进行冒泡排序 406 for ( j = 0; j < 5; j++) 407 { 408 for ( i = 0; i < 5-j; i++) 409 { 410 if(b[i]>b[i+1]) 411 { 412 temp = b[i]; 413 b[i] = b[i+1]; 414 b[i+1] = temp; 415 } 416 } 417 } 418 419 //再将排好序的一维数组b[6]转换成二维数组a[2][3] 420 //重新定义一个变量y z ,是为了避免变量之间的冲突 421 for ( y = 0; y < 2; y++) 422 { 423 for ( z = 0; z < 3; z++) 424 { 425 a[y][z] = b[y*3+z]; //一维数组转换为二维数组 426 } 427 } 428 429 //输出 430 for ( i = 0; i < 2; i++) 431 { 432 for ( j = 0; j < 3 ; j++) 433 { 434 printf("%3d",a[i][j]); 435 } 436 } 437 #endif //二维数组的冒泡排序 438 439 #if( 0 ) 440 // 用变量存储累加的结果 441 //int a[5][3] = {{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}}; 442 //int i, j; 443 //double math = 0 , c = 0 , foxpro = 0; 444 //for ( i = 0; i < 3; i++) 445 //{ 446 // for ( j = 0; j < 5; j++) 447 // { 448 // if(i==0) 449 // { 450 // math+=a[j][i]; 451 // } 452 // if(i==1) 453 // { 454 // c+=a[j][i]; 455 // } 456 // if(i==2) 457 // { 458 // foxpro+=a[j][i]; 459 // } 460 // } 461 //} 462 //math /= 5.0; c /= 5.0; foxpro /= 5.0; 463 //printf("math: %lf c: %lf foxpro: %lf and sum: %lf",math,c,foxpro,(math+c+foxpro)/3.0); 464 465 // 用一维数组存累加的结果(Preferable) 466 int a[5][3] = {{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}}; 467 int i, j; 468 double average, sum = 0, v[3]; 469 for ( i = 0; i < 3; i++) 470 { 471 for ( j = 0; j < 5; j++) 472 { 473 sum += a[j][i]; 474 } 475 v[i] = sum / 5.0; //将每一科的平均成绩存到一维数组里 476 sum = 0; 477 } 478 average = ( v[0] +v[1] + v[2]) / 3.0; 479 printf("math: %lf c: %lf foxpro: %lf and sum: %lf",v[0],v[1],v[2],average); 480 #endif //二维数组求平均成绩 481 482 #if( 0 ) 483 int a[2][3]={{5,2,0},{1,3,8}}, i, j; 484 for ( i = 0; i < 2; i++) 485 { 486 487 for ( j = 0; j < 3; j++) 488 { 489 printf("%3d",a[i][j]); 490 491 } 492 printf("\n"); 493 } 494 printf("\n"); 495 for ( i = 0; i < 3; i++) 496 { 497 498 for ( j = 0; j < 2; j++) 499 { 500 printf("%3d",a[j][i]); 501 502 } 503 printf("\n"); 504 } 505 #endif //将二维数组的行和列元素互换,存到另外一个二维数组里 506 507 #if( 0 ) 508 int i ,j,a[3][3]; 509 for ( i = 0; i < 3; i++) 510 { 511 for ( j = 0; j < 3; j++) 512 { 513 printf("a[%d][%d]=",i,j); 514 scanf_s("%d",&a[i][j]); 515 } 516 } 517 for ( i = 0; i < 3; i++) 518 { 519 for ( j = 0; j < 3; j++) 520 { 521 if( i == 1 || j == 1) 522 printf("%-6d",a[i][j]); 523 else 524 printf("%-6c",' '); 525 } 526 printf("\n"); 527 } 528 #endif //按照九宫格的形式,输入三行,按数组原来位置输出第一行和第一列的所有元素 529 530 #if( 0 ) 531 int i, j,temp, row=0, column=0; 532 int a[3][4] = {{12,20,31,4},{5,16,27,8},{9,10,11,12}}; 533 int max = a[0][0]; 534 for ( i = 0; i < 3; i++) 535 { 536 for ( j = 0; j < 4; j++) 537 { 538 if( a[i][j] > max ) 539 { 540 //temp = max; 541 max = a[i][j]; 542 //a[i][j] = temp; 543 row = i; 544 column = j; 545 } 546 } 547 } 548 printf("row: %2d , column:%2d max: %2d",row,column,max); 549 #endif //找出数组中最大值并且指出它的行号和列号 550 551 #if( 0 ) 552 static int a[M] = {-12,0,6,16,23,56,80,100,110,115}; 553 int n,low,mid,high,found; 554 low = 0; 555 high = M-1; 556 found = 0; 557 printf("Input a number to be searched\n"); 558 scanf("%d",&n); 559 while(low <= high) 560 { 561 mid = (low + high) / 2; 562 if(n == a[mid]) 563 { 564 found = 1; 565 break; 566 } 567 else if(n > a[mid]) 568 low = mid + 1; 569 else 570 high = mid-1; 571 } 572 573 if(found == 1) 574 printf("The index of %d is %d",n,mid); 575 else 576 printf("There is not %d",n); 577 #endif //二分法 578 579 #if( 0 ) 580 int power(int a, int b); 581 int x,y,z; 582 scanf("%d %d",&x,&y); 583 z=power(x,y); 584 printf("%d\n",z); 585 #endif //实现pow()函数 586 587 #if( 0 ) 588 int result; 589 int square(int x); 590 int factorical(int y); 591 result = factorical(square(2))+factorical(square(3)); 592 printf("计算结果:%d",result); 593 #endif //自定义平方函数 阶乘函数 594 595 #if( 0 ) 596 int result; 597 int factorical2(int n); 598 int num ; 599 scanf("%d",&num); 600 result = factorical2(num); 601 printf("计算结果:%d\n",result); 602 #endif //输一个数求它的阶乘 603 604 #if( 0 ) 605 void hanoi(int n , char x , char y , char z ); 606 int n; 607 scanf("%d",&n); 608 hanoi(n , 'X' , 'Y', 'Z'); 609 #endif //汉诺塔 610 611 #if( 0 ) 612 void average(int grade[]); 613 int score[10] = {10,20,30,40,50,60,70,80,90,100}; 614 average(score); 615 616 #endif //写一个average函数求平均成绩 617 618 #if( 0 ) 619 int i; 620 int fac(int a); 621 for ( i = 1; i <=5 ; i++) 622 { 623 printf("%d!=%d\n",i,fac(i)); 624 } 625 #endif //使用静态变量求阶乘 626 627 #if( 0 ) 628 //char string[50],char1; 629 //void removestr(char str[],char char2); 630 //puts("请输入一个字符串"); 631 //gets(string); 632 //printf("您输入的字符串是:%s\n",string); 633 //printf("请输入一个要删除的字符\n"); 634 //char1=getchar(); 635 //removestr(string,char1); 636 637 extern void enter_string(char str[]); 638 extern void delete_string(char str[],char ch); 639 extern void print_string(char str[]); 640 //以上声明是在本函数中将要调用的在其他文件中定义的函数 641 642 char string[50], char1; 643 enter_string(string); 644 puts("请输入一个要删除的字符"); 645 char1=getchar(); 646 delete_string(string,char1); 647 print_string(string); 648 649 #endif // 输入一个字符 在字符串中删除对应的字符 使用外部函数 650 651 #if( 0 ) 652 int a[10] = {9,8,7,6,5,4,3,2,1,0}; 653 int *p, i; 654 //for ( i = 0; i < 10; i++) 655 //{ 656 // printf("%d",*(a + i)); 657 //} 658 for (p = a; p < (a+10); p++) //数组名a表示数组第一个元素的地址 即 p = &a[0] 659 { 660 printf("%d", *p); 661 } 662 /****************************************************/ 663 int *p, i , a[10]; 664 p = a; 665 for ( i = 0; i < 10; i++) 666 { 667 scanf("%d",p++ ); 668 } 669 p = a; 670 for ( i = 0; i < 10; i++) 671 { 672 printf("%d",*p++); 673 #endif //用指针遍历数组 674 675 #if( 0 ) 676 int a[10] = {1,2,3,4,5,6,7,8,9,10}, 677 *p = &a[3], b; 678 b = p[5]; 679 printf("%d",b); 680 #endif //一维数组指针取址 681 682 #if( 0 ) 683 int a[3], *p, *s; 684 for (p = a; p < a+3; p++) 685 { 686 scanf("%d",p); 687 } 688 for (p = a, s = a; p < a+3; p++) 689 { 690 if(*p>*s) s = p; 691 } 692 printf("%d",s - a ); 693 #endif //一维数组借助指针变量求最大值及所在位置 694 695 #if( 0 ) 696 int x[] = {10,20,30}; 697 int *px = x; 698 printf("%3d", ++*px); printf("%3d", *px); 699 //px = x; 700 printf("%3d", (*px)++);printf("%3d", *px); 701 //px = x; 702 printf("%3d", *px++); printf("%3d", *px); 703 //px = x; 704 printf("%3d", *++px); printf("%3d\n", *px); 705 #endif //一维数组输出 *px++ , (*px)++ , *++px 706 707 #if( 0 ) 708 int a[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}}; 709 printf("a: %d\n",a); 710 printf("*a: %d\n",*a); 711 printf("a[0]: %d\n",a[0]); 712 printf("&a[0]: %d\n",&a[0]); 713 printf("&a[0][0]: %d\n",&a[0][0]); 714 #endif //输出二维数组的地址 715 716 #if( 0 ) 717 //int a[10] = {3,7,9,11,0,6,7,5,4,2}, b[10]; 718 //int *p1 = a , *p2, i ,j; 719 //for (i = 9, j=0; i >= 0; i--,j++) 720 //{ 721 // b[j] = *(p1 + i); 722 //} 723 //for ( j = 0; j < 10; j++) 724 //{ 725 // printf("%4d",b[j]); 726 //} 727 /*************************************************************/ 728 729 //int a[10] = {3,7,9,11,0,6,7,5,4,2}; 730 //int i; 731 //void reverse(int x[],int n); 732 //for ( i = 0; i < 10; i++) 733 //{ 734 // printf("%4d",a[i]); 735 //} 736 //printf("\n"); 737 //printf("反序为:\n"); 738 //reverse(a,10); 739 //for ( i = 0; i < 10; i++) 740 //{ 741 // printf("%4d",a[i]); 742 //} 743 //printf("\n"); 744 /*************************************************************/ 745 746 int a[10] = {3,7,9,11,0,6,7,5,4,2}; 747 int i; 748 void reverse1(int *x,int n); 749 for ( i = 0; i < 10; i++) 750 { 751 printf("%4d",a[i]); 752 } 753 printf("\n"); 754 printf("反序为:\n"); 755 reverse1(a,10); 756 for ( i = 0; i < 10; i++) 757 { 758 printf("%4d",*(a+i)); 759 } 760 printf("\n"); 761 #endif //将数组a中n个整数按相反顺序存放 762 763 #if( 0 ) 764 //int a[10],i; 765 //void sort(int a[], int n); 766 //for ( i = 0; i < 10; i++) 767 //{ 768 // scanf("%d",&a[i]); 769 //} 770 //sort(a,10); 771 //for ( i = 0; i < 10; i++) 772 //{ 773 // printf("%2d",a[i]); 774 // printf("\n"); 775 //} 776 777 int a[10],i; 778 void sort1(int *a, int n); 779 for ( i = 0; i < 10; i++) 780 { 781 scanf("%d",&a[i]); 782 } 783 sort1(a,10); 784 for ( i = 0; i < 10; i++) 785 { 786 printf("%2d",a[i]); 787 printf("\n"); 788 } 789 #endif //从大到小排列数组的10个数 790 791 #if( 0 ) 792 int a[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}}; 793 int (*p)[4]; //声明二维数组指针变量 (一行存4个元素地址的一维数组) “4”表示二维数组分解为多个一维数组的长度 “int *p[4]指针数组:这个数组大小为4,它的元素均为指针类型数据,称为指针数组” 794 int i,j; //定义行和列 795 p = a; //二维数组的地址指向了数组指针的地址 796 for ( i = 0; i < 3; i++) 797 { 798 for ( i = 0; i < 4; i++) 799 { 800 printf("%2d",*(*(p+i)+j)); // p+i:是行的地址 a[i]; *(p+i):是列的首地址 &a[i][0]; *(p+i)+j:是列的首地址+列的位置(二维数组i行j列的元素地址); *(*(p+i)+j):是行和列二维数组的值; 801 } 802 } 803 #endif //遍历二维数组(行指针) 804 805 #if( 0 ) 806 int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; 807 int (*p)[4]; //表示定义一个指针变量,它指向一个包含12个整型元素的一堆数组 808 int i=0; 809 p=a; 810 printf("%d\n",(*p)[11]); // 最后一个元素的值 811 #endif //数组指针变量 812 813 814 #if( 0 ) 815 //char string[] = "I love you"; 816 //puts(string); 817 818 //char *string = "I love you"; 819 //printf("%s",string); 820 821 //下标法 822 //char string[] = "I love you", b[50]; 823 //int i; 824 //for ( i = 0; *(string+i)!='\0'; i++) 825 //{ 826 // *(b+i) = *(string+i); 827 //} 828 //*(b + i) = '\0'; 829 //for ( i = 0; b[i] != '\0'; i++) 830 //{ 831 // printf("%c",b[i]); 832 //} 833 834 //指针方法 835 char string[] = "I love you", b[50], *p1 , *p2; 836 int i; 837 p1 = string; 838 p2 = b; 839 for ( ; *p1 != '\0'; p1++, p2++) 840 { 841 *p2 = *p1; 842 } 843 *p2 = '\0'; 844 printf("%s\n",string); 845 for ( i = 0; b[i] != '\0' ; i++) 846 { 847 printf("%c",b[i]); 848 } 849 #endif //输出字符串 850 851 #if( 0 ) 852 //指针方法 853 //void copy(char a[],char b[]); 854 //char str1[] = "Duck don't do it"; 855 //char str2[] = "You're thinking peach"; 856 //printf("str1:%s \n str2:%s \n",str1,str2); 857 //printf("copy string str1 to string str2:\n"); 858 //copy(str1,str2); 859 //printf("\nstr1:%s \n str2:%s\n",str1,str2); 860 861 //下标法 862 void copy1(char *a,char *b); 863 char *str1 = "you are a pig"; 864 char str2[] = "yes,you too"; 865 printf("str1:%s \n str2:%s \n",str1,str2); 866 printf("copy string str1 to string str2:\n"); 867 copy1(str1,str2); 868 printf("str1:%s \n str2:%s",str1,str2); 869 #endif //复制字符串 870 871 #if( 0 ) 872 int max(int ,int ); //声明一个求最大值的函数 873 int(*p)(int , int ); //声明指向函数max的指针变量p 874 int a,b,c; 875 p = max; //将函数名max赋值给指针变量p,使p指向函数max 876 scanf("%d %d",&a,&b); 877 c = (*p)(a,b); //通过指向函数max的指针变量p调用函数max 878 printf("a = %d, b = %d, max = %d \n", a ,b ,c); 879 #endif //用函数指针求最大值 880 881 #if( 0 ) 882 char *day[ ] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; 883 int today; 884 printf("今天是星期"); 885 scanf("%d", &today); 886 //char **today = day; 887 //int i; 888 //for ( i = 0; i < 7; i++) 889 //{ 890 // printf("%s",*(day + i)); 891 //} 892 switch (today) 893 { 894 case 1: 895 printf("%s",*day); 896 break; 897 case 2: 898 printf("%s",*(day+1)); 899 break; 900 case 3: 901 printf("%s",*(day+2)); 902 break; 903 case 4: 904 printf("%s",*(day+3)); 905 break; 906 case 5: 907 printf("%s",*(day+4)); 908 break; 909 case 6: 910 printf("%s",*(day+5)); 911 break; 912 case 7: 913 printf("%s",*(day+6)); 914 break; 915 } 916 #endif //指针数组输出星期几 917 918 #if( 0 ) 919 double area (double x1, double y1); 920 double length (double x2, double y2); 921 double cal(double x3, double y3, double (*p)(double , double )); 922 double x,y,S = 0, L = 0; 923 printf("please input two sides \n"); 924 scanf("%lf %lf",&x,&y); 925 S = cal(x, y, area); 926 L = cal(x, y, length); 927 printf("S = %.2lf\n", S); 928 printf("L = %.2lf",L); 929 #endif //调用同一个函数,第一次求三角形的面积,第二次求三角形的第三边长 930 931 #if( 0 ) 932 int max(int, int); 933 int min(int, int); 934 int add(int, int); 935 void process(int x,int y,int (*fun)(int,int) ); 936 937 int a,b; 938 printf("import a and b \n"); 939 scanf("%d %d",&a,&b); 940 941 printf("max = "); 942 process(a , b , max); 943 944 printf("\nmin = "); 945 process(a , b , min); 946 947 printf("\nadd = \n"); 948 process(a , b , add); 949 #endif //使用函数指针实现多态的功能(求最大值、最小值、之和) 950 951 #if( 0 ) 952 //double sorce[][4] = {{60.0,70.0,80.5,90.5},{56.0,89.0,67.0,88.0},{34.2,70.5,90.5,89.0},{95.0,68.0,78.5,88.0}}; 953 //double *search(double(*pointer)[4], int n);//声明一个指针函数 第一个形参是 数组指针(一行存4个元素地址的一维数组) 第二个形参是输入的学生序号 954 //double *p; 955 //int i, m; 956 //printf("please enter the number of student:"); 957 //scanf("%d",&m);//输入要查询的学生序号 958 //printf("The sorces of No %d are : \n",m); 959 //p = search(sorce, m); //调用指针函数 返回一个地址 960 //for ( i = 0; i < 4; i++) 961 //{ 962 // printf("%5.2f\t",*( p + i )); 963 //} 964 //printf("\n\n\n"); 965 966 //char *a; 967 //a = "abc"; 968 //char a[]={"abc"}; 969 //printf("%s",a); 970 971 972 973 //char str[10]; 974 //scanf("%s",str); 975 976 //char *a; //没有赋初值会随机指向 (不行) 977 //scanf("%s",a); 978 #endif //用指针函数输出学生的全部成绩 979 980 #if( 0 ) 981 int a[3][4] = {{1,9,5,13},{23,8,49,2},{30,15,20,6}}; 982 int (*p)[4],max,i,j,k = 0,t = 0; 983 p = a ; 984 max = a[0][0]; 985 for ( i = 0; i < 3; i++) 986 { 987 for ( j = 0; j < 4; j++) 988 { 989 if(*(*(p+i)+j) > max) 990 max = (*(*(p+i)+j)) 991 ,k = i, t = j; 992 993 } 994 } 995 996 printf("max = %d , k = %d , t = %d",max,k,t); 997 #endif //利用行指针求二维数组中的最大值及其下标值 998 999 #if( 0 ) 1000 int i,a[10],*p,max ,min , k = 0, t = 0, temp = 0; 1001 p = a; 1002 for ( i = 0; i < 10; i++) 1003 { 1004 scanf_s("%d",p++); 1005 } 1006 p = a; 1007 max = min = a[0]; 1008 for ( i = 0; i < 10; i++) 1009 { 1010 if(*(p+i)> max){ 1011 max = *(p+i), t = i ; 1012 1013 } 1014 } 1015 temp = a[9]; 1016 a[9] = max; 1017 a[t] = temp; 1018 1019 1020 for ( i = 0; i < 10; i++) 1021 { 1022 if(*(p+i) < min){ 1023 min = *(p+i), k = i ; 1024 } 1025 } 1026 temp = a[0]; 1027 a[0] = min; 1028 a[k] = temp; 1029 for ( i = 0; i < 10; i++) 1030 { 1031 printf("%3d",*p++); 1032 } 1033 #endif //输入10个数,将其中最小的数与第一个数对换,最大数与最后一个数对换 1034 1035 #if( 0 ) 1036 1037 int a[3][4] = {{1,9,5,13},{23,8,49,2},{30,15,20,6}}; 1038 int (*p)[4], i, j, min, k = 0 , t = 0; 1039 p = a; 1040 min = a[0][0]; 1041 for ( i = 0; i < 3; i++) 1042 { 1043 for ( j = 0; j < 4; j++) 1044 { 1045 if ( min > *(*(p+i)+j)) 1046 min = *(*(p+i)+j), k = i, t = j; 1047 1048 } 1049 } 1050 printf("min = %d, k = %d , t = %d",min , k , t); 1051 1052 #endif //最小值及最小值下标 1053 1054 #if( 0 ) 1055 char *p = "hello"; 1056 int i = 0; 1057 while (p[i] != '\0') 1058 { 1059 i++; 1060 } 1061 printf("%d\n",i); 1062 //char *p = "hello" ; 1063 //int i = 0; 1064 //while (*p != '\0') 1065 //{ 1066 // p++; 1067 // i++; 1068 //} 1069 //printf("%d\n",i); 1070 #endif //计算字符串长度(不调用strlen) 1071 1072 #if( 0 ) 1073 //char s[10], *p = "Chinese"; 1074 //int i = 0; 1075 //while (p[i] != '\0') 1076 //{ 1077 // s[i] = p[i]; 1078 // i++; 1079 //} 1080 //s[i] = '\0'; 1081 //printf("%s",s); 1082 #endif //不调用strcpy 复制字符串 1083 1084 #if( 0 ) 1085 char *t[3] = {"google","baidu","yahoo"}, *s[3], *min; 1086 int i= 0; 1087 for ( i = 0; i < 3; i++) 1088 { 1089 s[i] = t[i]; 1090 } 1091 if (strcmp(s[0],s[1]) < 0 && strcmp(s[0],s[2]) < 0) 1092 { 1093 min = s[0]; 1094 } 1095 if (strcmp(s[1],s[0]) < 0 && strcmp(s[1],s[2]) < 0) 1096 { 1097 min = s[1]; 1098 } 1099 if (strcmp(s[2],s[0]) < 0 && strcmp(s[2],s[1]) < 0) 1100 { 1101 min = s[2]; 1102 } 1103 printf("%s\n",min); 1104 1105 #endif //输出三个字符串中最小的字符串 1106 1107 #if( 0 ) 1108 int add(int x); 1109 int n, sum = 0; 1110 scanf("%d",&n); 1111 sum = add(n); 1112 printf("1 - %2d sum is %d",n,sum); 1113 #endif //(递归)计算1+2+3+4 、、、 +n的值 1114 1115 #if( 0 ) 1116 1117 void f(int *x,int *y); 1118 int a[8] = {1,2,3,4,5,6,7,8},i = 0, *p , *q; 1119 p = a; q = &a[7]; 1120 while (*p!=*q && i<8) 1121 { 1122 f(p,q); p++; q--; i++; //p从首地址加到&a[7] q从&a[7]减到首地址 i=7时 i=8时 *p = *q = -858993460 1123 } 1124 for ( i = 0; i < 8; i++) 1125 { 1126 printf("%d",a[i]); 1127 } 1128 #endif //指针练习题 1129 1130 #if( 0 ) 1131 int subString(char *a,char *b), num; 1132 char *p = "deffafaddd", *q = "da"; 1133 num = subString(p,q); 1134 printf("%d",num); 1135 #endif //查找一个子串在字符串中出现的个数 1136 1137 #if( 0 ) 1138 int x = 0, i ,m = 0 , n = 0 ; 1139 for ( i = 1; i <=99 ; i++) 1140 { 1141 if( i < 10){ 1142 x = pow((double)i,2); 1143 m = x % 10; 1144 n = x /10; 1145 if(i == m || i == n) printf("%d 和 %d 是同构数\n",i,x); 1146 } 1147 else 1148 { 1149 x = pow((double)i,2); 1150 //n = x /10 ; 1151 m = x%10+(x/10%10*10); 1152 if(i == m ) printf("%d 和 %d 是同构数\n",i,x); 1153 } 1154 } 1155 1156 #endif //同构数 1157 1158 #if( 1 ) 1159 //int i, x = 0 , y; 1160 //for ( y = 0, x = 1;x > ++y; x = i++ ) 1161 //{ 1162 // i = x; 1163 //} 1164 1165 1166 //int i , b ,k = 0; 1167 //for ( i = 1; i <=5 ; i++) 1168 //{ 1169 // b = i % 2; 1170 // while (b-->=0) k++; 1171 //} 1172 //printf("%d %d",k,b); 1173 1174 //int i = 32767; 1175 //do 1176 //{ 1177 // if(i<0) break; 1178 //} while (++i); 1179 1180 //int i = 1, j = 1; 1181 //do 1182 //{ 1183 // if(i % 3 == 2 && i % 5 == 3 && i % 7 ==2) 1184 // printf("%4d",i); 1185 // j = j +1; 1186 // if(j % 5 == 0) printf("\n"); 1187 // 1188 // 1189 // i = i +1; 1190 // 1191 //} while (i<1000); 1192 1193 //int i,j; 1194 // for(i=0;i<=3;i++) 1195 // {for(j=0;j<=5;j++) 1196 // if(i==0||j==0||i==3||j==5) printf("*"); 1197 // else printf(" "); 1198 // printf("\n"); 1199 // } 1200 1201 //char a[]="morning",t; 1202 // int i,j=0; 1203 // for(i=1;i<7;i++) 1204 //if(a[j]<a[i]) 1205 //j=i; 1206 //t=a[j]; 1207 //a[j]=a[7]; 1208 //a[7]=t; 1209 //puts(a); 1210 1211 1212 1213 #endif 1214 1215 return 0; 1216 } 1217 1218 int power(int a, int b) //自定义pow函数 1219 { 1220 int i,c = 1; 1221 for (i = 0; i < b; i++) 1222 { 1223 c *= a; 1224 } 1225 return c; 1226 } 1227 int square(int x) //平方函数 1228 { 1229 int z; 1230 z = x * x; 1231 return z; 1232 } 1233 int factorical(int y) //阶乘函数 1234 { 1235 int i,num=1; 1236 for ( i = 1; i <= y; i++) 1237 { 1238 num *= i; 1239 } 1240 return num; 1241 } 1242 int factorical2(int n) //递归 阶乘函数 1243 { 1244 int num=1; 1245 if(n < 0) 1246 { 1247 printf("Input error"); 1248 } 1249 else if(n == 0 || n == 1 ) 1250 { 1251 num = 1; 1252 } 1253 else 1254 { 1255 num = n * factorical2(n - 1); 1256 } 1257 return num; 1258 } 1259 void hanoi(int n , char x , char y , char z ) //汉诺塔 1260 { 1261 if (n == 1) 1262 { 1263 printf("%c => %c\n",x,z); 1264 } 1265 else 1266 { 1267 hanoi(n-1 , x , z , y); 1268 printf("%c => %c\n",x,z); 1269 hanoi(n-1 , y , x , z); 1270 } 1271 } 1272 void average(int grade[]) //求平均成绩 1273 { 1274 int sum = 0 , i; 1275 for ( i = 0; i < 10; i++) 1276 { 1277 sum += grade[i]; 1278 } 1279 printf("%g\n",sum / 10.0); 1280 } 1281 int fac(int a) //使用静态变量求阶乘 1282 { 1283 static int b = 1; 1284 b = b * a; 1285 return b; 1286 } 1287 extern void removestr(char str[],char char2) 1288 { 1289 int i,j; 1290 for ( i = j = 0; str[i] != '\0'; i++) 1291 { 1292 if(str[i] != char2) 1293 { 1294 str[j++] = str[i]; 1295 } 1296 } 1297 str[j] = '\0'; 1298 puts(str); 1299 } 1300 void reverse(int x[],int n) 1301 { 1302 int m, j, i, temp; 1303 m = (n - 1) / 2; 1304 for ( i = 0; i < m; i++) 1305 { 1306 j = n - 1 - i; 1307 temp = x[i]; 1308 x[i] = x[j]; 1309 x[j] = temp; 1310 1311 } 1312 } 1313 void reverse1(int *x,int n) 1314 { 1315 int m, *j, *i, *p,temp; 1316 m = (n - 1) / 2; 1317 i = x; // 数组元素第一个元素的地址 (i指向数组的第一个元素) 1318 j = x+n-1; // 数组元素最后一个元素的地址 1319 p = x + m; // 数组元素中间一个元素的地址 1320 for (; i <= p; i++, j--) 1321 { 1322 temp = *i; 1323 *i = *j; 1324 *j = temp; 1325 } 1326 } 1327 void sort(int a[], int n) 1328 { 1329 int temp, i, j; 1330 bool flag = false; 1331 for ( j = 0; j < n-1; j++) 1332 { 1333 for ( i = 0; i < 9-j; i++) 1334 { 1335 if(a[i] < a[i+1]) 1336 { 1337 temp = a[i]; 1338 a[i] = a[i+1]; 1339 a[i+1] = temp; 1340 flag = true; 1341 } 1342 } 1343 if(!flag) 1344 { 1345 break; 1346 } 1347 } 1348 //int i,j,k,t; 1349 //for ( i = 0; i < n-1; i++) 1350 //{ 1351 // k = i ; 1352 // for ( j = i+1; j < n; j++) 1353 // { 1354 // if(a[j] > a[k]) 1355 // { 1356 // t = a[j]; 1357 // a[j] = a[k]; 1358 // a[k] = t; 1359 // } 1360 // } 1361 //} 1362 1363 1364 } 1365 void sort1(int *a, int n) 1366 { 1367 //int i,j,k,t; 1368 //for ( i = 0; i < n-1; i++) 1369 //{ 1370 // k = i ; 1371 // for ( j = i+1; j < n; j++) 1372 // { 1373 // if(a[j] > a[k]) 1374 // { 1375 // t = a[j]; 1376 // a[j] = a[k]; 1377 // a[k] = t; 1378 // } 1379 // } 1380 //} 1381 1382 int *i, *j, *k ,t; 1383 for ( i = a; i < a+n-1; i++) 1384 { 1385 k = i; 1386 for ( j = i + 1; j < a + n; j++) 1387 { 1388 if(a[*j] > a[*k]) 1389 { 1390 t = a[*j]; 1391 a[*j] = a[*k]; 1392 a[*k] = t; 1393 } 1394 } 1395 } 1396 } 1397 void copy(char a[],char b[]) 1398 { 1399 int i = 0; 1400 for (; b[i] != '\0'; i++) 1401 { 1402 b[i] = a[i] ; 1403 } 1404 b[i] = '\0'; 1405 1406 } 1407 void copy1(char *a,char *b) 1408 { 1409 for (; *b != '\0'; a++,b++ ) 1410 { 1411 *b = *a; 1412 } 1413 *b = '\0'; 1414 } 1415 /* 1416 int max (int x, int y) 1417 { 1418 if(x>y) 1419 return x; 1420 else 1421 return y; 1422 } 1423 int min (int x, int y) 1424 { 1425 if(x > y) 1426 return y; 1427 else 1428 return x; 1429 } 1430 int add (int x, int y) 1431 { 1432 int z; 1433 z = x + y; 1434 return z; 1435 }*/ 1436 void process(int x,int y,int (*fun)(int,int) ) 1437 { 1438 printf("%d",(*fun)(x,y)); 1439 } 1440 double *search(double (*pointer)[4], int n) 1441 { 1442 double *pt; 1443 pt = *(pointer + n); 1444 return pt; 1445 } 1446 /*triangle 1447 double area (double x1, double y1) 1448 { 1449 return x1 * y1 * 0.5; 1450 } 1451 double length (double x2, double y2) 1452 { 1453 return sqrt(x2 * x2 + y2 * y2); 1454 } 1455 double cal(double x3, double y3, double (*p)(double x4, double y4)) 1456 { 1457 return (*p)(x3,y3); 1458 } 1459 */ 1460 int add(int x) 1461 { 1462 int sum = 0; 1463 if(x == 1) 1464 { 1465 x = 1; 1466 } 1467 else 1468 { 1469 sum += 1 + x + add(x-1); 1470 } 1471 return sum; 1472 } 1473 void f(int *x,int *y) 1474 { 1475 int t; 1476 t = *x; *x = *y; *y = t; 1477 } 1478 int subString(char *a,char *b) 1479 { 1480 int i , j, n = 0; 1481 for ( i = 0; i < 10; i++) 1482 { 1483 for ( j = 0; j < 10; j++) 1484 { 1485 if(a[i] != '\0' && b[i] != '\0' ) 1486 if(a[i] == b[j]) 1487 { 1488 n++; 1489 } 1490 } 1491 } 1492 return n; 1493 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix