首页 文章 精选 留言 我的

精选列表

搜索[c 14],共10000篇文章
优秀的个人博客,低调大师

C#解析JSON

使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/)。下载后添加dll引用就能用。 首先添加引用:using Newtonsoft.Json; 1.Json字符串普通格式解析(常用) string jsonText = "{\"one\":\"西瓜\",\"two\":\"南瓜\"}"; JObject aaa = (JObject)JsonConvert.DeserializeObject(jsonText); string one = aaa["one"].ToString(); string two = aaa["two"].ToString(); 2.Json字符串嵌套格式解析 string jsonText = "{\"fruit\":{\"name\":\"西瓜\",\"name_en\":\"watermelon\"}}"; JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); string name = jo["fruit"]["name"].ToString(); string name_en = jo["fruit"]["name_en"].ToString(); 3.Json字符串数组格式解析 string jsonArrayText = "[{'a':'a1','b':'b1'},{'a':'a2','b':'b2'}]"; //"[{'a':'a1','b':'b1'}]即使只有一个元素,也需要加上[] string jsonArrayText = "[{\"a\":\"a1\",\"b\":\"b1\"},{\"a\":\"a2\",\"b\":\"b2\"}]"; //上面写法和此写法效果一样 JArray jArray = (JArray)JsonConvert.DeserializeObject(jsonArrayText);//jsonArrayText必须是带[]数组格式字符串 string str = jArray[0]["a"].ToString();

优秀的个人博客,低调大师

C++小例子

#include <iostream> #include <conio.h> int main(){ std::cout<<"Enter two numbers:"<<std::endl; int v1,v2; std::cin>>v1>>v2; int lower,upper; if(v1<=v2){ lower = v1; upper = v2; }else{ lower = v2; upper = v1; } int sum = 0; for(int val = lower;val<=upper;++val){ sum +=val; } std::cout<<"Sum of "<<lower<<"to"<<upper <<" is " <<sum<<std::endl; getch(); return 0; } 输入两个数 计算出,他们之间的和 例如输入1、3 计算1+2+3 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/3723660.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(十八)

