1. 程式人生 > >廣工 AnyviewC C語言習題 第九章

廣工 AnyviewC C語言習題 第九章

Anyview 第九章


/**********

【習題9.023】結構體型別定義如下:

struct date{int year; int month; int day;}; //定義日期結構體型別
struct student
{  char name[20]; 
   struct date birth; //出生日期
};

結構體陣列s儲存了n個人的名字和出生日期。寫一函式,求這n個人中年齡最大
(即出生日期最小)者的姓名。

char *oldest(student s[], int n)
{
    struct student *s0,*od;
    
    for
(od=s,s0=s+1;s0<s+n;s0++) { if (od->birth.year>(s0)->birth.year) { *od=*(s0); } else if (od->birth.year == (s0)->birth.year) { if (od->birth.month>(s0)->birth.month) *od=*(s0); else if
(od->birth.month == (s0)->birth.month) { if (od->birth.day>(s0)->birth.day) *od=*(s0); } } } return (od->name); }

【習題9.033】日期和連結串列結點的結構體型別定義如下:

    struct date{int year; int month; int day;}; //日期結構體型別
    struct
studentNode //連結串列結點的結構體型別 { char name[10]; //人名 struct date birth; //出生日期 struct studentNode *next; };

結構體連結串列L儲存了n個人的名字和出生日期。寫一函式,求這n個人中
年齡最大(即出生日期最小)者的名字。

char *oldest(struct studentNode *L)
/* 若L是空表,則返回空指標null
   否則返回表中年齡最大者的名字
 */
{
    studentNode *p,*od;
    p=L;od=L;
    while (p!=NULL)
    {
        if (od->birth.year>p->birth.year)
        {
            *od=*p;
        }
        else if (od->birth.year == p->birth.year)
        {
            if (od->birth.month>p->birth.month)
                *od=*p;
            else if (od->birth.month == p->birth.month)
            {
                if (od->birth.day>p->birth.day)
                    *od=*p;
            }
        }
        p=p->next;
    }
    return (od->name);
}
#include <stdio.h> 
#include <string.h>

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
   否則返回表中年齡最大者的名字
 */
{
	 studentNode *p,*od;
    p=L+1;od=L;
    while (p->next!=NULL)
    {
        if (od->birth.year>p->birth.year)
		{
			*od=*p;
		}
		else if (od->birth.year == p->birth.year)
		{
			if (od->birth.month>p->birth.month)
				*od=*p;
			else if (od->birth.month == p->birth.month)
			{
				if (od->birth.day>p->birth.day)
					*od=*p;
			}
		}
        p=p->next;
    }
    return (od->name);
}
 
 //#define n 10
 
int main()
{
	int i;
	char o[20];
	char *old=o;
	struct studentNode a[3];
	for (i=0;i<3;i++)
	{
		printf("請輸入同學%d的姓名\n",i+1);
		scanf("%s",&a[i].name);
		printf("請輸入同學%d的生日\n",i+1);
		scanf("%d%d%d",&a[i].birth.year,&a[i].birth.month,&a[i].birth.day); 
		a[i]
	}
	
	old=oldest(a,3);
	
	printf("%s\n",old);
	return 0;
}

/**********

【習題9.063】結構體型別定義如下:

struct course
{  int   cID;       //課程號,取值0~99
   char  name[10];  //課程名
   float credit;    //學分,取值0~5
   int   semester;  //學期,取值1~8
};

結構體陣列c儲存了n門課程的資訊。寫一函式,求學期s的總學分。
**********/

float creditSum(struct course c[], int n, int s)
{
    int i=0;
	float sum=0;
	
	 for (i=0;i<n;i++)
	 {
	 	if (c[i].semester==s)
		 	sum+=c[i].credit;	
	 }
	 return sum;
}

/**********

【習題9.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; //注意這裡如果是0可能會報錯(型別不匹配)
    }        
    else
    {
        p=Lc;
        while (p!=NULL)
        {
            if (p->semester==s)
            sum+=p->credit;
            p=p->next;
        }
    }
    return sum;
}

/**********

【習題9.133】日期和結構體型別定義如下:

struct date{int year; int month; int day;}; //日期結構體型別
struct student    //結構體型別
{  char name[10];     //人名
   struct date birth; //出生日期
};

結構體陣列s儲存了n個人的名字和出生日期。寫一函式,由陣列s中n個人
的資訊及其順序構造相應的連結串列。連結串列的結點的結構體型別定義如下:
struct studentNode //結構體型別
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next
};
**********/

struct studentNode *CreateLinkList(struct student s[], int n)
{
    struct studentNode* L,*p0;
    L = (studentNode*) malloc (sizeof(studentNode));
    L->next=NULL;
    
    struct student *x;
    if (NULL==s || n==0) return NULL;    
     x=s;       
        L->birth.year=x->birth.year;
        L->birth.month=x->birth.month;
        L->birth.day=x->birth.day;
        strcpy(L->name,x->name);
        x++;
        p0=L;
    for (;x<s+n;x++)
    {
        struct studentNode* p;
        p = (studentNode*) malloc (sizeof(studentNode));
        p->birth.year=x->birth.year;
        p->birth.month=x->birth.month;
        p->birth.day=x->birth.day;
        strcpy(p->name,x->name);
        p->next=p0->next;
        p0->next=p;
        p0=p0->next;        
    }   
    return L;
}

/**********

【習題9.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;
    p=Lc;
    while (p!=NULL)
    {
        if (p->cID==c)
        {
           p->credit=t;
           return p;
        }
        p=p->next;
    }
    return NULL;
}

/**********

【習題9.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.302】單向連結串列的結點型別定義如下:

struct node{
  char  ch;
  struct node *next;
};

編寫函式,對單向連結串列L實現就地逆置,即將所有結點
的指標反向,原鏈頭當作鏈尾,原鏈尾當作鏈頭,並返
回逆置後連結串列的頭指標。
**********/
struct node *inverse(struct node *L)
{
struct node *t,*p;
p=L->next;
L->next=NULL;

while (p!=NULL)
{

    t=p->next;
    p->next=L;
    L=p;
    p=t;
}
return L;

}

/**********

【習題9.352】單向連結串列的結點型別定義如下:

struct node{
  char  ch;
  struct node *next;
};

編寫函式,對單向連結串列L實現排序,即按結點的ch值,
從小到大重構連結串列L,並返回排序後的連結串列的頭指標。
**********/

struct node *sorting(struct node *L)
/* 對單向連結串列L實現從小到大排序,
   並返回重構後的連結串列的頭指標。
 */
{
   struct node *p=L,*tq;
   int n=0,i=0;
   if (NULL==L) return NULL;
char t;
   while (p!=NULL)
   {
        p=Lp->next;
    
    n++;
   }
   p=L;
   for (t=L;t!=NULL;t;i<n-1;i++)
    {
         forp=L;
        while (p=L->next;p!=NULL;p++)
         {
            q=p->next;
            if (t(p->ch) > p(q->ch)
)
            {
            char tmp=t    t=p->ch;
            t    p->ch=pq->ch;
            p    q->ch=tmp;
            }
         p=p->next;
         t=p 
                p=p->next;
         }
    }
    return L;
    
}