我爱c++

2006年5月12日

二叉树结构

#include <iostream>
using namespace std;

typedef double DATA;

class bstree{
 struct bnode{
  DATA d;
  bnode* left;
  bnode* right;
  bnode( const DATA& cd )
  :d(cd), left(NULL), right(NULL)
  {}
 };
 bnode* root;
 int len;
 bstree(const bstree& cb){}
 bstree& operator=(const bstree& cb){return *this;}
 void clear( bnode* & ptree ){
  if( ptree==NULL )
   return;
  clear( ptree->left );
  clear( ptree->right );
  delete ptree;
  ptree = NULL;
  len--;
 }
 void insert( bnode* & ptree, bnode* np ){
  if( ptree==NULL )
   ptree = np;
  else if( np->d < ptree->d )
   insert( ptree->left, np );
  else
   insert( ptree->right, np );
 }
 void show( bnode* ptree ){
  if( ptree==NULL )
   return;
  show( ptree->left );
  cout << ptree->d << ' ';
  show( ptree->right );
 }
public:
 bstree():len(0){ root=NULL; }
 ~bstree(){ clear(); }
 void clear(){clear(root);}
 void insert( const DATA& cd ){
  insert( root, new bnode(cd) );
  len++;
 }
 void show(){
  show( root );
  cout << endl;
 }
 int size(){ return len; }
 bool empty(){ return root==NULL; }
};

int main()
{
 bstree bs;
 DATA d;
 while( cin.peek()!='\n' ){
  cin >> d;
  bs.insert( d );
 }
 cout << bs.size() << " data:" << endl;
 bs.show();
 return 0;
}

posted @ 2006-05-12 06:46 我爱c++ 阅读(330) | 评论 (0) | 编辑 收藏
 

2006年5月10日

链表超精彩
#include using namespace std; typedef int DATA; const unsigned npos=(unsigned)-1; class clink{ struct node{ DATA d; node* next; node( const DATA& cd ) :d(cd), next(NULL) {} }; node* head; int len; public: clink():head(NULL),len(0){} ~clink(){ clear(); } node* & getp( unsigned pos ){ if( pos==0 || head==NULL ) return head; node* p = head; for( int i=1; inext ) p = p->next; else break; } return p->next; } void insert( const DATA& cd, unsigned pos=0 ){ node* & lp = getp( pos ); node* np = new node( cd ); np->next = lp; lp = np; len++; } friend ostream& operator<<( ostream& os, const clink& cc ) { os << "{ "; node* p = cc.head; while( p ){ os << p->d << ' '; p = p->next; } os << "} "; return os; } unsigned find( const DATA& cd ){ node* p = head; unsigned pos=0; while( p ){ if( p->d==cd ) return pos; pos++; p = p->next; } return npos; } bool update( const DATA& d1, const DATA& d2 ){ unsigned pos=find( d1 ); node* p; if( pos==npos ) return false; p = getp( pos ); p->d = d2; return true; } bool erase( const DATA& cd ){ unsigned pos=find( cd ); node* p; if( pos==npos ) return false; node* & lp = getp( pos ); p = lp; lp = lp->next; delete p; len--; return true; } int size(){ return len; } bool empty(){ return head==NULL; } void clear(){ node* p; while( head ){ p = head->next; delete head; head = p; } len = 0; } }; int main() { clink ol; cout << ol << endl; ol.insert( 10 ); ol.insert( 20, npos ); ol.insert( 30, 0 ); ol.insert( 40 ); ol.insert( 50, 1 ); cout << ol << endl; DATA d; cout << "input a DATA for search:" << endl; cin >> d; unsigned pos=ol.find( d ); if( pos==(unsigned)-1 ) cout << "not found!" << endl; else cout << "found at " << pos << endl; DATA nd; for( int i=0; i<3; i++ ){ cout << "input old data and new data:\n"; cin >> d >> nd; ol.update( d, nd ); cout << ol << endl; } for( int i=0; i<3; i++ ){ cout << "input a data to remove:" << endl; cin >> d; ol.erase( d ); cout << ol.size() << ol << endl; } cout << "is empty?" << ol.empty() << endl; ol.clear(); cout << "is empty?" << ol.empty() << endl; cout << ol.size() << ol << endl; return 0; }
posted @ 2006-05-10 23:28 我爱c++ 阅读(450) | 评论 (1) | 编辑 收藏
 
输入年月日得到当日的星期
#include<iostream>
int nian(int year);
int yue(int year,int month);
using namespace std;
//-------------------------------
void main()
{
    
        int year,month,day,sum,pp;
char x;
cout<<"Please input a day:year/month/day!";
cin>>year>>x>>month>>x>>day;
sum=nian(year)+yue(year,month)+day;
pp=sum%7;

switch(pp)
{
case 0:
    cout<<"Sunday"<<endl;
    break;
case 1:
    cout<<"Monday"<<endl;
    break;
case 2:cout<<"Tuesday"<<endl;
    break;
case 3:cout<<"Wednesday"<<endl;
    break;
case 4:cout<<"Thursday"<<endl;
    break;
case 5:cout<<"Fiday"<<endl;
    break;
case 6:cout<<"Saturday"<<endl;
    break;

}
}
//-------------------------------------------


int nian(int year)
{
int i,sum_year=0;
for(i=1;i<year;i++)
{
if((i%4==0&&i%100!=0)||(i%400==0))
sum_year+=366;
else
sum_year+=365;
}
return (sum_year);
}
//-------------------------------------------
int yue(int year,int month)
{int yue1,day1;
yue1=month-1;
if((year%4==0&&year%100!=0)||(year%400==0))
{
switch(yue1)
{
case 1:day1=31;break;
case 2:day1=31+29;break;
case 3:day1=31+29+31;break;
case 4:day1=31+29+31+30;break;
case 5:day1=31+29+31+30+31;break;
case 6:day1=31+29+31+30+31+30;break;
case 7:day1=31+29+31+30+31+30+31;break;
case 8:day1=31+29+31+30+31+30+31+31;break;
case 9:day1=31+29+31+30+31+30+31+31+30;break;
case 10:day1=31+29+31+30+31+30+31+31+30+31;break;
case 11:day1=31+29+31+30+31+30+31+31+30+31+30;break;
case 12:day1=31+29+31+30+31+30+31+31+30+31+30+31;break;
}
}
else
{
switch(yue1)
{
case 1:day1=31;break;
case 2:day1=31+28;break;
case 3:day1=31+28+31;break;
case 4:day1=31+28+31+30;break;
case 5:day1=31+28+31+30+31;break;
case 6:day1=31+28+31+30+31+30;break;
case 7:day1=31+28+31+30+31+30+31;break;
case 8:day1=31+28+31+30+31+30+31+31;break;
case 9:day1=31+28+31+30+31+30+31+31+30;break;
case 10:day1=31+28+31+30+31+30+31+31+30+31;break;
case 11:day1=31+28+31+30+31+30+31+31+30+31+30;break;
case 12:day1=31+28+31+30+31+30+31+31+30+31+30+31;break;
}
}

return (day1);
}
posted @ 2006-05-10 07:39 我爱c++ 阅读(366) | 评论 (0) | 编辑 收藏
 
仅列出标题  
 
<2025年5月>
日一二三四五六
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

 导航

  • C++博客
  • 首页
  • 发新随笔
  • 发新文章
  • 联系
  • 聚合
  • 管理

 统计

  • 随笔: 3
  • 文章: 0
  • 评论: 1
  • 引用: 0

常用链接

  • 我的随笔
  • 我的评论
  • 我参与的随笔

留言簿(1)

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔档案

  • 2006年5月 (3)

搜索

  •  

最新评论

  • 1. re: 链表超精彩
  • 这样的代码叫人怎么看啊?
  • --beyonlin

阅读排行榜

  • 1. 链表超精彩(450)
  • 2. 输入年月日得到当日的星期(366)
  • 3. 二叉树结构(330)

评论排行榜

  • 1. 链表超精彩(1)
  • 2. 二叉树结构(0)
  • 3. 输入年月日得到当日的星期(0)

Powered by: 博客园
模板提供:沪江博客
Copyright ©2025 我爱c++