下了份《46家公司笔试题》做做,好久没接触这些基本知识了,熟悉下 1.完成下列程序 * *.*. *..*..*.. *...*...*...*... *....*....*....*....*.... *.....*.....*.....*.....*.....*..... *......*......*......*......*......*......*...... *.......*.......*.......*.......*.......*.......*.......*....... 复制代码 #include <iostream> using namespace std; const int ROWS = 8;//行数 void DoPrint() { int i,j,k; for(i=1;i<=ROWS;++i) { for(j=1;j<=i;++j) { cout<<"*"; for(k=1;k<=i-1;++k)cout<<"."; } cout<<endl; } } int main() { DoPrint(); return 0; } 复制代码 2.完成程序,实现对数组的降序排序 复制代码 #include <iostream> using namespace std; void printArray(int a[],int n) { for(int i=0;i<n;++i) cout<<a[i]<<"\t"; cout<<endl; } int partion(int a[],int left,int right) {//分割 int temp = a[left]; while (left<right) { while (left<right && a[right]<=temp)--right; a[left] = a[right]; while (left<right && a[left]>=temp)++left; a[right] = a[left]; } a[left] = temp; return left; } void quicksort(int a[],int left,int right) {//快速排序 if(left>=right) return; int pivotloc; pivotloc = partion(a,left,right); quicksort(a,left,pivotloc-1); quicksort(a,pivotloc+1,right); } int main() { int array[] = {45,56,76,234,1,34,23,2,3}; int size = sizeof(array)/sizeof(array[0]); quicksort(array,0,size-1); printArray(array,size); return 0; } 复制代码 3,费波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他 方法,但要说明你选择的理由。 复制代码 #include <iostream> using namespace std; int Pheponatch(int N) {//返回斐波那契数列第N项 int a[100]={1,1}; for (int i=2;i<N;++i) { a[i] = a[i-1]+a[i-2]; } return a[N-1]; } int main() { cout<<Pheponatch(4)<<endl; return 0; } 复制代码 4,错误原因:指针未初始化, 改正后的代码: 复制代码 #include <stdio.h> #include <malloc.h> #include <iostream> using namespace std; struct TNode { TNode* left; TNode* right; int value; }; TNode* root=NULL; void append(int N) { TNode* NewNode=(TNode *)malloc(sizeof(TNode)); NewNode->value=N; NewNode->left = NULL; NewNode->right = NULL; if(root==NULL) { root=NewNode; return; } else { TNode* temp; temp=root; while((N>=temp->value && temp->left!=NULL) || (N<temp->value && temp->right!=NULL)) { while(N>=temp->value && temp->left!=NULL) temp=temp->left; while(N<temp->value && temp->right!=NULL) temp=temp->right; } if(N>=temp->value) temp->left=NewNode; else temp->right=NewNode; return; } } void printTree(TNode* t) { if (t!=NULL) { printf("%d\n",t->value); printTree(t->left); printTree(t->right); } } int main() { append(63); append(45); append(32); append(77); append(96); append(21); append(17); // Again, 数字任意给出 printTree(root); return 0; } 复制代码 5,设计函数 int atoi(char *s) 。 复制代码 #include <iostream> using namespace std; int my_aoti(char* s) {//字符串转化为整数 int len = strlen(s); int result = 0; for (int i=0;i<len;++i) { result = result*10+s[i]-'0'; } return result; } int main() { char* str = "123"; int num = my_aoti(str); cout<<num<<endl; return 0; } 复制代码 6,实现双向链表删除一个节点P,在节点P 后插入一个节点,写出这两个函数 复制代码 //双向链表节点 struct DbLinkNode { struct DbLinkNode* prev;//前一个节点 struct DbLinkNode* next;//后一个节点 int value; }; bool Delete(DbLinkNode* head,int num) {//在双向链表(带头节点)中删除第一个值为num的节点 if (head->next!=NULL) {//表中有节点存在 struct DbLinkNode* pre = head,p = head->next; while (p!=NULL&&p->value!=num) { pre = p; p = p->next; } if (p==NULL) {//没找到 return false; } else if (p->next==NULL) {//待删除的是最后一个节点 pre->next = NULL; delete p; p = NULL; } else {//待删除的不是最后一个 pre->next = p->next; p->next->prev = pre; delete p; p = NULL; } return true; } else { return false; } } bool Insert(struct DbLinkNode* head,int target,int num) {//在节点target后插入节点num if (head->next!=NULL) {//表中有节点存在 struct DbLinkNode* p = head->next;//指向第一个节点 struct DbLinkNode* newNode = (struct DbLinkNode*)malloc(sizeof(DbLinkNode)); newNode->value = num; newNode->next = NULL; newNode->prev = NULL; while (p!=NULL && p->value!=num) { p = p->next; } if (p==NULL) { free(newNode); newNode = NULL; return false; } else if (p->next==NULL) {//目标节点是最后一个节点,新节点插入为尾节点 p->next = newNode; newNode->prev = p; } else {//目标节点不是最后一个 newNode->next = p->next; p->next->prev = newNode; p->next = newNode; newNode->prev = p; } return true; } else { return false; } } 复制代码 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2009/02/18/1393441.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C#函数重载

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static int MaxValue(int[] intArray) { int maxVal = intArray[0]; for (int i = 1; i < intArray.Length; i++) { if (intArray[i] > maxVal) { maxVal = intArray[i]; } } return maxVal; } /// <summary> /// 函数重载了 /// </summary> /// <param name="doubleArray"></param> /// <returns></returns> static double MaxValue(double[] doubleArray) { double maxVal = doubleArray[0]; for (int i = 1; i < doubleArray.Length; i++) { if (doubleArray[i] > maxVal) { maxVal = doubleArray[i]; } } return maxVal; } static void Main(string[] args) { int[] myArray = { 1, 3, 5, 8, 2, 10, 9, 100, 40 }; int maxVal = MaxValue(myArray); Console.WriteLine("最大值为:{0}", maxVal); double[] doubleArray = { 1, 3, 5, 8, 2, 10, 9.2, 100.11, 40 }; double maxDoubleVal = MaxValue(doubleArray); Console.WriteLine("最大值为:{0}", maxDoubleVal); Console.ReadKey(); } } } 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6748280.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(十)

1.1 找出第K大的数 方法1: #include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main() { int data[] = {3,54,254,52,13,667,234,67,256,78,467,32,65,324,889,34,5}; int len = sizeof(data)/sizeof(int); vector<int> v1(data,data+len); ostream_iterator<int> out(cout," "); copy(v1.begin(),v1.end(),out); cout<<endl; sort(v1.begin(),v1.end(),greater<int>()); copy(v1.begin(),v1.end(),out); cout<<endl; int k; cin>>k; cout<<v1[k-1]<<endl; system("pause"); return 0; } 方法2: #include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; class LessThan { public: LessThan(int val):value(val){} ~LessThan(){} bool operator()(int rhs) { return rhs<=value; } private: int value; }; int main() { int data[] = {3,54,254,52,475,667,234,67,256,78,467,32,65,324,889,34,5}; int len = sizeof(data)/sizeof(int); int k; cin>>k; list<int> list1(data,data+k); ostream_iterator<int> out(cout," "); list1.sort(greater<int>());//对前K个数进行排序 list<int>::iterator pos; for(int i=k;i<len;++i) { if(data[i]>list1.back()) {//比最后一个元素大 pos = find_if(list1.begin(),list1.end(),LessThan(data[i]));//找到第一个比要插入值小的元素位置 list1.insert(pos,data[i]);//插入新值 list1.pop_back();//删除最后一个多余的元素 } } copy(list1.begin(),list1.end(),out); cout<<endl; cout<<"第"<<k<<"大的数是: "<<endl; int count = 1; for(pos = list1.begin();pos!=list1.end();++pos,++count) { if(count==k) cout<<(*pos)<<endl; } system("pause"); return 0; } 方法3:用最大堆排序 #include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; template<typename T> void HeapAdjust(vector<T> &v,size_t start,size_t end) { T tmp = v[start]; size_t s = start; for(size_t j=(2*s+1);j<=end;j=(2*j+1)) { if(j<end&&v[j+1]>v[j]) j++; if(tmp<v[j]) { v[s] = v[j]; s = j; } } v[s] = tmp; } template<typename T> void Swap(T &a,T &b) { T tmp = a; a = b; b = tmp; } template<typename T> void HeapSort(vector<T> &v) { int i = 0,j=0; //建最大堆 for(i=(v.size()-1)/2;i>=0;--i) { HeapAdjust(v,i,v.size()-1); } cout<<"输入K:"<<endl; int k; cin>>k; for(i=v.size(),j=k;i>1&&j>0;--i,--j) { Swap(v[0],v[i-1]); if(j==1) { cout<<v[i-1]; } else { HeapAdjust(v,0,i-2); } } } int main() { int data[]={93,5,233,55,3,67,2,67,32,6,89,355}; int len = sizeof(data)/sizeof(int); vector<int> v1(data,data+len); HeapSort(v1); return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/03/31/1131862.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(五)

问题:在窗体上画一个圆,并且可以用鼠标拖动它在窗体上运动到任何位置 分析: 一个圆是由它的外接矩阵决定的,而外接矩阵可以由矩阵的左上角点和矩阵的高度和宽度表示,这里显然要求外接矩阵的大小是不变的,变化的仅仅是外接矩阵的左上角点,所以要处理的就是矩阵左上角点的变化。 为此设置类的成员变量如下: CPoint m_pointTopLeft;//外接矩形左上角点 const CSize m_sizeEcllipse;//外接矩形大小 CSize m_sizeOffSet;//光标距离左上角点距离 bool m_bCaptured;//鼠标是否处于被捕获状态 其中矩阵大小是常量,在初始化的时候指明,这里我指定左上角点为坐标原点(0,0),高度100,宽度100,初始的鼠标光标和原点重合(即两者距离都是0),并且鼠标未被捕获。 CTestView::CTestView():m_sizeEcllipse(100,100),m_pointTopLeft(0,0),m_sizeOffSet(0,0) { this->m_bCaptured = FALSE;//初始状态为未捕获 } 接下来就是在OnDraw里面画圆了,因为m_pointTopLeft和m_sizeEcllipse已经唯一决定了圆的位置与大小,因此直接利用这两个变量进行绘图就可以了。 void CTestView::OnDraw(CDC* pDC) { CE05Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CBrush brushHatch(HS_DIAGCROSS, RGB(255, 0, 0)); pDC->SelectObject(&brushHatch);//选择刷子 pDC->Ellipse(CRect(m_pointTopLeft, this->m_sizeEcllipse)); } 最后就是如何响应鼠标的三个事件:down,move,up了,再这里主要就是处理左上角点的变化,在down的时候,由于第一次点击圆内的点时就确定了鼠标光标与初始左上角点的距离,在以后的move中只需要用当前所在的point减去确定的m_sizeOffSet就可以确定此时的新左上角点,并且在move中利用前后两次的外接矩阵进行重画就可以了,最后在up中把鼠标的捕获释放掉。 void CTestView::OnLButtonDown(UINT nFlags, CPoint point) { CRect rt(this->m_pointTopLeft,this->m_sizeEcllipse); CRgn rg; CClientDC dc(this); OnPrepareDC(&dc); dc.LPtoDP(&rt);//转换到设备坐标 rg.CreateEllipticRgnIndirect(&rt); if(rg.PtInRegion(point)) {//在圆内单击左键了 this->SetCapture();//捕获光标 CPoint pointTopLeft(this->m_pointTopLeft);//前一次的左上角 this->m_sizeOffSet = point-pointTopLeft;//第一次点左键时光标距离左上角的距离 this->m_bCaptured = TRUE;//捕获标志设为true ::SetCursor(::LoadCursor(NULL,IDC_CROSS));//更改光标形状 } } void CTestView::OnLButtonUp(UINT nFlags, CPoint point) { if(this->m_bCaptured) {//光标处于捕获状态中 this->m_bCaptured = FALSE; //捕获标志设为false ::ReleaseCapture();//释放鼠标捕获 } } void CTestView::OnMouseMove(UINT nFlags, CPoint point) { if(this->m_bCaptured) {//光标处于捕获状态中 CClientDC dc(this); OnPrepareDC(&dc); CRect oldRect(this->m_pointTopLeft,this->m_sizeEcllipse);//老的外接矩形 dc.LPtoDP(&oldRect); this->InvalidateRect(&oldRect,TRUE);//强制重画 this->m_pointTopLeft = point-this->m_sizeOffSet;//新的左上角点 CRect newRect(this->m_pointTopLeft,this->m_sizeEcllipse);//新外接矩形 dc.LPtoDP(&newRect); this->InvalidateRect(&newRect,TRUE); //强制重画 } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/06/11/778954.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(十一)

