将一维数组转化成曲线图
譬如我有一个20大小的数组void main()
{
int a[20]={。。。。。。。。。。。};//被赋值
int i;
。。。。。。
} 通过什么方式才能得到以i(0到20)为横坐标,以a[i]为纵坐标的离散点图,或者曲线
或者如何用C++得到曲线图
求大神解答
#include <Windows.h> #include <locale> #include <conio.h> #include "Screen.h" const SHORT MaxRowNumber(24); // 屏幕最大行号 const SHORT MaxColNumber(79); // 屏幕最大列号 const SMALL_RECT statusBar = { 0, MaxRowNumber, MaxColNumber, MaxRowNumber }; const size_t MaxNumber(20); // 最大数据量 const int MaxValue(22); // 最大数据值 const clock_t Interval(1000); // 刷新画面的时间间隔(毫秒) Screen screen(GetStdHandle(STD_OUTPUT_HANDLE)); // 控制台对象 // 函数原型 void CreateData(int* pData, const size_t Count); void ShowStatusBar(void); void ShowData(const int Data[], size_t Count); // 程序入口 int wmain(int argc, wchar_t argv[]) { setlocale(LC_ALL, "chs"); screen.SetTitle(L"数据条形图"); screen.SetCursorVisible(false); clock_t StartClock(clock()); bool Show(true); while (!_kbhit() || (screen.ReadKey() != Screen::K_ESC)) { if (Show) { int Data[MaxNumber]; CreateData(Data, _countof(Data)); screen.Cls(BACKGROUND_BLUE); ShowStatusBar(); ShowData(Data, _countof(Data)); Show = false; } if (clock() - StartClock >= Interval) { Show = true; StartClock = clock(); } } screen.SetCursorVisible(true); return EXIT_SUCCESS; } void CreateData(int* pData, const size_t Count) { srand(static_cast<unsigned int>(_time32(NULL))); rand(); for (size_t i = 0; i < Count; ++i) { *pData++ = static_cast<int>(MaxValue * static_cast<double>(rand()) / (RAND_MAX + 1)); } } void ShowStatusBar(void) { WORD attr(screen.GetTextAttribute()); screen.SetTextAttribute(FOREGROUND_YELLOW | BACKGROUND_RED); screen.Clear(statusBar); screen.MoveCursor(statusBar.Left, statusBar.Top); screen.WriteText(L"按Esc键结束程序"); screen.SetTextAttribute(attr); } void ShowData(const int Data[], size_t Count) { size_t Number((Count > MaxNumber) ? MaxNumber : Count); for (size_t i = 0; i < Number; ++i) { COORD pos = { 4 * i, MaxRowNumber - 1 }; WCHAR buffer[10]; swprintf(buffer, sizeof(buffer) / sizeof(WCHAR), L"%2d", i + 1); screen.MoveCursor(pos).SetTextAttribute(FOREGROUND_GREEN | BACKGROUND_BLUE).WriteText(buffer); if (Data[i] < 0) { pos.Y = MaxRowNumber - 2; } else { int theData((Data[i] > MaxValue) ? MaxValue : Data[i]); for (int j = 0; j < theData; ++j) { pos.Y = MaxRowNumber - 2 - j; screen.MoveCursor(pos).SetTextAttribute(FOREGROUND_WHITE | BACKGROUND_BLUE).WriteText(L'■'); } --pos.Y; } WORD attr(FOREGROUND_RED | BACKGROUND_BLUE); if (Data[i] < 0) { attr = FOREGROUND_BLACK | BACKGROUND_BLUE; } if (Data[i] > MaxValue) { attr = FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE; } swprintf(buffer, sizeof(buffer) / sizeof(WCHAR), L"%d", Data[i]); screen.MoveCursor(pos).SetTextAttribute(attr).WriteText(buffer); } }