12
2012
10

linux下练习 c++ 栈中使用类模版例子

code:

//栈中使用类模版例子
#include <iostream>
#include <typeinfo>
using namespace std;
//typedef char T;
template<typename T,int len=4>//可以有默认值,有默认值的参数靠右
//如果成员函数在类外写,那么每个函数前要加语句: template<typename T> 
class Stack
{
T a[len];
int cur;
public:
Stack():cur(0){}
const char* stype()const//类型
{
return typeid(T).name();
}
int max_size()const//最大空间
{
return len;
}
void push(const T& d) throw(const char*)//入栈
{
if(full()) throw "满";
a[cur++]=d;
}
T pop() throw (const char*)//出栈
{
if(empty()) throw "空";
return a[--cur];
}
const T& top() const throw(const char*)//取顶元素
{
if(empty()) throw "空";
return a[cur-1];
}
bool empty()const//是否空
{
return cur==0;
}
bool full()const//是否满
{
return cur==len;
}
void clear()//清空
{
cur=0;
}
void travel()//遍历
{
while(!empty())
{
cout<<pop()<<' ';
}
cout<<endl;
}
int size()const{return cur;}//元素个数
};
int main()
{
Stack<char,10> s;
Stack<int> d;
s.push('+');s.push('-');s.push('*');s.push('/');
d.push(123);
d.push(42);
d.push(666);
cout<<s.stype()<<endl;
s.travel();
cout<<d.stype()<<endl;
d.travel();
return 0;
}


05
2012
10

linux下练习 c++ 几种排序算法

code:

#include <iostream>
#include<algorithm>
#include<ctime>
using namespace std;
void sort(int* a,int n)//普通冒泡排序
{
bool changed;
do
{
changed=false;
for(int i=1;i<n;i++)
{
if(a[i]<a[i-1])
{
swap(a[i],a[i-1]);
changed=true;
}
}
--n;
}
while(changed);
}
void sort1(int* a,int n)//插入排序
{
int j;
for(int i=1;i<n;i++)
{
int t=a[i];
for(j=i;j>0&&t<a[j-1];j--)
a[j]=a[j-1];
a[j]=t;
}
}
void sort2(int* a,int n)//高效冒泡排序
{
int j;
for(int i=0;i<n-1;i++)
{
int t=i;
for(j=i+1;j<n;j++)
if(a[j]<a[t]) t=j;
if(i!=t) swap(a[t],a[i]);
}
}
void sort3(int* a,int n)//快速(分组)排序
{
if(n<=1) return;
else if(n==2)
{
if(a[0]<=a[1]) return;
else swap(a[0],a[1]);
}
else
{
swap(a[n/2],a[0]);
int jie =a[0];
int* L=a+1;
int* R=a+n-1;
while(L<R)
{
while(L<R && *L<jie) ++L;
while(a<R && *R>=jie) --R;
if(L<R) swap(*L,*R);
}
if(*R <jie) swap(*R,a[0]);
sort(a,R-a);
sort(R+1,n-1-(R-a));
}
}
int main()
{
const int N=10240;
int a[N];
for(int i=0;i<N;i++)
{
a[i]=N-i;
}
clock_t t1=clock();
sort(a,N);
clock_t t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
//int b[5]={45,42,67,88,11};
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort1(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort2(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort3(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}


05
2012
10

linux下练习 c++ 二分查找

code:

#include<iostream>
using namespace std;
class person
{
string name;
int age;
public:
person(const char* n,int a):name(n),age(a){}
friend bool operator <(const person& a,const person& b)//运算符重载-比较年龄大小
{
return a.age<b.age;
}
friend bool operator >(const person& a,const person& b)//运算符重载-比较年龄大小
{
return a.age>b.age;
}
friend bool operator ==(const person& a,const person& b)//运算符重载-等于号
{
return a.age==b.age;
}
friend ostream& operator<<(ostream& o,const person& a)//运算符重载-输出
{
o<<a.name<<":"<<a.age<<endl;
}
};
person* bsearch(person* a,int n,const int age)//二分查找
{
if(n<=0) return NULL;
int mid=n/2;
person p("",age);
if(a[mid]==p) return a+mid;
if(p<a[mid]) return bsearch(a,mid,age);
else return bsearch(a+mid+1,n-mid-1,age);
}
person* bsearch2(person* a,int n,const int age)//二分查找
{
int b=0,e=n-1;
person t("",age);
while(b<=e)
{
int mid=(b+e)/2;
if(a[mid]==t) return a+mid;
if(t<a[mid]) e=mid-1;
else b=mid+1;
}
return NULL;
}
int main()
{
person a[5]={person("a1",34),
person("a2",25),
person("a3",16),
person("a4",77),
person("a5",40)};
for(int i=0;i<5;i++)//排序
{
for(int j=i+1;j<5;j++)
if(a[j]<a[i]) swap(a[j],a[i]);
}
for(int i=0;i<5;i++)
cout<<a[i];
int fage;
cout<<"请输入要查找的年龄:";
cin>>fage;
person* p=bsearch2(a,5,fage);//查找
if(p!=NULL) cout<<*p;
else cout<<"未找到!\n";
}


29
2012
09

linux下练习 c++ 链表操作

code:

#include <iostream>
using namespace std;
typedef int T;
class List
{
struct Node
{
T data;
Node * next;
Node(const T&d=T()):data(d),next(0){}
};
Node * head;
public:
List():head(NULL)
{
cout<<"…list初始化完成…\n";
}
void push_front(const T&d)//前插法
{
Node * p=new Node(d);
p->next=head;
head=p;
}
void push_behand(const T&d)//尾插法
{
Node *ph=head;
while(ph->next!=NULL) ph=ph->next;
Node * p=new Node(d);
ph->next=p;
}
void insert(const T&d,int pos)//任意位置插入
{
Node * p=new Node(d);
Node *& pn=getptr(pos);
p->next=pn;
pn=p;
/*
if(pos<=0)
{
    p->next=head;
    head=p;
}
else
{
Node * ph=head;
while(pos-->1 && ph->next !=NULL)
{
ph=ph->next;
}
p->next=ph->next;
ph->next=p;
}
*/
}
void remove(int pos)
{
if(pos<0) return ;
Node *& pn=getptr(pos);
Node *p=pn;
pn=pn->next;
delete p;
}
Node *& getptr(int pos)
{
if(pos<=0) return head;
Node * p =head;
while(pos-->1 && p->next !=NULL)
p=p->next;
return (*p).next; 
}
void travel()const//遍历
{
Node * p=head;
while(p!=NULL)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
void clear()//清空内存
{
while(head != NULL)
{
Node *p=head->next;
delete head;
head=p;
}
}
~List()
{
clear();
cout<<"…内存释放完成…\n";
}
};
int main()
{
List l;
l.push_front(5);
l.push_front(4);
l.push_front(7);
l.push_front(6);
l.push_behand(45);
l.push_behand(48);
l.insert(100,0);//小于0位置就插入到最前
l.insert(99,6);//插入到指定位置
l.insert(991,100);//大于最后位置就插入到最后
l.remove(1);
l.remove(0);
l.travel();
return 0;
}

 

28
2012
09

linux学习与c编程2

  1. cc -S c.c          --编译c.c 文件,会生成 c.s的汇编代码文件  

27
2012
09

linux学习与c编程1

sudo 在没有权限时,运行在命令前。  

  

运行文件要用文件绝对路径,当前路径 要用 ./文件名   

  

查看当前shell 用:ps  

进入另一个 shell,直接输入shell的名字,如:ksh/tcsh/sh/bash/  

退出一个shell 用 exit   

切换shell,如: exec tsh   

20
2012
05

ubuntu_linux 安装锐捷,连接网络

 

下载软件: 

里面有三个文件.libpcap.so.0.6.2    libstdc++.so.5  myxrgsu
可以用右键复制到/home/tmp 下面


...

23
2011
05

C++最小生成树问题

问题描述

求一个连通无向图的最小生成树的代价(图边权值为正整数)。

输入

第一行是一个整数N(1<=N<=20),表示有多少个图需要计算。以下有N个图,第i图的第一行是一个整数M(1<=M<=50),表示图的顶点数,第i图的第2行至1+M行为一个M*M的二维矩阵,其元素ai,j表示图的i顶点和j顶点的连接情况,如果ai,j=0,表示i顶点和j顶点不相连;如果ai,j>0,表示i顶点和j顶点的连接权值。

输出

07
2010
12

拓扑排序 C++实现

描述:

学校领导在大学生培养计划的制订中,涉及到课程的安排问题,由于课程较多,现在要求编程高手的你帮忙。假定课程之间的先修关系已确定,现在要求你根据先修关系,通过编程确定各门课程的先后关系,并生成一张课程先后安排顺序表,以帮助学校设置各年度课程。

输入:

输入只包括一个测试用例,第一行为一个自然数n,表示课程数量,第二行为n个单词,分别表示n门课程,一个单词表示一门课程,单词全为小写状态,各单词之间用一个空格隔开。

接下来为n*n行0和1构成的矩阵,表示各门课程之间的先修关系。假设i行j列为1,表示第i课程为第j课程的先修课。否则表示不存在先修关系。

07
2010
12

C++输入输出重载中的问题(转换IP为二进制)

#include<iostream.h>            //这个代码很简单,这里只要知道在VC 6.0里面,要用这个头文件,不然编译通不过
//#include<iostream>
//using namespace std;
class IP
{
public:
 IP(int i=0,int j=0,int k=0,int m=0)
 {
  i=a;
  j=b;
  k=c;
  m=d;
 }
 friend istream& operator >> (istream &input,IP &h);
 friend ostream& operator << (ostream &output,IP &h);
 void print()       
 {
  printt(a);
  cout<<".";
  printt(b);
  cout<<".";
  printt(c);
  cout<<".";
  printt(d);
  cout<<'/n';
 }
 void printt(int a)
 {
  int b=128;
        for(int i=1;i<=8;i++)  //转换为八位二进制数
  {
   cout<<a/b;
   a=a%b;
   b=b/2;
        }
 }
private:
 int a;
 int b;
 int c;
 int d;
};
istream& operator >> (istream &input,IP &h)
{
 input>>h.a;
 input.ignore();    //读掉“.”
 input>>h.b;
 input.ignore();   //读掉“.”
 input>>h.c;
 input.ignore();   //读掉“.”
 input>>h.d;
 return input;
}
ostream& operator << (ostream& output,IP &h)
{
 h.print();                  //调用print() 函数,也可以在这里直接调用类中成员输出
 return output;
}
int main()
{
    IP ip;
 cin>>ip;
 cout<<ip;
 return 0;
}