BrainFuck解释器
2011年2月01日 08:57 | Comments(19) | Category:C语言 | Tags:c BrainFuck 解释器
无注释版本:
#include <stdio.h> #include <stdlib.h> #define SIZE 5000 char data[SIZE], code[SIZE]; int ptr, flag; void interpreter(char *ip) { char* re; while (*ip) { switch(*ip++) { case '<': --ptr; break; case '>': ++ptr; break; case '+': ++data[ptr]; break; case '-': --data[ptr]; break; case '.': putchar(data[ptr]); fflush(stdout); break; case ',': data[ptr] = getchar(); fflush(stdout); break; case '[': for (flag=1,re=ip; flag && *ip; ++ip) flag += *ip=='[', flag -= *ip==']'; if(!flag) { ip[-1] = 0; while (data[ptr]) interpreter(re); ip[-1] = ']'; break; } case ']': puts("Unbalancded brackets!"), exit(-3); default : ;//SKIP } if (ptr < 0 || ptr > 100) puts("Out of Range"), exit(-4); } } int main(int argc, char *argv[]) { FILE *fin; int codelength; if (argc != 2) return puts("BrainFuck Interpreter v 0.1\nStar\nUsage:BFI filename"), 0; if ((fin = fopen(argv[1], "r")) == NULL) return puts("Cannot open file!"), -1; fseek(fin, 0, SEEK_END); if ((codelength = ftell(fin)) > SIZE) return puts("The program is too large."), -2; fseek(fin, 0, SEEK_SET); fread(code, codelength, 1, fin); code[codelength] = '\0'; interpreter(code); return 0; }
Windows屏幕飘雪
2011年1月23日 07:46 | Comments(10) | Category:Windows | Tags:Windows API SDK c
#include <windows.h> #include <stdlib.h> #define UNICODE //使用UNICODE字符 #define _UNICODE #define ID_TIME 1 #define NUMOFSNOW 214 //雪花数量 typedef struct tagSNOW { POINT pos; //雪花坐标 int r; //雪花半径 int xSpeed; //x,y速度 int ySpeed; }SNOW, *PSNOW; INT screen_x, screen_y; //屏幕坐标 #pragma comment(linker, "/subsystem: windows") LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) { TCHAR szAppName[] = TEXT ("屏幕飘雪"); //应用程序名 TCHAR szTitle[] = TEXT ("桌面窗口"); //窗口名 HWND hWnd; //窗口句柄 MSG msg; //消息循环 WNDCLASS wc; //窗口类 INT x, y; wc.cbClsExtra = 0; //不用额外窗口类和窗口空间 wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); //黑色背景 wc.hCursor = LoadCursor (hInstance, IDC_ARROW); //默认箭头 wc.hIcon = LoadIcon (hInstance, IDI_APPLICATION); //默认图标 wc.hInstance = hInstance; //程序句柄 wc.lpfnWndProc = WndProc; //窗口过程 wc.lpszClassName = szAppName; //程序名称 wc.lpszMenuName = NULL; //菜单置空 wc.style = CS_HREDRAW | CS_VREDRAW; //水平|垂直对齐 if (!RegisterClass (&wc)) { MessageBox (NULL, TEXT("窗口注册失败!"), TEXT("ERROR"), MB_OK | MB_ICONINFORMATION); return -1; } screen_x = GetSystemMetrics (SM_CXSCREEN); screen_y = GetSystemMetrics (SM_CYSCREEN); hWnd = CreateWindow (szAppName, szTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, screen_x, screen_y, NULL, NULL, hInstance, NULL); if (!hWnd) { MessageBox (NULL, TEXT("创建窗口失败!"), TEXT("ERROR"), MB_OK | MB_ICONINFORMATION); return -2; } SetWindowPos (hWnd, HWND_TOPMOST, 0, 0, screen_x, screen_y, SWP_NOMOVE); while (GetMessage (&msg, NULL, 0, 0) > 0) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HDC hScrDC, hSaveDC, hMemDC; static HBITMAP hSaveBmp, hBmp; static SNOW snows[NUMOFSNOW]; static RECT rtScr; INT i; switch (message) { case WM_CREATE: //创造窗口时 { hScrDC = CreateDC ("DISPLAY", NULL, NULL, NULL); //召唤2个DC,并以此为祭品,召唤2个BMP hSaveDC = CreateCompatibleDC(hScrDC); hMemDC = CreateCompatibleDC(hScrDC); hSaveBmp = CreateCompatibleBitmap(hScrDC, screen_x, screen_y); hBmp = CreateCompatibleBitmap(hScrDC, screen_x, screen_y); SelectObject (hSaveDC, hSaveBmp); SelectObject (hMemDC , hBmp); //将屏幕拷贝到SAVE BitBlt (hSaveDC, 0, 0, screen_x, screen_y, hScrDC, 0, 0, SRCCOPY); SetRect (&rtScr, 0, 0, screen_x, screen_y); srand ((unsigned)GetTickCount()); //随机变量数据 for (i = 0; i < NUMOFSNOW; ++i) { snows[i].pos.x = rand() % screen_x; snows[i].pos.y = rand() % (screen_y / 3); //屏幕1/3以上召唤雪 snows[i].r = rand() % 4 + 1; //1~4 snows[i].xSpeed = rand() % 2 - 1; //0~1 snows[i].ySpeed = rand() % 4 + 2; //2~4 } ShowCursor (FALSE); SetTimer (hWnd, ID_TIME, 12, NULL); } break; case WM_TIMER: { for(i = 0; i < NUMOFSNOW; ++i) { snows[i].pos.x += snows[i].xSpeed; //移动 snows[i].pos.y += snows[i].ySpeed; if (!PtInRect (&rtScr, snows[i].pos)) //如果跑出屏幕外 { snows[i].pos.x = rand() % screen_x; snows[i].pos.y = rand() % (screen_y / 3); } } //将存好的放入内存中 BitBlt (hMemDC, 0, 0, screen_x, screen_y, hSaveDC, 0, 0, SRCCOPY); //创建雪 for(i = 0; i < NUMOFSNOW; ++i) { SelectObject (hMemDC, GetStockObject (NULL_PEN)); Ellipse (hMemDC, snows[i].pos.x, snows[i].pos.y, snows[i].pos.x + 2*snows[i].r, snows[i].pos.y + 2*snows[i].r); } //将雪放在屏幕上 BitBlt (hScrDC, 0, 0, screen_x, screen_y, hMemDC, 0, 0, SRCCOPY); } break; case WM_LBUTTONDOWN: SendMessage (hWnd, WM_CLOSE, 0, 0); break; case WM_DESTROY: { //清理所有垃圾 ShowCursor (TRUE); KillTimer (hWnd, ID_TIME); DeleteObject (hBmp); DeleteObject (hSaveBmp); DeleteDC (hSaveDC); DeleteDC (hScrDC); InvalidateRect (NULL, NULL, TRUE); PostQuitMessage (0); } break; default: return DefWindowProc (hWnd, message, wParam, lParam); } return 0; }
表的3个例子
2010年10月03日 21:00 | Comments(5) | Category:C语言 | Tags:c 双向链表 链表 线性表 数据结构
1.线性表的例子
#include "List.h" int data[5] = {2, 3, 6, 7, 11}; int main(void) { //线性表的简单示例 int i, j, k; SQLIST L; k = InitSqlist(&L, 10); //初始化线性表 if(k == 0) { puts("ERROR"); exit(-1); } for(i = 0; i < 5; ++i) AppendSqlistElem(&L, data[i]); //把data数据放入线性表 TraverseSqlist(L); //遍历线性表 putchar(10); j = SearchSqlist(L, 6); //查找数据6 DeleteSqlistElemAt(&L, j); //删除 InsertSqlistElemAt(&L, 5, j); //插入5 TraverseSqlist(L); DestroySqlist(&L); //销毁线性表 return 0; }
2.单链表的例子
#include "List.h" int data[5] = {2, 3, 6, 7, 11}; int main(void) { //链表示例 int i, j, k; LINKLIST L = NULL; NODEPTR S, SPRE; k = InitLinklist(&L); //初始化链表 if(k == 0) { puts("ERROR"); exit(-1); } for(i = 0; i < 5; ++i) AddLinklistElem(L, data[4-i]); //添加数据 TraverseLinklist(L); //遍历 putchar(10); DeleteLinklistElem(L, 6); //删除元素6 InsertLinklistElemAt(L, 5, 3); //插入元素5 TraverseLinklist(L); DestroyLinklist(&L); //释放表 return 0; }
3.双向链表的例子
#include "List.h" int data[5] = {2, 3, 6, 7, 11}; int main(void) { //双向链表的简单示例 int i, j, k; DBLINKLIST L; DBNODEPTR t; k = InitDBLinklist(&L); //初始化双向链表 if(k == 0) { puts("ERROR"); exit(-1); } for(i = 0, t = L; i < 5; ++i,t = t->next) InsertDBLinklistElemAfter(t, data[i]); //把data数据放入双向链表 TraverseDBLinklist(L); //遍历双向链表 putchar(10); t = SearchDBLinklist(L, 6); //找到6 DeleteDBLinklistElem(t); //删除6 t = SearchDBLinklist(L, 3); //找到3 InsertDBLinklistElemAfter(t, 5); //插入5 TraverseDBLinklist(L); DestroyDBLinklist(&L); //销毁双向链表 return 0; }
表的头文件
2010年10月03日 20:54 | Comments(1) | Category:C语言 | Tags:c 数据结构 线性表 链表 双向链表
/* 数据结构 表的实现 List.h * author : star */ //防止重复定义 #ifndef LIST_H #define LIST_H #include "defs.h" //初始化容量为n的线性表 //时间复杂度O(1) int InitSqlist(SQLIST *L, int n) { L->data = (ElemType *)malloc(n * sizeof(ElemType)); if(L->data == NULL) return 0; L->size = n; L->len = 0; return 1; } //销毁线性表 //时间复杂度O(1) void DestroySqlist(SQLIST *L) { free(L->data); L->data = NULL; L->size = 0; L->len = 0; } //判断线性表的空与满 //时间复杂度O(1) int IsSqlistFull(SQLIST L) { return L.len == L.size; } int IsSqlistEmpty(SQLIST L) { return L.len == 0; } //获取第i个数据 //时间复杂度O(1) //if(i >= 0 || i <= L->len) return L->data[i-1]; //追加数据 //时间复杂度O(1) int AppendSqlistElem(SQLIST *L, ElemType e) { if(IsSqlistFull(*L)) return 0; L->data[L->len] = e; ++L->len; return 1; } //在i(i>=0)处插入数据 //时间复杂度O(n) int InsertSqlistElemAt(SQLIST *L, ElemType e, int i) { int k; if(i > L->len || IsSqlistFull(*L)) return 0; for(k = L->len - 1; k >= i; --k) L->data[k+1] = L->data[k]; L->data[i] = e; ++L->len; return 1; } //删除表中i(i>=0)处的数据 //时间复杂度O(n) int DeleteSqlistElemAt(SQLIST *L, int i) { int k; if(i >= L->len || IsSqlistEmpty(*L)) return 0; for(k = i+1; k < L->len; ++k) L->data[k-1] = L->data[k]; --L->len; return 1; } //遍历线性表 //时间复杂度O(n) void TraverseSqlist(SQLIST L) { int i; for(i = 0; i < L.len; ++i) printf(FORMATSTR, L.data[i]); } //查找数据 //时间复杂度O(n) int SearchSqlist(SQLIST L, ElemType key) { int i; for(i = 0; i < L.len; ++i) if(L.data[i] == key) return i; return -1; } //初始化链表 //时间复杂度O(1) int InitLinklist(LINKLIST *L) { *L = (LINKLIST)malloc(sizeof(NODE)); if(*L == NULL) return 0; (*L)->next = NULL; return 1; } //销毁链表 //时间复杂度O(n) void DestroyLinklist(LINKLIST *L) { NODEPTR p, q; p = *L; while(p) { q = p; p = p->next; free(q); } *L = NULL; } //获取第i(i>=1)个元素 //时间复杂度O(n) NODEPTR GetLinklistElem(LINKLIST L, int i, NODEPTR *pre) { NODEPTR p; int k; p = L; k = 0; while(k < i && p != NULL) { *pre = p; p = p->next; ++k; } if(k < i) *pre = NULL; return p; } //添加数据 //时间复杂度O(1) int AddLinklistElem(LINKLIST L, ElemType e) { NODEPTR p; p = (NODEPTR)malloc(sizeof(NODE)); if(p == NULL) return 0; p->data = e; p->next = L->next; L->next = p; return 1; } //在i(i>=1)处插入数据 //时间复杂度O(n) int InsertLinklistElemAt(LINKLIST L, ElemType e, int i) { NODEPTR p, q, pre; q = (NODEPTR)malloc(sizeof(NODE)); if(q == NULL) return 0; q->data = e; p = GetLinklistElem(L, i, &pre); if(pre == NULL) return 0; q->next = p; pre->next = q; return 1; } //删除表中i(i>=1)处的数据 //时间复杂度O(n) int DeleteLinklistElemAt(LINKLIST L, int i) { NODEPTR p, pre; p = GetLinklistElem(L, i, &pre); if(p == NULL) return 0; pre->next = p->next; free(p); return 1; } //遍历线性表 //时间复杂度O(n) void TraverseLinklist(LINKLIST L) { NODEPTR p; p = L->next; while(p) { printf(FORMATSTR, p->data); p = p->next; } } //查找数据 //时间复杂度O(n) NODEPTR SearchLinklist(LINKLIST L, ElemType e, NODEPTR *pre) { NODEPTR p; *pre = L; p = L->next; while(p != NULL && p->data != e) { *pre = p; p = p->next; } if(p == NULL) *pre = NULL; return p; } //删除表中的数据 //时间复杂度O(n) int DeleteLinklistElem(LINKLIST L, ElemType e) { NODEPTR p, pre; p = SearchLinklist(L, e, &pre); if(p == NULL) return 0; pre->next = p->next; free(p); return 1; } //初始化链表 //时间复杂度O(1) int InitDBLinklist(DBLINKLIST *L) { *L = (DBLINKLIST)malloc(sizeof(DBNODE)); if(*L == NULL) return 0; (*L)->next = (*L)->prior = NULL; return 1; } //销毁双向链表 //时间复杂度O(n) void DestroyDBLinklist(DBLINKLIST *L) { DBNODEPTR p, q; p = *L; while(p) { q = p; p = p->next; free(q); } *L = NULL; } //在双向链表结点后插入元素 //时间复杂度O(1) int InsertDBLinklistElemAfter(DBNODEPTR p, ElemType e) { DBNODEPTR q; q = (DBNODEPTR)malloc(sizeof(DBNODE)); if(q == NULL) return 0; q->data = e; q->next = p->next; q->prior = p; if(p->next != NULL) p->next->prior = q; p->next = q; return 1; } //遍历双向链表 //时间复杂度O(n) void TraverseDBLinklist(DBLINKLIST L) { DBNODEPTR p; p = L->next; while(p) { printf(FORMATSTR, p->data); p = p->next; } } //删除双向链表中的某个结点 //时间复杂度O(1) void DeleteDBLinklistElem(DBNODEPTR p) { DBNODEPTR pre; pre = p->prior; if(pre != NULL) pre->next = p->next; if(p->next != NULL) p->next->prior = pre; free(p); } //查找双向链表的数据 //时间复杂度O(n) DBNODEPTR SearchDBLinklist(DBLINKLIST L, ElemType e) { DBNODEPTR p; p = L->next; while(p != NULL && p->data != e) p = p->next; return p; } #endif
数据结构头文件Ver1.0
2010年10月03日 20:43 | Comments(27) | Category:C语言 | Tags:c 数据结构 线性表 链表 静态表 广义表
/* 数据结构 全部结构定义文件 defs.h * author : star */ //引入所有需要的头文件 #include <stdio.h> #include <stdlib.h> //防止重复定义 #ifndef DEF_H #define DEF_H //定义执行状态返回结果 #define TRUE 1 //成功 #define FALSE 0 //失败 #ifndef FORMATSTR #define FORMATSTR "%d " //输出格式 typedef int ElemType; //基本数据类型 #endif //线性表 typedef struct { ElemType *data; //数据 int size; //容量 int len; //长度 }SQLIST; //单链表 typedef struct link_node { ElemType data; //数据 struct link_node *next; //结点 }NODE, *NODEPTR, *LINKLIST; //双向链表 typedef struct dlink_node { ElemType data; //数据 struct dlink_node *next, *prior; //前后结点 }DBNODE, *DBNODEPTR, *DBLINKLIST; //循环单向链表 原理同单链表 //循环双向链表 原理同双向链表 //静态链表 #define SLEN 512 //静态链表长度 typedef struct slink_node { ElemType data; //数据 int next; //指针 }SNODE, *SLINKLIST; /*静态链表是给没有指针的语言写的,C语言有指针,* *而且静态链表不实用,定义多而杂,因此不写了 */ //广义表 enum{ATOM,LIST}; typedef struct glist_node { int tag; //ATOM或LIST union { ElemType data; //ATOM struct glist_node *head; //LIST }item; struct glist_node *next; //结点 }GLNODE, *GLIST; #endif