对于C语言的初学者,基本上只能写一些控制台程序。然而有时候会涉及一些对光标的简单操作,现在一般都是用的VC++6.0,不再支持以前TC中的wherex、wherey、gotoxy等函数了,那么在VC中该怎样做呢?接下来,我就简单讲讲如何在VC中实现以上三个函数。
【以下x、y分别代表列数和行数】
//获取光标的位置x
int wherex()
{
CONSOLE_SCREEN_BUFFER_INFO pBuffer;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &pBuffer);
return (pBuffer.dwCursorPosition.X+1);
}
//获取光标的位置y
int wherey()
{
CONSOLE_SCREEN_BUFFER_INFO pBuffer;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &pBuffer);
return (pBuffer.dwCursorPosition.Y+1);
}
//设置光标的位置
void gotoxy(int x,int y)
{
COORD c;
c.X=x-1;
c.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
###注意:在用这些函数的时候要引用头文件#includ<windows.h>###
下面来看一个具体应用的例子:
#include<stdio.h>
#include<windows.h>
int wherex()
{
CONSOLE_SCREEN_BUFFER_INFO pBuffer;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &pBuffer);
return (pBuffer.dwCursorPosition.X+1);
}
//获取光标的位置y
int wherey()
{
CONSOLE_SCREEN_BUFFER_INFO pBuffer;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &pBuffer);
return (pBuffer.dwCursorPosition.Y+1);
}
//设置光标的位置
void gotoxy(int x,int y)
{
COORD c;
c.X=x-1;
c.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
int main()
{
int x, y;
int select;
gotoxy(10,5);
printf(" 学生C语言成绩管理系统");
gotoxy(15,8);
printf("*********************主菜单*********************");
gotoxy(15,9);
printf("* 1 输入 2 删除 *");
gotoxy(15,10);
printf("* 3 查找 4 修改 *");
gotoxy(15,11);
printf("* 5 插入 6 统计 *");
gotoxy(15,12);
printf("* 7 排序 8 保存 *");
gotoxy(15,13);
printf("* 9 显示 0 退出 *");
gotoxy(15,14);
printf("************************************************");
gotoxy(15,15);
printf("请输入你的选择(0-9):[ ]");
x=wherex();
y=wherey();
gotoxy(x-2,y);
scanf("%d",&select);
return 0;
}
----------- #include <stdlib.h> system("cls"); 是简单了一点,但是确实就可以
命令行执行C语言程序时清屏命令
先导入表头文件
#include <stdlib.h>
然后调用system()函数
括号内的指令执行效果和直接在命令行执行的指令是一致的,所以命令行可以执行的命令,在这里都可以执行
Windows
使用cls指令
system("cls");
MacOS
使用clear指令
system("clear");
当c语言程序执行到这一行时就会把命令行清屏,实际上是跳页。
-----------
关于sleep()函数在windows系统和linux系统下是两个不同的函数,差别较大,但功能是相同的,都是将进程挂起一段时间。
windows系统下函数名为Sleep(),其函数原型为:
#include <windows.h> 函数使用头文件
void Sleep(DWORD dwMilliseconds); 参数为毫秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 参考代码: #include <windows.h> //win头文件 #include<stdio.h> int main() { int i; printf ( "你" ); fflush (stdout); //强制刷新缓存,输出显示 for ( i=0;i<10;i++ ) { Sleep(1000); /* windows 使用Sleep,参数为毫秒 */ printf ( "." ); fflush (stdout); //强制刷新缓存,输出显示 } printf ( "好\n" ); /*输出“你”和“好”之间会间隔10秒,并输出10个点*/ return 0; } |
linux系统下函数名为sleep(),其函数原型为:
#include <unistd.h> 函数使用头文件
unsigned int sleep(unsigned int seconds);参数为毫秒 (如果需要更精确可以用usleep,单位为微秒)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 修改上面的代码,以适应linux系统 #include <unistd.h> //1、linux 头文件 #include<stdio.h> int main() { int i; printf ( "你" ); fflush (stdout); //强制刷新缓存,输出显示 for ( i=0;i<10;i++ ) { sleep(1); /*2、linux 使用sleep,参数为秒*/ printf ( "." ); fflush (stdout); //强制刷新缓存,输出显示 } printf ( "好\n" ); /*输出“你”和“好”之间会间隔10秒,并输出10个点*/ return 0; } |
#include <windows.h>
Sleep函数:
功 能: 执行挂起一段时间
用 法: unsigned sleep(unsigned seconds);
注意:
1.在VC中使用带上头文件#include <windows.h>,在Linux下,gcc编译器中,使用的头文件因gcc版本的不同而不同#include <unistd.h>
2.在VC中,Sleep中的第一个英文字符为大写的"S" ,在linux下不要大写,在标准C中是sleep, 不要大写,简单的说VC用Sleep, 别的一律使用sleep。
3.在VC中,Sleep()里面的单位,是以毫秒为单位,所以如果想让函数滞留1秒的话,应该是Sleep(1000); 在Linux下,sleep()里面的单位是秒,而不是毫秒。
示例:
#include<stdio.h>
#include <windows.h>
int main()
{
int a=100;
Sleep(3000);
printf("%d",a);
return 0;
}
usleep函数:
功能: usleep功能把进程挂起一段时间, 单位是微秒us(百万分之一秒)。
语法: void usleep(int micro_seconds);
返回值: 无
注意:这个函数不能工作在 Windows 操作系统中。
usleep() 与sleep()类似,用于延迟挂起进程。进程被挂起放到reday queue。只是一般情况下,延迟时间数量级是秒的时候,尽可能使用sleep()函数。且此函数已被废除,可使用nanosleep。
如果延迟时间为几十毫秒,或者更小,尽可能使用usleep()函数。这样才能最佳的利用CPU时间。
delay函数:
功 能: 将程序的执行暂停一段时间,单位是毫秒ms(千分之一秒)
用 法: void delay(unsigned milliseconds);
示例:
#include<dos.h>
int main(void)
{
sound(440);
delay(500);
nosound();
return 0;
}
delay()是循环等待,该进程还在运行,占用处理器。
sleep()不同,它会被挂起,把处理器让给其他的进程。