iOS開發-Day4-C的複習

1、一維數組

//一維數組、多維數組、字符數組
        int a[kCount] = {1,2,3,4,5};
        for (int i = 0; i < 5; i++)
        {
            //a[i]是數組a的第i個元素,a代表數組首地址,a+i是a[i]的地址,*(a+i)是*指向a[i]的地址
            printf("%d",a[i]);
            printf("%d \n",*(a+i));
        }
//        int b[kCount] = {1,2,7,8,9};
        
        //變量,指針都是標量類型  數組,結構體,共用體屬於聚合類型
        //數組中的成員叫數組元素
        
        //數組地址是依次後排,加的字節數跟數據類型有關
        printf("%d %d %p %p\n",a[0],a[1],a,a+1); //%p打印地址
        

 //數組跟for循環一般一起出現,計算總和,遍歷,平均值,最大值等操作

2、宏定義

//宏定義一般最後不加分號,可以根據實際情況判斷
#define kCount  8
#define kMax1(a,b) a>b?a:b //獲取ab中大的一個

printf("%d",kMax1(13, 12));

3、getchar()  putchar()  gets  puts  fgets

//scanf不能用於有空格的輸入,因爲空格回車會作爲scanf輸入結束的判斷
                char a99[50];
                scanf("%s",a99);
                printf("%s",a99);

//如果想要輸入空格到字符串變量中,gets以回車結束,不會進行數組越界上限判斷
                gets(a99);//warning: this program uses gets(), which is unsafe.
                printf("%s",a99);
        
        //fgets從stdin中讀入7個,剩餘最後一個自動寫入'\0',fgets用於替換gets方法,提高安全性
                fgets(a99, 8, stdin);
                puts(a99);

fgets fputs 讀取和寫入文件


//sscanf 從字符串中讀入到另一個字符串
        printf("\n");
                sscanf("1234abcdABCD AA", "%s",a99);//將前面的字符串以%s的形式讀入a99,sscanf跟scanf一樣,不能讀入空格
                puts(a99);//在printf的基礎上加了一個\n
                printf("%s\n",a99);
        
        //正則表達式 用於匹配,做篩選用的,一般用於查詢篩選
        sscanf("1234abcdABCD AA", "%[^a-z]",a99);//讀到a-z中任一字符終止,後邊即時還有其他字符也不做判斷
        puts(a99);
        
        strcpy(a99, "");
        printf("-----");
        sscanf("1234abcdABCD AA", "%[a-z]",a99);//只讀a-z範圍之內的,讀到非a-z的任一字符終止,後邊即時還有其他字符也不做判斷
        puts(a99);
        
        strcpy(a99, "");
        printf("-----");
        sscanf("1234dcbaABCD AA", "%[a-z1-9]",a99);//只讀a-z範圍之內的,讀到非a-z的任一字符終止,後邊即時還有其他字符也不做判斷
        //[a-z1-9]方括號裏邊只說明範圍,不管順序
        puts(a99);
        

        int a100;
        scanf("%*d %d",&a100);//%*d是過濾掉他讀入的數值
        printf("%d",a100);

        //提取12abc,前面iOS/過濾掉,後邊@sina.com過濾掉
        printf("\n*****\n");
        sscanf("iOS/[email protected]", "%*[^2]2%[^@]",a99);//[^2]2中兩個2是一個字符,替換這兩個代表過濾到2,與%*配合使用
        printf("%s\n",a99);
        
        //多維數組
        char b111[20][50] = {"abc","def","ghi"};//存姓名
        int a110[2][20];//a[0]存年齡,a[1]存成績
        printf("%lu\n",strlen(b111[0]));
        strcpy(b111[3], b111[0]);
        printf("%s\n",b111[3]);
        
        //>0是大於,<0是小於 ==0是等於
        printf("%d\n",strcmp(b111[0], b111[1]));
        printf("%d\n",strcmp(b111[0], b111[3]));
        
        //把右邊的字符串拼到前面的字符串後邊
        strcat(b111[0], "123");
        puts(b111[0]);



補充部分day4的練習工程

//
//  main.m
//  C04 array


#import <Foundation/Foundation.h>

