ahojte, muzete mi prosim nekdo z Vas pomoci s ukolem dynamicka matice, jejiz zadani zni: Vytvořte program ve kterém budete moci za běhu programu zadávat rozměry matice a alokovat a uvolňovat pro ní pamět z paměťové haldy (memory heap) pomocí funkcí malloc a free. Problem je v tom, ze pouzivam operatory z C++, a i kdyz jsem operatory new a delete prepsal pomoci operatoru malloc a free, porad mi to nejede. Diky moc za rady.
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void menu(void);
void con(void);
void minus(int);
void plus(int);
void udelej(void);
void napln(void);
void vypis(int);
void pridej(void);
void smaz(void);
void pocet(void);
typedef struct bunka
{
int h;
struct bunka *a;
} BUNKA;
int rad=0, slo=0, go=1, x=1, y=1, g;
int **mat1,*mat2;
BUNKA *pole, *p, *n;
int main(void)
{
system("cls");
printf("\n\n Tvorba dynamickeho pole");
printf("\n #######################\n\n\n");
do
{
menu();
}while(go);
if(pole!=NULL) delete pole;
return 1;
}
void menu(void)
{ char q;
printf("\n\n\n Menu - vyber akci");
printf("\n vytvoreni dyn. pole ... [1]");
printf("\n vygenerovani hodnot ... [2]");
printf("\n cteni pole ... [3]");
printf("\n pridani pole ... [4]");
printf("\n smazani pole ... [5]");
printf("\n konec programu ... [Esc]");
q=getch();
switch(q)
{
case '1':udelej();break;
case '2':napln();break;
case '3':vypis(2);break;
case '4':pridej();break;
case '5':smaz();break;
case 27:go=0;{delete pole; break;}
default:printf("\n\n neplatna volba \n\n");
}
}
void con(void)
{
int i;
if(pole!=NULL) delete pole;
pole=new BUNKA;
pole->a=NULL;
pole->h=0;
}
void minus(int q)
{
int i,k;
BUNKA *pom;
k=1;
p=pole;
while(p->a!=NULL)
{
if(k==(q-1))pom=p;
if(k==q)break;
k++;
p=p->a;
}
pom->a=p->a;
delete p;
}
void plus(int q)
{
int i,j,k;
p=pole;
k=1;
while(p->a!=NULL)
{
if(k==q)break;
k++;
p=p->a;
}
n=new BUNKA;
n->h=0;
n->a=p->a;
p->a=n;
}
void udelej(void)
{
int i,j=0;
int q;
printf("\n\n napiste, kolik bunek ma dane pole obsahovat: ");
scanf("%i",&q);
con();
for(i=1; i!=q; i++){plus(q); j++;}
printf("\n\n hotovo");
}
void pocet(void)
{
int i=0;
p=pole;
printf("\n");
while(p!=NULL)
{
i++;
printf("%i. h=%i p=%i a=%i \n",i,p->h,p,p->a);
p=p->a;
}
system("pause");
}
void pridej(void)
{
int q;
printf("\n\n napiste, za kterou bunku se ma vytvorit nova: ");
scanf("%i",&q);
plus(q);
printf("\n bunka byla pridana s nulovou hodnotou.");
}
void smaz(void)
{
int q;
printf("\n\n napiste, kterou bunku si prejete smazat: ");
scanf("%i",&q);
minus(q);
printf("\n bunka byla smazana.");
}
void napln(void)
{
p=pole;
srand((unsigned)time(NULL));
while((p!=NULL))
{
p->h=(rand()%98)+1;
p=p->a;
}
printf("\n\n hotovo");
}
void vypis(int q)
{
p=pole;
printf("\n\n\n Hodnoty pole jsou:\n ");
while(p!=NULL)
{
printf("%2i; ",p->h);
p=p->a;
}
}
Fórum › C / C++
Dynamická matice


Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 42 hostů
Podobná vlákna
Dynamická matice — založil Zke
Dynamická alokace matice načtené pomocí fgets? — založil Inu-des
Matice - inverzní matice — založil Mara
Dynamická alokace — založil Bananovnik
Dynamická Struktura — založil Petr
Moderátoři diskuze