создать инструмент, который управляет отношением эквивалентности, которое может быть изменено программой определенным образом

В чем дело:
Итак, я написал программу, которая управляет отношениями эквивалентности и не включает в себя основную. Я должен протестировать его в отдельном файле, где я делаю ER (отношение эквивалентности), объединяю некоторые вещи и показываю результат.

Что мне нужно помочь с:
Я не уверен, как записать тестовый файл. Я не думаю, что я делаю это правильно, потому что это не работает, но я не знаю, что случилось. Я не уверен, должен ли я просто запустить функцию или использовать оператор print, чтобы показать, что она возвращает?

Код, который я буду отображать:
Я покажу экв.ч (это код для отдельного файла, который мне нужно использовать и который мне дал мой профессор), экв.cpp (код со всеми функциями, над которыми я работал, и мне нужно test) и testequiv.cpp (файл, который я должен использовать для проверки каждой функции) в этом порядке.

экв.ч:

// These #ifndef and #define lines make it so that, if this file is
// read more than once by the compiler, its body is skipped on all
// but the first time it is read.

#ifndef EQUIV_H
#define EQUIV_H

// An equivalence relation is an array of integers.
// So ER abbreviates int*.

typedef int* ER;

// Public functions

ER   newER      (const int n);
void destroyER  (ER R);
bool equivalent (ER R, const int x, const int y);
void merge      (ER R, const int x, const int y);

// The following is advertised here solely for debugging.  These must
// only be used for debugging.

void showER(const ER R, const int n);
int  leader(ER R, const int x);

#endif

экв.cpp:

// This program is a tool that manages an equivalence relation that can be
// changed in a specific way by the program#include <cstdio>
using namespace std;

// newER(n) returns an equivalence relation that can handle set {1, 2, …, n}.

int* newER(int n)
{
int* R = new int[n];
for(int i = 1; i < n; i++)
{
R[i] = i;
}
return R;
}

//showER(R, n) prints the entire contents of array R (of size n) in a
//readable form for debugging.

void showER(int* R, int n)
{
for (int i = 1; i <= n; i++)
{
printf("%d",R[i]);
}
printf("\n");
}

//leader(R, x) returns the leader of x in equivalence relation R.

int leader(int* R,int x)
{
if (R[x] == 0)
{
return x;
}
else
{
return leader(R, R[x]);
}
}

//equivalent(R, x, y) returns true if x and y are currently in the same
//equivalence class in equivalence relation R.

bool equivalent(int* R,int x,int y)
{
if (leader(R, x) == leader(R, y))
{
return true;
}
else
{
return false;
}
}

//merge(R, x, y) merges the equivalence classes of x and y in R.

void merge(int* R, int x, int y)
{
if(!equivalent(R, x, y))
{
int xLeader = leader(R,x), yLeader = leader(R, y);
R[xLeader] = yLeader;
}
}

//destroyER(R) deallocates R.

int destroyER(int* R)
{
delete R;
}int main()
{
return 0;
}

testequiv.cpp Настройка для проверки функции newER:

#include <cstdio>
#include "equiv.h"using namespace std;

int main()
{
newER(7);

return 0;
}

НОВЫЙ КОД!
Так что я работал над этим, и мои новые коды следующие:
equiv.cpp

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
typedef int* ER;

// newER(n) returns an equivalence relation that can handle set {1, 2, …, n}.

ER newER(const int n)
{
ER R = new int[n+1];

int i;

for(i=0; i<=n + 1; i++)
{
R[i]= i;
}
return R;
}

//showER(R, n) prints the entire contents of array R (of size n) in a
//readable form for debugging.

void showER(const ER R, const int n)
{
for (int i=1; i<=n; i++)
{
printf("%d",R[i]);
}
printf("\n");
}

//leader(R, x) returns the leader of x in equivalence relation R.

int leader(ER R,const int x)
{
if(R[x]==0)
{
return x;
}
if(R[x]==x)
{
return x;
}
else
{
return leader(R, R[x]);
}
}

//equivalent(R, x, y) returns true if x and y are currently in the same
//equivalence class in equivalence relation R.

bool equivalent(ER R,const int x,const int y)
{
if (leader(R, x) == leader(R, y))
{
return true;
}
else
{
return false;
}
}

//merge(R, x, y) merges the equivalence classes of x and y in R.

void merge(ER R,const int x,const int y)
{
int x1, y1;
if(!equivalent(R, x, y))
{
x1=leader(R,x);
y1=leader(R,y);
R[x1]=y1;
}
}

//destroyER(R) deallocates R.

void destroyER(ER R)
{
delete[] R;
}int main()
{
return 0;
}

testequiv.cpp

#include <cstdio>
#include "equiv.h"#include <iostream>
#include <string>

using namespace std;
typedef int* ER;
int main()
{
//Create new ER

ER R = newER(7);

//Display ER

cout<<"Show ER:"<<endl;

showER(R, 7);

cout<<endl;

//Call Function to merge

merge(R, 1, 5);

cout<<"\nER after merging 1 and 5:"<<endl;

//Display ER after merging

showER(R, 7);

cout<<endl;

//Call Function to merge

merge(R, 2, 7);

cout<<"\nER after merging 2 and 7:"<<endl;

//Display ER after merge

showER(R, 7);

cout<<endl;

//Call Function to test 1 and 5 are present in the same equivalence class

bool ts1 = equivalent(R, 1, 5);

//Check condition

if( ts1)

{

//Print message

cout<<"{1,5} is equivalence class"<<endl;

}

//If condition fails

else

//Print message

cout<<"{1,5} does not form an equivalence class."<<endl;

//Call Function to test 1 and 7 are present in the same equivalence class

bool ts2 = equivalent(R, 1, 7);

//Check condition

if( ts2)

{

//Print message

cout<<"1 and 7 are in the same equivalence class"<<endl;

}

else

//Print message

cout<<"1 and 7 are not in the same equivalence class."<<endl;

//Call function to merge 5 and 7 in the same equivalence class

merge(R, 5, 7);

cout<<"\nER after merging 5 and 7:"<<endl;

showER(R, 7);

cout<<endl;

//Call Function to test 2 and 5 are present in the same equivalence class

ts1 = equivalent(R, 2, 5);

//Check condition

if( ts1)

{

//Print message

cout<<"2 and 5 are present in the same equivalence class"<<endl;

}

else

//Print message

cout<<"2 and 5 are not present in the same equivalence class"<<endl;

//Call Function to test 2 and 3 are present in the same equivalence class

ts1 = equivalent(R, 2, 3);

if( ts1)

{

//Print message

cout<<"2 and 3 are present in the same equivalence class"<<endl;

}

else

//Print message

cout<<"2 and 3 are not present in the same equivalence class"<<endl;

//Call function to merge 2 and 3

merge(R, 2, 3);

cout<<"\nER after merging 2 and 3:"<<endl;

showER(R, 7);

cout<<endl;

//Call Function to test 3 and 7 are present in the same equivalence class

ts1 = equivalent(R, 3, 7);

//Check condition

if( ts1)

{

//Print message

cout<<"3 and 7 are present in the same equivalence class"<<endl;

}

else

//Print message

cout<<"3 and 7 are not present in the same equivalence class"<<endl;

//Call Function to test 4 and 7 are present in the same equivalence class

ts1 = equivalent(R, 4, 7);

//Check condition

if( ts1)

{

//Print message

cout<<"4 and 7 are present in the same equivalence class"<<endl;

}

else

//Print message

cout<<"4 and 7 are not present in the same equivalence class"<<endl;

//Call Function to merge 4 and 6 in the same equivalence class

merge(R, 4, 6);

cout<<"\nER after merging 4 and 6:"<<endl;

showER(R, 7);

cout<<endl;

//Call function to merge 2 and 3 in the same equivalence class

merge(R, 2, 3);

cout<<"\nER after merging 2 and 3:"<<endl;

showER(R, 7);

cout<<endl;

//pause window

cin.get();

//stop

return 0;
}

Почему он говорит такие вещи, как:
testequiv.cpp :(. text + 0xe): неопределенная ссылка на newER(int)'
testequiv.cpp:(.text+0x3f): undefined reference to
showER (int *, int) ‘

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector