一、编写函数void count(char a[],char w[][10],int n,int b[])。功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字母字符看作单词分割符),拧将统计结果依次保存在b指向的数组中。
#include <stdio.h>
#include <string.h>
#define N 4 //宏定义N为4
void count( char a[], char w[N][10], int n, int b[N] )
{
int i = 0;
int j = 0;
int k = 0;
int count = 0;
int len = strlen(a);
int len_w[N] = {0}; //定义数组len_w,存放字符数组w中每个字符串的长度
for( i = 0; i < N; i++ )
{
len_w[i] = strlen(w[i]); //求出字符数组每个字符串的长度依次存入数组len_w中
}
for( k = 0; k < N; k++ )
{
count = 0; //让count清零
for( i = 0; i < len - len_w[k] + 1; i++ )
{
for( j = 0; j < len_w[k]; j++ ) //当j小于数组w中第k个字符串的长度时进行循环
{
if( w[k][j] != a[i + j] )
{
break; //当a中从i开始的字符不和w[k]中的字符相等时,说明不是相同字符串,退出本次循环
}
}
if( j == len_w[k] ) //当j等于w[k]字符串的长度时,代表a从i开始的字符和w[k]相同
{
count++; //count+1
}
}
b[k] = count; //将累计的count值传到数组b中
}
}
int main()
{
char a[] = "hello world hello date hi world date"; //定义字符串a
char w[N][10] = {"hello","world","hi","date"}; //定义字符串数组w
int b[N] = {0};
int i = 0;
count( a, w, N, b ); //调用count函数
for( i = 0; i < N; i++ )
{
printf("%s----%d\n",w[i],b[i]); //输出字符串数组w和其中字符串在a中出现的次数b
}
return 0;
}
二、编写函数int stat(int a[],int n,int c[][2])。a指向的数组中保存了由n个1位整数组成的数列(n为偶数)。函数从前至后依次将a数组中每两个相邻元素拼成一个不超过2位的整数,从而生成有n/2个元素组成的整数数列;统计该数列中不同整数各自出现的次数,并将统计结果保存到c指向的二维数组中。函数返回不同整数的个数。
#include <stdio.h>
#define N 10 //宏定义N为10
int stat(int a[N], int n,int c[N / 2][2])
{
int i = 0;
int j = 0;
int k = 0;
int count = N / 2;
for( ; i < n; j++, i += 2 )
{
c[j][0] = a[i] * 10 + a[i + 1]; //合并a[i]和a[i+1]
c[j][1] = 1; //合并后该数出现了一次,让c[j][1]等于1
}
for( i = 0; i < N / 2; i++ )
{
for( j = i + 1; j < count; j++ )
{
if( c[i][0] == c[j][0] )
{
c[i][1]++; //有一个相同的数就让c[i][1]加1
for( k = j; k < count; k++ )
{
c[k][0] = c[k + 1][0];
c[k][1] = c[k + 1][1]; //把跟前面相同的c[j]覆盖
}
count--; //有相同的数,count - 1
}
}
}
return count; //返回共有几个不同的整数
}
int main()
{
int a[N] = {5,8,4,9,2,7,1,3,6,0};
int c[N / 2][2] = {0};
int i = 0;
int num = 0;
num = stat( a, N, c ); //调用stat函数,并让num等于其返回值
for( i = 0; i < num; i++)
{
printf("%d appear %d times.\n",c[i][0],c[i][1]); //输出合并后的整数各出现了几次
}
return 0;
}
三、编写函数fun(int *a, int n, int *odd, int *even),功能是:求出数组a[]中所有奇数之和以及所有偶数之和。并利用指针odd返回奇数之和,利用指针even返回偶数之和。 例如:a[]的值依次为:1,9,2,3,11,6;则利用指针odd返回奇数之和24;利用指针even 返回偶数之和 8。
#include <stdio.h>
void fun( int *a, int n, int *odd, int *even )
{
int i = 0;
for( i = 0; i < n; i++ )
{
if( *(a + i) % 2 != 0 )
{
*odd += *(a + i); //当*(a + i)为奇数时,累加给odd
}
else
{
*even += *(a + i); //当*(a + i)为偶数时,累加给even
}
}
}
int main()
{
int a[] = {1,9,2,3,11,6};
int odd = 0;
int even = 0;
fun( a, 6, &odd, &even ); //调用fun函数
printf("奇数和为:%d\n",odd); //输出odd的值
printf("偶数和为:%d\n",even); //输出even的值
return 0;
}
四、程序功能:建立一个带有头结点的单向链表,并将存储在数组中的字符依次转储到链表的各个结点中。
#include <stdio.h>
#include <stdlib.h>
struct node //定义结构体,作为链表中的节点
{
char c;
struct node * next;
};
typedef struct node Node; //重定义类型名
typedef struct node * Link;
void creat_link(Link *head)
{
*head = NULL; //创造头节点
}
void judge_malloc(Link newnode) //判断动态分配空间是否完成
{
if( newnode == NULL ) //如果newnode为空,没有动态分配空间,输出error返回-1
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_newnode(Link *newnode) //创建新的节点
{
*newnode = (Link)malloc(sizeof(Node));
judge_malloc(*newnode); //调用judge_malloc函数,判断动态空间是否完成
}
void insert_node_tail(Link *head,Link newnode) //将新节点从尾节点插入链表
{
Link temp;
temp = *head;
if( temp == NULL ) //判断链表是否为空,如果是空链表,头节点和尾节点相同
{
newnode->next = *head;
*head = newnode;
}
else //如果不是尾节点,就遍历到尾节点的前一个节点,开始插入节点
{
while( temp->next != NULL )
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
}
}
void display_node(Link head)
{
Link temp;
temp = head;
while( temp != NULL )
{
printf("%c",temp->c);
temp = temp->next; //遍历链表并且输出每个节点的值
}
printf("\n");
}
int main()
{
Link head = NULL; //定义头指针
Link newnode = NULL; //定义新节点
char s[10] = {"qwertyuio"};
int i;
creat_link(&head); //调用creat_link函数,创建新链表
for( i = 0; i < 10; i++ )
{
creat_newnode(&newnode); //调用creat_newnode函数,创建新节点
newnode->c = s[i]; //将数组里面的值赋给新节点的char型变量
insert_node_tail(&head,newnode); //调用insert_node_tail函数,从尾节点插入
}
display_node(head); //调用display_node函数,输出整个链表每个节点的值
return 0;
}
五、编写程序STUDENT *Create(STUDENT studs[],int n)。STUDENT是一个结构类型,包含姓名、成绩和指针域。studs数组中存储了n个STUDENT记录。create函数的功能是根据studs数组建立一个链表,链表中结点按成绩降序排列,函数返回链表头指针。
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
int score;
struct student * next;
}; //创建学生结构体
typedef struct student STUDENT; //重定义类型名
STUDENT *Creat( STUDENT *studs,int n )
{
int i = 0;
int j = 0;
STUDENT t;
STUDENT *head;
for( i = 0; i < n - 1; i++ ) //冒泡排序,由大到小
{
for( j = 0; j < n - 1 - i; j++ )
{
if( studs[j].score < studs[j + 1].score )
{
t = studs[j];
studs[j] = studs[j + 1];
studs[j + 1] = t;
}
}
}
head = &studs[0];
for( i = 0; i < n - 1; i++ ) //链接成链表
{
studs[i].next = &studs[i + 1];
}
studs[i].next = NULL;
return head;
}
void Display( STUDENT *head ) //打印链表节点数据
{
STUDENT *p= head;
while( p != NULL )
{
printf("%s %d",p->name, p->score);
printf("\n");
p = p->next;
}
}
int main()
{
STUDENT studs[40];
int n = 0;
int i = 0;
STUDENT * head;
printf("number of students :\n");
scanf("%d",&n); //录入n的值
printf("please input students name and score:\n");
for( i = 0; i < n; i++ )
{
getchar();
scanf("%s,%d",studs[i].name,&studs[i].score); //录入学生的姓名和成绩
}
head = Creat( studs, n ); //调用Creat函数,对学生成绩由大到小排序,返回头节点给head
printf("the result is:\n");
Display( head ); //调用Display函数,输出链表节点数据
printf("\n");
return 0;
}