Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 25.06.2015, 16:49   #1
jdk
Новичок
Джуниор
 
Регистрация: 25.06.2015
Сообщений: 3
По умолчанию

Код:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstring>

using namespace std;

class Logfunc{
	private:
		int _n; // the number of input variables
		int _m; // the number of functions
		int _step; // step
		unsigned int _c; // the number of rows in the table
		unsigned int* func; // generated function
		void generate(); // function generation
		bool* tobool(unsigned int p, int n); // transfer of an integer to an array of booleans
		
	public:
		Logfunc(int n=3, int m=2, int step=2); // the constructor
		~Logfunc(); // the destructor
		bool* calc(bool* params); // Evaluation of the function
		void write(char* filename); // write dates to file
};

// class constructor
Logfunc::Logfunc(int n, int m, int step): _n(n), _m(m), _step(step){
	generate(); // generate function
}
	
// destructor  	
	Logfunc::~Logfunc(){
		free (func); // make free memory from the table
}

// function generator
void Logfunc::generate(){
	_c=1 << _n; // rows quantity of table(2**n)
	func=(unsigned int*)malloc(_c * sizeof(unsigned int)); // allocates memory for the table
	int max=1<<_m; // maximum decimal value of the output combinations(2**m)
	int r=(_step>=0)?0:max-1; //Function result for the first row in the table
	for (unsigned int i=0;i<_c;i++){
		func[i]=r; //write value in the table
		r+=_step; //increase value for the next step
		if(r>=max) // cycle for the succession increasing
			r=0;
		if(r<0) // cycle for the succession decreasing
			r=max-1;
	}
}

//transfer from an integer to an array of booleans
bool* Logfunc::tobool(unsigned int p, int n){
	bool* r=(bool*)malloc(n * sizeof(bool)); // allocates memory for an answer
	memset(r, 0, n * sizeof(bool)); // fills an array by 0
	int i=n;
	while (p>0 && i>0){
		bool b=p%2; // determine the value of the last bit
		p/=2; // seperate last bit
		r[--i]=b; // write bit value to an array of result	
	}
	return r;
}

// calculation of the values of function
bool* Logfunc::calc(bool* params){
	unsigned int ind=0;
	for(int i=0; i<_n; i++) // define line number in the table
		ind=(ind<<1)+params[i];
	unsigned int r=func[ind]; // define function value
	return tobool(r, _m);
}

// write result to file
void Logfunc::write(char* filename){
	ofstream fout(filename); // create file
	for(unsigned int i=0; i<_c; i++){ // We go through all the rows
		bool* p=tobool(i,_n); // We get a list of Boolean values
		bool* r=calc(p); // We calculate the value of the function
		for(int j=0;j<_n;j++)
			fout<<p[j]?1:0; // displays a list of variables
		fout<<" "; // separation gap
		for(int j=0; j<_m; j++)
			fout<<r[j]?1:0; // displays a list of variables
		fout<<endl; //displays end of row
		free(p); //free memory
		free(r); //free memory
	}
	fout.close(); // close file
}

int main()
{
	int step = 1;
	int n, m;
	cout<<"The number of input variables: ";
	cin>>n;
	if(n<1 || n>28){
		cout<<"Input error"<<endl;
		system("pause");
		return 1;
	}
	cout<<"Number of functions m : ";
	cin>>m;
		if(m<1 || m>32){
		cout<<"Input error"<<endl;
		system("pause");
		return 1;
	}	
	
	Logfunc f(n, m, step); //create function
	f.write("output.txt"); 
	cout<<"Result is saved to file - output.txt"<<endl;
	system("pause"); //delay before programm closing
	return 0;	
}
Помогите пожалуйста переделать код на С!

Последний раз редактировалось Stilet; 25.06.2015 в 16:56.
jdk вне форума Ответить с цитированием
Старый 25.06.2015, 16:56   #2
Stilet
Белик Виталий :)
Старожил
 
Аватар для Stilet
 
Регистрация: 23.07.2007
Сообщений: 57,097
По умолчанию

А как задание-то звучит хоть?
I'm learning to live...
Stilet вне форума Ответить с цитированием
Старый 25.06.2015, 17:27   #3
jdk
Новичок
Джуниор
 
Регистрация: 25.06.2015
Сообщений: 3
По умолчанию

Создать программу, генерирующую систему логических функций с заданными параметрами n - число входных переменных, m - число функций.
В соответствии с заданным чмслом генерируется (2 ** n) входных комбинаций и каждой входной комбинации по индивидуальному заданию присваиваится одна из (2 ** m) выходных комбинаций (начиная с комбинации все нули) в циклической последовательности.
Записать сгенерированную систему логических функций в виде файла.
При создании функции предусмотреть возможность задания параметров m, n в качестве аргументов командной строки.
Генерация выходных комбинаций в порядке возрастания с шагом 1.
jdk вне форума Ответить с цитированием
Старый 25.06.2015, 19:17   #4
jdk
Новичок
Джуниор
 
Регистрация: 25.06.2015
Сообщений: 3
По умолчанию

Помогите пожалуйста.
jdk вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
помогите переделать программу!Delphi екатерина новикова Общие вопросы Delphi 2 09.12.2013 05:37
Помогите,пожалуйста,переделать программу. vep Общие вопросы C/C++ 5 19.10.2009 00:35
Помогите переделать программу на VS C++ Many man Помощь студентам 1 16.10.2009 19:26
Помогите переделать программу. Valentin49 Помощь студентам 1 26.03.2009 13:58
Помогите переделать программу немного. texcel Общие вопросы C/C++ 1 16.02.2009 19:42