1, #include <d3d9.h> #pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h> #pragma warning( default : 4996 ) //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Most parameters are // zeroed out. We set Windowed to TRUE, since we want to do D3D in a // window, and then set the SwapEffect to "discard", which is the most // efficient method of presenting the back buffer to the display. And // we request a back buffer format that matches the current desktop display // format. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create the Direct3D device. Here we are using the default adapter (most // systems only have one, unless they have multiple graphics hardware cards // installed) and requesting the HAL (which is saying we want the hardware // device rather than a software one). Software vertex processing is // specified since we know it will work on all cards. On cards that support // hardware vertex processing, though, we would see a big performance gain // by specifying hardware vertex processing. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { //释放D3D设备对象 if( g_pd3dDevice != NULL) g_pd3dDevice->Release(); //释放D3D对象 if( g_pD3D != NULL) g_pD3D->Release(); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { if( NULL == g_pd3dDevice ) return; // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Rendering of scene objects can happen here // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; case WM_PAINT: Render(); ValidateRect( hWnd, NULL ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { //初始化成功 // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; while(true) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {//消息处理 if(msg.message==WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else {//空闲时绘制 Render(); } } } UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; } 2, #include <d3d9.h> #pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h> #pragma warning( default : 4996 ) LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices 顶点缓冲区 // A structure for our custom vertex type struct CUSTOMVERTEX { FLOAT x, y, z, rhw; // The transformed position for the vertex DWORD color; // The vertex color }; // Our custom FVF, which describes our custom vertex structure #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) HRESULT InitD3D( HWND hWnd ) { // Create the D3D object. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create the D3DDevice if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here return S_OK; } HRESULT InitVB() { // Initialize three vertices for rendering a triangle CUSTOMVERTEX vertices[] = { { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, }, { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, }, }; // Create the vertex buffer. Here we are allocating enough memory // (from the default pool) to hold all our 3 custom vertices. We also // specify the FVF, so the vertex buffer knows what data it contains. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) { return E_FAIL; } // Now we fill the vertex buffer. To do this, we need to Lock() the VB to // gain access to the vertices. This mechanism is required becuase vertex // buffers may be in device memory. VOID* pVertices; if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, vertices, sizeof(vertices) ); g_pVB->Unlock(); return S_OK; } VOID Cleanup() { if( g_pVB != NULL ) g_pVB->Release(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); } VOID Render() { // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Draw the triangles in the vertex buffer. g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 ); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { //初始化成功 // Create the vertex buffer if( SUCCEEDED( InitVB() ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; while(true) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {//消息处理 if(msg.message==WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else {//空闲时绘制 Render(); } } } } UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/04/26/1172475.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(七)

