#include <iostream>
#include <time.h>
#include <Windows.h>
#include <math.h>
using namespace std;
struct Point
{
int x;
int y;
};
//下面的函式演示了如何使用速度,而不是時間,來求得下一個位置
Point posg = {0,0};
int vy=0;
//彈體運動函式
Point Bonus(double t)
{
posg.x++;
vy++;
posg.y+=vy;
if(posg.y>25)
vy=-vy;
return posg;
}
//一次函式
Point Line (double t)
{
posg.x = t - 20;
posg.y = (posg.x + 20) * (25.0 / 40.0);
return posg;
}
//二次函式
Point Quad (double t)
{
posg.x = t - 20;
posg.y = (25.0/20.0/20.0) * posg.x * posg.x + 1;
return posg;
}
//指數函式
Point Exp (double t)
{
posg.x = t - 20;
posg.y = 10 * pow(pow(2.5, 1.0/20), posg.x);
return posg;
}
//三角函式
Point Sin(double t)
{
posg.x = t - 20;
posg.y = 13 + 12 * sin(acos(-1) * posg.x / 10 );
return posg;
}
//對數函式
Point Log(double t)
{
posg.x = t - 20;
posg.y = 15 * log(posg.x + 20) / log(10);
return posg;
}
//反比例函式
Point Reverse(double t)
{
posg.x = t - 20;
posg.y = 1.0 / posg.x * 10 + 12.5;
return posg;
}
typedef Point(*fc)(double);
Point GraphFunction(int t,fc fuc)
{
Point pos = fuc(t);
if(fuc!=Bonus) //如果不是彈體運動就變換坐標原點
{
pos.y = 25 - pos.y;
pos.x = pos.x + 20;
}
return pos;
}
int choice=1;
int Choice()
{
cout<< "1.畫一次函式" << endl
<< "2.畫二次函式" << endl
<< "3.畫指數函式" << endl
<< "4.畫三角函式" << endl
<< "5.畫對數函式" << endl
<< "6.畫彈體運動曲線" << endl
<< "7.畫反比例函式曲線" << endl
<< "0.退出" << endl << endl;
cout<<"請輸入你的選擇: ";
cin>>choice;
char buffer[80];
//處理輸入是否正確
while(!cin.good())
{
cin.clear();
cin.getline(buffer,80);
cout<<"輸入錯誤,請重新輸入一個整數\n";
cin>>choice;
}
return choice;
}
fc Deal()
{
while(choice=Choice())
{
system("cls");
switch(choice)
{
case 1:
return Line;
case 2:
return Quad;
case 3:
return Exp;
case 4:
return Sin;
case 5:
return Log;
case 6:
posg.x = 0;
posg.y = 0;
vy = 0;
return Bonus;
case 7:
return Reverse;
default:
cout<<"錯誤輸入"<<endl;
break;
}
}
return NULL;
}
//如何畫星號
void FillInPosition(Point pos)
{
//獲得句柄
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//獲得當前螢屏資訊
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
//從(x1,y1)開始用指定格式覆寫到(x2,y2)
COORD home;
home.X = 2 * pos.x;
home.Y = pos.y;
unsigned long num;
FillConsoleOutputCharacter(hOut, '*', 1, home, &num);
//恢復原來的屬性
SetConsoleTextAttribute(hOut, bInfo.wAttributes);
//恢復原來的游標位置
SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition);
}
/*行程暫停*/
//下面設定兩點之間的時間間隔
void wait(clock_t span)
{
clock_t tStart = clock();
int now;
while((now = clock())-tStart<span);
}
int main()
{
while(choice)
{
int t=0;
system("cls");
fc Fc;
Fc=Deal();
if(Fc)
{
while(t < 40)
{
FillInPosition(GraphFunction(t,Fc));
wait(100);
t++;
}
system("pause");
system("cls");
}
posg.x=0;
posg.y=0;
vy=0;
}
}
uj5u.com熱心網友回復:
要干什么呢
uj5u.com熱心網友回復:
在界面繪制曲線?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/94365.html
