广工 AnyviewC C语言习题 第十、十一章

Anyview 第十、十一章


第十章

10.011 请编写一个函数func(char s[], char t[], int n),

由数组s中长度为n的字符序列构造其逆序列,并存储在数组t中。
例如,由给定字符序列s=“are”求得逆序列t=“era”;由s=“time”
求得t=“emit”。

void func(char s[], char t[], int n) 
/* 数组s的前n个元素存放给定的字符序列,
   数组t的前n个元素存放s的逆序列。
   注意:数组的下标从0开始。
*/
{
    for (int i=0;i<n;++i)
    {
        int j=n;
        t[j-1-i]=s[i];
    }
}

10.015 对长度为n的字符串s1,除首、尾字符外,将其余字符

按ASCII码降序排列,组合成一个新的字符串s2。

要求实现函数:

void func(char *s1, char *s2, int n); 
/* s1为字符串的起始地址,
   s2为新字符串的起始地址,
   n为字符串的长度。
   要求:s1串不能发生改变,
         s2串存放新的字符串。
*/
{
    int  i,j;
    strcpy(s2,s1);
    for (i=1;i<n-1;i++)
    {
        for (j=2;j<n-i;j++)
        {
            if (s2[j]>s2[j-1])
            {
                char t=s2[j];
                s2[j]=s2[j-1];
                s2[j-1]=t;
            }
        }
    } 
}

10.016 对字符串s1,除首、尾字符外,将其余字符

按ASCII码降序排列,组合成一个新的字符串s2。

要求实现函数:

