2010年8月15日
#include <iostream> #include <string.h> using namespace std; bool is_equal(char str[]) { int len=strlen(str); if (len==0) return false; else if (len%2==0) { int mid=len/2; bool flag=true; int i,j; for(i=0, j=len-1; i<mid; i++,j--) { if (str[i]==str[j]) continue; else { flag=false; break; } } return flag; } else return false; } int main() { char str[]="helleh"; int return_val; if (is_equal(str)) return_val=1; else return_val=0; cout<<return_val<<endl; }
2010年8月12日
//有序多项式加法 #include <iostream> using namespace std; struct Node { int coef; int exp; Node *next; }; class MExpression { private : Node *first; public : MExpression(); void InsertNode(int coef,int exp); void DeleteNode(int exp); void Add(MExpression me); void PrintAll(); }; MExpression::MExpression() { first->next=NULL; } void MExpression::InsertNode(int coef,int exp) { Node *s=new Node(); Node *p=first; while(p->next!=NULL) p=p->next; s->coef=coef; s->exp=exp; s->next=p->next; p->next=s; } void MExpression::DeleteNode(int exp) { Node *p=first->next; Node *q; q=first; while(p!=NULL) { if (p->exp==exp) break; q=p; p=p->next; } q->next=p->next; delete p; } void MExpression::Add(MExpression me) { int i=0,j=0; Node *p=first->next; Node *q=me.first->next; Node *pp,*qq; pp=first; qq=me.first; while(p&&q) { if (p->exp>q->exp) { InsertNode(q->coef,q->exp); q=q->next; } else if(p->exp==q->exp) { p->coef+=q->coef; p=p->next; q=q->next; } else { p=p->next; } } while(q) { InsertNode(q->coef,q->exp); q=q->next; } } void MExpression::PrintAll() { cout<<"=== coef c exp e ==="<<endl; Node *p=first->next; while(p!=NULL) { cout<<p->coef<<" c "<<p->exp<<" e "; p=p->next; } } int main() { MExpression *me1=new MExpression(); MExpression *me2=new MExpression(); me1->InsertNode(1,1); me1->InsertNode(2,2); me1->InsertNode(3,3); me1->InsertNode(4,4); me2->InsertNode(1,2); me2->InsertNode(2,3); me2->InsertNode(3,4); me2->InsertNode(4,5); me1->Add(*me2); me1->PrintAll(); }
/**//* 循环双链表 */
#include <iostream> using namespace std; struct Node { int data; Node *next; Node *prior; };
class CycleDLList { private : Node *first; public: CycleDLList(); void InsertNode(int data); void DeleteNode(int data); void PrintAll(); };
CycleDLList::CycleDLList() { first->prior=first; first->next=first; } void CycleDLList::InsertNode(int data) { Node *s=new Node(); s->data=data; Node *p=first->next; while(p->next!=first) { p=p->next; } s->prior=p; s->next=p->next; p->next->prior=s; p->next=s; } void CycleDLList::DeleteNode(int data) { Node *p=first->next; Node *q; while(p!=first) { if(p->data==data) break; q=p; p=p->next; } if (p!=first) { q->next=p->next; p->next->prior=q; delete p; } } void CycleDLList:: PrintAll() { Node *p=first->next; Node *q=first->prior; cout<<"p=p->next"<<endl; while(p!=first) { cout<<p->data<<" "; p=p->next; } cout<<endl; cout<<"q=q->prior"<<endl; while(q!=first) { cout<<q->data<<" "; q=q->prior; } } int main() { CycleDLList *cd=new CycleDLList(); cd->InsertNode(5); cd->InsertNode(4); cd->InsertNode(3); cd->InsertNode(2); cd->PrintAll(); cd->DeleteNode(2); cd->PrintAll();
}
2010年8月9日
1#include <iostream> 2using namespace std; 3 4struct Node 5 { 6 int data; 7 Node *next; 8 }; 9 class CycleLinkList 10 { 11 private : 12 Node *first; 13 public : 14 CycleLinkList(); 15 void InsertNode(int data); 16 void DeleteNode(int data); 17 void PrintAll(); 18 }; 19 20 CycleLinkList:: CycleLinkList() 21 { 22 first=first->next; 23 } 24 void CycleLinkList::InsertNode(int data) 25 { 26 Node *s=new Node(); 27 s->data=data; 28 Node *p=first; 29 if (p->next==first) 30 { 31 s->next=first->next; 32 first->next=s; 33 } 34 else 35 { 36 while(p->next!=first) p=p->next; 37 s->next=p->next; 38 p->next=s; 39 } 40 } 41 42 void CycleLinkList::DeleteNode(int data) 43 { 44 Node *p=first->next; 45 Node *q=first->next; 46 while(p!=first) 47 { 48 if (p->data==data) break; 49 else 50 { 51 q=p; 52 p=p->next; 53 } 54 } 55 q->next=p->next; 56 delete p; 57 } 58 void CycleLinkList:: PrintAll() 59 { 60 Node *p=first->next; 61 62 while(p!=first) 63 { 64 cout<<p->data<<" "; 65 p=p->next; 66 } 67 } 68 int main() 69 { 70 CycleLinkList *cl=new CycleLinkList(); 71 cl->InsertNode(3); 72 cl->InsertNode(4); 73 cl->InsertNode(5); 74 cl->InsertNode(6); 75 cl->PrintAll(); 76 cl->DeleteNode(4); 77 cl->PrintAll(); 78 }
2009年10月25日
public class SortDemo{ private int[] sortArray;
public SortDemo(){ sortArray=new int[8]; }
public void swap(int i,int j){ int t=sortArray[i]; sortArray[i]=sortArray[j]; sortArray[j]=t; }
public void insertArray(int pos,int val){ sortArray[pos]=val; }
public void selectSort(){ int t,tt; for(int j=0;j<sortArray.length-1;j++){
for(int i=j+1;i<sortArray.length;i++){
if(sortArray[i]<sortArray[j]) swap(i,j); } } }
public void bubbleSort(){ int exchange=sortArray.length; int bound; while(exchange!=0){ bound=exchange-1; exchange=0; for(int i=0;i<bound;i++){ if(sortArray[i]>sortArray[i+1]){ swap(i,i+1); exchange=i+1; } } }
}
public void insertSort(){ for(int i=1;i<sortArray.length;i++){ int temp=sortArray[i]; int j=i; while(j>0&&temp<sortArray[j-1]){ sortArray[j]=sortArray[j-1]; j--; } sortArray[j]=temp; }
}
public void showResult(){ for(int i =0;i<sortArray.length;i++) System.out.print(sortArray[i]+" "); System.out.println(""); }
public static void main(String[] args){
SortDemo so=new SortDemo(); so.insertArray(0,2); so.insertArray(1,23); so.insertArray(2,22); so.insertArray(3,12); so.insertArray(4,42); so.insertArray(5,32); so.insertArray(6,62); so.insertArray(7,52); so.showResult(); so.selectSort(); so.showResult(); so.bubbleSort(); so.showResult(); so.insertSort(); so.showResult();
} }
2009年5月11日
2009年4月30日
CFileDialog( BOOL bOpenFileDialog, LPCTSTR
lpszDefExt = NULL, LPCTSTR lpszFileName
= NULL, DWORD dwFlags =
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR
lpszFilter = NULL, CWnd* pParentWnd =
NULL );
bOpenFileDialog
Set to TRUE to construct a File Open dialog box or
FALSE to construct a File Save As dialog box. // CFileDialog dlg(false,"TXT",NULL,NULL,"Text file(*.txt)|*.TXT|",NULL); 读取文件: virtual UINT Read( void* lpBuf,
UINT nCount ); throw( CFileException
); lpbuf缓冲区指针 nCount要读的长度 /*
int len=file.GetLength(); char *buffer=new char[len+1]; if(!buffer) { MessageBox("Allocating fail"); } else { try { file.Read(buffer,len); } catch(CFileException *e) { MessageBox("Reading file error"); file.Close(); e->Delete(); return ; } */ 获取当前时间:
current_time=CTime::GetCurrentTime(); str_year.Format("%d",current_time.GetYear()); str_month.Format("%d",current_time.GetMonth()); str_day.Format("%d",current_time.GetDay()); CString m_date=str_year+"-"+str_month+"-"+str_day;
2009年4月29日
单文档:利用画刷填充矩形,很简单 CRect rc(10,10,20,200); CBrush bru; bru.CreateHatchBrush (HS_BDIAGONAL ,RGB(0,0,255)); pDC->SelectObject (&bru); pDC->Rectangle (&rc); pDC->SelectObject (&bru); bru.DeleteObject (); 这是二维的,下面是三维的 三维的直方图,只是照我们手画圆柱体,在二维的基础上在上面和侧面加了平行四边 CRect rc(50,50,100,200); CBrush bru; CPoint r[4]; bru.CreateHatchBrush (HS_BDIAGONAL ,RGB(0,0,255)); pDC->SelectObject (&bru); pDC->Rectangle(&rc); //顶部四边形 r[0].x=50;r[0].y=50; r[1].x =100;r[1].y=0; r[2].x=150;r[2].y=0; r[3].x=100;r[3].y=50; pDC->Polygon (r,4); r[0].x=100;r[0].y=200; r[1].x =150;r[1].y=150; r[2].x=150;r[2].y=0; r[3].x=100;r[3].y=50; pDC->Polygon (r,4); pDC->SelectObject (&bru); bru.DeleteObject ();
2009年4月28日
置于ONDRAW函数里面(单文档):
CBitmap bm,*pbm; //定义位图对象 BITMAP bmMetric; //定义位图结构变量,保存位图的参数如宽,高 bm.LoadBitmap (IDB_BITMAP2); bm.GetBitmap (&bmMetric); //保存位图的参数到结构变量bmMetric
CDC memDC; //定义设备环境类对象 memDC.CreateCompatibleDC (pDC); //创建内存设备环境 pbm=memDC.SelectObject (&bm); //将位图选入设备环境 //将内存设备环境的位图传输到设备环境 pDC->BitBlt (0,0,bmMetric.bmWidth ,bmMetric.bmHeight ,&memDC,0,0,SRCCOPY); memDC.SelectObject (pbm); //恢复原设备环境对象 bm.DeleteObject (); memDC.DeleteDC();
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 |
|
常用链接
留言簿(1)
随笔分类
随笔档案
搜索
最新评论
阅读排行榜
评论排行榜
|
|