Помощь - Поиск - Пользователи - Календарь
Полная версия: граф!
Форум «Всё о Паскале» > Pascal, Object Pascal > Задачи
Guzel
Как упростить эту прогу с графом. Хочется че-нить по-проще!!!Помогите кто как может…пожалуйста!
Код
{******PROGRAMMA*DLYA*IZOBRAZHENIYA*GRAFA*PO*SMEZHNOI*MATRICE***************}
      {******************************************************}
uses Crt, Graph; {biblioteki}
{*****************************CONSTANTS*************************************}
const pi=3.1415; {chislo pi}
rad=120;         {rasstoyanie ot centra do vershin
                  v pixelakh (luchshe menshe 200)}
cf=0;            {cvet fona}
c1=5;            {osnovnoi cvet dug}
c2=3;            {cvet concov dug}
cv=1;            {cvet vershin}
ct=7;            {cvet teksta}
cn=4;            {cvet nomerov vershin}
xt=10;yt=10;     {koordinaty matricy}
d=0.1;           {dlina konchikov}
u=0.2;           {ugol(v radianakh)}
pet=0.2;         {radius po otn. k petle}
shag=10;         {rasst.m/u bukvami v matrice}
k=15;             {max kol-vo vershin}
{*****************************TYPES*****************************************}
type koord=record{tip-koordinaty tochki}
x:real;          {*}
y:real           {*}
end;             {*}
{*****************************VARIANTS**************************************}
var              {*}
Gd, Gm: Integer; {graficheskie peremennye}
t:real;          {parametr}
a:array[1..k,1..k]of integer;{matricy smezhnosti i incidentnosti}
b:array[1..k]of koord;{koordinatnye polozheniya vershin}
x,y:integer;     {seredina ekrana}
i,j,j1:byte;        {schetchiki}
n:integer;       {kolichestvo vershin}
s:string;        {dlya perevoda integer->string}
xt1,yt1:integer; {*}
c:char;          {*}
x0,y0:real;      {*}
alfa:real;       {*}
z:char;          {*}
p:integer;       {*}
l1,l2:array[1..k*k]of boolean;   {*}
{******************************MAIN*****************************************}
begin                {*}
{---------------------------------------------------------------------------}
  clrscr;
  randomize;         {generator sluch.chisel}
  repeat             {cikl zanovo}
  clrscr;
  writeln('Vyberite deistvie!');
  writeln('S - vvesti matricu smezhnosti;');
  writeln('I - vvesti matricu incidentnosti;');
  writeln('Drugie knopki - sgenerirovat sluchainuiu matricu;');
  writeln('Q - vychod iz programmy;');
  z:=readkey;
  if z='q' then break;
{------------------------Vvod-kolichestva-vershin---------------------------}
  repeat
  clrscr;            {ochistka ekrana}
  write('Vvedite kolichestvo vershin: ');
  readln(n);         {vvod kolichestva vershin}
  until (n>0)and(n<k+1);{*}
{------------------------Vvod-matricy-smezhnosti---------------------------}
  if z='s' then
  for j:=1 to n do
  for i:=1 to n do
  begin
  repeat
  write('Vvedite A(',i,',',j,')=');
  c:=readkey;
  until (c='0')or(c='1');
  writeln(c);
  val(c,a[j,i],p);
  end;
{------------------------Vvod-matricy-incidentnosti------------------------}
  if z='i' then
  begin
  write('Vvedite kolichestvo dug: ');
  readln(p);
  for i:=1 to p do
  begin
  repeat
  write('Vvedite nomer ishodnoi vershiny dugi nomer ',i,': ');
  readln(j);
  until (j>0)and(j<=n);
  repeat
  write('Vvedite nomer konechnoi vershiny dugi nomer ',i,': ');
  readln(j1);
  until (j1>0)and(j1<=n);
  a[j,j1]:=1;
  end;
  end;
{------------------------Vvod-sluchainoi-matrici---------------------------}
  if (z<>'s')and(z<>'i') then
  for i:= 1 to n do  {cikl strok}
  begin              {*}
    write('(');      {*}
    for j:=1 to n do {cikl stolbtsov}
    begin            {*}
      a[i,j]:=random(2);{generiruem chisla v matrice}
      write(a[i,j]:2,' '){vyvodim matricu}
    end;             {*}
    write(');');     {okonchanie stroki}
    writeln;         {*}
  end;               {*}
  readkey;           {programma ozhidaet nazhatie}
{-------------------------Vkluchaem-grafiky---------------------------------}
  Gd := Detect;                {vkluchaetsya graf.rezhim}
  InitGraph(Gd, Gm, 'c:\TP\BGI');       {*}
  if GraphResult <> grOk then  {*}
    Halt(1);                   {*}
  setbkcolor(cf);              {cvet fona}
{---------------------------------------------------------------------------}
  y:=(getmaxy div 2);  {nahodim \/}
  x:=(getmaxx div 2);  {centr ekrana}
{--------------------Zanosim-koordinaty-vershin-v-otdelnuyu-matricu---------}
  for i:=0 to n-1 do     {cikl-vvod koordinat dlya vershin}
  begin                {*}
    t:=(2*pi*i)/n;     {vychislenie parametra}
    b[i+1].x:=cos(t);    {ordinata}
    b[i+1].y:=sin(t);    {abcissa}
  end;                 {*}
{--------------------Chertim-osnovnye-linii---------------------------------}
  setcolor(c1);                   {cvet linii}
  for i:=1 to n do                {cikly dlya chercheniya osnovhykh linii\/}
  for j:=1 to n do                {i chercheniya petel}
  begin                           {*}
    if a[i,j]=1 then              {nahozhdenie edinic v matrice}
    begin                         {*}
      if i<>j then                {esli ne petlya -> to}
      begin                       {*}
      line( round(x+b[j].x*rad),  {liniya}
            round(y+b[j].y*rad),  {formula\/}
            round(x+b[i].x*rad),  {<centr>+<koord.vershiny>*<rasst.do centra>}
            round(y+b[i].y*rad)); {*}
      end                         {*}
      else circle( round(x+b[j].x*rad*(1+pet)),{petlya(okruzhnost)}
      round(y+b[j].y*rad*(1+pet)),{*}
                   round(rad*pet));{radius petli}
    end;                          {*}
  end;                            {*}
{---------------------Chertim-koncy-----------------------------------------}
  setlinestyle(0,0,2);{menyaem stil linii na tolstyi}
  setcolor(c2);       {cvet koncov vektora}
  for i:=1 to n do    {novyi cikl dlya chercheniya koncov}
  for j:=1 to n do    {*}
    if (a[i,j]=1)and(i<>j) then {esli ne petlya}
    begin                       {*}
      y0:=b[i].y-b[j].y;x0:=b[i].x-b[j].x;{*}
      if x0<>0                   {esli m/o delit}
      then                       {*}
        if x0>0 then alfa:=arctan(y0/x0)    {sprava ot ordinaty}
              else alfa:=pi+arctan(y0/x0) {sleva ot ordinaty}
      else if y0>0 then alfa:=pi/2 else alfa:=3*pi/2;{nuli}
      line( round(x+b[j].x*rad),{liniya 1}
            round(y+b[j].y*rad),{*}
            round(x+b[j].x*rad+((cos(alfa-u))*(d)*rad)),{*}
            round(y+b[j].y*rad+((sin(alfa-u))*(d)*rad)));{*}
      line( round(x+b[j].x*rad),{liniya 2}
            round(y+b[j].y*rad),{*}
            round(x+b[j].x*rad+((cos(alfa+u))*(d)*rad)),{*}
            round(y+b[j].y*rad+((sin(alfa+u))*(d)*rad)));{*}
    end;                        {*}
{--------------------Vyvodim-nomera-vershin---------------------------------}
  for j:=1 to n do   {novyi cikl dlya vivoda nomerov vershin}
  begin              {*}
    setcolor(cn);    {cvet nomerov vershin}
    str(j,s);        {*}
    outtextxy(round(x+(b[j].x)*rad+3),round(y+(b[j].y)*rad+3), s);{*}
    setcolor(cv);    {cvet vershin}
    circle(round(x+(b[j].x)*rad),round(y+(b[j].y)*rad),1){*}
  end;               {*}
{-------------------Vyvodim-matricu-v-graf.-rezhime-------------------------}
  setcolor(ct);      {cvet matrici}
  outtextxy(xt,yt+round(((n-1)/2)*shag),'A(G)=');{vyvod 'A(G)='po vert.c-ru}
  yt1:=yt;           {ordinata sovpadaet}
  for i:=1 to n do   {novyi cikl vyvoda matrici}
  begin{*}
    xt1:=xt+5*shag;  {shtoby ne bylo nalozheniya na 'A(G)='}
    for j:=1 to n do {*}
    begin            {*}
      str(a[i,j],s); {znachenie v stroku}
      outtextxy(xt1,yt1, s);{vyvod po koordinatam}
      xt1:=xt1+shag; {perehod po x}
    end;             {*}
    yt1:=yt1+shag;   {perehod po y}
  end;               {*}
  setlinestyle(0,0,1);{*}
  line(xt+(5+n)*shag-2,yt,xt+(5+n)*shag-2,yt+(n)*shag-2);{*}
  line(xt+5*shag-2,yt,xt+5*shag-2,yt+(n)*shag-2);{*}
{-------------------Zavershenie---------------------------------------------}
  setcolor(ct);
  outtextxy(10,getmaxy-10,'Esche?(y,n) ');
  repeat
  c:=readkey;
  until (c='n')or(c='y');
  CloseGraph;        {konec grafiki}
  until c='n';       {*}
