天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理


//静态顺序表的各种操作
#include<stdio.h>
#define MaxSize 10
void insertElem(int Sqlist[],int *len,int i,int x)//表首地址、表长、插入元素位置、待插入的元素值
{
    
int t,temp;
    
if(*len==MaxSize||i<1||i>*len+1)
    {
        printf(
"This insert is illegal\n");
        
return;
    }
    
for(t=*len-1;t>=i-1;t--)
        Sqlist[t
+1]=Sqlist[t];//i-1后的元素都向后移一位
    Sqlist[i-1]=x;//插入元素
    *len=*len+1;//表长加1
}

void DelElem(int Sqlist[],int *len,int i)//向顺序表中删除元素
{
    
int j;
    
if(i<1||i>*len)
    {
        printf(
"This insert is illegal");
        
return;
    }
    
for(j=i;j<=*len-1;j++)
        Sqlist[j
-1]=Sqlist[j];//将第i个元素之后的元素前移,覆盖即删除
    *len=*len-1;
}

int main()
{
    
int Sqlist[MaxSize];
    
int len;
    
int i;
    
for(i=0;i<6;i++)
        scanf(
"%d",&Sqlist[i]);
    len
=6;
    
for(i=0;i<len;i++)
        printf(
"%d ",Sqlist[i]);
    printf(
"\nThe spare length is %d\n",MaxSize-len);//显示表中剩余空间
    insertElem(Sqlist,&len,3,0);//在表中第3个位置插入整数0
    for(i=0;i<len;i++)
        printf(
"%d ",Sqlist[i]);
    printf(
"\nThe spare length is %d\n",MaxSize-len);
    insertElem(Sqlist,
&len,11,0);
    DelElem(Sqlist,
&len,6);
    
for(i=0;i<len;i++)
        printf(
"%d ",Sqlist[i]);
    printf(
"\nThe spare length is %d\n",MaxSize-len);
    
return 0;
}

posted @ 2012-03-01 00:53 hoshelly 阅读(218) | 评论 (0)编辑 收藏

#include<stdio.h> //使用数组创建队列
#define MAXQUEUE 10    //队列的最大容量
int queue[MAXQUEUE]; //队列的数组声明
int front=-1;//队列的队头
int rear=-1; //队列的队尾
//队列数据的存入
int enqueue(int value)
{
if(rear>=MAXQUEUE)
return -1; //检查队列是否全满
rear++; //队尾指针往前移
queue[rear]=value; //存入队列
}
//队列数据的取出,取出时队头指针往后移
int dequeue()
{
if(front==rear) //检查队列是否是空
return -1;  //无法取出
front++; //队头指针往前移(即向队尾指针方向移)
return queue[front]; //队列取出
}
//主程序:模拟队列操作
//输出输入的内容都会存储在数组中,接着输出数组内容来看其结果
void main()
{
int input[100];//存储输入的元素
int output[100];//存储取出的元素
int select;
int i_count=0; //数组input的索引
int o_count=0;
int loop=1;
int i,temp;
while(loop)
{
printf("[1]输入 [2]取出 [3]列出全部内容 ==>");
scanf("%d",&select);
switch(select)
{
case 1:printf("请输入存入队列的值(%d)==> ",i_count+1);
   scanf("%d",&temp);
   if(enqueue(temp) == -1)
   printf("队列全满.\n");
   else
   input[i_count++]=temp;
   break;
case 2:if((temp=dequeue())==-1)
   printf("队列是空的.\n");
   else
   {
   printf("取出队列元素:%d\n",temp);
   output[o_count++]=temp;
   }
   break;
case 3: loop=0;
    break;
}
}
printf("输入队列的元素:");
for(i=0;i<i_count;i++)
printf("[%d]",input[i]);
printf("\n取出队列的元素: ");
for(i=0;i<o_count;i++)
printf("[%d]",output[i]);
printf("\n剩下队列的元素:");
while((temp=dequeue())!=-1)
printf("[%d]",temp);
printf("\n");
}

