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)`
函数功能:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数,将统计结果依次保存在b指向的数组中。
#include2.编写函数int stat(int a[], int n, int c[][2])#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", "aba"}; char a[100 * MAX_SIZE] = {"Sakura, Uni, Pilot, Schneider, abababa"}; 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; } 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); } } int pattern_matching(char *m_str, char *p_str) { int count = 0; int i = 0, j, tmp; while (*(m_str+i) != 0) { tmp = i+1; j = 0; while (*(p_str+j) != 0) { if (*(m_str+i) == *(p_str+j)) { i++; j++; } else { break; } } if (*(p_str+j) == 0) count++; i = tmp; } return count; }
函数功能:a指向的数组中保存了n个1位整数(n为偶数),stat()函数从前至后依次将a数组中每两个相邻元素拼成一个不超过2位的整数,从而生成有n/2个元素的整数数列,统计该数列中不同整数出现的次数,并将统计结果保存到c指向的二维数组中,函数返回不同整数的个数。
#include3.编写函数fun(int *a, int n, int *odd, int *even)#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:%dn", c[i][0], c[i][1]); i++; } }
函数功能:求出数组a[]中所有奇数之和以及所有偶数之和,并利用指针odd返回奇数之和,利用指针even返回偶数之和。
#include4.建立一个带有头结点的单向链表,并将存储在数组中的数据依次转储到链表的各个结点中#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; }
#include5.编写函数Student *create(Student studs[], int n)#include #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); }
函数功能:Student是一个结构类型,包含姓名、成绩和指针域,studs数组中存储了n个Student记录,create函数的功能是根据studs数组建立一个链表,链表中结点按成绩降序排列,函数返回链表头指针。
#include#include #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, %dn", p->name, p->grade); p = p->next; } }



