Anyview数据结构第一章(按需自取)

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.
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sɪʟᴇɴᴛ໊ོ5329

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值