//宏定義一般最後不加分號,可以根據實際情況判斷
#define kCount  8
#define kMax1(a,b) a>b?a:b
#define M   200

//struct Candidate
//{
//    long num;
//    int poll;
//};

//struct Person
//{
//    int num;
//    int goOutOrder;
//    
//};
//
//void sortArray(int aa[],int n)
//{
//    int tmp,count = n;
//    for (int i = 0; i < count-1; i++)
//    {
//        for (int j = 0; j < count-1-i; j++)
//        {
//            if (aa[j] > aa[j+1])
//            {
//                tmp = aa[j];
//                aa[j] = aa[j+1];
//                aa[j+1] = tmp;
//            }
//        }
//    }
//}
//
//void candidateCopy(struct Candidate a1,struct Candidate a2)
//{
//    a1.num = a2.num;
//    a1.poll = a2.poll;
//}
//
//void printCandidate(struct Candidate aa[],int n)
//{
//    for (int i = 0; i < n; i++)
//    {
//        printf("%ld %d\n",aa[i].num,aa[i].poll);
//    }
//}
//
//void sortCandidateByPoll(struct Candidate aa[],int n)
//{
//    struct Candidate tmp;
//    int count = n;
//    for (int i = 0; i < count-1; i++)
//    {
//        for (int j = 0; j < count-1-i; j++)
//        {
//            if (aa[j].poll > aa[j+1].poll)
//            {
//                candidateCopy(tmp, aa[j]);
//                candidateCopy(aa[j], aa[j+1]);
//                candidateCopy(aa[j+1], tmp);
//            }
//        }
//    }
//}
//
//void personArraySetInitialValue(struct Person aa[],int n,int initialValue)
//{
//    for (int i = 0; i < n; i++)
//    {
//        aa[i].num = i+1;
//        aa[i].goOutOrder = initialValue;
//    }
//}
//
//void personCopy(struct Person *aa,struct Person *bb)
//{
//    aa->num = bb->num;
//    aa->goOutOrder = bb->goOutOrder;
//}
//
//void personFromArrayGoOut(struct Person aa[],int i,int currentCount)
//{
//    struct Person tmp;
//    personCopy(&tmp, &aa[i]);
//    for (int j = i; j < currentCount-1; j++)
//    {
//        personCopy(&aa[j], &aa[j+1]);
//    }
//    personCopy(&aa[currentCount-1], &tmp);
//}
//
//void sortPersonByNum(struct Person aa[1],int n)
//{
//    for (int i = n-1; i >= 0; i--)
//    {
//        aa[i].goOutOrder = n-i;
//    }
//    
//    struct Person tmp;
//    int count = n;
//    for (int i = 0; i < count-1; i++)
//    {
//        for (int j = 0; j < count-1-i; j++)
//        {
//            if (aa[j].num > aa[j+1].num)
//            {
//                personCopy(&tmp, &aa[j]);
//                personCopy(&aa[j], &aa[j+1]);
//                personCopy(&aa[j+1], &tmp);
//            }
//        }
//    }
//}
//
//void printGoOutOrder(struct Person aa[1],int n)
//{
//    for (int i = 0; i < n; i++)
//    {
//        printf("第%d個人出列次序是%d\n",aa[i].num,aa[i].goOutOrder);
//    }
//}
//
//void printArray(int aa[1],int n)
//{
//    for (int i = 0; i < n; i++)
//    {
//        printf("%d ",aa[i]);
//    }
//    printf("\n");
//}

//int getDay(int year,int month,int day)
//{
//    int dayCount[2][13] =
//    {
//        {0,31,28,31,30,31,30,31,31,30,31,30,31},
//        {0,31,29,31,30,31,30,31,31,30,31,30,31}
//    };
//    int leapYear = (year%4==0&&year%100!=0)||(year%400==0);
//    int allDay = 0;
//    for (int i = 1; i < month; i++)
//    {
//        allDay += dayCount[leapYear][i];
//    }
//    return allDay+day;
//}