posted @ 2012-02-28 00:38 hoshelly 阅读(356) | 评论 (0)编辑 收藏

//应用栈来走迷宫
#include<stdio.h>
#include<stdlib.h>
struct stack_node
{
    int x;//路径坐标x
    int y;//路径坐标y
    struct stack_node *next;//指向下一结点
};
typedef struct stack_node stack_list;
typedef stack_list *link;
link path=NULL;//路径栈指针
//栈数据的存入
link push(link stack,int x,int y)
{
    link new_node;//新结点指针
   
//分配结点内存
    new_node=(link)malloc(sizeof(stack_list));
    if(!new_node)
    {
        printf("内存分配失败!\n");
        return NULL;
    }
    new_node->x=x; //存入路径坐标x
    new_node->y=y; //存入路径坐标y
    new_node->next=stack;//新结点指向原开始
    stack=new_node; //新结点成为栈开始
    return stack;
}
//栈数据的取出
link pop(link stack,int *x,int *y)
{
    link top;//指向栈顶端
    if(stack!=NULL)
    {
        top=stack;
        stack=stack->next;//栈指针指向下结点
        *x=stack->x;//取出路径坐标x
        *y=stack->y;//取出路径坐标y
        free(top);
        return stack;
    }
    else
        *x=-1;
}
//主程序:用回溯的方法在数组迷宫找出口
//数字0:表示是可以走的路
//数字1:表示是墙壁,不可走的路
//数字2:表示是走过的路
//数字3:表示是回溯的路
void main()
{
    int maze[7][10]={
        1,1,1,1,1,1,1,1,1,1,
        1,0,1,0,1,0,0,0,0,1,
        1,0,1,0,1,0,1,1,0,1,
        1,0,1,0,1,1,1,0,0,1,
        1,0,1,0,0,0,0,0,1,1,
        1,0,0,0,1,1,1,0,0,1,
        1,1,1,1,1,1,1,1,1,1,};
    
    int i,j;
    int x=5;//迷宫入口坐标
    int y=8;
    while(x!=1||y!=1)//是否是迷宫出口
    {
        maze[x][y]=2;//标示走过的路
        if(maze[x-1][y]<=0) //往上方走
        {
            x=x-1;
            path=push(path,x,y);//存入路径
        }
        else if(maze[x+1][y]<=0)//往下方走
        {
                x=x+1;
                path=push(path,x,y);
        }
        else if(maze[x][y-1]<=0)//往左方走
        {
            y=y-1;
            path=push(path,x,y);
        }
        else if(maze[x][y+1]<=0)//往右方走
        {
            y=y+1;
            path=push(path,x,y);
        }
        else
        {
            maze[x][y]=3;
            path=pop(path,&x,&y);//退回一步
        }
    }
    maze[x][y]=2;
    printf("迷宫的路径如下图所示:\n");
    for(i=1;i<6;i++)//输出迷宫图形
    {
        for(j=1;j<9;j++)
            printf("%d",maze[i][j]);//输出各坐标
        printf("\n");
    }
}
        

posted @ 2012-02-28 00:37 hoshelly 阅读(656) | 评论 (0)编辑 收藏

//利用链表创建栈
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
struct stack_node
{
    int data;
struct stack_node*next;
};
typedef struct stack_node stack_list;
typedef stack_list *link;
link stack=NULL;
//栈数据的存入
int push(int value)
{
   link new_node;
   new_node=(link)malloc(sizeof(stack_list));
   if(!new_node)
   {
      printf("内存分配失败!\n");
  return -1;
}
new_node->data=value;
new_node->next=stack;
stack=new_node;
}
//栈数据的取出
int pop()
{
   link top;
   int temp;
   if(stack!=NULL)
   {
      top=stack;
  stack=stack->next;
  temp=top->data;
  free(top);
  return temp;
}
6
    else
return -1;
}
int empty()
{
   if(stack == NULL)
    return 1;
   else
    return 0;
}
void main()
{
  int card[52];
  int pos;
  int i,temp;
  for(i=0;i<52;i++)
    card[i]=0;
  i=0;
  while(i!=52)
  {
     pos=rand()%52;
 if(card[pos] == 0)
 {
    push(pos);
card[pos]=1;
i++;
 }
  }
  
  printf("  1    2    3    4\n");
  printf("======================\n");
  for(i=0;i<5;i++)
  {
     temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 printf("\n");
   }
}

