Ïåðåéòè èç ôîðóìà íà ñàéò.

ÍîâîñòèÔàéëîâûå àðõèâû
ÏîèñêÀêòèâíûå òåìûÒîï ëèñò
ÏðàâèëàÊòî â on-line?
Âõîä Çàáûëè ïàðîëü? Ïåðâûé ðàç íà ýòîì ñàéòå? Ðåãèñòðàöèÿ
Êîìïüþòåðíûé ôîðóì Ru.Board » Êîìïüþòåðû » Ïðèêëàäíîå ïðîãðàììèðîâàíèå » Âîïðîñû ïî ïðîãðàììèðîâàíèþ íà C/Ñ++

Ìîäåðèðóåò : ShIvADeSt

 Âåðñèÿ äëÿ ïå÷àòè • ÏîäïèñàòüñÿÄîáàâèòü â çàêëàäêè
Íà ïåðâóþ ñòðàíèöóê ýòîìó ñîîáùåíèþê ïîñëåäíåìó ñîîáùåíèþ

Îòêðûòü íîâóþ òåìó     Íàïèñàòü îòâåò â ýòó òåìó

LuckyELF

Newbie
Ðåäàêòèðîâàòü | Ïðîôèëü | Ñîîáùåíèå | Öèòèðîâàòü | Ñîîáùèòü ìîäåðàòîðó

#include <stdio.h>
#include <conio.h>
#include <bios.h>
#include <string.h>
#include <stdlib.h>
 
#define FALSE 0
#define TRUE 1
 
// Êîäû êëàâèøü èñïîëüçóåìûõ â ìåíþ
#define ESC 283
#define ENTER 7181
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
 
// Êîíñòàíòû äëÿ ïðîðèñîâêè ðàìîê îêîí
const char up_left[3]={" &#9484;&#9556;"}, up_right[3]={" &#9488;&#9559;"}, down_left[3]={" &#9492;&#9562;"};
const char down_right[3]={" &#9496;&#9565;"}, line_g[3]={" -&#9552;"}, line_v[3]={" &#9474;&#9553;"};
 
// Ñòðóêòóðà - îïèñàòåëü ýëóìåíòîâ ïîäìåíþ
typedef struct _tagMenuSubItem
{
    char *text;
    struct _tagMenuSubItem *next;
}tagMenuSubItem;
 
// Ñòðóêòóðà - îïèñàòåëü ýëóìóíòîâ ìåíþ
typedef struct _tagMenuItem
{
    char *text;
    tagMenuSubItem *first;
    struct _tagMenuItem *next;
}tagMenuItem;
 
void ShowWindow( int, int, int, int, char, char, char, char, char * );
 
tagMenuItem* AddItem( char *, tagMenuSubItem *, tagMenuItem * );
tagMenuSubItem* AddSubItem( char *, tagMenuSubItem * );
void DeleteMenuItem( tagMenuItem * );
tagMenuSubItem* ShowItemCursor( tagMenuItem *firstItem, int id, int *xCoor, char cursColor, char cursPaper );
void ShowMenuItem( tagMenuItem *, char, char );
void InitStatusLine( char * );
int ExecMenu( int *, int *, tagMenuItem *, char, char, char, char, char, char );
void InputData( void );
void FreeMatrix( void );
void InputMatrix( void );
void ShowMatrix( void );
int ShowMatrixSegment( int, int );
 
unsigned long memUsed = 0;
float **theMatrix;
int M, N;
 
int ExecMenu( int *id1, int *id2, tagMenuItem *firstItem,
                    char textColor, char textPaper,
                    char cursColor, char cursPaper,
                    char winBorder, char winShadow )
{
    tagMenuItem *p, *p1;
    tagMenuSubItem *pp, *pp1;
    int key, iid1=1, iid2, iCount1=0, iCount2=0, xCoor, yCoor, i;
    int wx1, wx2, wy1, wy2, maxLen, zSize;
    char subOpen = FALSE, *zBuf;
    zBuf = NULL;
    p = firstItem;
    _setcursortype( _NOCURSOR );
    while( p )
    {
        p = p -> next;
        iCount1++;
    }
 
    char quit=TRUE;
    do{
        // Show
        ShowMenuItem( firstItem, textColor, textPaper );
//        xCoor = ShowItemCursor( firstItem, iid1, CYAN, BLUE );
        pp = ShowItemCursor( firstItem, iid1, &xCoor, cursColor, cursPaper );
        if ( pp && subOpen )
        {
            pp1 = pp;
            yCoor = wy1 + 1;
            i = 1;
            while( pp1 )
            {
                if ( i == iid2 )
                {
                    textcolor( cursColor );
                    textbackground( cursPaper);
                }
                else
                {
                    textcolor( textColor );
                    textbackground( textPaper);
                }
                gotoxy( wx1 + 1, yCoor );
                cprintf( "%s", pp1 -> text );
                pp1 = pp1 -> next;
                yCoor++;
                i++;
            }
        }
 
        key = bioskey(0);
        switch( key )
        {
            case ESC:
                if ( subOpen )
                {
                    if ( zBuf )
                    {
                        puttext( wx1,wy1,wx2+2,wy2+1, zBuf );
                        free( zBuf );
                    }
                    subOpen = FALSE;
                }
                else
                {
                    quit=FALSE;
                }
                break;
            case ENTER:
                if ( subOpen )
                {
                    quit = FALSE;
                }
                else
                {
                    if ( pp )
                    {
                        subOpen = TRUE;
                        iid2 = 1;
///////////////////////////////////////////////////////////////////
                    if ( zBuf )
                    {
                        puttext( wx1,wy1,wx2+2,wy2+1, zBuf );
                        free( zBuf );
                    }
                    pp1 = pp;
                    iCount2 = 0;
                    maxLen = 0;
                    while( pp1 )
                    {
                        if ( maxLen < strlen( pp1 -> text ) )
                            maxLen = strlen( pp1 -> text );
                        pp1 = pp1 -> next;
                        iCount2++;
                    }
                    wx1 = xCoor - 1;
                    wx2 = wx1 + maxLen + 1;
                    wy1 = 2;
                    wy2 = wy1 + iCount2 + 1;
                    zSize = ( wx2 - wx1 + 2 ) + ( wy2 - wy1 + 1 );
                    zBuf = (char *) malloc( zSize );
                    if ( !zBuf )
                    {
                        printf("Not enough memory to allocate buffer\n");
                        exit(1);  /* terminate program if out of memory */
                    }
                    // Show window
                    gettext( wx1,wy1,wx2+2,wy2+1, zBuf );
                    ShowWindow( wx1, wy1, wx2, wy2, textColor, textPaper, winBorder, winShadow, "" );
///////////////////////////////////////////////////////////////////
                    }
                }
                break;
 
            case LEFT:
                iid1--;
                if ( iid1 < 1 ) iid1 = iCount1;
                if ( subOpen )
                {
                    iid2 = 1;
                    pp = ShowItemCursor( firstItem, iid1, &xCoor, CYAN, BLUE );
///////////////////////////////////////////////////////////////////
                    if ( zBuf )
                    {
                        puttext( wx1,wy1,wx2+2,wy2+1, zBuf );
                        free( zBuf );
                    }
                    pp1 = pp;
                    iCount2 = 0;
                    maxLen = 0;
                    while( pp1 )
                    {
                        if ( maxLen < strlen( pp1 -> text ) )
                            maxLen = strlen( pp1 -> text );
                        pp1 = pp1 -> next;
                        iCount2++;
                    }
                    wx1 = xCoor - 1;
                    wx2 = wx1 + maxLen + 1;
                    wy1 = 2;
                    wy2 = wy1 + iCount2 + 1;
                    zSize = ( wx2 - wx1 + 2 ) + ( wy2 - wy1 + 1 );
                    zBuf = (char *) malloc( zSize );
                    if ( !zBuf )
                    {
                        printf("Not enough memory to allocate buffer\n");
                        exit(1);  /* terminate program if out of memory */
                    }
                    // Show window
                    gettext( wx1,wy1,wx2+2,wy2+1, zBuf );
                    ShowWindow( wx1, wy1, wx2, wy2, textColor, textPaper, winBorder, winShadow, "" );
///////////////////////////////////////////////////////////////////
                }
                break;
 
            case RIGHT:
                iid1++;
                if ( iid1 > iCount1 ) iid1 = 1;
                if ( subOpen )
                {
                    iid2 = 1;
                    pp = ShowItemCursor( firstItem, iid1, &xCoor, CYAN, BLUE );
///////////////////////////////////////////////////////////////////
                    if ( zBuf )
                    {
                        puttext( wx1,wy1,wx2+2,wy2+1, zBuf );
                        free( zBuf );
                    }
                    pp1 = pp;
                    iCount2 = 0;
                    maxLen = 0;
                    while( pp1 )
                    {
                        if ( maxLen < strlen( pp1 -> text ) )
                            maxLen = strlen( pp1 -> text );
                        pp1 = pp1 -> next;
                        iCount2++;
                    }
                    wx1 = xCoor - 1;
                    wx2 = wx1 + maxLen + 1;
                    wy1 = 2;
                    wy2 = wy1 + iCount2 + 1;
                    zSize = ( wx2 - wx1 + 2 ) + ( wy2 - wy1 + 1 );
                    zBuf = (char *) malloc( zSize );
                    if ( !zBuf )
                    {
                        printf("Not enough memory to allocate buffer\n");
                        exit(1);  /* terminate program if out of memory */
                    }
                    // Show window
                    gettext( wx1,wy1,wx2+2,wy2+1, zBuf );
                    ShowWindow( wx1, wy1, wx2, wy2, textColor, textPaper, winBorder, winShadow, "" );
///////////////////////////////////////////////////////////////////
                }
                break;
 
            case UP:
                if ( subOpen )
                {
                    iid2--;
                    if ( iid2 < 1 ) iid2 = iCount2;
                }
                break;
            case DOWN:
                if ( subOpen )
                {
                    iid2++;
                    if ( iid2 > iCount2 ) iid2 = 1;
                }
                break;
        }
    }while( quit );
    _setcursortype( _NORMALCURSOR );
    if ( zBuf )
    {
        puttext( wx1,wy1,wx2+2,wy2+1, zBuf );
        free( zBuf );
    }
    ShowMenuItem( firstItem, textColor, textPaper );
    *id1 = iid1;
    *id2 = iid2;
    return key;
}
 
tagMenuSubItem* ShowItemCursor( tagMenuItem *firstItem, int id, int *xCoor, char cursColor, char cursPaper )
{
    tagMenuItem *p;
    int x=2, i=1;
    textcolor( cursColor );
    textbackground( cursPaper );
    p = firstItem;
    gotoxy( 1,1 );
    while( p )
    {
        if ( i == id )
        {
            gotoxy( x, 1 );
            cprintf( "%s", p -> text );
            *xCoor = x;
            return p -> first;
        }
        x += strlen( p -> text );
        p = p -> next;
        i++;
    }
 
    return NULL;
}
 
void ShowMenuItem( tagMenuItem *firstItem, char lineColor, char linePaper )
{
    tagMenuItem *p;
    int x;
    gotoxy(1,1);
    textcolor( lineColor );
    textbackground( linePaper );
    for( x=1; x<=80; x++ )
        cprintf( " " );
    p = firstItem;
    gotoxy( 2,1 );
    while( p )
    {
        cprintf( "%s", p -> text );
        p = p -> next;
    }
}
 
void DeleteMenuItem( tagMenuItem *firstItem )
{
    tagMenuItem *p, *p1;
    tagMenuSubItem *pp, *pp1;
    p = firstItem;
    while( p ) // Öèêë óäàëåíèÿ ýëóìåíòîâ ìåíþ
    {
        p1 = p -> next;
        pp = p -> first;
        while( pp ) // Öèêë óäàëåíèÿ ýëåìåíòîâ ïîä ìåíþ
        {
            pp1 = pp -> next;
            free( pp -> text ); // Îñâîáîæäàåì ïàìÿòü èç ïîä òåêñòîâîãî ïîëÿ
            free( pp ); // Óäàëÿåì âåñü ýëåìåíò ïîäìåíþ
            pp = pp1;
        }
        free( p -> text ); // Îñâîáîæäàåì ïàìÿòü èç ïîä òåêñòîâîãî ïîëÿ
        free( p ); // Óäàëÿåì âåñü ýëåìåíò ìåíþ
        p = p1;
    }
}
 
tagMenuSubItem* AddSubItem( char *str, tagMenuSubItem *nextItem )
{
    tagMenuSubItem *p;
    p = (tagMenuSubItem *) malloc( sizeof(tagMenuSubItem) );
 
    if ( !p )
    {
        printf("Not enough memory to allocate buffer\n");
        exit(1);  /* terminate program if out of memory */
    }
 
    p -> text = (char *)malloc( strlen(str) + 1 );
    if ( !p -> text )
    {
        printf("Not enough memory to allocate buffer\n");
        exit(1);  /* terminate program if out of memory */
    }
    strcpy( p -> text, str );
    p -> next = nextItem;
 
    return p;
}
 
tagMenuItem* AddItem( char *str, tagMenuSubItem *subItem, tagMenuItem *nextItem )
{
    tagMenuItem *p;
    p = (tagMenuItem *)malloc( sizeof(tagMenuItem) );
 
    if ( !p )
    {
        printf("Not enough memory to allocate buffer\n");
        exit(1);  /* terminate program if out of memory */
    }
 
    p -> text = (char *)malloc( strlen(str) + 1 );
    if ( !p -> text )
    {
        printf("Not enough memory to allocate buffer\n");
        exit(1);  /* terminate program if out of memory */
    }
    strcpy( p -> text, str );
    p -> first = subItem;
    p -> next = nextItem;
 
    return p;
}
 
void ShowWindow( int x1, int y1, int x2, int y2, char wc, char wp, char b, char s, char *h )
{
    int x,y,l;
    char buf[200];
    textcolor( wc );
    textbackground( wp );
    for( y=y1; y<=y2; y++ )
        for( x=x1; x<=x2; x++ )
        {
            gotoxy( x,y );
            cprintf(" ");
        }
 
    for( y=y1; y<=y2; y++ )
    {
        gotoxy( x1,y ); cprintf("%c", line_v[b]);
        gotoxy( x2,y ); cprintf("%c", line_v[b]);
    }
 
    for( x=x1; x<=x2; x++ )
    {
        gotoxy( x,y1 ); cprintf("%c", line_g[b]);
        gotoxy( x,y2 ); cprintf("%c", line_g[b]);
    }
 
    gotoxy( x1,y1 ); cprintf("%c",up_left[b]);
    gotoxy( x2,y1 ); cprintf("%c",up_right[b]);
    gotoxy( x1,y2 ); cprintf("%c",down_left[b]);
    gotoxy( x2,y2 ); cprintf("%c",down_right[b]);
 
    l = strlen( h );
    y = (x2-x1)/2;
    x = x1 + y - l/2 + 1;
    gotoxy( x, y1 );
    cprintf("%s", h );
 
    if (s)
    {
        gettext( x2+1, y1+1, x2+2, y2+1, buf );
        for( x=1; x<=170; x+=2 )
            buf[x] = LIGHTGRAY;
        puttext( x2+1, y1+1, x2+2, y2+1, buf );
        gettext( x1+2, y2+1, x2+2, y2+1, buf );
        for( x=1; x<=170; x+=2 )
            buf[x] = LIGHTGRAY;
        puttext( x1+2, y2+1, x2+2, y2+1, buf );
    }
}
 
void InitStatusLine( char *s )
{
    int x,a,l,c=FALSE;
    l = strlen( s );
    textcolor( BLACK );
    textbackground( WHITE );
    window( 1, 25, 80, 25 );
    clrscr();
    for( x=1, a=0; a<l; a++ )
    {
        gotoxy( x,25 );
        if ( s[a]=='~' ) { c=!c; continue; }
        if ( c ) textcolor( RED ); else textcolor( BLACK );
        cprintf("%c", s[a] );
        x++;
        if ( x>80 ) break;
    }
    window( 1,1,80,25 );
}
 
void FreeMatrix( void )
{
    int i;
    if ( theMatrix )
    {
        for( i = 0; i <= M; i++ )
            free( theMatrix[i] );
        free( theMatrix );
    }
}
 
void InputData( void )
{
    int i;
    char quit=TRUE;
    FreeMatrix();
    do{
        ShowWindow( 1, 2, 80, 24, YELLOW, BLUE, 2, 0, "" );
        gotoxy( 4,4 ); textcolor( CYAN ); textbackground( BLUE );
        cprintf( "Ðàçìåðíîñòü ìàòðèöû (MxN):" );
        gotoxy( 8,6 ); textcolor( BLUE ); textbackground( WHITE );
        cprintf("          ");
        gotoxy( 30, 6 ); cprintf("          ");
        gotoxy( 4,6 ); textcolor( CYAN ); textbackground( BLUE );
        cprintf( "M =" );
        gotoxy( 26,6 ); cprintf( "N =" );
        _setcursortype( _NORMALCURSOR );
        gotoxy( 8,6 ); textcolor( BLUE ); textbackground( WHITE );
        scanf( "%d", &M );
        gotoxy( 30,6 ); textcolor( BLUE ); textbackground( WHITE );
        scanf( "%d", &N );
        _setcursortype( _NOCURSOR );
 
        theMatrix = (float**) malloc( sizeof(float*) * M );
        if ( !theMatrix )
        {
            gotoxy( 4,8 ); textcolor( CYAN ); textbackground( BLUE );
            cprintf( " Íå õâàòàåò ïàìÿòè äëÿ ñîçäàíèÿ ìàññèâà!" );
        }
        else
        {
            memUsed += ( sizeof(float*) * M );
 
            for( i = 0; i <= M; i++ )
            {
                theMatrix[i] = (float*) malloc( sizeof(float) * N );
                memset( theMatrix[i], 0, sizeof(float) * N );
 
                if ( !theMatrix )
                {
                    gotoxy( 4,8 ); textcolor( CYAN ); textbackground( BLUE );
                    cprintf( " Íå õâàòàåò ïàìÿòè äëÿ ñîçäàíèÿ ìàññèâà!" );
                    free( theMatrix );
                    memUsed -= ( sizeof(float*) * M );
                }
                else
                {
                    quit = FALSE;
                    memUsed += ( sizeof(float) * N );
                }
            }
        }
 
    }while( quit );
 
    //InputMatrix();
    ShowMatrix();
}
 
void ShowMatrix( void )
{
    int key, stat, xs = 0, ys = 0;
    char quit = TRUE;
    do{
        stat = ShowMatrixSegment( ys , xs );
        key = bioskey( 0 );
        switch( key )
        {
            case ESC: quit = FALSE; break;
            case DOWN:
                if ( (stat == 1 || stat == 3) && ys < M )
                    ys++;
                break;
            case UP:
                if ( ys >= 1 ) ys--;
                break;
            case LEFT:
                if ( xs >= 1 ) xs--;
                break;
            case RIGHT:
                if ( (stat == 2 || stat == 3) && xs < N )
                    xs++;
                break;
        }
    }while (quit);
}
 
int ShowMatrixSegment( int is, int js )
{
    int i, j, x, y;
    int result = 0;
    window( 2, 3, 79, 23 );    textbackground( BLUE );    clrscr();
    window( 1, 1, 80, 25 ); textcolor( LIGHTGRAY );
    gotoxy( 6, 3 ); cprintf( "N" );
    gotoxy( 3, 4 ); cprintf( "M" );
    for( x = 8, j = js; (j < N) && (x < 75); j++, x += 8 )
    {
        gotoxy( x, 3 ); cprintf( "%d", j+1 );
    }
 
    for( y = 5, i = is; (i < M) && (y < 24); i++, y++ )
    {
        gotoxy( 3, y ); cprintf( "%d", i+1 );
    }
 
    textcolor( BLUE ); textbackground( WHITE );
    for( y = 5, i = is; (i < M) && (y < 24); i++, y++ )
    {
        for( x = 8, j = js; (j < N) && (x < 75); j++, x+= 8 )
        {
            gotoxy( x,y ); cprintf( "%-4.1f", theMatrix[i][j] );
        }
    }
 
    if ( i < M && y >= 24 ) result = 1;
    if ( j < N && x > 75 ) result = 2;
    if (( i < M && y >= 24 ) && ( j < N && x > 75 )) result = 3;
    return result;
}
 