end.               {vykhod}
{***************************END*OF*FILE*************************************}
Guzel
че никто не поможет шоли,а???Ну,пожалуйста!
virt
что ты подразумеваешь под словом упростить? Там в коментариях написано что делает каждая строчка!
Malice
Так проще ?
uses Crt, Graph;
const pi=3.1415;
rad=120;

xt=10;yt=10;
d=0.1;
u=0.2;
pet=0.2;
shag=10;
k=15;
type koord=record
x:real;
y:real
end;
var
n,p,xt1,yt1,x,y,Gd, Gm: Integer;
alfa,x0,y0,t:real;
a:array[1..k,1..k]of integer;
b:array[1..k]of koord;
i,j,j1:byte;
s:string;
z,c:char;
l1,l2:array[1..k*k]of boolean;
begin
randomize;
repeat
clrscr;
writeln('Vyberite deistvie!');
writeln('S - vvesti matricu smezhnosti;');
writeln('I - vvesti matricu incidentnosti;');
writeln('Drugie knopki - sgenerirovat sluchainuiu matricu;');
writeln('Q - vychod iz programmy;');
z:=readkey;
if z='q' then break;
repeat
clrscr;
write('Vvedite kolichestvo vershin: ');
readln(n);
until (n>0)and(n<k+1);
if z='s' then
for j:=1 to n do
for i:=1 to n do begin
repeat
write('Vvedite A(',i,',',j,')=');
c:=readkey;
until (c='0')or(c='1');
writeln©;
val(c,a[j,i],p);
end;

