c语言(练习题汇总 结构体篇 谭浩强书)

1.定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。

int main()
{
    struct Date
    {
        int year;
        int month;
        int day;
    };
    printf("输入日期(xxxx-xx-xx):");
    struct Date date;
    scanf("%d-%d-%d",&date.year,&date.month,&date.day);
    int Days[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int i,days=0;
    for(i=1;i<date.month;i++)
    {
        days += Days[i];
    }
    days += date.day;
    if(date.month >2)
    {
        if(date.year%400 == 0 || (date.year%4 == 0 && date.year%100 != 0) )
           {
               ++days;
           }
    }
    printf("这是今年第%d天",days);
    return 0;

}

2.写一个函数days,实现第1 题的计算。由主函数将年、月、日传递给days函数,计算后将日子数传回主函数输出。

struct Date
    {
        int year;
        int month;
        int day;
    };
int days(struct Date date)
{
    int Days[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int i,num=0;
    for(i=1;i<date.month;i++)
    {
        num+= Days[i];
    }
    num += date.day;
    if(date.month >2)
    {
        if(date.year%400 == 0 || (date.year%4 == 0 && date.year%100 != 0) )
           {
               ++num;
           }
    }
    return num;

}
int main()
{

    printf("输入日期(xxxx-xx-xx):");
    struct Date date;
    scanf("%d-%d-%d",&date.year,&date.month,&date.day);
    printf("这是今年第%d天",days(date));
    return 0;

}

6、13个人围成一圈,从第1个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子中的人原来的序号。要求用链表实现。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 13
//13个人围成一圈,从第1个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子中的人原来的序号。要求用链表实现。
typedef struct People
{
    int num;
    struct people *next;
}people;
int main()
{
    int count=N;
    people p[N];
    people *head;
    head = p;

    //建立环状链表
    for(int i=0;i<N;i++)
    {
        head->num =i+1;
        head->next = &p[i+1];
        head = head->next;
    }
    p[N-1].next = p;

    //开始报数
    head =p;
    int i=1;
    while(count>1)
    {
        //跳过已淘汰的
        if(head->num == 0)
        {
            head = head->next;
            continue;
        }
        //如果报数到3,置0,人数-1
        if(i==3)
        {
            head->num = 0;
            count--;
        }
        head = head->next;
        i++;
        if(i>3)
        {
            i=1;
        }
    }
    while(head->num ==0)
    {
        head = head->next;
        if(head->num != 0)
        {
            printf("留下的是%d号",head->num);
        }
    }

    return 0;

}

7.在第9章例9.9和例9.10的基础上,写一个函数del,用来删除动态链表中指定的节点

#include <stdio.h>
#include <stdlib.h>

typedef struct LNode
{
    int num;
    struct LNode *next;
}node;
/*node* creat(void)
{
    node *head,*p1,*p2;
    int n=0;
    p1=p2=(node *)malloc(N);
    scanf("%d",&p1->num);
    head = NULL;
    while (p1->num!=0)
    {
        n = n+1;
        if(n==1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (node)malloc(N);
        scanf("%d",p1->num);
    }
    p2->next =NULL;
    return head;
}*/
node* creat(int n)
{
    node *head,*p;
    head = (node *)malloc(N);
    p = head;
    head->num = 0;
    head->next = NULL;
    for(int i=1;i<=n;i++)
    {
        node *newNode = (node *)malloc(N);
        newNode->num =i;
        newNode->next = NULL;
        p->next =newNode;
        p = p->next;
    }
    return head;
}
void print(node *head)
{
    node *p = head;
    if(head!=NULL)
    do
    {
        printf("%d ",p->num);
        p = p->next;
    }while(p!=NULL);
}
node* del(node *head,int target)
{
    node *pre= head;
    node *p = head->next;
    while (p!=NULL)
    {
        if(p->num==target)
        {
            pre->next = p->next;
            free(p);
            break;
        }
        else
        {
            pre = p;
            p = p->next;
        }

    }
    return head;
}
int main()
{
    node *head = creat(10);
    head = del(head,3);
    print(head);
    return 0;
}

10.已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并, 按学号升序排列。

typedef struct Student
{
    int num;
    float score;
    struct Student *next;
} student;
student* creat(void)
{
    student *head,*p1,*p2;
    int n=0;
    p1=p2=(student *)malloc(N);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while (p1->num!=0)
    {
        n = n+1;
        if(n==1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (student *)malloc(N);
        scanf("%d %f",&p1->num,&p1->score);
    }
    p2->next =NULL;
    return head;
}
void print(student *head)
{
    //printf("ok");
    student *p = head;
    if(head!=NULL)
        do
        {
            printf("%d %5.1f\n",p->num,p->score);
            p = p->next;
        }
        while(p!=NULL);
}
student *merge(student *head1,student *head2)
{
    student *pre,*cur;
    pre = head1;
    cur = head1->next;
    //合并
    while(pre->next!=NULL)
    {
        pre = pre->next;
    }
    pre->next = head2;
    //排序
    pre = head1;
    while(pre->next !=NULL)
    {
        cur = head1->next;
        while(cur !=NULL)
        {
            if(cur->num < pre->num)
            {
                int num = cur->num;
                float score = cur->score;

                cur->num = pre->num;
                cur->score = cur ->score;

                pre->num = num;
                pre->score = score;
            }
            cur = cur->next;

        }
        pre = pre->next;
    }

    return head1;

}
int main()
{
    printf("组1:\n");
    student *head1 = creat();
    //print(head1);
    printf("组2:\n");
    student *head2 =creat();
    printf("排序后:\n");
    print(merge(head1,head2));
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值