Dll的示例代码 namespace MathFuncs { class MyMathFuncs { public: // Returns a + b static __declspec(dllexport) double Add(double a, double b); // Returns a - b static __declspec(dllexport) double Subtract(double a, double b); // Returns a * b static __declspec(dllexport) double Multiply(double a, double b); // Returns a / b // Throws DivideByZeroException if b is 0 static __declspec(dllexport) double Divide(double a, double b); }; } #include "MathFuncsDll.h" #include <stdexcept> using namespace std; namespace MathFuncs { double MyMathFuncs::Add(double a, double b) { return a + b; } double MyMathFuncs::Subtract(double a, double b) { return a - b; } double MyMathFuncs::Multiply(double a, double b) { return a * b; } double MyMathFuncs::Divide(double a, double b) { if (b == 0) { throw new invalid_argument("b cannot be zero!"); } return a / b; } } // MyExecRefsDll.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "MathFuncsDll.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { double a = 7.4; int b = 99; cout << "a + b = " << MathFuncs::MyMathFuncs::Add(a, b) << endl; cout << "a - b = " << MathFuncs::MyMathFuncs::Subtract(a, b) << endl; cout << "a * b = " << MathFuncs::MyMathFuncs::Multiply(a, b) << endl; cout << "a / b = " << MathFuncs::MyMathFuncs::Divide(a, b) << endl; cin>>a; return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/11/16/961857.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(八)

全排列问题: #include "stdafx.h" #include <math.h> #include <stdio.h> #include <iostream> using namespace std; void swap(int& a,int& b) { int tmp; tmp = a; a = b; b = tmp; } void Rerange(int a[],int m,int n) { if(m==n) { for(int j=0;j<=m;++j) cout<<a[j]<<'\t'; cout<<endl; } else { for(int i=m;i<=n;++i) { swap(a[m],a[i]); Rerange(a,m+1,n); swap(a[m],a[i]); } } } int main(void) { int a[] = {1,2,3},tmp; Rerange(a,0,2); cin>>tmp; return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/11/19/963829.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(一)

一个3D向量类 // Vertex3D.h: interface for the Vertex3D class. // ////////////////////////////////////////////////////////////////////// class Vertex3D {//3维向量类 private: double x,y,z; public: Vertex3D(); Vertex3D(double tx,double ty,double tz); Vertex3D(const Vertex3D& vt);//拷贝构造函数 Vertex3D& operator = (const Vertex3D &vt);//重载赋值运算符 bool operator == (const Vertex3D &vt)const;//重载"=="运算符 bool operator != (const Vertex3D &vt)const;//重载"!="运算符 Vertex3D operator -()const;//负向量 Vertex3D operator - (const Vertex3D &vt)const;//向量减法 Vertex3D operator + (const Vertex3D &vt)const;//向量加法 Vertex3D operator * (double fac)const;//与标量相乘 Vertex3D operator / (double fac)const;//与标量相除 Vertex3D& operator += (const Vertex3D &vt); Vertex3D& operator -= (const Vertex3D &vt); Vertex3D& operator *= (double fac);//与标量相乘 Vertex3D& operator /= (double fac);//与标量相除 double operator * (const Vertex3D &vt)const;//向量点乘 void reset(); void normalize();//向量单位化 double VertexLength()const;//向量模 virtual ~Vertex3D(); friend Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b); friend double Distance(const Vertex3D &a,const Vertex3D &b); }; // Vertex3D.cpp: implementation of the Vertex3D class. // ////////////////////////////////////////////////////////////////////// #include "Vertex3D.h" #include <math.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Vertex3D::Vertex3D() { this->x = 0.0f; this->y = 0.0f; this->z = 0.0f; } Vertex3D::Vertex3D(double tx,double ty,double tz) { this->x = tx; this->y = ty; this->z = tz; } Vertex3D::Vertex3D(const Vertex3D &vt) { this->x = vt.x; this->y = vt.y; this->z = vt.z; } Vertex3D& Vertex3D::operator = (const Vertex3D& vt) { this->x = vt.x; this->y = vt.y; this->z = vt.z; return *this; } bool Vertex3D::operator == (const Vertex3D& vt)const {//判断向量是否相等 return this->x==vt.x&&this->y==vt.y&&this->z==vt.z; } bool Vertex3D::operator != (const Vertex3D& vt)const { return this->x!=vt.x&&this->y!=vt.y&&this->z!=vt.z; } Vertex3D Vertex3D::operator - ()const {//负向量 return Vertex3D(-x,-y,-z); } Vertex3D Vertex3D::operator - (const Vertex3D& vt)const {//向量减法 return Vertex3D(x-vt.x,y-vt.y,z-vt.z); } Vertex3D Vertex3D::operator + (const Vertex3D& vt)const {//向量加法 return Vertex3D(x+vt.x,y+vt.y,z+vt.z); } Vertex3D Vertex3D::operator * (double fac)const {//与标量乘法 return Vertex3D(x*fac,y*fac,z*fac); } Vertex3D Vertex3D::operator / (double fac)const {//与标量相除 return Vertex3D(x/fac,y/fac,z/fac); } Vertex3D& Vertex3D::operator += (const Vertex3D &vt) { this->x += vt.x; this->y += vt.y; this->z += vt.z; return *this; } Vertex3D& Vertex3D::operator -= (const Vertex3D &vt) { this->x -= vt.x; this->y -= vt.y; this->z -= vt.z; return *this; } Vertex3D& Vertex3D::operator *= (double fac) { this->x *= fac; this->y *= fac; this->z *= fac; return *this; } Vertex3D& Vertex3D::operator /= (double fac) { this->x /= fac; this->y /= fac; this->z /= fac; return *this; } void Vertex3D::reset() {//置为零向量 this->x = 0.0f; this->y = 0.0f; this->z = 0.0f; } double Vertex3D::VertexLength()const {//向量长度 double tmp = x*x+y*y+z*z; return sqrt(tmp); } void Vertex3D::normalize() {//向量单位化 double len = this->VertexLength();//获取向量长度 if(len>0.0f) { double tmp = 1.0f/len; this->x *= tmp; this->y *= tmp; this->z *= tmp; } } double Vertex3D::operator * (const Vertex3D &vt)const {//向量点乘 return x*vt.x+y*vt.y+z*vt.z; } Vertex3D::~Vertex3D() { } Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b) {//向量叉乘 return Vertex3D(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x); } double Distance(const Vertex3D &a,const Vertex3D &b) {//向量距离 double dx = a.x - b.x,dy = a.y - b.y,dz = a.z - b.z; return sqrt(dx*dx+dy*dy+dz*dz); } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/04/29/732444.html,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册