//int getDay(int year,int month,int day)
//{
//    int allday = 0;
//    int daysCount[2][13] =
//    {
//        {0,31,28,31,30,31,30,31,31,30,31,30,31},
//        {0,31,29,31,30,31,30,31,31,30,31,30,31}
//    };
//    
//    int leapYear = ((year % 4==0)&&(year % 100 != 0)) || (year %400 == 0);
//    
//    for (int i = 1; i < month; i++)
//    {
//        allday += daysCount[leapYear][i];
//    }
//    allday += day;
//    return allday;
//}



struct Candidate
{
    char num;
    int poll;
};

enum candidateNum
{
    candidateNum_A,
    candidateNum_B,
    candidateNum_C,
    candidateNum_D,
};

void sortCandidateByPoll(struct Candidate a[],int n)
{
    for (int i = 0; i < n-1; i++)
    {
        for (int j = 0; j < n-1-i; j++)
        {
            if (a[j].poll < a[j+1].poll)
            {
                struct Candidate tmp = a[j];
                a[j] = a[j+1];
                a[j+1] = tmp;
            }
        }
    }
}

void printCandidate(struct Candidate a[],n)
{
    for(int i = 0; i < n;i++)
    {
        printf("編號爲%c的候選人獲得了%d票\n",a[i].num,a[i].poll);
    }
}

struct Person
{
    int num;
    int goOutNum;
};
void initPerson(struct Person p[],int n)
{
    for (int i = 0; i < n; i++)
    {
        p[i].num = i+1;
        p[i].goOutNum = -1;
    }
}

void personGoOut(struct Person p[],int index,int n)
{
    struct Person tmp = p[index];
    for (int i = index; i < n-1; i++)
    {
        p[i] = p[i+1];
    }
    p[n-1] = tmp;
}

//void personWriteGoOutNum(struct Person p[],int n)
//{
//    for (int i = n-1; i >= 0; i--)
//    {
//        p[i].goOutNum = n - i;
//    }
//}
//
//void personSortByGoOutNum(struct Person p[],int n)
//{
//    struct Person tmp;
//    for (int i = 0; i < n-1; i++)
//    {
//        for (int j = 0; j < n-1-i; j++)
//        {
//            if (p[j].num > p[j+1].num)
//            {
//                tmp = p[j];
//                p[j] = p[j+1];
//                p[j+1] = tmp;
//            }
//        }
//    }
//}

void printPerson(struct Person p[],int n)
{
    for (int i = n-1; i >= 0; i--)
    {
        printf("第%d次出列的人是第%d個人\n",n-i,p[i].num);
    }
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        
/*----------------------------------*/
        //一維數組、多維數組、字符數組
//        int a[kCount] = {1,2,3,4,5};
//        for (int i = 0; i < 5; i++)
//        {
//            //a[i]是數組a的第i個元素,a代表數組首地址,a+i是a[i]的地址,*(a+i)是*指向a[i]的地址
////            printf("%d",a[i]);
//            printf("%d \n",*(a+i));
//        }
//        int b[kCount] = {1,2,7,8,9};
        
/*----------------------------------*/
        //變量,指針都是標量類型  數組,結構體,共用體屬於聚合類型
        //數組中的成員叫數組元素
        
/*----------------------------------*/
        //數組地址是依次後排,加的字節數跟數據類型有關
//        printf("%d %d %p %p\n",a[0],a[1],a,a+1);
        
/*----------------------------------*/
        //數組跟for循環一般一起出現,計算總和,遍歷,平均值,最大值等操作
        
/*----------------------------------*/
        //有參宏計算最大值
//        printf("%d",kMax1(13, 12));
        
/*----------------------------------*/
        //getchar() putchar() gets puts fgets
//        while (1)
//        {
//            putchar(getchar());
//        }

/*----------------------------------*/
        //scanf不能用於有空格的輸入,因爲空格回車會作爲scanf輸入結束的判斷
        char a99[50];
//        scanf("%s",a99);
//        printf("%s",a99);

/*----------------------------------*/
        //如果想要輸入空格到字符串變量中,gets以回車結束,不會進行數組越界上限判斷
//        gets(a99);//warning: this program uses gets(), which is unsafe.
//        printf("%s",a99);
        
/*----------------------------------*/
        //fgets從stdin中讀入7個,剩餘最後一個自動寫入'\0',fgets用於替換gets方法,提高安全性
//        fgets(a99, 8, stdin);
//        puts(a99);
        
/*----------------------------------*/
        //fgets fputs 自己嘗試讀取和寫入文件
        
/*----------------------------------*/
        //sscanf 從字符串中讀入到另一個字符串
//        printf("\n");
//        sscanf("1234abcdABCD AA", "%s",a99);//將前面的字符串以%s的形式讀入a99,sscanf跟scanf一樣,不能讀入空格
//        puts(a99);//在printf的基礎上加了一個\n
//        printf("%s\n",a99);
        
/*----------------------------------*/
        //正則表達式 用於匹配,做篩選用的,一般用於查詢篩選
//        sscanf("1234abcdABCD AA", "%[^a-z]",a99);//讀到a-z中任一字符終止,後邊即時還有其他字符也不做判斷
//        puts(a99);

/*----------------------------------*/
//        strcpy(a99, "");
//        printf("-----");
//        sscanf("1234abcdABCD AA", "%[a-z]",a99);//只讀a-z範圍之內的,讀到非a-z的任一字符終止,後邊即時還有其他字符也不做判斷
//        puts(a99);

/*----------------------------------*/
//        strcpy(a99, "");
//        printf("-----");
//        sscanf("1234dcbaABCD AA", "%[a-z1-9]",a99);//只讀a-z範圍之內的,讀到非a-z的任一字符終止,後邊即時還有其他字符也不做判斷
//        //[a-z1-9]方括號裏邊只說明範圍,不管順序
//        puts(a99);
        
/*----------------------------------*/
//        int a100;
//        scanf("%*d %d",&a100);//%*d是過濾掉他讀入的數值
//        printf("%d",a100);
        
/*----------------------------------*/
        //提取12abc,前面iOS/過濾掉,後邊@sina.com過濾掉
//        printf("\n*****\n");
//        sscanf("iOS/[email protected]", "%*[^2]2%[^@]",a99);//[^2]2中兩個2是一個字符,替換這兩個代表過濾到2,與%*配合使用
//        printf("%s\n",a99);

/*----------------------------------*/
//        //多維數組
//        char b111[20][50] = {"abc","def","ghi"};//存姓名
//        int a110[2][20];//a[0]存年齡,a[1]存成績
        
/*----------------------------------*/
//        printf("%lu\n",strlen(b111[0]));
//        strcpy(b111[3], b111[0]);
//        printf("%s\n",b111[3]);
//
//        //>0是大於,<0是小於 ==0是等於
//        printf("%d\n",strcmp(b111[0], b111[1]));
//        printf("%d\n",strcmp(b111[0], b111[3]));
//        
//        //把右邊的字符串拼到前面的字符串後邊
//        strcat(b111[0], "123");
//        puts(b111[0]);
        

/*----------------------------------*/
        /*
         fgets fputs讀寫文件
         */
        
        FILE *file;
        //fopen第一個參數是文件路徑
        /*
          r只讀 文件必須存在
          r+可讀寫 文件必須存在
          w 只寫文件覆蓋,沒有則創建
          w+ 可讀寫文件覆蓋,沒有則創建
          */
//        char path[50];
//        strcpy(path, __FILE__);
//        
//        for (int i = (int)strlen(path)-1; i >= 0; i--)
//        {
//            if (path[i] == '/')
//            {
//                path[i] = '\0';
//                break;
//            }
//        }
//        strcat(path, "/File.txt");
//        
//        
//        if ((file = fopen(path/*"/Users/zhangjing/Documents/File.txt"*/, "r+")) == NULL)
//        {//NULL說明打不開
//            printf("文件打開失敗");
//        }
//        else
//        {
//            fputs("my name is wang fang shuai", file);
//            fclose(file);
//        }
//        
//        //讀取
//        char a[50];
//        if ((file = fopen(path/*"/Users/zhangjing/Documents/File.txt"*/, "r+")) == NULL)
//        {//NULL說明打不開
//            printf("文件打開失敗");
//        }
//        else
//        {
//            fgets(a, 50, file);
//            fclose(file);
//        }
//        printf("%s",a);
        
        
/*----------------------------------*/
        //linux,unix,mac下獲取當前運行目錄,工程目錄的方法
//        printf("The current path : %s\n", getenv("PWD"));//得到當前運行後編譯生成可執行文件所在目錄
//        printf("%s\n",__FILE__);//得到當前文件目錄,需要上級目錄的話將最後的main.m去掉
        
/*----------------------------------*/
        //堆棧
        /*
         棧(操作系統):由操作系統自動分配釋放 ,存放函數的參數值,局部變量的值等。其操作方式類似於數據結構中的棧。
         堆(操作系統): 一般由程序員分配釋放, 若程序員不釋放,程序結束時可能由OS回收,分配方式倒是類似於鏈表。
         */
        
//        char a[50];
//        char *b = malloc(sizeof(char)*50);
//        
//        free(b);
        
        
/*----------------------------------*/
        /*
         1.隨機對有十個元素的數組賦值(20-30),計算找出最大值和所有元素的和
         */
//        int a[10],sum = 0,max = 0;
//        for (int i = 0; i < 10; i++)
//        {
//            a[i] = arc4random()%11+20;
//            sum += a[i];
//            if (a[i] > max)
//            {
//                max = a[i];
//            }
//        }
//        printf("和是%d,最大值是%d",sum,max);
        
        
/*----------------------------------*/
        /*
         2,生成一個具有20個元素的數組,每個元素的取值範圍是30-70,求數組元素的和
         */
//        int aa2[20],sum = 0;
//        for (int i = 0; i < 20; i++)
//        {
//            aa2[i] = arc4random()%41+30;
//            printf("%d ",aa2[i]);
//            sum += aa2[i];
//        }
//        printf("\n和是:%d\n",sum);

        
/*----------------------------------*/
        /*
         2,複製一個數組,即兩個數組容量一樣,把其中一個數組中的元素複製到另一個數組中。
         */
//        int a[10],b[10];
//        for (int i = 0; i < 10; i++)
//        {
//            a[i] = arc4random()%100;
//            b[i] = a[i];
//        }
//        for (int i = 0; i < 10; i++)
//        {
//            printf("%d ",b[i]);
//        }
        
    
/*----------------------------------*/
        /*
         4,生成2個數組,每個數組都有10個元素,元素取值範圍20-40之間,數組對應元素相加,放到另外一個數組中
         */
//        int a[10],b[10],c[10];
//        for (int i = 0; i < 10; i++)
//        {
//            a[i] = arc4random()%21+20;
//            b[i] = arc4random()%21+20;
//            c[i] = *(a+i)+*(b+i);
//        }
//        for (int i = 0; i < 10; i++)
//        {
//            printf("%d ",a[i]);
//        }
//        printf("\n");
//        for (int i = 0; i < 10; i++)
//        {
//            printf("%d ",b[i]);
//        }
//        printf("\n");
//        for (int i = 0; i < 10; i++)
//        {
//            printf("%d ",c[i]);
//        }
//        printf("\n");
        
        
/*-----------------------------------*/
        /*
         查找字符串中的空格數
         */
//        char a[50];
//        int count = 0;
//        fgets(a, 50, stdin);
//        a[strlen(a)-1] = '\0';
//        
//        for (int i = 0; a[i] != '\0'; i++)
//        {
//            if (a[i] == ' ')
//            {
//                count++;
//            }
//        }
//        printf("空格數是:%d",count);

        
        
/*-----------------------------------*/
        /*
         使字符串倒轉
         */
        
        
//        char a[20],tmp;
//        fgets(a, 20, stdin);
//        
//        //fgets會讀入\n,一般不需要的話執行下面的語句
//        a[strlen(a)-1] = '\0';
////        sscanf(a, "%s",a);
//        unsigned long count = strlen(a);
//        for (int i = 0; i < count/2; i++)
//        {
//            tmp = a[i];
//            a[i] = a[count-i-1];
//            a[count-i-1] = tmp;
//        }
//        puts(a);
        
//        char a[20],tmp;
//        scanf("%s",a);
//        unsigned long count = strlen(a);
//        for (int i = 0; i < count/2; i++)
//        {
//            tmp = a[i];
//            a[i] = a[count-i-1];
//            a[count-i-1] = tmp;
//        }
//        puts(a);
        
//        int count = 10;
//        int a[count],tmp;
//        for (int i = 0; i < count; i++)
//        {
//            a[i] = arc4random()%100;
//        }
//
//        /*
//         冒泡排序(升序)
//         最簡單的排序方式但效率較低
//         此算法取名:水泡一點一點的浮出水面
//         越大的元素經由交換慢慢浮到數列頂端故名
//         
//         冒泡排序外層控制趟數,內層控制每趟比較次數
//         相鄰的兩個數做比較,內層第一次循環結束將有一個最大數放到數組末尾
//         內層第二次循環結束將有第二大數放到數組倒數第二個
//         直到所有數據排好
//         */
//        for (int i = 0; i < count-1; i++)
//        {
//            for (int j = 0; j < count-1-i; j++)
//            {
//                if (a[j] > a[j+1])
//                {
//                    tmp = a[j];
//                    a[j] = a[j+1];
//                    a[j+1] = tmp;
//                }
//            }
//        }
        
        
//        /*
//         選擇排序(升序)
//         選擇排序外層控制從0到倒數第2個
//         內層循環從數組右邊所有數中找出最小值所在的index
//         內存循環結束將最小值跟i對應的元素交換
//         依次執行
//         */
//        int minIndex;
//        for (int i = 0; i < count-1; i++)
//        {
//            minIndex = i;
//            for (int j = i+1; j < count; j++)
//            {
//                if (a[minIndex] > a[j])
//                {
//                    minIndex = j;
//                }
//            }
//            tmp = a[i];
//            a[i] = a[minIndex];
//            a[minIndex] = tmp;
//        }
        
//        /*
//         插入排序(升序)
//         第一次排序時將第2個元素放入tmp,然後跟前面的第一個元素比較,如果第一個元素大於第二個元素,則第二個元素賦值爲第一個元素,然後將第一個元素賦值爲tmp
//         此時前兩個爲有序表
//         
//         第二次排序時將第3個元素放入tmp,然後跟前面的第二個元素比較,如果第二個元素大於第三個元素,則第三個元素賦值爲第二個元素,再判斷第一個元素大於第二個元素時,將第二個元素賦值爲第一個元素,然後將第一個元素賦值爲tmp
        
        //81
        //6 7 32 61 81 87 12 84 10 97
//        for (int i = 1; i < count; i++)
//        {
//            int j;
//            tmp = a[i];
//            for (j = i-1; j >= 0; j--)
//            {
//                if (tmp < a[j])
//                {
//                    a[j+1] = a[j];
//                }
//                else
//                {
//                    break;
//                }
//            }
//            a[j+1] = tmp;
//        }
        
//        for (int i = 0; i < count; i++)
//        {
//            printf("%d ",a[i]);
//        }
        
        /*
         6、編寫一個程序,輸入兩個包含5個元素的數組,先將兩個數組升序排序,然後將這兩個數組合併成一個升序數組。
         
         定義一個函數返回指針,傳入三個數組,一個目標數組,兩個原數組,返回目標數組的指針
         */
//        int count = 5;
//        int a[count],b[count],c[count*2];
//        printf("請輸入第一個數組(逗號分隔):");
//        scanf("%d,%d,%d,%d,%d",&a[0],&a[1],&a[2],&a[3],&a[4]);
//        printf("請輸入第二個數組(逗號分隔):");
//        scanf("%d,%d,%d,%d,%d",&b[0],&b[1],&b[2],&b[3],&b[4]);
//        sortArray(a,count);
//        sortArray(b,count);
//        
//        for (int i = 0,ai = 0, bi = 0; i < count*2; i++)
//        {
//            if (ai == 5 && bi != 5)
//            {
//                c[i] = b[bi++];
//            }
//            else if (ai != 5 && bi == 5)
//            {
//                c[i] = a[ai++];
//            }
//            else
//            {
//                if (a[ai] > b[bi])
//                {
//                    c[i] = b[bi++];
//                }
//                else
//                {
//                    c[i] = a[ai++];
//                }
//            }
//        }
//        
//        printArray(a,count);
//        printArray(b,count);
//        printArray(c,count*2);
        
        /*
         7、給定某年某月某日,將其轉換成這一年的第幾天並輸出。
         */
        
//        int year,month,day;
//        scanf("%d,%d,%d",&year,&month,&day);
//        int allday = getDay(year, month, day);
//        printf("%d",allday);
        
        
        /*
         8、編寫整型值數組排序程序(冒泡排序--升序)
         int a[] = {3,-9,32,77,63,-24,14,0,21,45};
         */
//        int a[] = {3,-9,32,77,63,-24,14,0,21,45};
//        sortArray(a, 10);
//        printArray(a, 10);
        
        /*
         10、char result[50] = {0};
         char str1[] = “Shangcheng ";
         char str2[] = "class ";
         char str3[] = " is niu best!";
         把str1, str2, str3合併到result數組中。
         “Shangcheng class is niu best!”
         */
//        char aaa[5] = {"abcd"};
//        char result[50] = {""};//必須要有足夠的空間容納要拼接的字符串,如果空間不足會將字符串寫到aaa裏邊
//        char str1[] = "Shangcheng ";
//        char str2[] = "class ";
//        char str3[] = " is niu best!";
//        sprintf(result, "%s%s%s",str1,str2,str3);//格式化輸出到左邊的字符串
//        puts(result);
//        
//        //只能從一個字符串讀到另一個字符串,以某種格式,或者篩選
//        char result2[50] = {0};
//        sscanf(result, "%s",result2);//sscanf不能讀入空格
//        puts(result2);
        
//        gets(<#char *#>)//可以讀空格
//        fgets(<#char *restrict#>, <#int#>, <#FILE *#>)//可以讀空格
//        scanf(<#const char *restrict, ...#>)//空格作爲結束和不同變量間分隔,不能讀
        
//        strcat(result, str1);
//        strcat(result, str2);
//        strcat(result, str3);
//        puts(result);
//        puts(aaa);
        
        
        /*
         11、找出下面程序的錯誤
         */
        //string 地址是0x111 str1的地址是多少0x107
        //string在前面定義,存高地址,str1往低地址走10個字節開始存
//        char string[10], str1[10];
//        int i;
//        for(i = 0; i < 10; i++)
//        {
//            str1[i] = 'a';//puts %s讀到'\0',str1讀到最後沒有找到'\0'
//        }
//        strcpy(string, str1);
//        puts(str1);//20
//        puts(string);//10
        
        
        /*
         12、下面這個程序執行後會有什麼錯誤或者效果:
         */
//        unsigned char str[10], i;//i取值範圍0-255
//        for(i = 0; i < 256; i++)//最大值自增後變成最低值,死循環
//        {
//            if (i==255)
//            {
//                
//            }
//            str[i] = i;}
        
        /*
         5、模擬n個人參加選舉的過程,並輸出選舉結果:假設候選人有四人,分別用A、B、C、D表示,當選某候選人時,直接輸入其編號(編號由計算機隨機產生),
         若輸入的不是A、B、C、D則視爲無效票,選舉結束後按得票數從高到低輸出候選人編號和所得票數。
         */
        
//        struct Candidate c[4] = {'A',0,'B',0,'C',0,'D',0};
//        int n = 100,randNum;
//        for (int i = 0; i < 100; i++)
//        {
//            randNum = arc4random()%5;
//            switch (randNum)
//            {
//                case candidateNum_A:
//                {
//                    c[0].poll++;
//                }
//                    break;
//                case candidateNum_B:
//                {
//                    c[1].poll++;
//                }
//                    break;
//                case candidateNum_C:
//                {
//                    c[2].poll++;
//                }
//                    break;
//                case candidateNum_D:
//                {
//                    c[3].poll++;
//                }
//                    break;
//                    
//                default:
//                    break;
//            }
//        }
//        sortCandidateByPoll(c, 4);
//        printCandidate(c, 4);
        
        
        /*
         13、思考題:編程在一個已知的字符串中找最長單詞,假定字符串中只含字母和空格,空格用來分隔不同單詞。
         比如:"ni hap world",最長單詞是world
         */
//        char *words = "ni hap world";
//        int longLocation = 0,longLength = 0,tmpLocation = 0,tmpLength = 0;
//        for (int i = 0; ; i++)
//        {
//            if (words[i] == '\0' || words[i] ==  ' ')
//            {
//                if (longLength < tmpLength)
//                {
//                    longLength = tmpLength;
//                    longLocation = tmpLocation;
//                }
//                tmpLocation = i+1;
//                tmpLength = 0;
//                if (words[i] == '\0')
//                {
//                    break;
//                }
//            }
//            else
//            {
//                tmpLength++;
//            }
//        }
//        printf("%d %d\n",longLocation,longLength);
//        char longWord[20];
//        strncpy(longWord, &words[longLocation], longLength);
//        puts(longWord);
        


//查找最長單詞簡易實現方法
void find1(){
    char a[]="nibbbbb hap worlbb",b[50],max[50];
    int i;
    int j=0;
    int temp=0;
    a[(int)strlen(a)]=' ';
    for (i=0; i<=strlen(a); i++){
        if (a[i]==32){
            if(strlen(b)>strlen(max)){
                strcpy(max, b);
                for (int c=0;c<10; c++) {
                    b[c]='\0';
                }
                temp=i+1;
                j=0;
            }
            else {*b=0;j=0;temp=i+1;}
            
        }
        else{
            for (; j<=i-temp; j++) {
                b[j]=a[i];
            }
            
        }
    }
    puts(max);
}


        
        /*
         14、思考題:	約瑟夫環的數組實現
         
         約瑟夫(Josephus)問題是由古羅馬的史學家約瑟夫提出的,他參加並記錄了公元66-70年猶太人反抗羅馬的起義。約瑟夫作爲一個將軍,設法守住了裘達伯特城達47天之久,在城市淪陷之後,他和40名將士在附近的一個洞穴中避難。在哪裏,將士們羣情激奮並表示:要投降毋寧死。於是,約瑟夫建議每個人輪流殺死他旁邊的人,而這個順序是由抽籤決定的。約瑟夫有預謀地抓到了最後一簽並且做爲洞穴中兩個倖存者之一生存下來。
         約瑟夫環問題的具體描述是:設有編號爲1,2,......,n的n(n>0)個人圍成一個圈,從第一個人開始報數,報到m時停止報數,報m的人出圈,再從他的下一個人起重新報數,報到m時停止報數,報m的出圈,......,如此下去,知道只剩下一人爲止。當任意給定 n和 m後,設計算法求 n個人出圈的次序。
         */
        
//        printf("請輸入n,m,逗號分隔:");
//        
//        //currentCursor當前報數報到幾,currentCount表示有人出圈後當前圈裏還有幾個人,currentIndex當前數組的下標
//        int n,m,currentCursor = 0,currentCount,currentIndex = 0;
//        scanf("%d,%d",&n,&m);
//        currentCount = n;
//        
//        struct Person p[n];
//        
//        initPerson(p, n);
//        
//        for (int i = 0; i < n; i++)//控制出列次數
//        {
//            while (currentCount > 0)
//            {
//                currentCursor++;
//                currentIndex++;
//                if (currentIndex == currentCount-1)
//                {
//                    currentIndex = 0;
//                }
//                if (currentCursor == m)
//                {
//                    personGoOut(p,currentIndex,n);
//                    currentCount--;
//                    currentCursor = 0;
//                }
//            }
//        }
//        printPerson(p, n);
        
    }
    return 0;
}


//約瑟夫環的簡易實現
//void Joseph (int n,int m){
//    int sum, a[n], i, del = 0;
//    for (i = 0; i <= n-1; i++)
//        a[i] = i + 1;
//    for (sum = n; sum != 1; sum--) {
//        del = (del + m-1) % sum;
//        printf("第%d個出局%d\n",n-i+1, a[del]);
//        for (i = del; i <= sum - 2; i++)
//            a[i] = a[i+1];
//    }
//    printf("留下的是%d\n\n", a[0]);
//}



















發佈了38 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章