分解质因数
分解质因数可谓是在奥数里躲不开的一关,同时,通过分解质因数也会使你发现许多有趣的数。可是有些人可能不会分解质因数,今天我就给你写一个小程序
记得加关注呦
下面是代码:
头文件
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <windows.h>
使用空间:
using namespace std;
准备阶段:
void gotoxy(int x, int y)
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void hidden()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;
SetConsoleCursorInfo(hOut,&cci);
}
class Timer
{
private:
long start_time;
long pause_time;
bool is_pause;
bool is_stop;
public:
Timer();
bool isPause();
bool isStop();
void Start();
void Pause();
void Stop();
inline long getStartTime() {return start_time;}
void show();
};
Timer::Timer()
{
is_pause = false;
is_stop = true;
}
bool Timer::isPause()
{
if(is_pause)
return true;
else
return false;
}
bool Timer::isStop()
{
if(is_stop)
return true;
return false;
}
void Timer::Start()
{
if(is_stop)
{
start_time = time(0);
is_stop = false;
}
else if(is_pause)
{
is_pause = false;
start_time += time(0)-pause_time;
}
}
void Timer::Pause()
{
if(is_stop||is_pause)
return;
else
{
is_pause = true;
pause_time = time(0);
}
}
void Timer::Stop()
{
if(is_stop)
return ;
else if(is_pause)
{
is_pause = false;
is_stop = true;
}
else if(!is_stop)
{
is_stop = true;
}
}
void Timer::show()
{
long t = time(0) - start_time;
gotoxy(35,12);
cout<<setw(2)<<setfill('0')<<t/60/60<<":"
<<setw(2)<<setfill('0')<<t/60<<":"
<<setw(2)<<setfill('0')<<t%60<<endl;
}
主函数:
int main()
{
Timer t;
char ch;
hidden();//隐藏光标
system("color 02");
gotoxy(35,12);
cout<<"00:00:00";
gotoxy(20,18);
cout<<"按a开始,按空格暂停,按s停止";
while(1)
{
if(kbhit())
{
ch = getch();
switch (ch)
{
case 'a':t.Start();break;
case 's':t.Stop();break;
case ' ':t.Pause();break;
default :break;
}
}
if(!t.isStop()&&!t.isPause())
{
t.show();
}
}
}
下面是完整代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y)
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void hidden()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;
SetConsoleCursorInfo(hOut,&cci);
}
class Timer
{
private:
long start_time;
long pause_time;
bool is_pause;
bool is_stop;
public:
Timer();
bool isPause();
bool isStop();
void Start();
void Pause();
void Stop();
inline long getStartTime() {return start_time;}
void show();
};
Timer::Timer()
{
is_pause = false;
is_stop = true;
}
bool Timer::isPause()
{
if(is_pause)
return true;
else
return false;
}
bool Timer::isStop()
{
if(is_stop)
return true;
return false;
}
void Timer::Start()
{
if(is_stop)
{
start_time = time(0);
is_stop = false;
}
else if(is_pause)
{
is_pause = false;
start_time += time(0)-pause_time;
}
}
void Timer::Pause()
{
if(is_stop||is_pause)
return;
else
{
is_pause = true;
pause_time = time(0);
}
}
void Timer::Stop()
{
if(is_stop)
return ;
else if(is_pause)
{
is_pause = false;
is_stop = true;
}
else if(!is_stop)
{
is_stop = true;
}
}
void Timer::show()
{
long t = time(0) - start_time;
gotoxy(35,12);
cout<<setw(2)<<setfill('0')<<t/60/60<<":"
<<setw(2)<<setfill('0')<<t/60<<":"
<<setw(2)<<setfill('0')<<t%60<<endl;
}
int main()
{
Timer t;
char ch;
hidden();//隐藏光标
system("color 02");
gotoxy(35,12);
cout<<"00:00:00";
gotoxy(20,18);
cout<<"按a开始,按空格暂停,按s停止";
while(1)
{
if(kbhit())
{
ch = getch();
switch (ch)
{
case 'a':t.Start();break;
case 's':t.Stop();break;
case ' ':t.Pause();break;
default :break;
}
}
if(!t.isStop()&&!t.isPause())
{
t.show();
}
}
}
本文为转载文章