Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9701
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-29 18:10
文章分类
文章存档

2017年(6)

我的朋友
最近访客

分类: C/C++

2017-01-01 17:11:09

原文地址:C++指针作业 作者:luozhiyong131

1.耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:123123……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛徒,请找出它原来的序号

 

/*

 * 使用循环链表实现要求

 * Lzy 2011-8-1

 */

 

#include

#include

using namespace std;

 

typedef struct Node

{

    int data;

    struct Node *next;

}node;

 

int main(void)

{

    node *head = new node;       //创建头节点

    head->next = head;           //循环链表

    node *p = head;  

    p->data = 1;              //给第一个节点赋值

 

    int i;

    for(i=12; i>0; i--)

    {

        node *p = new node;      //分配新节点

        p->data = i+1;            //13减一开始赋值

        p->next = head->next;    //指向域指向下一个节点

        head->next = p;           //插到头节点后

    }

   

    int count=0;             

    node *q, *pq=NULL;              

    p = head;                 

 

    while(1)

    {

        count++;           //计数加一

        q = p;             //保存前一节点方便删除

        p=p->next;         //沿链表移动指针

       

        if(count==2)      //计数到3

        {

            if(pq == p)       //链表只剩最后一个节点

            {

                cout<<p->data<<endl;

                return 0;

            }

            /*删除节点*/

            q->next = p->next;

            delete[] p;

           

            count=0;   //计数清0

            pq = p = q->next; //p指向下一节点,pq标志

        }

    }

 

    return 0;

}

 

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

/* 一年的第几天

 * Lzy 2011-8-1

 */

#include

#include

 

using namespace std;

 

struct Date

{

    int year;

    int month;

    int date;

}dat;

 

int IsLeapYear(int year)

{

    return (year%4==0 && year%100!=0)||(year%400==0);

}

 

int main(void)

{

    cout<<"输入年 月 日"<<endl;

    cin>>dat.year>>dat.month>>dat.date;

   

    int date = dat.date;

 

    switch(dat.month-1)

    {

    case 11:

        date += 30;

    case 10:

        date += 31;

    case 9:

        date += 30;

    case 8:

        date += 31;

    case 7:

        date += 31;  

    case 6:

        date += 30;

    case 5:

        date += 31;

    case 4:           

        date += 30;

    case 3:       

        date += 31;

    case 2:

        date += IsLeapYear(dat.year)+28;

    case 1:

        date += 31;

    }

   

    cout<<""<<date<<""<<endl;

    cout<<"星期"<<(date-4)%8<<endl;

    return 0;

}

 

3.给定一个日期,求出该日为星期几(已知2002-3-28为星期四)

 

4.建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去。

/*

 * 使用循环链表实现要求

 * Lzy 2011-8-1

 */

 

#include

#include

#include

using namespace std;

 

typedef struct Node

{

    int id;

    char ***[3];

    int age;

    struct Node *next;

}node;

 

void LinkListInit(node **p)

{

    (*p) =new node;

    (*p)->next = NULL;

}

 

void LinkListInput(node *head, node *p)

{

    p->next = head->next;

    head->next = p;

}

 

void LinkListAdd(node *head)

{

    char ch;

 

    while(1)

    {

        node *p = new node;     

 

        cout<<"输入\n学号\t"<<"性别\t"<<"年龄\n";

        cin>>p->id>>p->***>>p->age;

        cout<<"Save(y\\n): ";

 

        cin>>ch;

        if(ch == 'y')

            LinkListInput(head, p);

        else

            delete p;

        cout<<"Continue(y\\n): ";

 

        cin>>ch;

        if(ch == 'n')

            break;

    }  

}

 

void LinkListDelete(node *head, int id)

{

    node *p = head;

    node *q=p;

 

    while(p)

    {

        q = p;

        p = p->next;

 

        if(p->id == id)

        {

            q->next = p->next;

            cout<<p->id<<'\t'<<p->***<<'\t'<<p->age<<endl;

            delete[] p;

            p = q->next;

        }      

    }

}

 

void LinkListDisplay(node *head)

{

    node *p = head->next;

 

    cout<<"\n学号\t"<<"性别\t"<<"年龄\n";

    while(p)

    {

        cout<<p->id<<'\t'<<p->***<<'\t'<<p->age<<endl;

        p = p->next;

    }  

}

 

int main(void)

{

    node *head = new node;       //创建头节点

   

    LinkListInit(&head);

    LinkListAdd(head);

 

    int id;

    cout<<"要删除的学号:";

    cin>>id;

 

    LinkListDelete(head,id);

    LinkListDisplay(head);

    return 0;

}

 

 

5.有一个unsigned long型整数,先要分别将其前2个字节和后2个字节用为两个unsigned int型整数输出(设一个int型数据占2个字节),试编写一函数partition实现上述要求。要求在主函数输入该long型整数,在函数partition中输出结果

 

要求用在linux 下用c++实现,礼拜四提交给班长

阅读(985) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:C++构造函数

给主人留下些什么吧!~~