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   

02
2012
09

MVC思想精髓





01
2012
09

mvc中json格式的使用方法示例


//View:   

<script type="text/javascript">  

    $(document).ready(function() {  

        GetByJquery();  

01
2012
09

[转载]验证正则表达式集合-1

常用表达式:

asp.net 验证正则表达式

整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$
只能输入数字:"^[0-9]*$"。
只能输入n位的数字:"^\d{n}$"。
只能输入至少n位的数字:"^\d{n,}$"。
只能输入m~n位的数字:。"^\d{m,n}$"
只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。
只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。
只能输入非零的正整数:"^\+?[1-9][0-9]*$"。
只能输入非零的负整数:"^\-[1-9][]0-9"*$。


01
2012
09

c#写扩展方法

学习MVC时,学会了写扩展方法,用起来很方便。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
namespace MvcApp1.Content
{
    //<%@ Import Namespace="MvcApp1.Content" %>
    public static class MyHtmlHelper
    {
        public static string RedLable(this HtmlHelper helper, string lbstr)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<span style='color:red;'>");
            sb.Append(lbstr);
            sb.Append("</span>");
            return sb.ToString();
        }
    }
}


28
2012
08

Linq to DataSet 与linq to entities 实现权限设置模块

数据库(数据库名为test)中表的内容: