实验十一  图的基本操作

| June 5, 2008 11:20 | timmy | Via Original
Test11.cpp

#include<iostream.h>
#include<stdlib.h>
#include<strstrea.h>
#include "test11_AdjM.h"
#include "test11_AdjL.h"
void testAdjM()
{
  int n,k1,k2;
  cout<<"输入待处理图的顶点数:";
  cin>>n;
  cout<<"输入图的有无向和有无权选择:";
  cin>>k1>>k2;
  adjmatrix ga;
  InitMatrix(ga,k2);
  cout<<"输入图的边集:";
  char *a=new char[100];
  cin>>a;
  CreateMatrix(ga,n,a,k1,k2);
  PrintMatrix(ga,n,k1,k2);
}
void testAdjL()
{
  int n,k1,k2;
  cout<<"输入待处理图的顶点数:";
  cin>>n;
  cout<<"输入图的有无向和有无权选择:";
  cin>>k1>>k2;
  adjlist GL;
  InitAdjoin(GL);
  cout<<"输入图的边集:";
  char *a=new char[100];
  cin>>a;
  CreateAdjoin(GL,n,a,k1,k2);
  PrintAdjoin(GL,n,k1,k2);
}
void main()
{
  char judge;
  cout<<"测试图的邻接矩阵?(Y/N)"<<endl;
  cin>>judge;
  if(judge=='Y')
    testAdjM();
  cout<<"测试图的邻接表?(Y/N)"<<endl;
  cin>>judge;
  if(judge=='Y')
    testAdjL();
}

Test11_AdjM.h

const int MaxVertexNum=10;
const int MaxEdgeNum=10;
typedef int WeightType;
typedef int VertexType;
const WeightType MaxValue=1000;
typedef VertexType vexlist[MaxVertexNum];
typedef int adjmatrix[MaxVertexNum][MaxVertexNum];

void InitMatrix(adjmatrix GA,int k)
{
  int i,j;
  for(i=0;i<MaxVertexNum;i++)
    for(j=0;j<MaxVertexNum;j++)
      if(i==j)
        GA[i][j]=0;
      else if(k)
        GA[i][j]=MaxValue;
      else
        GA[i][j]=0;
}

void CreateMatrix(adjmatrix GA,int n,char*s,int k1,int k2)
{
  istrstream sin(s);
  char c1,c2,c3;
  int i,j;
  WeightType w;
  sin>>c1;
  if(k1==0&&k2==0)
    do{
      sin>>c1>>i>>c2>>j>>c3;
      if(i*j<0||i>n-1||j>n-1)
        exit(1);
      GA[i][j]=GA[j][i]=1;
      sin>>c1;
      if(c1=='}')
        break;
    }while(1);
    else if(k1==0&&k2!=0)
    do{
      sin>>c1>>i>>c2>>j>>c3>>w;
      if(i*j<0||i>n-1||j>n-1)
        exit(1);
      GA[i][j]=GA[j][i]=w;
      sin>>c1;
      if(c1=='}')
        break;
    }while(1);
  else if(k1!=0&&k2==0)
    do{
      sin>>c1>>i>>c2>>j>>c3;
      if(i*j<0||i>n-1||j>n-1)
        exit(1);
      GA[i][j]=1;
      sin>>c1;
      if(c1=='}')
        break;
    }while(1);
  else if(k1!=0&&k2!=0)
    do{
      sin>>c1>>i>>c2>>j>>c3>>w;
      if(i*j<0||i>n-1||j>n-1)
        exit(1);
      GA[i][j]=w;
      sin>>c1;
      if(c1=='}')
        break;
    }while(1);
}

void PrintMatrix(adjmatrix GA,int n,int k1,int k2)
{
  int i,j;
  cout<<"V={";
  for(i=0;i<n-1;i++)
    cout<<i<<',';
  cout<<n-1<<')'<<endl;
  cout<<"E={";
  if(k2==0){
    for(i=0;i<n;i++)
      for(j=0;j<n;j++)
        if(GA[i][j]==1)
          if(k1==0){
            if(i<j)
              cout<<'('<<i<<','<<j<<')'<<',';
          }
          else
            cout<<'<'<<i<<','<<j<<'>'<<',';
  }
  else{
    for(i=0;i<n;i++)
      for(j=0;j<n;j++)
        if(GA[i][j]!=0&&GA[i][j]!=MaxValue)
          if(k1==0){
            if(i<j)
              cout<<'('<<i<<','<<j<<')'<<GA[i][j]<<',';
          }
          else
            cout<<'<'<<i<<','<<j<<'>'<<GA[i][j]<<',';
  }
  cout<<'}'<<endl;
}

Test11_AdjL.h

struct edgenode{
  int adjvex;
  WeightType weight;
  edgenode *next;
};
typedef edgenode *adjlist[MaxVertexNum];

void InitAdjoin(adjlist GL)
{
  for(int i=0;i<MaxVertexNum;i++)
    GL[i]=NULL;
}

void CreateAdjoin(adjlist GL,int n,char*s,int k1,int k2)
{
  istrstream sin(s);
  char c1,c2,c3;
  int i,j;
  WeightType w;
  edgenode *p;
  sin>>c1;
  if(k2==0){
    do{
      sin>>c1>>i>>c2>>j>>c3;
      p=new edgenode;
      p->adjvex=j;
      p->weight=1;
      p->next=GL[i];
      GL[i]=p;
      if(k1==0){
        p=new edgenode;
        p->adjvex=i;
        p->weight=1;
        p->next=GL[j];
        GL[j]=p;
      }
      sin>>c1;
    }while(c1==',');
  }
  else{
    do{
      sin>>c1>>i>>c2>>j>>c3>>w;
      p=new edgenode;
      p->adjvex=j;
      p->weight=w;
      p->next=GL[i];
      GL[i]=p;
      if(k1==0){
        p=new edgenode;
        p->adjvex=i;
        p->weight=w;
        p->next=GL[j];
        GL[j]=p;
      }
      sin>>c1;
    }while(c1==',');
  }
}

void PrintAdjoin(adjlist GL,int n,int k1,int k2)
{
  int i,j;
  edgenode *p;
  cout<<"V={";
  for(i=0;i<n-1;i++)
    cout<<i<<',';
  cout<<n-1<<'}'<<endl;
  cout<<"E={";
  for(i=0;i<n;i++){
    if(k2==0){
      p=GL[i];
      while(p){
        j=p->adjvex;
        if(k1==0){
          if(i<j)
            cout<<'('<<i<<','<<j<<')'<<',';
        }
        else
          cout<<'<'<<i<<','<<j<<'>'<<',';
        p=p->next;
      }
    }
    else{
      p=GL[i];
      while(p){
        j=p->adjvex;
        if(k1==0){
          if(i<j)
            cout<<'('<<i<<','<<j<<')'<<p->weight<<',';
        }
        else
          cout<<'<'<<i<<','<<j<<'>'<<p->weight<<',';
        p=p->next;
      }
    }
  }
  cout<<'}'<<endl;
}

Download ( 0 downloads)
Only registered users can download this file. Please Register or Login
Tags: ,
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember