C语言数字动态显示问题

陌上_桑 2013-08-19 03:45:58
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>
#include<time.h>
struct SevenD_point
{
int X,Y,Z_C,Z_N,Z_P,U_P,U_M;
};
int check(char *input,char *container)
{
int i,j;
for(i=0,j=0;i<7;i++)
{
if(strcmp(input,container+40*i)==0)
{
if(j!=6)
{
printf("System check that you wanna %s.\n",container+40*i);
break;
}
else
{
printf("Program Terminated!");
break;
}
}
else j++;
}
if(j==7)
printf("System cannot check what you wanna do,please check input.\n");
return j;
}
void Initialize()
{
long Max(struct SevenD_point *p);
void disp(struct SevenD_point *p);
struct SevenD_point Present={rand(),rand(),rand(),rand(),rand(),rand(),rand()},*p;
int i;
long max;
p=&Present;
printf("Present position:(%d,%d,%d,%d,%d,%d,%d).\n",\
Present.X,Present.Y,Present.Z_C,Present.Z_N,Present.Z_P,Present.U_P,Present.U_M);
max=Max(p);
printf("Now start to decrease %ld times:\n",max+1);
for(;max>0;max--)
{
disp(p);
}
printf("\nInitialize position accomplished!\nAnd now position is(%d,%d,%d,%d,%d,%d,%d).\n",\
Present.X,Present.Y,Present.Z_C,Present.Z_N,Present.Z_P,Present.U_P,Present.U_M);
delay(500);
}
void Change()
{
}
void Add()
{
}
void Oscillate()
{
}
void Small()
{
}
void Big()
{
}
long Max(struct SevenD_point *p)
{
long z_max,u_max,zu_max,xy_max,max;
z_max=((p->Z_C)>(p->Z_N))?(p->Z_C):(p->Z_N);
z_max=(z_max>(p->Z_P))?z_max:(p->Z_P);
u_max=((p->U_P)>(p->U_M))?(p->U_P):(p->U_M);
zu_max=(z_max>u_max)?z_max:u_max;
xy_max=((p->X)>(p->Y))?(p->X):(p->Y);
max=zu_max+xy_max;
return max;
}
void disp(struct SevenD_point *p)
{
int i;
long z,u,x,y;
z=(p->Z_C)+(p->Z_N)+(p->Z_P);
u=(p->U_P)+(p->U_M);
x=(p->X),y=(p->Y);
if(z>0)
{
if((p->Z_C)>0)
(p->Z_C)--;
if((p->Z_N)>0)
(p->Z_N)--;
if((p->Z_P)>0)
(p->Z_P)--;
}
if(u>0)
{
if((p->U_P)>0)
(p->U_P)--;
if((p->U_M)>0)
(p->U_M)--;
}
if((z==0)&&(u==0))
{
if(x>0)
(p->X)--;
if(y>0)
(p->Y)--;
}
printf("Present Point (7-Dimension):(%5d,%5d,%5d,%5d,%5d,%5d,%5d)",\
p->X,p->Y,p->Z_C,p->Z_N,p->Z_P,p->U_P,p->U_M);
for(i=0;i<71;i++)
printf("\b");
}


void main()
{
char func[7][40]={"Initialize position","Change medicine",\
"Add medicine","Oscillate sample",\
"Fetch and Spit sample","All over in",\
"Abort"},excute[40];
int choose=7;
srand((unsigned)time(NULL));
while(choose==7)
{
printf("Please tell what you wanna do:");
gets(excute);
choose=check(excute,func[0]);
}
if(choose==0)
Initialize();
}
这是源程序,不完整,但是可以运行

奇怪的是每次运行提示输入后,输入Initialize position,执行的结果都不一样。按照我的想法是每次闪烁到最后都应该是0才对。请高手看看,能不能指导指导什么地方的问题,小弟不胜感激。

还有就是C的转义字符里面有没有哪个是直接回到上一行的?就是说光标本来在第六行,有什么转义字符能让他回到第五行?
...全文
267 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2013-08-21
  • 打赏
  • 举报
回复
标准库提供的都是流式IO,做不到你想要的效果。 估计你想要的效果要么用GUI,要么用终端IO这样的API才能解决。 代码太长,没有重点,懒得看了。
陌上_桑 2013-08-20
  • 打赏
  • 举报
回复
都没有人看看吗?
陌上_桑 2013-08-20
  • 打赏
  • 举报
回复
有没有人啊,来看看啊,多谢指导啊。谢谢啦。
陌上_桑 2013-08-20
  • 打赏
  • 举报
回复
我用的是Win7 32位 编译器是Turbo C 3.0
赵4老师 2013-08-20
  • 打赏
  • 举报
回复
需要在CONFIG.SYS中添加一行 DEVICE=ANSI.SYS 并重启DOS
赵4老师 2013-08-20
  • 打赏
  • 举报
回复
In the following list of ANSI escape sequences, the abbreviation ESC represents the ASCII escape character 27 (1Bh), which appears at the beginning of each escape sequence. ESC[PL;PcH Cursor Position: Moves the cursor to the specified position (coordinates). If you do not specify a position, the cursor moves to the home position--the upper-left corner of the screen (line 0, column 0). This escape sequence works the same way as the following Cursor Position escape sequence. ESC[PL;Pcf Cursor Position: Works the same way as the preceding Cursor Position escape sequence. ESC[PnA Cursor Up: Moves the cursor up by the specified number of lines without changing columns. If the cursor is already on the top line, ANSI.SYS ignores this sequence. ESC[PnB Cursor Down: Moves the cursor down by the specified number of lines without changing columns. If the cursor is already on the bottom line, ANSI.SYS ignores this sequence. ESC[PnC Cursor Forward: Moves the cursor forward by the specified number of columns without changing lines. If the cursor is already in the rightmost column, ANSI.SYS ignores this sequence. ESC[PnD Cursor Backward: Moves the cursor back by the specified number of columns without changing lines. If the cursor is already in the leftmost column, ANSI.SYS ignores this sequence. ESC[s Save Cursor Position: Saves the current cursor position. You can move the cursor to the saved cursor position by using the Restore Cursor Position sequence. ESC[u Restore Cursor Position: Returns the cursor to the position stored by the Save Cursor Position sequence. ESC[2J Erase Display: Clears the screen and moves the cursor to the home position (line 0, column 0). ESC[K Erase Line: Clears all characters from the cursor position to the end of the line (including the character at the cursor position).
陌上_桑 2013-08-19
  • 打赏
  • 举报
回复
首先谢谢二楼,握着这系统不能include windows.h,画图也画不了哦。 我想知道的是我的程序哪个地方是有问题的。
赵4老师 2013-08-19
  • 打赏
  • 举报
回复
仅供参考
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(void);
void ClearConsoleToColors(int ForgC, int BackC);
void SetColorAndBackground(int ForgC, int BackC);
void SetColor(int ForgC);
void HideTheCursor(void);
void ShowTheCursor(void);

int main(int argc, char* argv[])
{
   HideTheCursor();
   ClearConsoleToColors(15, 1);
   ClearConsole();
   gotoXY(1, 1);
   SetColor(14);
   printf("This is a test...\n");
   Sleep(5000);
   ShowTheCursor();
   SetColorAndBackground(15, 12);
   ConPrint("This is also a test...\n", 23);
   SetColorAndBackground(1, 7);
   ConPrintAt(22, 15, "This is also a test...\n", 23);
   gotoXY(0, 24);
   SetColorAndBackground(7, 1);
   return 0;
}

//This will clear the console while setting the forground and
//background colors.
void ClearConsoleToColors(int ForgC, int BackC)
{
   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //not used but we need to capture this since it will be
   //written anyway (passing NULL causes an access violation).
   DWORD count;

   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   SetConsoleTextAttribute(hStdOut, wColor);
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

//This will clear the console.
void ClearConsole()
{
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //   not used but we need to capture this since it will be
   //   written anyway (passing NULL causes an access violation).
   DWORD count;
   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

//This will set the position of the cursor
void gotoXY(int x, int y)
{
   //Initialize the coordinates
   COORD coord = {x, y};
   //Set the position
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
   WORD wColor;
   //We will need this handle to get the current background attribute
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_SCREEN_BUFFER_INFO csbi;

   //We use csbi for the wAttributes word.
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //Mask out all but the background attribute, and add in the forgournd color
      wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
      SetConsoleTextAttribute(hStdOut, wColor);
   }
}

//This will set the forground and background color for printing in a console window.
void SetColorAndBackground(int ForgC, int BackC)
{
   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

//Direct console output
void ConPrint(char *CharBuffer, int len)
{
   DWORD count;
   WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
}

//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
   DWORD count;
   COORD coord = {x, y};
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hStdOut, coord);
   WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}

//Hides the console cursor
void HideTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = FALSE;
	  SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}

//Shows the console cursor
void ShowTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = TRUE;
	  SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