/*===========================================================================*/
/*程序名称:2004_1.c */
/*程序目的:计算一个“零幺串”当中最长的零/幺串 */
/*Writen by Apechn ,Soft Lab of JLU */
/*===========================================================================*/#include
考研试题
/*====================================================================================*/
/*程序名称:2004_2.c */
/*程序目的:两个多项式相加 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include
#include
struct list /* 节点结构声明 */
{
int power; /* 多项式中变量的幂 */
int coeff; /* 多项式中变量的系数 */
struct list *next;
};
typedef struct list node;
typedef node *poly;
/*-------------------------------------*/
/* 打印出一个多项式的各项 */
/*-------------------------------------*/
void printPoly(poly head)
{
poly pointer; /* 临时指针变量 */
pointer = head;
while (pointer != NULL) /* 打印各节点 */
{
printf("[%d,%d] ",pointer->coeff,pointer->power);
pointer = pointer->next;
}
printf("/n");
}
/*------------------------------------------------------------------------------*/
/* 从键盘输入一个多项式的项数和各项的幂和系数,多项式的系数不能为零 */
/*------------------------------------------------------------------------------*/
poly createPoly(poly head) /* 从键盘输入一个多项式的项数和各项的幂和系数 */
{
poly newNode; /* 临时节点 */
poly pointer;
int n,i;
int coeffTemp,powerTemp; /* 临时变量 */
head = (poly) malloc(sizeof(node)); /* 内存配置 */
if (head == NULL)
printf("Memory allocate Failure!!/n"); /* 内存配置失败 */
else
{
printf("请输入要创建的多项式的项数:/n");
scanf("%d",&n);
if (n == 0)
return NULL;
printf("请按x降幂依次输入各项的系数和幂:/n");
scanf("%d%d",&coeffTemp,&powerTemp);/* 输入多项式一项的系数和幂 */
head->coeff = coeffTemp; /* 定义首节点 */
head->power = powerTemp;
head->next = NULL;
pointer = head;
for (i = 1;i < n;i++) /* 依次定义其它节点 */
{
scanf("%d%d",&coeffTemp,&powerTemp); /* 输入各项的系数和幂 */
newNode = (poly) malloc(sizeof(node));
newNode->coeff = coeffTemp;
newNode->power = powerTemp;
newNode->next = NULL;
pointer->next = newNode; /* 将新定义的节点连接到原链表的表尾 */
pointer = newNode; /* pointer指针后移 */
}
}
printPoly(head);
return head;
}
/*-----------------------------------*/
/* 释放一个链表的空间 */
/*-----------------------------------*/
void freeList(poly head)
{
poly pointer; /* 临时指针变量 */
while (head != NULL)
{
pointer = head;
head = head->next;
free(pointer);
}
}
/*--------------------------------------------*/
/* 计算两个多项式的和,并将结果存入另一个链表 */
/*--------------------------------------------*/
poly addPoly(poly head1,poly head2)
{
poly pointer1,pointer2,pointer,newNode,head;/* 临时指针变量 */
pointer1 = head1;
pointer2 = head2;
head = (poly) malloc(sizeof(node));
pointer = head;
while ((pointer1 != NULL) || (pointer2 != NULL)) /* 若两个多项式至少有一个没有访问结束 */
{
if (pointer1 !=NULL && pointer2 != NULL &&(pointer1->power > pointer2->power))
{/* 若两个多项式当前指针指向的项的幂不相等 */
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer1->power;
newNode->coeff = pointer1->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;
pointer1 = pointer1->next;
}
if (pointer1 !=NULL && pointer2 != NULL &&(pointer1->power < pointer2->power))
{/* 若两个多项式当前指针指向的项的幂不相等 */
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer2->power;
newNode->coeff = pointer2->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;/* 指向存储结果的链表的指针后移 */
pointer2 = pointer2->next;
}
if (pointer1 !=NULL && pointer2 != NULL &&(pointer1->power == pointer2->power))
{ /* 若两个多项式当前指针指向的项的幂相等 */
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer1->power;
newNode->coeff = pointer1->coeff + pointer2->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;/* 指向存储结果的链表的指针后移 */
pointer1 = pointer1->next;
pointer2 = pointer2->next;
}
if (pointer1 == NULL && pointer2 != NULL)
{
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer2->power;
newNode->coeff = pointer2->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;/* 指向存储结果的链表的指针后移 */
pointer2 = pointer2->next;
}
if (pointer2 == NULL && pointer1 != NULL)
{
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer1->power;
newNode->coeff = pointer1->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;
pointer1 = pointer1->next;
}
}
pointer = head; /* 由于多用了一个节点,这3行代码将多出的节点释放 */
head = head->next;
free(pointer);
return (head);
}
/*------------------------------------------------------------------------------*/
/* 主程序 */
/*------------------------------------------------------------------------------*/
void main()
{
poly p,q,r; /* 定义三个代表多项式的链表 */
printf("这是多项式p:/n"); /* 创建多项式p */
p = createPoly(p);
printf("这是多项式q:/n"); /* 创建多项式q */
q = createPoly(q);
r = (poly) malloc(sizeof(node));
printf("这是多项式p与多项式q的和r:/n");
r = addPoly(p,q); /* 将多项式p和多项式q的和赋值给r */
printPoly(r);
freeList(p); /* 释放程序中使用过的链表所占用的空间 */
freeList(q);
freeList(r);
}
考研试题
/*=============================================================================*/
/*程序名称:2004_3.c */
/*程序目的:求sin(x)的近似值 */
/*Writen by Apechn ,Soft Lab of JLU */
/*=============================================================================*/
#include
#include
#define MIN 0.00005 /* 设置运算停止条件 */
/*------------------------------------------------------------------------------*/
/* 计算弧度数为angle的角的正弦值第i项 */
/*------------------------------------------------------------------------------*/
float spill(int i,float angle) /* 计算第i项 */
{
int symbol,j;
float temp = angle; /* 临时变量 */
if (i % 2) /* 设置该项的符号 */
symbol = 1;
else
symbol = -1;
if (i == 1) /* 若只需要计算一项,直接返回角度值 */
return (angle);
for (j = 2;j <= i;j++) /* 循环计算一项的值 */
{
temp *= angle * angle / ((2 * j - 2) * (2 * j - 1));
}
temp *= symbol;
return (temp);
}
/*------------------------------------------------------------------------------*/
/* 递归求解sin(angle)的近似值,其中angle表示用户输入的弧度值 */
/*------------------------------------------------------------------------------*/
float f(int i,float angle)
{
float temp; /* 临时变量 */
if (i == 1) /* 递归出口 */
return angle;
temp = spill(i,angle); /* 需要加上的项 */
return (f((i - 1),angle) + temp); /* 递归运算 */
}
/*------------------------------------------------------------------------------*/
/* 计算需要的级数的项数,其中angle表示用户输入的弧度值 */
/*------------------------------------------------------------------------------*/
int loopTime(float angle)
{
int i = 1;
float seed = angle; /* 临时变量 */
while ((seed >= MIN) || (seed <= (-1 * MIN))) /* 循环条件 */
{
i++;
seed *= angle * angle /((2 * i -1) * (2 * i - 2)) * (-1);
}
return i;
}
/*------------------------------------------------------------------------------*/
/* 主程序 */
/*------------------------------------------------------------------------------*/
void main()
{
float x; /* 用来保存用户输入的弧度数 */
int count = 1; /* 用来保存需要递归的次数 */
float sinOfAngle = 0; /* 用来保存最终结果 */
printf("请输入一个浮点数,它代表一个弧度值:");
scanf("%f",&x); /* 用户从键盘输入一个弧度数 */
count = loopTime(x);
sinOfAngle = f(count,x);
printf("sin %f 的近似值为 %f./n",x,sinOfAngle); /* 在屏幕上打印出最终结果 */
}
考研试题
/*=============================================================================*/
/*函数名称:2004_4.c */
/*函数目的:递归的求出一个给定结构树的高度、节点数和路径个数 */
/*Writen by Siyee ,Soft Lab of JLU */
/*=============================================================================*/
int depth = 0; /* 用来保存深度的变量 */
void H(tree *T, int *h, int *n, int *k)
{
++*n;
++depth;
*h = max(*h, depth); /* 如果h比depth小,则更新h */
if(T->kind=='two'){ /* 若节点有两个子节点 */
H(T->son1, h, n, k); /* 递归计算两个子节点 */
H(T->son2, h, n, k);
}
else if(T->kind=='one'){ /* 若节点有一个子节点 */
H(T->son, h, n, k); /* 递归计算这一个子节点 */
}
else{
++*k; /* 如果是叶节点,则路径数k增一 */
}
--depth; /* 回溯,深度减一 */
}
考研试题
/*=============================================================================*/
/*程序名称:2004_5.c */
/*程序目的:删除整数单链表中重复的节点 */
/*Writen by Apechn ,Soft Lab of JLU */
/*=============================================================================*/
#include
#include
#define N 6 /* 节点个数 */
struct list /* 节点结构声明 */
{
int number;
struct list *next;
};
typedef struct list node;
typedef node *link;
int data[N] = {0,3,0,3,1,3}; /* 输入数据 */
/*------------------------------------------------------------------------------*/
/* 以已有的数组为基础建立单链表 */
/*------------------------------------------------------------------------------*/
link createList(link head)
{
link newNode;
link pointer; /* 函数中需要用到的临时变量 */
int i;
head = (link) malloc(sizeof(node));
if (head == NULL) /* 若分配内存失败 */
printf("Memory allocate Failure!!/n");
else
{
head->number = data[0]; /* 设置首节点 */
head->next = NULL;
pointer = head;
for (i = 1;i < N;i++) /* 依次设置其它节点 */
{
newNode = (link) malloc(sizeof(node));
newNode->number = data[i];
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;
}
}
return head;
}
/*------------------------------------------------------------------------------*/
/* 递归地删除链表中的重复节点 */
/*------------------------------------------------------------------------------*/
link delSame(link head)
{
link pointer,temp = head; /* 函数中需要用到的临时变量 */
if (head->next == NULL) /* 若head为空则直接返回 */
return head;
head->next = delSame(head->next); /* 递归地删除重复节点 */
pointer = head->next;
while (pointer != NULL)
{
if (head->number == pointer->number) /* 删除重复节点 */
{
temp->next = pointer->next;
free(pointer); /* 释放被删除节点所占用的空间 */
pointer = temp->next;
}
else
{
pointer = pointer->next; /* 如果没有重复节点则指针后移 */
temp = temp->next;
}
}
return head;
}
/*------------------------------------------------------------------------------*/
/* 释放链表所占用的空间 */
/*------------------------------------------------------------------------------*/
void freeList(link head)
{
link pointer; /* 临时变量 */
while (head != NULL)
{
pointer = head;
head = head->next;
free(pointer); /* 一次释放一个节点所占的空间 */
}
}
/*------------------------------------------------------------------------------*/
/* 打印链表各项 */
/*------------------------------------------------------------------------------*/
void printList(link head)
{
link pointer; /* 临时变量 */
pointer = head;
while (pointer != NULL)
{
printf("%d ",pointer->number);
pointer = pointer->next;
}
printf("/n");
}
/*------------------------------------------------------------------------------*/
/* 主程序 */
/*------------------------------------------------------------------------------*/
void main()
{
link head;
link pointer;
head = createList(head); /* 以已有的数组为基础建立一个链表 */
if (head != NULL) /* 打印未删除重复节点前的数组 */
{
printf("初始链表中各元素依次如下所示:/n");
printList(head);
}
pointer = delSame(head); /* 递归地删除链表中的重复节点 */
printf("删除重复节点后的链表中各元素依次如下所示:/n");
printList(pointer); /* 打印删除重复节点后的数组 */
freeList(head);
}