void InputMatrix( void )
{
    int i, j;
    for( i = 0; i < M; i++ )
    {
        for( j = 0; j < N; j++ )
        {
        }
    }
}
 
void main( void )
{
    char quit=TRUE;
    int key;
    tagMenuItem *mainMenu;
 
    textbackground( BLACK );
    clrscr();
 
    mainMenu = AddItem( " Ôàéë ",
                    AddSubItem( " Ââîä èñõîäíûõ äàííûõ  ",
                    AddSubItem( " Ðåäàêòèðîâàíèå äàííûõ ",
                    AddSubItem( " Ñîõðàíåíèå äàííûõ     ",
                    AddSubItem( " Âûâîä ðåçóëüòàòîâ     ",
                    AddSubItem( " Âûõîä                 ", NULL ))))),
                  AddItem( " Îáðàáîòêà ",
                    AddSubItem( " Ïðîèçâåäåíèå ñóìì ",
                    AddSubItem( " Min & Max         ", NULL )),
                  AddItem( " Ñïðàâêà ",
                    AddSubItem( " Î ïðîãðàììå ... ",
                    AddSubItem( " Ïîìîùü          ",
                    NULL )),
                    NULL )) );
 
    int id1, id2;
    ShowWindow( 1, 2, 80, 24, YELLOW, BLUE, 2, 0, "" );
    InitStatusLine( " Ïàìÿòè âûäåëåíî: ~65535~  Min: ~YYYYYYYYYYY~  Max: ~ZZZZZZZZZ~  Ìàòðèöà: ~MMM~x~NNN~" );
 
 
    do{
        key = ExecMenu( &id1, &id2, mainMenu, BLUE, CYAN, CYAN, BLUE, 2, 1 );
        if ( key == ESC )    {    id1 = 1;    id2 = 5;    }
 
        switch( id1 )
        {
            case 1:
                switch( id2 )
                {
                    case 1:
                        char str[255];
                        InputData();
                        sprintf (str, " Ïàìÿòè âûäåëåíî: ~%ld~  Min: ~YYYYYYYYYYY~  Max: ~ZZZZZZZZZ~  Ìàòðèöà: ~MMM~x~NNN~", memUsed);
                        InitStatusLine(str);
                        break;
                    case 2: ShowMatrix (); break;
                    case 3: break;
                    case 4: break;
                    case 5: quit = FALSE; break;
                }
                break;
 
            case 2:
                switch( id2 )
                {
                    case 1: break;
                    case 2: break;
                }
                break;
 
            case 3:
                switch( id2 )
                {
                    case 1: break;
                    case 2: break;
                }
                break;
        }
    }while( quit );
 
    DeleteMenuItem( mainMenu );
    FreeMatrix();
}

Âñåãî çàïèñåé: 29 | Çàðåãèñòð. 06-03-2005 | Îòïðàâëåíî: 22:20 19-05-2008 | Èñïðàâëåíî: LuckyELF, 22:29 19-05-2008
Îòêðûòü íîâóþ òåìó     Íàïèñàòü îòâåò â ýòó òåìó

Íà ïåðâóþ ñòðàíèöóê ýòîìó ñîîáùåíèþê ïîñëåäíåìó ñîîáùåíèþ

Êîìïüþòåðíûé ôîðóì Ru.Board » Êîìïüþòåðû » Ïðèêëàäíîå ïðîãðàììèðîâàíèå » Âîïðîñû ïî ïðîãðàììèðîâàíèþ íà C/Ñ++


Ðåêëàìà íà ôîðóìå Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Ðåéòèíã.ru