void func(char *s1, char *s2); 
/* s1为字符串的起始地址,
   s2为新字符串的起始地址,
   注意:字符串尾字符之后跟随着一个结束符‘\0’,
         即ASCII码为0的字符,结束符不属于字符串。
   要求:s1串不能发生改变,
         s2串存放新的字符串。
*/
{
    int  i,j;
    int n=strlen(s1);
    strcpy(s2,s1);
    for (i=1;i<n-1;i++)
    {
        for (j=2;j<n-i;j++)
        {
            if (s2[j]>s2[j-1])
            {
                char t=s2[j];
                s2[j]=s2[j-1];
                s2[j-1]=t;
            }
        }
}

/**********

10.018 以字符串s第m(>=0)个字符开始的所有字符,

按升序的次序构成字符串t。
**********/

void substr(char *s, int m, char *t)
/* s为字符串的起始地址,
   m>=0,
   t为新字符串的起始地址,
   注意:字符串尾字符之后跟随着一个结束符‘\0’,
         即ASCII码为0的字符,结束符不属于字符串。
   要求:s串不能发生改变,
         t串存放新的字符串。
*/
{
   char *p=s+m;
    while (*p!='\0')
    {
    	*t=*p;
    	p++;
	}
}

10.033 编写函数,计算年份year中第yearday天相应的月和日。例如,

调用函数month_day(2000,61,&m,&d)之后,m=3,d=1,即2000年的第
61天是3月1日。

要求实现函数:
int month_day(int year, int yearday, int pmonth, int pday)
/
year是年,
yearday是天数,
若year和yearday合理,
pmonth和*pday是计算得出的月和日,函数返回1;
否则,函数返回0。
*/

int month_day(int year, int yearday, int *pmonth, int *pday) 
/* year是年,
   yearday是天数,
   若year和yearday合理,
   则*pmonth和*pday是计算得出的月和日,函数返回1;
   否则,函数返回0。
*/
{
     int month[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 
    int i,z,y,flag=0;
    int *p1,*p2;
    
    if (year>0 && yearday>0)
    {
        if ((year%4==0 && year %100 !=0) || (year%400==0))
        {
            flag=1;
        }
        
        if (flag)
        {
            month[1]+=1;
            if (yearday>366)
                return 0;
        }
        else
        {
            if (yearday>365)
                return 0;
        }
        
        i=0,z=0;
        y=yearday;
        while (y>0)
        {
            y-=month[i];
            i++;
            z++;
        }
        y+=month[i-1];
        
        if (y==0)
        {
            z-=1;
            y=month[z-1];
        }
        *pmonth=z;
        *pday=y;
        return 1;
    }
    return 0;
}

10.044 请编写一个函数func,通过略去非数字字符,将字符串s转换为

一个整数(不得调用C语言提供的将字符串转换为整数的函数)。

要求实现函数:

long func(char *s) 
/* s是一个数字字符串的地址,
   函数返回值为由s含有的数字字符转换得到的数(包含正负数情况) 
*/
{
    long sum=0;
  	int e=0,i,j=1;
    char *t=s;
    while (*s!='\0')
    {
    	if (*s>='0' && *s<='9')
    	{
    		if (e>0)
				j=10;
			else
				j=1;	
    		sum=sum*j+*s-'0';
        	
        	e++;
    	}
    	s++;
    }
    
    if (*t=='-')
    sum=-sum;
    return sum;
}

10.105 请编写一个函数findmax(int []s, int n),返回数组s中n(>0)个整数

中的最大值。注意:要求在函数中采用指针(而不是下标)来处理数组元素。int findmax(int s[], int n)

int findmax(int s[], int n)    
/* 返回s中n(>0)个整数的最大值。
   注意:要求在函数中采用指针(而不是下标)来处理数组元素。
*/
{

}

第十一章

11.033 链表L存储了多个人的信息。写一函数,求这些人中年龄最大

(即出生日期最小)者的名字。
结构体类型定义如下:

    struct date{int year; int month; int day;}; //日期结构体类型
    struct studentNode    //链表结点的结构体类型
    {  char name[10];     //人名
       struct date birth; //出生日期
       struct studentNode *next
    };

*/

char *oldest(struct studentNode *L)
/* 若L是空表,则返回空指针null
   否则返回表中年龄最大者的名字
 */
{
    struct studentNode *p;
   struct studentNode *max;
   char *n;
   
   max=L;
   if (NULL==L) return NULL;
   p=L->next;
   while (p)
   {
        if (max->birth.year > p->birth.year)
            max=p;
        else if(max->birth.year == p->birth.year)
        {
            if (max->birth.month > p->birth.month)
                max= p;
            else if(max->birth.month == p->birth.month)
            {
               if (max->birth.day > p->birth.day)
                    max= p;
            }
        }                                                            
        p=p->next;
   }
   return max->name;
}

11.073 课程链表结点的结构体类型定义如下:

    struct courseNode   //课程链表结点的结构体类型
    {  int   cID;       //课程号,取值0~99
       char  name[10];  //课程名
       float credit;    //学分,取值0~5
       int   semester;  //学期,取值1~8
       struct courseNode *next;
    };
  结构体链表Lc存储了各学期多门课程的信息。写一函数,
  求学期s的总学分。
 */

float creditSum(struct courseNode *Lc, int s)
/* 若Lc是空表,则返回0;
   否则返回学期s的总学分
 */
{
    struct courseNode *p;
   float sum=0.0;
   if (NULL==Lc) return 0.0;
   p=Lc;
   while (p)
   {
        if (p->semester==s)
            sum+=p->credit;
        p=p->next;
   }
   return sum;
}

11.173 课程链表结点的结构体类型定义如下:

    struct courseNode   //课程链表结点的结构体类型
    {  int   cID;       //课程号,取值0~99
       char  name[10];  //课程名
       float credit;    //学分,取值0~5
       int   semester;  //学期,取值1~8
       struct courseNode *next;
    };
    ```
  结构体链表Lc存储了多门课程的信息。写一函数,将课程号
  为c的课程的学分修改为t。
 */
struct courseNode *creditChange(struct courseNode *Lc, int c, float t)
/* 若课程c不存在,则修改不成功,返回null;
   否则修改该课程的学分为t,返回指向该课程结点的指针。
 */
{
    struct courseNode *p;
    int flag=0;
    if (NULL==Lc) return NULL;
    if (c==Lc->cID)
    {
        p=Lc;
        p->credit=t;
        return p;
    }
    p=Lc->next;
    while (Lc->next)
    {
        if (Lc->next->cID==c)
        {
            p=Lc->next;
            p->credit=t;
            flag++;
        }
            
        Lc=Lc->next;
    }
    if (flag)
        return p;
    else
        return NULL;    
}

11.183 课程链表结点的结构体类型定义如下:

    struct courseNode   //课程链表结点的结构体类型
    {  int   cID;       //课程号,取值0~99
       char  name[10];  //课程名
       float credit;    //学分,取值0~5
       int   semester;  //学期,取值1~8
       struct courseNode *next;
    };

结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的课程结点删除。

要求实现下列函数:

struct courseNode *deleteCourse(struct courseNode **Lc, int c)
/* 若在链表Lc中课程c不存在,则删除不成功,返回null;
   否则从链表Lc中删除该课程结点,并返回指向该课程结点的指针。
 */
{
    struct courseNode*p;
    p=*Lc;
    while (p!=NULL)
    {
        if (p->next->cID==c)
        {
            struct courseNode*t=p->next;
           p->next=p->next->next;
           return t;
        }
        if (p->next==NULL)
        {
            if (p->cID==c)
            {               
               Lc[0]=NULL;
               return p;
            }
        }
        p=p->next;
    }
    return NULL;
}
9.25实现下列函数int Search(SSTable s, KeyType k); /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ 静态查找表的类型SSTable定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct { ElemType *elem; int length; } SSTable; int Search(SSTable a, KeyType k) /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ { int i; if(a.length==0) return ERROR; //先判断a是否为空 a.elem[a.length+1].key=k; for(i=1;a.elem[i].key>k;i++) if(i>=a.length||a.elem[i].key<k) //不能忽略i=a.length这种情况 return ERROR; return i; } /* { int i; a.elem[a.length+1].key=k; for(i=1;a.elem[i].key>k;i++) if(i>a.length||a.elem[i].key<k) return ERROR; return i; } */ 9.26② 试将折半查找算法改写成递归算法。 实现下列函数int BinSearch(SSTable s, int low, int high, KeyType k); /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ 静态查找表的类型SSTable定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct { ElemType *elem; int length; } SSTable; int BinSearch(SSTable s, int low, int high, KeyType k) /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ { int mid; if(low<=high) { mid=(low+high)/2; if(s.elem[mid].key==k) return mid; if(s.elem[mid].key<k) return BinSearch(s,mid+1,high,k); if(s.elem[mid].key>k) return BinSearch(s,low,high-1,k); } return 0; } /* { int mid; if(low<=high) { mid=(low+high)/2; if(s.elem[mid].key<k) return BinSearch(s,mid+1,high,k); else return BinSearch(s,low,high-1,k); } return 0; } */ 9.31④ 试写一个判别给定二叉树是否为二叉排序树的算法,设此二叉树以二叉链表作存储结构。且树中结点的关键字均不同。 实现下列函数: Status IsBSTree(BiTree t); /* 判别给定二叉树t是否为二叉排序树。*/ /* 若是,则返回TRUE,否则FALSE */ 二叉树的类型BiTree定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct BiTNode { ElemType data; BiTNode *lchild,*rchild; }BiTNode, *BiTree; KeyType predt=-32767; Status IsBSTree(BiTree t) /* 判别给定二叉树t是否为二叉排序树。*/ /* 若是,则返回TRUE,否则FALSE */ { if( t )//&& ! ( t->lchild || t->rchild ) )//空树和叶子不用判断 { if( t->lchild && ( t->data.key < t->lchild->data.key ) )//左孩子不空,左孩子的key比本身的大 return FALSE; else if( t->rchild && ( t->data.key > t->rchild->data.key ) )//右孩子不空,右孩子的key比本身的大 return FALSE; else if( !IsBSTree( t->lchild ) )//判断左子树 return FALSE; else if( !IsBSTree( t->rchild ) )//判断右子树 return FALSE; } return TRUE; } /* { if(!t) return OK; if(t&&!t->lchild&&!t->rchild) return OK; else { if(t->lchild->data.key<t->data.key) IsBSTree(t->lchild); if(t->lchild->data.key>=t->data.key) return ERROR; if(t->rchild->data.key>t->data.key) IsBSTree(t->rchild); else return ERROR; return OK; } } */ 9.33③ 编写递归算法,从大到小输出给定二叉排序树中所有关键字不小于x的数据元素。要求你的算法的时间复杂度为O(log2n+m),其中n为排序树中所含结点数,m为输出的关键字个数。 实现下列函数void OrderOut(BiTree t, KeyType x, void(*visit)(TElemType)); /* Output is to use visit(t->data); */ 二叉树的类型BiTree定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct BiTNode { ElemType data; BiTNode *lchild,*rchild; }BiTNode, *BiTree; void OrderOut(BiTree t, KeyType x, void(*visit)(TElemType)) /* Output is to use visit(t->data); */ { if(t->rchild) OrderOut(t->rchild,x,visit); if(t->data.key>=x) visit(t->data); if(t->lchild)OrderOut(t->lchild,x,visit); } /* { if(t->rchild) OrderOut(t->rchild,x); if(t->data<x) exit(); visit(x); if(t->lchild)OrderOut(t->lchild,x); } */ 9.44④ 已知某哈希表的装载因子小于1,哈希函数 H(key)为关键字(标识符)的第一个字母在字母表中的序号,处理冲突的方法为线性探测开放定址法。试编写一个按第一个字母的顺序输出哈希表中所有关键字的算法。 实现下列函数void PrintKeys(HashTable ht, void(*print)(StrKeyType)); /* 依题意用print输出关键字 */ 哈希表的类型HashTable定义如下: #define SUCCESS 1 #define UNSUCCESS 0 #define DUPLICATE -1 typedef char StrKeyType[4]; typedef struct { StrKeyType key; void *any; } HElemType; int hashsize[] = { 7,11,17,23,29,37,47 }; typedef struct { HElemType elem[MAXLEN]; int count; int sizeindex; } HashTable; int H(char *s)//求Hash函数 { if( s[0] ) return s[0]-'A'+1; //求关键字第一个字母的字母序号(小写) else return 0; } void PrintKeys(HashTable ht, void(*print)(StrKeyType)) /* 依题意用print输出关键字 */ { int i,j; for( i = 1; i <= 26; i++ ) { for( j = (i-1)%hashsize[ht.sizeindex]; ht.elem[j].key[0]; ) { if( H ( ht.elem[j].key ) == i ) print(ht.elem[j].key); j = (j+1)%hashsize[ht.sizeindex]; } } } /* void PrintKeys(HashTable ht, void(*print)(StrKeyType)) /* 依题意用print输出关键字 { int i,j; for(i=1;i<=26;i++) for(j=i;ht.elem[j].key;j=(j+1)%MAXLEN) if(H(ht.elem[j].key)==i) print(ht); } int H(char *s) { if(s) return s[0]-96; else return 0; } */ 9.45③ 假设哈希表长为m,哈希函数为H(x),用链地址法处理冲突。试编写输入一组关键字并建造哈希表的算法。 实现下列函数int BuildHashTab(ChainHashTab &H, int n, HKeyType es[]) /* 直接调用下列函数 */ /* 哈希函数: */ /* int Hash(ChainHashTab H, HKeyType k); */ /* 冲突处理函数: */ /* int Collision(ChainHashTab H, HLink &p); */ { int i = 0,l,flag; HLink p,node; while( es[i] ) { l = Hash( H, es[i] ); node = ( HLink )malloc( sizeof( HNode ) ); node->data = es[i]; node->next = NULL; i++; if( !H.elem[l] ) H.elem[l] = node; else { flag = 0; p = H.elem[l]; if( p->data == node->data ) flag = 1; while( Collision( H, p ) ) if( p->data == node->data ) { flag = 1; break; } if( !flag ) { p = H.elem[l]; node->next = p; H.elem[l] = node; } } } } "+userLink+""; $('miniAd').show(); } }, onFailure: function(){} }}); } showMiniAd();
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值