1. 程式人生 > >C語言--二維陣列,字串陣列,多維陣列

C語言--二維陣列,字串陣列,多維陣列

 

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
//    int a[2][3]={
//        {1,2,3},
//        {4,5,6}
//    };
//    int a[2][3]={1,2,3,4,5,6};
//    //列印單個元素
//    printf("%d",a[1][1]);
//    //元素沒賦全,預設為0
//    int b[2][3]={{1,2,3},{4}};
//    
//    
//    
//    //3行4列浮點型資料
//    float c[3][4]={
//        {2.5},
//        {3.3},
//        {2.1,4.8}
//    };
//    for (int i = 0; i<3; i++) {
//        for (int j = 0; j<4; j++) {
#pragma mark----------------------%g科學計數法,保留小數後面,去掉0後面的
//     //%g科學計數法,保留小數後面,去掉0後面的
//            printf("%g ",c[i][j]);
//        }
//        printf("\n");
//    }
    //定義一個三行四列的整數,隨機範圍0-30
//    int a[3][4]={0};
//    for (int i = 0; i<3; i++) {
//        for (int j = 0 ; j<4; j++) {
//            a[i][j]=arc4random()%(30+1);
//            printf("%2d ",a[i][j]);
//        }
//        printf("\n");
//    }
    //把行和列交換,放到一個新的陣列
//    int b[4][3]={0};
//    for (int i =0; i<3; i++) {
//        for (int j = 0; j<4; j++) {
//            b[j][i]=a[i][j];
//        }
//    }
//    printf("\n");
//    for (int i = 0; i<4; i++) {
//        for (int j = 0; j<3; j++) {
//            printf("%-2d ",b[i][j]);
//        }
//        printf("\n");
//    }
    
    //找出最大元素,並輸入行和列
//    int max = 0;
//    for (int i =0 ; i<3; i++) {
//        for (int j =0; j<4; j++) {
//            if (max<a[i][j]) {
//                max=a[i][j];
//            }
//        }
//    }
//    for (int i = 0; i<3; i++) {
//        for (int j = 0; j<4; j++) {
//            if (max==a[i][j]) {
//                printf("max=%d,i=%d,j=%d",max,i,j);
//            }
//        }
//    }
    
    
//    char str[6][20]={
//        "zuoyoudong",
//        "fanghao",
//        "yaozhaodi",
//        "dengyongjun",
//        "makeyu",
//        "yangzhigang"
//    };
    //操作字串
    //列印單個字串
    //printf("%s",str[第一維下標])
    //列印字串陣列中所有字串
//    long maxLength = 0;
//    for (int i = 0; i<6; i++) {
//        printf("%s ",str[i]);
//      printf("%ld",strlen(str[i]));//求長度
//      printf("%s",strcpy(str[i], "zhangsan"));//賦值
//      printf("%s",strcat(str[i], "bushiwoge"));//拼接
//      printf("%d",strcmp(str[i], "zuoyoudong"));//比較
//        if (maxLength < strlen(str[i])) {
//            maxLength = strlen(str[i]);
//        }
//    }
#pragma mark----------對字串操作,對字元操作
    //對字串操作,
    //第一,列印
    //第二,求長度
    //第三,賦值
    //第四,拼接
    //第五,比較
    //第六,排序(冒泡)
    
    
    //對字元操作
    //第一,賦值
    //第二,取值
    //需要陣列名,加雙下標
    
//    printf("\n%c",str[0][6]);
    
    //陣列是一個容器
//    printf("\n");
//    for (int i = 0; i<6-1; i++) {
//        for (int j = 0; j<6-1-i; j++) {
//            if (strcmp(str[j], str[j+1])>0) {//如果第j個字串大於第j+1個字串
//                char temp[20] ={0};//定義一個臨時的字串,以便交換值
//                strcpy(temp, str[j]);
//                strcpy(str[j],str[j+1]);
//                strcpy(str[j+1], temp);
//            }
//        }
//    }
//    printf("\n");
//    for (int i = 0; i<6; i++) {
////        printf("%s ",str[i]);
//        if (maxLength == strlen(str[i]) ) {
//            printf("最長的單詞是%s,長度是%lu   ",str[i],maxLength);
//        }
//    }
    
    
    
    
    
    
    
    //1.將一個二維陣列的行和列交換,放到一個新的陣列中去
//    int a[2][3]={
//        {1,5,9},
//        {3,2,8}
//    };
//    int b[3][2]={0};
//    for (int i = 0; i<2; i++) {
//        for (int j = 0; j<3; j++) {
//            b[j][i]=a[i][j];
//        }
//    }
//    for (int i = 0; i<3; i++) {
//        for ( int j = 0; j<2; j++) {
//            printf("%d ",b[i][j]);
//        }
//        printf("\n");
//    }
    //2.有一個三行四列的二維陣列,找出其中的最大值並寫出所在行和列
//    int a[3][4]={
//        {7,4,3,8},
//        {2,12,18,6},
//        {21,33,50,25},
//    };
//    int max = 0;
//    for (int i = 0; i<3; i++) {
//        for (int j = 0; j<4; j++) {
//            if (max<a[i][j]) {
//                max=a[i][j];
//            }
//        }
//    }
//    for (int i = 0; i<3; i++) {
//        for (int j = 0; j<4; j++) {
//            if (max==a[i][j]) {
//                printf("最大值是%d,所在行是%d和列是%d",max,i,j);
//            }
//        }
//    }
    //3.輸入三個單詞,查詢並輸出最長的單詞
//    printf("請輸入三個單詞:");
//    char a[3][10]={
//        "zuo",
//        "you",
//        "dong"
//    };
    //輸入三個字串
//    char a[3][20]={0};
//    for (int i = 0; i<3; i++) {
//         scanf("%s",a[i]);
//    }
////    printf("%s",a[0]);
//    long maxLength = 0;
//    for (int i =0; i<3; i++) {
//        if (maxLength<strlen(a[i])) {
//            maxLength=strlen(a[i]);
//        }
//    }
//    for (int i = 0; i<3; i++) {
//        if (maxLength==strlen(a[i])) {
////            printf("%s",a[i]);
//            printf("%ld %s",maxLength,a[i]);
//        }
//    }
    
    //有10個姓名,按從小到大排列出來
//    char str[10][20]={
//        "zhangsan",
//        "lisi",
//        "wangerma",
//        "cuiyayun",
//        "yunjie",
//        "fanghao",
//        "zuoyoudong",
//        "liuping",
//        "zuoyouwei",
//        "liuhui"
//    };
//    char temp[20]={0};
//    for (int i = 0; i<10-1; i++) {
//        for (int j = 0; j<10-1-i; j++) {
//            if (strcmp(str[j], str[j+1])>0) {
//                strcpy(temp, str[j]);
//                strcpy(str[j], str[j+1]);
//                strcpy(str[j+1], temp);
//            }
//        }
//    }
//    for (int i = 0; i<10; i++) {
//        printf("%s ",str[i]);
//    }
    //把字元變成全大寫,AC碼大寫A65,a97,減去32
    
    
    return 0;
}