posted @ 2012-02-28 00:37 hoshelly 阅读(214) | 评论 (0)编辑 收藏

#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#define MAXSTACK 100//栈的最大容量
int stack[MAXSTACK];//栈的数组声明
int top =-1; //栈的顶端
//栈的数据存入
int push(int value)
{
   if(top>=MAXSTACK)//是否超过容量
   {
      printf("栈的内容全满\n");
      return -1;
    }
    top++;
    stack[top]=value;//栈指针加1,存入栈
}
//栈数据的取出
int pop()
{
   int temp;
   if(top<0)
   {
      printf("栈内容是空的\n");
      return -1;
    }
    temp = stack[top];
    top--;
    return temp;
}
//检查栈是否是空的
int empty()
{
  if(top == -1)
  return 1;
  else
  return 0;
 }
 
 //主程序:运用empty()检查牌是否发完
 
//红心:数组0~12,方块:数组13~25,梅花:数组26~38,黑桃:数组39~51
 
 void main()
 {
    int card[52];
    int pos;
    int i,temp;
    for(i=0;i<52;i++)
      card[i]=0;
    i=0;
    while(i!=5)//洗五张牌循环
    {
       pos=rand()%52;//随机数取值0~51
       if(card[pos] == 0) //是否是未洗牌
       {
          push(pos);//存此张牌进栈
          card[pos]=1;//设置此张牌洗过
          i++;//下一张牌
        }
    }
    
    while(!empty())//发完栈全部的牌
    {
       temp=pop(); //取出栈数据
       printf("[%c%3d]",temp/13+3,temp%13+1);
    }
    printf("\n");
}

posted @ 2012-02-28 00:36 hoshelly 阅读(186) | 评论 (0)编辑 收藏

#include<stdlib.h>
#include<stdio.h>
struct dlist  //双向链表结构声明
{
   int data;
   struct dlist *front;//指向下一结点的指针
   struct dlist *back; //指向前一结点的指针
};
typedef struct dlist dnode;//双向链表新类型
typedef dnode *dlink;//双向链表指针新类型
void printdlist(dlink head)
{
   while (head!=NULL)
   {
      printf("[%d]",head->data);
  head=head->front;
    }
printf("\n");
}
//双向链表结点的插入
dlink insertnode(dlink head,dlink ptr,int value)
{
   dlink new_node;
   //创建新结点,分配结点内存
   new_node=(dlink)malloc(sizeof(dnode));
   if(!new_node)
   return NULL;
   new_node->data=value;
   new_node->front=NULL;
   new_node->back=NULL;
   
   if(head == NULL)
   return new_node;
   
   if(ptr == NULL)
   {
       //第一种情况:插在第一个结点之前,成为链表开始
   new_node->front=head;
   head->back=new_node;
   head=new_node;
}
else
{
   if(ptr->front == NULL)
   {
      //第二种情况:插在链表的最后
  ptr->front=new_node;//最后结点指向新结点
  new_node->back=ptr;//新结点指回最后结点
}
   else
   {
      //第三种情况:插入结点至链表中间结点内
  ptr->front->back=new_node;//下一结点指回新结点
  new_node->front=ptr->front;//新结点指向下一结点
  new_node->back=ptr; //新结点指回插入结点
  ptr->front=new_node; //插入结点指向新结点
}
}
return head;//返回链表起始指针
}
//主程序:使用插入结点的方式来创建链表,完成后将链表内容输出
void main()
{
   dlink head = NULL;//循环链表指针
   dlink tail = NULL;//链表最后的指针
   int list[6]={1,2,3,4,5,6};
   int i;
   head = insertnode(head,head,list[0]);
   printdlist(head);
   tail = head;//保留链表最后指针
   //第一种情况:插在第一个结点之前
   head=insertnode(head,NULL,list[1]);
   printdlist(head);
   //第二种情况:插在链表的最后
   head = insertnode(head,tail,list[2]);
   printdlist(head);
   for(i=3;i<6;i++)
   {
      //第三种情况:插入结点至链表中间结点内
  head = insertnode(head,head,list[i]);
  printdlist(head);
    }
}

