示例#1
0
void main()
{
	float i=0;
	int year ,j;
	int a,b;
	int sum;
	/*求两个数的最大公约数*/
	scanf("%d",&a);
	scanf("%d",&b);
	/*/test of function call*/
	printf("%d ",gcd(a,b));
	getch();
/*
	//无实际意义,测试各种情况下的变量定义的合法性
	int mmm;
	{int mmm;}
	if(pi==3.15) int mmm;
	if(pi==3.15) int mmm; else int mmm = 3;
	for(a=0; a<10; a++)	int mmm;
	while(0) int mmm;
	do int mmm; while(0);
	//statements which have no use but legal
*/
	;
	a;
	2343;
	(((b)));
	{}
	{b;}
	/*/statements which may cause errors*/
	for(;0;);
	for(a=0; a<10; a++);
	while(0);
	do ; while(0);

	a=VGA,b=VGAHI;
	/*/绘图,绘制辅导教材中logo程序的示例图形*/
	initgraph(&a,&b,"");

	xOrg = 160 + 60;
	yOrg = 240;
	rot = 0;
	xScale = 80;
	yScale = 80;

	i = -pi;

	x = cos(i);
	y = sin(i);
	CalcCoord();
	moveto(x,y);

	/*/for-loop without the first expression*/
	for(; i<=pi+pi/50; i+=pi/50)
	{
		x = cos(i);
		y = sin(i);
		CalcCoord();
		lineto(x,y);
	}

	i = 0;
	x = (1-1/(10.0/7))*cos(i) + 1/(10.0/7)*cos(-i*((10.0/7)-1));
	y = (1-1/(10.0/7))*sin(i) + 1/(10.0/7)*sin(-i*((10.0/7)-1));
	CalcCoord();
	moveto(x,y);
	for(; i<=20*pi; i+=pi/50)
	{
		x = (1-1/(10.0/7))*cos(i) + 1/(10.0/7)*cos(-i*((10.0/7)-1));
		y = (1-1/(10.0/7))*sin(i) + 1/(10.0/7)*sin(-i*((10.0/7)-1));
		CalcCoord();
		lineto(x,y);
	}

	xOrg = 480-60,	yOrg = 240;
	xScale = 80,	yScale = 80/3;

	j = 0;
	/*/a nest of while-loop and for-loop*/
	while(j<3)
	{
		/*/nest of if-else staments and 'if' without 'else'*/
		if(j==0)
			rot = pi/2 + 0*pi/3;
		else if(j==1)
			rot = pi/2 + 2*pi/3;
		else if(j==2)
			rot = pi/2 - 2*pi/3;
		j++;
		setcolor(j*3+1);
		i = -pi;
		x = cos(i);
		y = sin(i);
		CalcCoord();
		moveto(x,y);
		/*/for-loops without the first and third expression
		//since there's no "break" and "goto" statement in this
		//language, a for-loop without the second expression
		//will be a dead-loop, it cannot be tested here*/
		for(; i<=pi+pi/50;)
		{
			x = cos(i);
			y = sin(i);
			CalcCoord();
			lineto(x,y);
			i+=pi/50;
		}
	}

	getch();
	closegraph();

	/*/print the multiplication table, test of nested for-loops
	//here's two for-loops with all the three expressions*/
	for(j=1; j<=9; j++)
	{
		int k;
		for(k=1; k<=9; k++)
			printf("%d*%d=%2d ",j, k, j*k);
		printf("\n");
	}

	/*/do-while-loop
	//get the sum of the numbers between 1 and 100*/
	j = 0;
	sum = 0;
	do{
		j++;
		sum += j;
	}while(j<100);
	printf("%d ",sum);

	/*/print the leap years between 2000 and 2100
	//quite a complex condition justice*/
	printf("\n");
	for(year=2000; year<=2100; year++)
		if((year%4==0 && year%100!=0) || (year%400==0))
			printf("%d ",year);
}
示例#2
0
void	MainLoop(void)
{
    int	px = 0, py = 0, sx = 9, sy = 9, w = 0, h = 0;
    int	osx, osy, ow, oh;
    
#define		INPUT_TEXT_SIZE		16
    PutParts();
    while (1) {
	char	string_length, input_text[INPUT_TEXT_SIZE];
	
	XFlush(display);
	if (input.button[2]) {
	    eye.x +=  (input.x - WINW / 2) / SCROLL_SPDIV;
	    eye.y +=  (input.y - WINH / 2) / SCROLL_SPDIV;
	    PutParts();
	    XSync(display, 0);
	    if (0 == XEventsQueued(display, QueuedAfterReading)) continue;
	}
	while (XEventsQueued(display, QueuedAfterReading) > 0) {
	    XNextEvent(display, &event);
	    switch (event.type) {
	      case	Expose:
		XCopyArea(display, scratchmap, scratch, gc,
			  0, 0, WINW, WINH, 0, 0);
		break;
	      case	MappingNotify:
		XRefreshKeyboardMapping((XMappingEvent *)&event);
		break;
	      case	KeyPress:
		string_length = XLookupString((XKeyEvent *)&event,
					      input_text, INPUT_TEXT_SIZE, &key, NULL);
		if (1 == string_length) {
		    switch (input_text[0]) {
		      case	'q':
			return;
		      case	'u':
			eye.scale += 256;
			AffineParts();
			break;
		      case	'd':
			if (eye.scale > 256) eye.scale -= 256;
			AffineParts();
			break;
		    }
		    PutParts();
		}
		break;
	      case	MotionNotify:
		if (CompressMNEvent(&event)) {
		    input.x = event.xmotion.x;
		    input.y = event.xmotion.y;
		    break;
		}
		if (scratch != event.xmotion.window) {
		    int	rx, ry, rw, rh;
		    
		    osx = sx;	osy = sy;
		    ow = w;		oh = h;
		    
		    XCopyArea(display, rootmap, cutmap, gc,
			      osx - 9, osy - 9, ow + 10, oh + 10,
			      osx - 9, osy - 9);
		    CutRectangle(px, py, event.xmotion.x, event.xmotion.y,
				 &sx, &sy, &w, &h);
		    XSetForeground(display, gc, black);
		    if (CantCut(w, h)) {
			XDrawRectangle(display, cutmap, gc,
				       sx - 1, sy - 1,
				       w + 1, h + 1);
			XCopyArea(display, rootmap, cutmap, gc,
				  sx, sy, w, h, sx, sy);
		    } else {
			XDrawRectangle(display, cutmap, gc,
				       sx - 9, sy - 9,
				       w + 1, h + 1);
			DrawShadow(cutmap, sx, sy, w, h, 8);
			XCopyArea(display, rootmap, cutmap, gc,
				  sx, sy, w, h, sx - 8, sy - 8);
		    }
		    MaxArea(&rx, &ry, &rw, &rh,
			    sx - 9, sy - 9, w + 10, h + 10,
			    osx - 9, osy - 9, ow + 10, oh + 10);
		    XCopyArea(display, cutmap, cut, gc,
			      rx, ry, rw, rh, rx, ry);
		}
		if (input.button[0] && NULL != in_parts_hd) {
		    if (RAISED == in_parts_hd->stat) {
			CalcCoord(event.xmotion.x - in_parts_hd->bx,
				  event.xmotion.y - in_parts_hd->by,
				  in_parts_hd);
			PutParts();
		    } else {
			PointPart(event.xmotion.x, event.xmotion.y);
			PutParts();
		    }
		} else {
		    PointPart(event.xmotion.x, event.xmotion.y);
		    PutParts();
		}
		break;
	      case	ButtonPress:
		input.button[event.xbutton.button - 1] = 1;
		if (1 == event.xbutton.button && scratch != event.xbutton.window) {
		    px = event.xbutton.x;		py = event.xbutton.y;
		    CutMode(px, py);
		    break;
		}
		if (1 == event.xbutton.button) {
		    RaisePart();
		    PutParts();
		    break;
		}
		break;
	      case	ButtonRelease:
		input.button[event.xbutton.button - 1] = 0;
		sx = sy = 9;	w = h = 0;
		
		if (1 == event.xbutton.button && scratch != event.xbutton.window) {
		    int	sx, sy, w, h;
		    PARTTL	*part;
		    
		    XDestroyWindow(display, cut);
		    CutRectangle(px, py, event.xmotion.x, event.xmotion.y,
				 &sx, &sy, &w, &h);
		    if (!CantCut(w, h)) {
			if (NULL == out_parts_hd) {
			    fputs("parts over flow.\n",
				  stderr);
			    exit(1);
			}
			part = out_parts_hd;
			out_parts_hd = out_parts_hd->n;
			
			part->p = NULL;
			part->n = in_parts_hd;
			if (NULL != in_parts_hd) {
			    in_parts_hd->p = part;
			}
			in_parts_hd = part;
			
			part->stat = NONE;
			part->x = eye.x + rand() % (WINW / 2) - WINW / 2;
			part->y = eye.y + rand() % (WINH / 2) - WINH / 2;
			part->w = w;	part->h = h;
			part->source = XCreatePixmap(display, root, w, h, depth);
			XCopyArea(display, rootmap,
				  part->source, gc,
				  sx, sy, w, h, 0, 0);
			{
			    int	px = 0, py = 0;
			    int	pw = 0, ph = 0;
			    ;
			    CalcView(part, &px, &py,
				     &pw, &ph);
			    AffinePart(part, pw, ph, 1);
			}
			PutParts();
		    }
		    break;
		}
		break;
	    }
	}
    }
}