引用:
原帖由 kevin88 于 2008-9-24 18:08 发表 
在书上看来的,但有错误,可不可以帮忙看一下,还有后两个头文件在C++中相应是?
这个代码较老,是C的代码。
conio.h在VC中是有的,但是很多函数如gotoxy啊、clrscr什么的都没了。windows.h底下有一个sleep可以替代delay函数。因此解决方法是自写2个函数替代gotoxy和clsscr,然后再加入windows.h,将delay替代为sleep。嗯嗯,加分吧。
复制内容到剪贴板
代码:
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include<time.h>
void clrscr()
{
HANDLE hConsole;
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize; /* number of character cells in the current buffer */
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);
}
void gotoxy(int x, int y)
{
HANDLE hConsole;
COORD point;
point.X = x; point.Y = y;
hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsole,point);
}
int main()
{
int x=15,y=4,depth=20,times=20,m=1,i,j;
for(;depth!=0;)
{
m=-m;
if(m==1)
depth--;
for(i=1;i<=depth;i++)
{
printf("***********\n");
printf("the program will show\n");
printf("the free falling \n");
printf("************\n");
gotoxy(x,y);
printf("***\n");
gotoxy(x,y+1);
printf("***\n");
gotoxy(x,y+2);
printf("***");
for(j=1;j<=times;j++)
Sleep(10);
clrscr();
if(m==-1)
{
y++;
times--;
}
else
{
y--;
times++;
}
}
}
getch();
return 1;
}