posted @ 2012-02-28 00:35 hoshelly 阅读(616) | 评论 (0)编辑 收藏

#include<stdio.h>
#include<stdlib.h>
//双向链表结构
struct dlist
{
    int data;
struct dlist *front;//指向下一结点的指针
struct dlist *back;//指向前一结点的指针
};
typedef struct dlist dnode;
typedef dnode *dlink;
dlink createdlist(int *array,int len)
{
   dlink head;
   dlink before;
   dlink new_node;
   int i;
   //创建第一个结点,分配指针内存
   head=(dlink)malloc(sizeof(dnode));
   if(!head)
   return NULL;
   head->data=array[0];
   head->front=NULL;
   head->back=NULL;
   before=head;//指向第一个结点
   
   for(i=1;i<len;i++)//用循环创建其他结点
   {
      new_node=(dlink)malloc(sizeof(dnode));
  if(!new_node)
  return NULL;
  new_node->data=array[i];
  new_node->front=NULL;
  new_node->back=before;//将新结点指向前结点
  before->front=new_node;//将前结点指向新结点,构成循环链表
  before=new_node;//新结点成为前结点
}
return head;
}
//双向链表的输出
void printdlist(dlink head,dlink now)
{
   while(head!=NULL) //链表循环遍历
   {
      if(head == now)
  printf("#%d#",head->data);
  else
     printf("[%d]",head->data);
  head=head->front;
}
printf("\n");
}
void main()
{
   dlink head;
   dlink now=NULL;
   int list[6]={1,2,3,4,5,6};
   int select;
   
   head=createdlist(list,6);
   if(head==NULL)
   {
      printf("内存分配失败!\n");
  exit(1);
    }
now=head;
while(1)
{
   printf("链表内容是:");
   printdlist(head,now);
   printf("[1]往下移动 [2]往回移动 [3]离开 ==> ");
   scanf("%d",&select);
   switch(select)
   {
      case 1: if(now->front!=NULL)
           now=now->front;
   break;
  case 2: if(now->back!=NULL)
           now=now->back;
   break;
  case 3:  exit(1);
    }
}
}

posted @ 2012-02-28 00:34 hoshelly 阅读(158) | 评论 (0)编辑 收藏

