请教一个函数问题

sxh53 2011-08-24 02:43:09

#include<iostream>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
using namespace std;
/*
希望能够以停止的*的整齐度来评分
*/
int move(int a)
{

for(int i=0;i<75;i++)
{
for(int j=0;j<i;j++)
{
cout<<" ";
}
cout<<"*"<<endl;
Sleep(70);
if(kbhit())
break;
system("cls");
}
return i;
i=0;
}
int main(void)
{

int score[6];
float aver,result;
for(int i=0;i<6;i++)
{
score[i]=move(i);
}

aver=(score[0]+score[1]+score[2]+score[3]+score[4]+score[5])/6;
int temp[6];
for(int j=0;j<6;j++)
{
temp[j]=(score[j]-aver)*(score[j]-aver);
}
result=100-temp[0]-temp[1]-temp[2]-temp[3]-temp[4]-temp[5];
cout<<"your score is"<<result<<endl;

system("pause");
return 0;
}



程序如上
问题是为什么*只移动了一次 而不是6次?
...全文
148 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-08-25
  • 打赏
  • 举报
回复
#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(500);
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;
}
}

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

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

limang89 2011-08-25
  • 打赏
  • 举报
回复
你可以把次数改小点好看(把75改成10) 是移动完一次再回到起始位置继续移动。。每次移动都是一样的。

难道你是希望有6个*同时移动?

Sleep(); 一个重要作用就是挂起线程。。
sxh53 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 limang89 的回复:]

我估计你是没搞清楚Sleep(70); 的用法
[/Quote]

这句话不就是停留70ms 然后再继续运行下一条语句吗? 有点像断点 不过是计时的 我说的对吗?
sxh53 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 limang89 的回复:]

是移动了6次啊。。。请说清楚你到底想实现什么样的效果..
[/Quote]

实际运行效果是
第一个*一直在移动
第二个*和以后的*就自动的停在第一个位置了。
我希望他们能和第一个*一样的效果。
limang89 2011-08-24
  • 打赏
  • 举报
回复
我估计你是没搞清楚Sleep(70); 的用法
limang89 2011-08-24
  • 打赏
  • 举报
回复
是移动了6次啊。。。请说清楚你到底想实现什么样的效果..
sxh53 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyh12345 的回复:]
system("cls");
都清掉了
[/Quote]


使用textout 会有影响么??
sxh53 2011-08-24
  • 打赏
  • 举报
回复
那该怎么办呢。。。
tony2278 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyh12345 的回复:]
system("cls");
都清掉了
[/Quote]
++4
jackyjkchen 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyh12345 的回复:]

system("cls");
都清掉了
[/Quote]
++3
至善者善之敌 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyh12345 的回复:]
system("cls");
都清掉了
[/Quote]

++2
luciferisnotsatan 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyh12345 的回复:]

system("cls");
都清掉了
[/Quote]
++
ouyh12345 2011-08-24
  • 打赏
  • 举报
回复
system("cls");
都清掉了
sxh53 2011-08-24
  • 打赏
  • 举报
回复
木有人回答么...
感觉不太对啊。

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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