103

#include "allinclude.h" //DO NOT edit this line
int main()
{
int a, b;
//第1种交换a和b的值的方法
printf("第1种交换a和b的值的方法:\n", a, b);
a = 8;
b = 10;
printf("交换前:a = %d, b = %d\n", a, b);
int c = b;
b = a;
a = c;
printf("交换后:a = %d, b = %d\n\n", a, b);
//第2种交换a和b的值的方法
printf("第2种交换a和b的值的方法:\n", a, b);
a = 8;
b = 10;
printf("交换前:a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("交换后:a = %d, b = %d\n\n", a, b);
//第3种交换a和b的值的方法
printf("第3种交换a和b的值的方法:\n", a, b);
a = 8;
b = 10;
printf("交换前:a = %d, b = %d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后:a = %d, b = %d\n", a, b);
//试一试:在第3种交换方法中,令a和b的值相等,例如都为8,会出现什么现象?
return 0;
}
106

#include "allinclude.h" //DO NOT edit this line
void Descend(int &a, int &b, int &c) // 通过交换,令 a >= b >= c
{ // Add your code here
int *p=&a,*q=&b,*t=&c;
if(*q<*t)
{
*q=*t+*q;
*t=*q-*t;
*q=*q-*t;
}
if(*p<*q)
{
*p=*p+*q;
*q=*p-*q;
*p=*p-*q;
}
if(*q<*t)
{
*q=*t+*q;
*t=*q-*t;
*q=*q-*t;
}
}
108

#include "allinclude.h" //DO NOT edit this line
float Polynomial(int n, int a[], float x0)
{ // Add your code here
int i;
double p=a[0];
for(i=1;i<=n;i++)
{
p+=(a[i]*pow(x0,i));
}
return p;
}
111

#include "allinclude.h" //DO NOT edit this line
Status Fibonacci(int k, int m, int &f)
{
// Add your code here
if(m<0||k<2)
{
return ERROR;
}
if(m<k-1)//k-1项前面的都是0
{
f=0;
return OK;
}
if(m==k-1||m==k)//第k-1项和k都是1
{
f=1;
return OK;
}
else//第k项后面的项
{
int arr[m+1]={0},i,j,q;
i=k;
arr[k-1]=1;//第k-1项是1
for(;i<=m;i++)//i从k项开始
{
for(q=0,j=i-k;j<=i;j++)
{
q+=arr[j];//求k到m项的数
}
arr[i]=q;
}
f=arr[m];
return OK;
}
}
118

#include "allinclude.h" //DO NOT edit this line
Status Series(int a[], int n)
{
// Add your code here
if(n<=0)//判断长度是否小于等于0
{
return ERROR;//返回错误
}
int i,j,k,x=1,y=0;
for(i=0,j=1;i<=n-1,j<=n;i++,j++)
{
x=1;
y=0;
for(k=1;k<=j;k++)
{
x*=k;//累乘器
}
y=x*pow(2,k);//计算式子的值
if(y>MAXINT)
{
return EOVERFLOW;//超过返回出错
}
else {
a[i]=y;//存储不超过的值
}
}
return OK;//均不超过,返回OK
}
120
#include "allinclude.h"
void printName(stuType student[], int index[], int n)
{ // Add your code here
int j;
for(int i=0;i<n;i++)
{
j=index[i];
printf("%s\n",student[j].name);
}
}
121

#include "allinclude.h"
float highestScore(stuType *student[], int n)
/* 返回最高成绩 */
{ // Add your code here
float a[n];
for(int i=0;i<n;i++)
{
a[i]=student[i]->score;
}
float max=a[0];
for(int i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
return max;
}
122

#include "allinclude.h"
void printFirstName_HighestScore(stuType *student[], int n)
{ // Add your code here
float max=student[0]->score;
int j=0;
char max_name[4];
for(int i=0;i<n;i++)
{
if(max<student[i]->score)
{
max=student[i]->score;
j=i;
}
}
strcpy(max_name,student[j]->name);
for(int i=0;i<4;i++)
printf("%c",max_name[i]);
printf("\n%.2f\n",max);
}
123

#include "allinclude.h"
void printLastName_HighestScore(stuType *student[], int n)
{ // Add your code here
float max=student[0]->score;
int j=0;
char max_name[4];
for(int i=0;i<n;i++)
{
if(max<=student[i]->score)
{
max=student[i]->score;
j=i;
}
}
strcpy(max_name,student[j]->name);
for(int i=0;i<4;i++)
printf("%c",max_name[i]);
printf("\n%.2f\n",max);
}
125

#include "allinclude.h" //DO NOT edit this line
void A() {
printf("X\n");
}
void B() {
printf("Y\n");
}
int main()
{
void (*funcp)(); //定义函数指针
funcp = A;
(*funcp)(); // 实际上调用了A( );
funcp = B;
(*funcp)(); // 实际上调用了B( );
return 0;
}
126

#include "allinclude.h" //DO NOT edit this line
void hello()
{
printf("Hello world!\n");
}
void runFun(void (*pFun)())
{
pFun(); //函数指针;
}
int main()
{
runFun(hello); //hello是实际要调用的函数
return 0;
}
130

#include "allinclude.h"
StrSequence* reverseStr(StrSequence* strSeq)
/*返回一个结构体,该结构体将strSeq中的字符串逆序存放*/
{ // Add your code here
int n=strSeq->length;
char a;
for(int i=0;i<n/2;i++)
{
a=strSeq->elem[i];
strSeq->elem[i]=strSeq->elem[n-i-1];
strSeq->elem[n-i-1]=a;
}
return strSeq;
}
149

#include "allinclude.h" //DO NOT edit this line
Status CreateSequence(Sequence &S, int n, ElemType *a)
{
// Add your code here
if(n<=0)
{
return ERROR;
}
S.elem=(ElemType*)malloc(n*sizeof(ElemType));
for(int i=0;i<n;i++)
{
S.elem[i]=a[i];
}
S.length=n;
return OK;
}
161

#include "allinclude.h" //DO NOT edit this line
LinkList MakeNode(ElemType x) {
// Add your code here
LNode *p;
p=(LNode*)malloc(sizeof(LNode));
if(p==NULL)
{
return NULL;
}
else
{
p->data=x;
}
}
163

#include "allinclude.h" //DO NOT edit this line
LinkList CreateLinkList(ElemType x, ElemType y) {
// Add your code here
LNode*p;
p=(LNode*)malloc(3*sizeof(LNode));
if(p!=NULL)
{
p->data=x;
p->next=(LNode*)malloc(3*sizeof(LNode));
if(p->next==NULL)
{
return NULL;
}
else {
p->next->data=y;
p->next->next=NULL;
return p;
}
}
else
{
return NULL; // This is a temporary code. Change it if necessary.
}
}
165

#include "allinclude.h" //DO NOT edit this line
LinkList CreateOrdLList(ElemType x, ElemType y) {
// Add your code here
LNode*p;
p=(LNode*)malloc(3*sizeof(LNode));
if(p!=NULL)
{
p->data=(x>y)?y:x;
p->next=(LNode*)malloc(3*sizeof(LNode));
if(p->next==NULL)
{
return NULL;
}
else {
p->next->data=(x>y)?x:y;
p->next->next=NULL;
return p;
}
}
else
{
return NULL; // This is a temporary code. Change it if necessary.
}
}