C语言程序设计,高分求答案啊!!!!!
题目33:设计一个模拟的时间条的程序基本要求:一般我们在安装程序的时候,会经常看到有一个时间条表示程序安装的进度,这样用户就能够了解到安装还剩余多少时间。本程序实现的就是这个功能。当然,若要将其真正运用到某个程序中,还要加以修改,以便使之的进度能够真正的与应用程序的进度吻合。
创新要求:在基本要求达到后,进行创新设计
答案:#include <windows.h>
#include <iostream>
#include <bitset>
using namespace std;
const int MAX_SIZE =16;
int Random(int a,int b);
void gotoxy(SHORT x,SHORT y);
int Enter(int x1, int y);
HANDLE hOut,hIn;
typedef BOOL (WINAPI *PROCGETCONSOLEDISPLAYMODE)(LPDWORD);
typedef BOOL (WINAPI *PROCSETCONSOLEDISPLAYMODE)(HANDLE,DWORD,LPDWORD);
PROCGETCONSOLEDISPLAYMODE GetConsoleDisplayMode;
PROCSETCONSOLEDISPLAYMODE SetConsoleDisplayMode; // 全局API声明
int main()
{
Enter(14,12);
}
int Enter(int x,int y)
{
int i;
int result=0;
COORD pos; // 储存涂写坐标的结构体
WORD color; // 储存涂写颜色的变量
DWORD written; // 储存涂写长度的变量
hOut=GetStdHandle(STD_OUTPUT_HANDLE); // 获取输出句柄
SetConsoleTitle("【读取数据】");
WORD textcolor; // 储存涂写颜色的变量
textcolor=FOREGROUND_RED; // |BACKGROUND_GREEN; // 设置涂写颜色
SetConsoleTextAttribute(hOut,textcolor); // 设置为默认涂写颜色
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci); //获取光标句柄
cci.bVisible=false; //隐藏光标
SetConsoleCursorInfo(hOut,&cci); //设置为默认隐藏
gotoxy(x-13,y);
printf("正在载入文件");
gotoxy(x+13,y+2);
printf("已载入 %d%%"result);
pos.X=x;
pos.Y=y;
textcolor=BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,textcolor);
FillConsoleOutputAttribute(hOut,textcolor,50,pos,&written); // 涂写(5,1)处50个字符
textcolor=FOREGROUND_RED|BACKGROUND_RED&BACKGROUND_GREEN&BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,textcolor);
Sleep(800);
for(i=0;i<1000;i++)
{
Sleep(50);
result=Random(result,result+3);
if(result>100)result=100;
FillConsoleOutputAttribute(hOut,color,result/2,pos,&written);
gotoxy(x+13,y+2);
printf("已载入 %d%%"result);
Sleep(100);
if(result>=100)break;
}
Sleep(500);
cci.bVisible=true; //隐藏光标
SetConsoleCursorInfo(hOut,&cci); //设置为默认隐藏
}
void gotoxy(SHORT x,SHORT y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(hOut,c);
}
int Random(int a,int b) //此处注意,产生的随机数范围是 a-b 故a<b
{
int i,k,range;
int min,max;
double j;
int myrand[MAX_SIZE] = {0};
bitset <MAX_SIZE> bs;
bs.reset();
srand((unsigned int)time(0));
int m = 0;
for( ; m < MAX_SIZE; )
{
int tmp = rand() % MAX_SIZE;
if( bs.test(tmp) )
{
continue;
}
bs.set(tmp);
myrand[m] = tmp;
m++;
}
min=a;
max=b;
range=max-min;
i=rand();
j=((double)i/(double)RAND_MAX);
i=(int)(j*(double)range);
i+=min;
return i;
}
这个是用随机数实现进度条的随机变化,C写的#include <iostream>
#include <Windows.h>class BarGoLink
{
public:
BarGoLink(int);
~BarGoLink();void Step(void);
private:
static char const * const empty;
static char const * const full;int rec_no;
int rec_pos;
int num_rec;
int indic_len;
};int main()
{
BarGoLink bar(100);
for(int i = 0; i < 100; i++)
{
bar.Step();
Sleep(100);
}
}/**
* BarGoLink 相关定义
*/
char const* const BarGoLink::empty = " ";
char const* const BarGoLink::full = "\x3D";BarGoLink::BarGoLink(int count)
{
rec_no = 0;
rec_pos = 0;
indic_len = 50;
num_rec = count;
printf("\x3D");
for(int i = 0; i < indic_len; i++)
printf(empty);
printf("\x3D 0%%\r\x3D");
fflush(stdout);
}BarGoLink::~BarGoLink()
{
printf("\n");
fflush(stdout);
}void BarGoLink::Step()
{
int i, n;if(num_rec == 0)
return;++rec_no;
n = rec_no * indic_len / num_rec;
if( n != rec_pos )
{
printf("\r\x3D");
for(i = 0; i < n; i++)
printf(full);
for(; i < indic_len; i++)
printf(empty);
float percent = (((float)n / (float)indic_len) * 100);
printf("\x3D %i%% \r\x3D", (int)percent);
fflush(stdout);rec_pos = n;
}
}//运行效果: