⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ms_p1.cpp

📁 It s an entire application that uses knowdledge of probabilistic and statistical math to generate an
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/////////////////////////////////////////////////////////////////////////////
/////                                                                   /////
/////                        BIBLIOTECI INCLUSE                         /////
/////                                                                   /////
/////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "graphics.h"
#include "string.h"

#include "MT.H"



/////////////////////////////////////////////////////////////////////////////
/////                                                                   /////
/////                   DEFINITII MACROINSTRUCTIUNI                     /////
/////                                                                   /////
/////////////////////////////////////////////////////////////////////////////

#define _N    1500                        // numarul implicit de valori de observatie a variabilei aleatoare X
#define _n    50                          // numarul implicit de variabile aleatoare discrete

#define _NUM_VIEWS       4                // numarul de viewporturi

#define _VIEW_COLOR      9                // culori implicite in viewport
#define _SEL_VIEW_COLOR  4
#define _BACKGROUND      0
#define _DRAW_COLOR      14
#define _RULER_COLOR     3
#define _INTERP_COLOR    10
#define _COORD_SYS_COLOR 7
#define _COORD_SYS_DXY   15               // pozitia sistemului de coordonate fata de conturul viewportului
#define _HALF_RULER      3                // jumatate dintr-o liniuta de rigla

#define _BUBBLE_SIZE     2                // marimea unei buline indicator

#define _CS_CH_WIDTH         77           // contante in consola
#define _CS_CH_HEIGHT        20
#define _CS_SCR_DXY          3
#define _CS_FILL_COLOR       8
#define _CS_SIDE_COLOR1      7
#define _CS_SIDE_COLOR2      15
#define _CS_TEXT_SCR_HEIGHT  10
#define _CS_TEXT_RSPACE_SCR  3
#define _CS_CMDLINE_COLOR    10
#define _CS_CMD_COLOR        3
#define _CS_MAX_TOKENS       3
#define _CS_MAX_TOKEN_LENGTH 30
#define _CS_REPLY_COLOR      12
#define _CS_MAX_COMMANDS     6
#define _CS_MAX_INFO_LENGTH  640
#define _CS_MAX_INFO_LENGTH2 450

#define _ESCAPE              27           // tastele extinse
#define _ENTER               13
#define _BACKSPACE           8
#define _UP                  72



/////////////////////////////////////////////////////////////////////////////
/////                                                                   /////
/////                        VARIABILE GLOBALE                          /////
/////                                                                   /////
/////////////////////////////////////////////////////////////////////////////


struct viewport
{
    unsigned short x,                     // coordonatele stanga-sus ale viewportului
		   y;
    unsigned short width,                 // dimensiunile pe ecran ale viewportului
		   height;
    unsigned short csx0,                  // coordonatele stanga-sus, dreapta-jos ale sistemului de coordonate
		   csy0,
		   csx1,
		   csy1;

    unsigned long seed;                   // valoare initiala pentru functia de random
    unsigned int n,                       // numarul de variabile aleatoare discrete
		 N;                       // numarul de valori de observatie a variabilei aleatoare X

    char color,                           // culoarea cu care se deseneaza histograma
	 rulercolor,                      // culoarea riglei
	 interpcolor,                     // culoarea interpolatorului
	 backcolor;                       // culoarea de fundal a viewportului

    char SwitchBar,                       // flag pt activ optiunii de bara verticala
	 SwitchBubble,                    // flag pt activ optiunii de bulina indicator
	 SwitchRuler,                     // flag pt activ riglei
	 SwitchGrid,                      // flag pt activ gridului
	 SwitchInterp;                    // flag pt activ interpolatorului

    float RulerResX,                      // resolutia riglei/gridului (intre 0 si 1)
	  RulerResY;

    int *X;                               // variabila aleatoare discreta
    int *nr;                              // de cate ori apare X[i] in cele N numere generate
    double *P,                            // distributia de probabilitate
	   *F;                            // functia de repartitie
}
    Views[_NUM_VIEWS];                    // viewporturile (4)


unsigned char SelView;                    // viewportul selectat
char SwitchFScreen,                       // flag pt activ optiunii de fullscreen
     SwitchConsole;                       // flag pt activ consolei

char BackColor;                           // culoarea de fundal

char quit;                                // flag pentru iesirea din program

signed short csX,                         // coordonatele stanga-sus ale consolei
	     cstY,
	     csbY;
unsigned short csWidth,                   // dimensiunile consolei
	       cstHeight,
	       csbHeight;

unsigned int csNextLine;                  // indicele urmatoarei linii din consola
					  // liniile de text ale consolei
char csTextLines[_CS_CH_HEIGHT][_CS_CH_WIDTH+1];

char csLineTextColor[_CS_CH_HEIGHT];      // culorile scrisului de pe fiecare linie a consolei

char csCmdLine [_CS_CH_WIDTH+1];          // linia de comanda
char csCmdLine2[_CS_CH_WIDTH+1];          // linia anterioara de comanda
					  // tokenurile rezultate din descompunerea liniei de comanda
char csCmdTokens[_CS_MAX_TOKENS][_CS_MAX_TOKEN_LENGTH+1];
					  // numarul de tokenuri
unsigned int csNumTokens;
					  // informatii referitoare la anumite comenzi (activate cu comanda "MAN")
char csInfos[_CS_MAX_COMMANDS][_CS_MAX_INFO_LENGTH]=
{
 "\n05SET sets a value for a variable:\n06SET set_modes value\n06set_modes:\n11  -0-Selected Viewport [0..3]\n11  -1-Rand initial value (0 for randomize)\n11  -2-N observation values\n11  -3-n discrete random variables\n11  -4-FullScreen flag (0->1)\n11  -5-Show Bubble flag (0->1)\n11  -6-Line/Bar mode flag (0->1)\n11  -7-Drawing color (0..15)\n11  -8-Background color (0..15)\n11  -9-Show Ruler flag (0->1)\n11  -10-Show Grid flag (0->1)\n11  -11-Grid/ruler X resolution (float)\n11  -12-Grid/ruler Y resolution (float)\n11  -13-Grid/ruler color (0..15)\n11  -14-Show interpolator flag (0..1)\n11  -15-Interpolator color (0..15)",
 "\n05GET gets the value of a variable:\n06GET get_modes value\n06get_modes:\n11  -0-Selected Viewport [0..3]\n11  -1-Rand initial value (0 for randomize)\n11  -2-N observation values\n11  -3-n discrete random variables\n11  -4-FullScreen flag (0->1)\n11  -5-Show Bubble flag (0->1)\n11  -6-Line/Bar mode flag (0->1)\n11  -7-Drawing color (0..15)\n11  -8-Background color (0..15)\n11  -9-Show Ruler flag (0->1)\n11  -10-Show Grid flag (0->1)\n11  -11-Grid/ruler X resolution (float)\n11  -12-Grid/ruler Y resolution (float)\n11  -13-Grid/ruler color (0..15)\n11  -14-Show interpolator flag (0..1)\n11  -15-Interpolator color (0..15)",
 "\n05PRINT prints a field of the selected distribution/viewport:\n06PRINT field index\n06 field:\n11  -0-X random values\n11  -1-P probabilities\n11  -2-F repartition functions\n11  -3-NR value interval frequency\n06index:\n11  -ALL-prints all values of the selected field\n11  -?-a number specifying the index of the field",
 "\n05MAN prints infos about the folowing command:\n06MAN command\n06command:\n11  -SET-set a variable value\n11  -GET-get a variabile value\n11  -PRINT-prints distribution dependent fields\n11  -QUIT- exits from the program",
 "\n05QUIT exits from the program",
 "\n05INTRO shows the console introduction"
};
					  // introducere consola
char csFirstInfo[_CS_MAX_INFO_LENGTH2]="\n04Console\n05You can modify/view/print all sorts of entities in this program\n05For more infos write MAN folowing one of the commands:\n11  -SET\n11  -GET\n11  -PRINT\n11  -MAN\n11  -QUIT\n11  -INTRO\n00\n09 I hope you like it!\n00\n07 Project team:\n12  -Popescu Cosmin\n12  -Pocsai Ildiko\n12  -Pucea Gheorghe\n12  -Poenaru Daniel\n12  -Petrov Olivera\n12  -Tigan Adriana\n00\n14 Press '~' (SHIFT+`) to hide this console";



/////////////////////////////////////////////////////////////////////////////
/////                                                                   /////
/////                      PROTOTIPURI FUNCTII                          /////
/////                                                                   /////
/////////////////////////////////////////////////////////////////////////////

void _InitConsole
   (
   );

int _PushConsoleText
   (
    char *text
   );

void _PushConsoleTextLines
   (
    char *text
   );

void _ClearConsole
   (
   );

void _TokenizeCmdLine
   (
   );

void _CompileCmdLine
   (
   );

void _PrintConsoleText
   (
   );

void _PrintConsoleCmdText
   (
   );

void _DrawConsole
   (
   );

void _DropConsole
   (
   );

double HN
   (
    unsigned int n
   );

int BinSearch
   (
    double far *F,
    unsigned int n,
    double u
   );

void _DiscreteSimulation
   (
    unsigned int view
   );

void _GenerateViews
   (
   );

void _DrawViews
   (
   );

void _DrawRV
   (
    unsigned char view
   );

void _ClearScreen
   (
    char color
   );

void _FullScreenView
   (
    unsigned int view
   );

void _DrawFrame
   (
   );

void _FreeBuffers
   (
   );

void _FreeViewportBuffers
   (
    unsigned int view
   );

void _InitDistributions
   (
   );

void _RefreshDistrib
   (
    unsigned int view
   );

int main
   (
   );



/////////////////////////////////////////////////////////////////////////////
/////                                                                   /////
/////                         CORPURI FUNCTII                           /////
/////                                                                   /////
/////////////////////////////////////////////////////////////////////////////


/***************************************************************************/
/***************************************************************************/
/***           Initializeaza consola si afiseaza introducerea            ***/
/***************************************************************************/
/***************************************************************************/

void _InitConsole
   (
   )

{
    csWidth  =getmaxx()     -2*_CS_SCR_DXY;
    cstHeight=(getmaxy()>>1)-2*_CS_SCR_DXY;
    csbHeight=_CS_TEXT_SCR_HEIGHT+(_CS_TEXT_RSPACE_SCR<<1);

    csX =_CS_SCR_DXY;
    cstY=-(cstHeight+(_CS_SCR_DXY<<1))-_CS_TEXT_SCR_HEIGHT-(_CS_TEXT_RSPACE_SCR<<1);
    csbY=cstY+cstHeight+_CS_SCR_DXY+_CS_SCR_DXY;

    csNextLine=0;

    SwitchConsole=0;

    _PushConsoleTextLines(csFirstInfo);
}



/***************************************************************************/
/***************************************************************************/
/***       "Impinge" o linie de text in consola de jos in sus            ***/
/***       cu culoarea specificata in codul de culori pe linii		 ***/
/***************************************************************************/
/***************************************************************************/

int _PushConsoleText
   (
    char *text
   )

{
    unsigned int i;

    if (csNextLine==_CS_CH_HEIGHT)
    {
	for (i=0; i<_CS_CH_HEIGHT-1; i++)
	{
	    strcpy(csTextLines[i],csTextLines[i+1]);
	    csLineTextColor[i]=csLineTextColor[i+1];
	}

	strcpy(csTextLines[_CS_CH_HEIGHT-1],text);
    }
    else
    {
	strcpy(csTextLines[csNextLine],text);

	csNextLine++;
    }


    return(csNextLine-1);
}



/***************************************************************************/
/***************************************************************************/
/***      "Impinge" mai multe linii de text pe ecran, linii date de      ***/
/***    stringul "text" care trebuie sa contina la fiecare inceput de    ***/
/***    linie de text caracterul '\n' urmat de 2 caractere ce codifica   ***/
/***    culoarea cu care va fi scrisa linia de text (neaparat doua cifre)***/
/***************************************************************************/
/***************************************************************************/

void _PushConsoleTextLines
   (
    char *text
   )

{
    char str[_CS_CH_WIDTH+1],col[2],color;
    char *nextLine;
    unsigned int pos,nextpos;


    if (text[0]!='\n')
    {
	return;
    }

    pos=1;
    while (1)
    {
	col[0]=text[pos];
	col[1]=text[pos+1];

	color=atoi(col);

	nextLine=strchr(text+pos+2,'\n');

	if (nextLine)
	{
	    nextpos=nextLine-(text+pos+1);
	}
	else
	{
	    nextpos=text+strlen(text)-(text+pos+1);
	}

	memset(str,0,_CS_CH_WIDTH+1);
	strncpy(str,text+pos+2,nextpos-1);

	csLineTextColor[_PushConsoleText(str)]=color;

	if (!nextLine)
	{
	    break;
	}

	pos=nextLine-text+1;
    }
}



/***************************************************************************/
/***************************************************************************/
/***                     Sterge textul din consola                       ***/
/***************************************************************************/
/***************************************************************************/

void _ClearConsole
   (
   )

{
    csNextLine=0;
}