/*含头结点的循环链表的多项式*/
# include<stdio.h>
# include<stdlib.h>
struct plist             /* 多项式结构声明*/
{
int coef;                 /*多项式的系数*/
int exp;                  /*多项式的指数*/
struct plist *next;       /*指向下一结点的指针*/
};
typedef struct plist pnode; /* 多项式新类型*/
typedef pnode *plink;        /* 多项式指针新类型*/
/*链表输出*/
void printpoly(plink poly)
{
plink ptr;
ptr=poly->next;       /*指向链表开始*/
while(poly!=ptr)      /*链表遍历循环*/
{
/*输出结点数据*/
printf("%dX^%d",ptr->coef,ptr->exp);
ptr=ptr->next;      /* 指向下一结点*/
if(poly!=ptr) printf("+");
}
printf("\n");       /* 换行*/
}
/*使用数组值创建多项式*/
plink createpoly(int *array,int len)
{
plink head;    /*循环链表的指针*/
plink before; /*前一结点的指针*/
plink new_node;   /*新结点的指针*/
int i;
/*创建头结点,分配结点内存*/
head=(plink)malloc(sizeof(pnode));
if(!head) return NULL; /*检查内存指针*/
head->exp=-1;           /*创建结点内容*/
before=head;            /*指向第一个结点*/
for(i=len-1;i>=0;i--)   /*用循环创建其他结点*/
if(array[i]!=0)
{
/*分配结点内存*/
new_node=(plink)malloc(sizeof(pnode));
if(!new_node) return NULL;    /*检查内存指针*/
new_node->coef=array[i];      /*创建系数内容*/
new_node->exp=i;              /*创建指数内容*/
new_node->next=NULL;          /*设置指针初值*/
before->next=new_node;        /*将前结点指向新结点*/
before=new_node;              /*新结点成为前结点*/
}
new_node->next=head;           /*创建环状链接*/
return head;                   /*返回链表起始指针*/
}
/*多项式相加*/
plink polyadd(plink poly1,plink poly2)
{
plink head1;                 /*多项式1的开始*/
plink head2;                 /*多项式2的开始*/
plink result;                /*多项式的结果*/
plink before;                /*前一结点的指针*/
plink new_node;              /*新结点的指针*/
head1=poly1->next;           /*指向多项式1的开始*/
head2=poly2->next;           /*指向多项式2的开始*/
/*创建头结点且分配结点内存*/
result=(plink)malloc(sizeof(pnode));
if(!result) return NULL;       /*检查内存指针*/
result->exp=-1;                /*创建结点内容*/
before=result;                 /*指向第一个结点*/
while(poly1!=head1||poly2!=head2)
{
/*分配结点内存*/
new_node=(plink)malloc(sizeof(pnode));
if(!new_node) return NULL; /*检查内存指针*/
if(head1->exp<head2->exp)      /*多项式2的指数大*/
{
new_node->coef=head2->coef;    /*设置系数*/
new_node->exp=head2->exp;      /*设置指数*/
head2=head2->next;             /*指向下一结点*/
}
else if(head1->exp>head2->exp)      /*多项式1的指数大*/
   {
   new_node->coef=head1->coef;        /*设置系数*/
   new_node->exp=head1->exp;          /*设置指数*/
   head1=head1->next;                 /*指向下一结点*/
   }
   else                   /*多项式的指数相等*/
    {
/*系数相加*/
    new_node->coef=head1->coef+head2->coef;
    new_node->exp=head1->exp;       /*设置指数*/
    head1=head1->next;             /* 指向下一结点*/
    head2=head2->next;             /* 指向下一结点*/
    }
before->next=new_node;              /*将前一结点指向新结点*/
before=new_node;                    /*新结点成为前结点*/
}
new_node->next=result;               /*创建环状链接*/
return result;                       /*返回多项式的指针*/
}
void main()
{
plink poly1;       /*多项式1的指针*/
plink poly2;       /*多项式2的指针*/
plink result;      /*多项式结果的指针*/
int list1[6]={4,0,3,0,7,0};   /*数组1的内容*/
int list2[6]={9,7,1,0,5,6};   /*数组2的内容*/
poly1=createpoly(list1,6);    /*创建多项式1*/
printf("the content1:");
printpoly(poly1);           /*输出多项式1*/
poly2=createpoly(list2,6);     /*创建多项式2*/
printf("the content2:");
printpoly(poly2);           /*输出多项式2*/
result=polyadd(poly1,poly2);      /*多项式相加*/
printf("the add:");
printpoly(result);         /*输出多项式结果*/
}

posted @ 2012-02-28 00:34 hoshelly 阅读(288) | 评论 (0)编辑 收藏


描述

辉辉、姗姗和佳佳是好朋友,他们一起参加了在湖南长沙长郡中学举办的第二十一届全国青少年信息学奥林匹克竞赛(NOI2004)。他们很早就来到了长沙,可是报名还没有开始。怎么办呢?他们决定分头出去玩一天,晚上回到宿舍以后给大家说说自己这一天做了什么有意义的事情。
你一定想不到辉辉干嘛去了——他睡了一天。他想:“比赛前几天老是写程序到深夜,头晕晕的……没关系,好好睡一觉,然后我会精神抖擞。醒了之后,我要做有意义的事情。”这一睡可不得了,辉辉从早上a点b分c秒一直睡到了下午d点e分f秒。他睡了多少秒钟呢?

输入

测试数据包含多组输入。 每组输入一行,仅包含六个非负整数a, b, c, d, e, f,以空格分离。1<=a, d<=11, 0<=b, c, e, f<=59。如输入6 5 4 3 2 1表示辉辉从06:05:04睡到15:02:01。 输入以六个零结尾。

输出

每组输出一行,仅包含一个整数s,即辉辉睡觉的总秒数。

样例输入
6 5 4 3 2 1 0 0 0 0 0 0
样例输出
32217 

注意秒、分、时之间当不同大小时要进行适当的借1运算
源代码如下(感觉挺丑陋的):

#include
<iostream>
using namespace std;
int main()
{
    
int a,b,c,d,e,f;
    
int h,m,s,sum;
    
while(cin>>a>>b>>c>>d>>e>>f)
    {
        
if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)
            
break;

              
if(b>e)//醒来的时刻的分钟数大于睡时的分钟数
               {
                h
=d-a+11;//小时减1
                
if(c>f)
                {
                    s
=f-c+60;
                    m
=e-b+59;
                }
                
else
                {
                    s
=f-c;
                    m
=e-b+60;
                }
               }
             
else if(b<e)
             {
                h
=d-a+12;
                
if(c>f)
                {
                    s
=f-c+60;
                    m
=e-b-1;
                }
                
else
                {
                    s
=f-c;
                    m
=e-b;
                }
              }
            
else if(b==e)
            {
                
if(c>f)
                {
                    h
=d-a+11;
                    m
=e-b+59;
                    s
=f-c+60;
                }
                
else
                {
                    h
=d-a+12;
                    m
=e-b;
                    s
=f-c;
                }
             }

             sum
=h*3600+m*60+s;//全部换算成统一单位——秒
             cout
<<sum<<endl;
    }
    
return 0;
}

posted @ 2012-02-26 20:14 hoshelly 阅读(170) | 评论 (0)编辑 收藏

为防止在对数组动态赋值时发生数组越界,C++提供了一种能够解决此问题的方法——重载运算符[]。示例程序:
#include<iostream>
class CArray
{
   
public:
   CArray(
int l)
   {
       length
=l;
       Buff
=new char[length];
   }
   
~CArray(){delete Buff;}
   
int GetLength(){return length;}
   
char& operator [](int i);
   
private:
   
int length;
   
char *Buff;
};

char & CArray::operator[](int i)
{
    
static char ch=0;
    
if(i<length && i>=0)
        
return Buff[i];
    
else
    {
        cout
<<"\nIndex out of range.";
        
return ch;
    }
}

void main()
{
    
int cnt;
    CArray string1(
6);
    
char *string2="string";
    
for(cnt=0;cnt<string1.GetLength();cnt++)
        string1[cnt]
=string2[cnt];
        cout
<<"\n";
        
for(cnt=0;cnt<string1.GetLength();cnt++)
           cout
<<string1[cnt];
        cout
<<"\n";
    cout
<<string1.GetLength()<<endl;
}
在重载下标运算符函数时注意:
1)该函数只带一个参数,不可带多个参数。
2)得重载为友元函数,必须是非static类的成员函数。

posted @ 2012-02-24 00:08 hoshelly 阅读(1648) | 评论 (0)编辑 收藏

仅列出标题
共12页: First 4 5 6 7 8 9 10 11 12