if z='i' then begin
write('Vvedite kolichestvo dug: ');
readln(p);
for i:=1 to p do begin
repeat
write('Vvedite nomer ishodnoi vershiny dugi nomer ',i,': ');
readln(j);
until (j>0)and(j<=n);
repeat
write('Vvedite nomer konechnoi vershiny dugi nomer ',i,': ');
readln(j1);
until (j1>0)and(j1<=n);
a[j,j1]:=1;
end;
end;
if (z<>'s')and(z<>'i') then
for i:= 1 to n do begin
write('(');
for j:=1 to n do begin
a[i,j]:=random(2);
write(a[i,j]:2,' ')
end;
write(');');
writeln;
end;
readkey;

Gd := Detect;
InitGraph(Gd, Gm, 'c:\TP\BGI');
if GraphResult <> grOk then
Halt(1);
setbkcolor(0);
y:=(getmaxy div 2); x:=(getmaxx div 2);
for i:=0 to n-1 do begin
t:=(2*pi*i)/n;
b[i+1].x:=cos(t);
b[i+1].y:=sin(t);
end;
setcolor(5);
for i:=1 to n do
for j:=1 to n do begin
if a[i,j]=1 then begin
if i<>j then
begin
line( round(x+b[j].x*rad),
round(y+b[j].y*rad),
round(x+b[i].x*rad),
round(y+b[i].y*rad));
end
else circle( round(x+b[j].x*rad*(1+pet)),
round(y+b[j].y*rad*(1+pet)),
round(rad*pet));
end;
end;

setlinestyle(0,0,2);
setcolor(3);
for i:=1 to n do
for j:=1 to n do
if (a[i,j]=1)and(i<>j) then
begin
y0:=b[i].y-b[j].y;x0:=b[i].x-b[j].x;
if x0<>0
then
if x0>0 then alfa:=arctan(y0/x0)
else alfa:=pi+arctan(y0/x0)
else if y0>0 then alfa:=pi/2 else alfa:=3*pi/2;
line( round(x+b[j].x*rad),
round(y+b[j].y*rad),
round(x+b[j].x*rad+((cos(alfa-u))*(d)*rad)),
round(y+b[j].y*rad+((sin(alfa-u))*(d)*rad)));
line( round(x+b[j].x*rad),
round(y+b[j].y*rad),
round(x+b[j].x*rad+((cos(alfa+u))*(d)*rad)),
round(y+b[j].y*rad+((sin(alfa+u))*(d)*rad)));
end; {*}

for j:=1 to n do
begin
setcolor(4);
str(j,s);
outtextxy(round(x+(b[j].x)*rad+3),round(y+(b[j].y)*rad+3), s);
setcolor(1);
circle(round(x+(b[j].x)*rad),round(y+(b[j].y)*rad),1)
end;

setcolor(7);
outtextxy(xt,yt+round(((n-1)/2)*shag),'A(G)=');
yt1:=yt;
for i:=1 to n do
begin
xt1:=xt+5*shag;
for j:=1 to n do
begin
str(a[i,j],s);
outtextxy(xt1,yt1, s);
xt1:=xt1+shag;
end;
yt1:=yt1+shag;
end;
setlinestyle(0,0,1);
line(xt+(5+n)*shag-2,yt,xt+(5+n)*shag-2,yt+(n)*shag-2);
line(xt+5*shag-2,yt,xt+5*shag-2,yt+(n)*shag-2);
setcolor(ct);
outtextxy(10,getmaxy-10,'Esche?(y,n) ');
repeat
c:=readkey;
until (c='n')or(c='y');
CloseGraph;
until c='n';
end.

Становится проще для понимания когда мозгам не приходится отвлекаться на расшифровку транслитных комментариев..
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.