/***************************************************************************/
/***************************************************************************/
/***       Imparte linia de comanda in tokenuri ce vor forma apoi        ***/
/***               comenzile, optiunile si valorile de comanda		 ***/
/***************************************************************************/
/***************************************************************************/

void _TokenizeCmdLine
   (
   )

{
    char *nextToken;
    unsigned int pos,nextpos;


    pos=0;
    csNumTokens=0;

    while (csCmdLine[pos]==' ')
    {
	pos++;
    }

    while (1)
    {
	nextToken=strchr(csCmdLine+pos,' ');

	if (nextToken)
	{
	    nextpos=nextToken-(csCmdLine+pos);
	}
	else
	{
	    nextpos=csCmdLine+strlen(csCmdLine)-(csCmdLine+pos);
	}

	csNumTokens++;

	memset(csCmdTokens[csNumTokens-1],0,_CS_MAX_TOKEN_LENGTH+1);
	strncpy(csCmdTokens[csNumTokens-1],csCmdLine+pos,nextpos);

	if (!nextToken)
	{
	    break;
	}

	if (csNumTokens>=_CS_MAX_TOKENS)
	{
           break;
	}

	pos=nextToken-csCmdLine+1;

	while (csCmdLine[pos]==' ')
	{
	    pos++;
	}
    }
}



/***************************************************************************/
/***************************************************************************/
/***                  Interpreteaza linia de comanda                     ***/
/***************************************************************************/
/***************************************************************************/

void _CompileCmdLine
   (
   )

{
    unsigned int iarg,iarg2,i,count;
    char str[100],flag;
    float farg;


    flag=0;

    switch (csNumTokens)
    {
	case 1:
	if (!strcmp(strupr(csCmdTokens[0]),"QUIT"))
	{
	    quit=1;
	    return;
	}
	else
	if (!strcmp(strupr(csCmdTokens[0]),"INTRO"))
	{
	    _PushConsoleTextLines(csFirstInfo);
	    flag=1;
	}
	break;

	case 2:
	if (!strcmp(strupr(csCmdTokens[0]),"MAN"))
	{
	    if (!strcmp(strupr(csCmdTokens[1]),"SET"))
	    {
		_PushConsoleTextLines(csInfos[0]);
	    }
	    else
	    if (!strcmp(strupr(csCmdTokens[1]),"GET"))
	    {
		_PushConsoleTextLines(csInfos[1]);
	    }
	    else
	    if (!strcmp(strupr(csCmdTokens[1]),"PRINT"))
	    {
		_PushConsoleTextLines(csInfos[2]);
	    }
	    else
	    if (!strcmp(strupr(csCmdTokens[1]),"MAN"))
	    {
		_PushConsoleTextLines(csInfos[3]);
	    }
	    else
	    if (!strcmp(strupr(csCmdTokens[1]),"QUIT"))
	    {
		_PushConsoleTextLines(csInfos[4]);
	    }
	    else
	    if (!strcmp(strupr(csCmdTokens[1]),"INTRO"))
	    {
		_PushConsoleTextLines(csInfos[5]);
	    }

	    flag=1;
	}
	else
	if (!strcmp(strupr(csCmdTokens[0]),"GET"))
	{
	    flag=1;

	    sscanf(csCmdTokens[1],"%d",&iarg);

	    switch (iarg)
	    {
		case 0:
		iarg2=SelView;
		break;

		case 1:
		iarg2=Views[SelView].seed;
		break;

		case 2:
		iarg2=Views[SelView].N;
		break;

		case 3:
		iarg2=Views[SelView].n;
		break;

		case 4:
		iarg2=SwitchFScreen;
		break;

		case 5:
		iarg2=Views[SelView].SwitchBubble;
		break;

		case 6:
		iarg2=Views[SelView].SwitchBar;
		break;

		case 7:
		iarg2=Views[SelView].color;
		break;

		case 8:
		iarg2=Views[SelView].backcolor;
		break;

		case 9:
		iarg2=Views[SelView].SwitchRuler;
		break;

		case 10:
		iarg2=Views[SelView].SwitchGrid;
		break;

		case 11:
		farg=Views[SelView].RulerResX;
		break;

		case 12:
		farg=Views[SelView].RulerResY;
		break;

		case 13:
		iarg2=Views[SelView].rulercolor;
		break;

		case 14:
		iarg2=Views[SelView].SwitchInterp;
		break;

		case 15:
		iarg2=Views[SelView].interpcolor;
		break;
	    }

	    if (
		(iarg!=11)&&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -