文章目录
- 1.编写函数`void count(char a[], char w[][100], int n, int b[])`
 - 2.编写函数`int stat(int a[], int n, int c[][2])`
 - 3.编写函数`fun(int *a, int n, int *odd, int *even)`
 - 4.建立一个带有头结点的单向链表,并将存储在数组中的数据依次转储到链表的各个结点中
 - 5.编写函数`Student *create(Student studs[], int n)`
 
 
1.编写函数void count(char a[], char w[][100], int n, int b[])
 
函数功能:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数,将统计结果依次保存在b指向的数组中。
#include <stdio.h>
#define MAX_SIZE 5
void count(char a[], char w[][100], int n, int b[]);
int pattern_matching(char *main_str, char*pattern_str);
int main() {
    char w[MAX_SIZE][100] = {"Sakura", "Uni", "Pilot", "Schneider", "Zebra"};
    char a[100 * MAX_SIZE] = {"Sakura, Uni, Pilot, Schneider, Zebra"};
    int frequency[MAX_SIZE] = {0};
    count(a, w, MAX_SIZE, frequency);
    for (int i = 0; i < MAX_SIZE; i++)
        printf("%d ", frequency[i]);
    return 0;
}
/*function definition*/
void count(char a[], char w[][100], int n, int b[]) {
    for (int i = 0; i < n; i++) {
        char *cursor_a = a;
        char *cursor_w = w[i];
        b[i] = pattern_matching(cursor_a, cursor_w);
    }
}
/*match main string with pattern string, return the number of match times*/
int pattern_matching(char *m_str, char *p_str) {
    int count = 0;
    int i, j, tmp;
    for (i = 0; *(m_str+i) != 0; i++) {
        tmp = i;
        j = 0;
        while (*(p_str+j) != 0) {
            if (*(m_str+i) == *(p_str+j)) {
                i++;
                j++;
            }
            else {
                break;
            }
        }
        *(p_str+j) == 0 ? (count++) : (i = tmp);
    }
    return count;
}
 
2.编写函数int stat(int a[], int n, int c[][2])
 
函数功能:a指向的数组中保存了n个1位整数(n为偶数),stat()函数从前至后依次将a数组中每两个相邻元素拼成一个不超过2位的整数,从而生成有n/2个元素的整数数列,统计该数列中不同整数出现的次数,并将统计结果保存到c指向的二维数组中,函数返回不同整数的个数。
#include <stdio.h>
#define MAX_SIZE 12
int stat(int a[], int n, int c[][2]);
void arr_out(int c[][2]);
int main() {
    int a[MAX_SIZE] = {1, 2, 3, 4, 1, 2, 5, 6, 1, 2, 3, 4};
    int c[MAX_SIZE][2] = {0};
    printf("total %d integers:\n", stat(a, MAX_SIZE, c));
    arr_out(c);
    return 0;
}
int stat(int a[], int n, int c[][2]) {
    int i, j, tag_same;
    int count = 0;//the number of integer
    for (i = 0; i < n; i += 2) {
        tag_same = 0;
        for (j = 0; j < count; j++) {
            if (c[j][0] == a[i] * 10 + a[i+1]) {
                c[j][1] += 1;
                tag_same = 1;//this integer has occurred
            }
        }
        if (tag_same == 0) {
            c[count][0] = a[i] * 10 + a[i+1];
            c[count][1] = 1;
            count++;
        }
    }
    return count;
}
void arr_out(int c[][2]) {
    int i = 0;
    while (c[i][0] != 0) {
        printf("num:%d, times:%d\n", c[i][0], c[i][1]);
        i++;
    }
}
 
3.编写函数fun(int *a, int n, int *odd, int *even)
 
函数功能:求出数组a[]中所有奇数之和以及所有偶数之和,并利用指针odd返回奇数之和,利用指针even返回偶数之和。
#include <stdio.h>
#define MAX_SIZE 6
void fun(int *a, int n, int *odd, int *even) {
    for (int i = 0; i < n; i++)
        a[i] % 2 == 0 ? ((*even) += a[i]) : ((*odd) += a[i]);
}
int main() {
    int a[MAX_SIZE] = {1, 9, 2, 3, 11, 6};
    int even = 0, odd = 0;
    fun(a, MAX_SIZE, &odd, &even);
    printf("the value of odd:%d, the value of even:%d", odd, even);
    return 0;
}
 
4.建立一个带有头结点的单向链表,并将存储在数组中的数据依次转储到链表的各个结点中
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
typedef struct LNode{
    int data;
    struct LNode *next;
}LNode, *LinkList;
void list_creat(int n, LinkList L);//establishing single linked list by tail insert
void list_assignment(int *a, int n, LinkList L);
void list_out_free(LinkList L);
int main() {
    int a[MAX_SIZE] = {12, 34, 56, 78, 90};
    LinkList L = (LinkList)malloc(sizeof(LNode));
    L->data = 0;//the data of head node is used to record the number of nodes
    list_creat(MAX_SIZE, L);
    list_assignment(a, L->data, L);
    list_out_free(L);
    return 0;
}
void list_creat(int n, LinkList L) {
    LNode *head = L, *tail = L, *p;
    while (n) {
        p = (LNode*)malloc(sizeof(LNode));
        tail->next = p;
        tail = p;
        head->data++;
        n--;
    }
    tail->next = NULL;
}
void list_assignment(int *a, int n, LinkList L) {
    LNode *p = L->next;
    while (n) {
        p->data = a[L->data-n];
        p = p->next;
        n--;
    }
}
void list_out_free(LinkList L) {
    LNode *p = L->next, *tmp;
    printf("total %d numbers:", L->data);
    while (p != NULL) {
        printf("%d ", p->data);
        tmp = p;
        p = p->next;
        free(tmp);
    }
    free(L);
}
 
5.编写函数Student *create(Student studs[], int n)
 
函数功能:Student是一个结构类型,包含姓名、成绩和指针域,studs数组中存储了n个Student记录,create函数的功能是根据studs数组建立一个链表,链表中结点按成绩降序排列,函数返回链表头指针。
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
typedef struct Student {
    char name[10];
    int grade;
    struct Student *next;
}Student, *ListStudent;
void str_cpy(char *dest, char *src);
Student *create(Student studs[], int n);
void out(ListStudent L);
int main() {
    Student studs[MAX_SIZE] = {{"Mike", 100}, {"Bob", 99}, {"Tom", 98}};
    ListStudent L = create(studs, 3);
    out(L);
    return 0;
}
void str_cpy(char *dest, char *src) {
    int i = 0;
    while (src[i] != 0) {
        dest[i] = src[i];
        i++;
    }
}
Student *create(Student studs[], int n) {
    ListStudent head = (ListStudent)malloc(sizeof(Student));
    Student *tail = head, *p = head;
    for (int i = 0; i < n; i++) {
        p = (Student*)malloc(sizeof(Student));
        str_cpy(p->name, studs[i].name);
        p->grade = studs[i].grade;
        tail -> next = p;
        tail = p;
    }
    tail->next = NULL;
    return head;
}
void out(ListStudent L) {
    Student *p = L->next;
    while (p != NULL) {
        printf("%s, %d\n", p->name, p->grade);
        p = p->next;
    }
}










