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

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

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

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

Ответ
 
Опции темы Поиск в этой теме
Старый 24.12.2013, 02:52   #1
Мишаня@
 
Регистрация: 01.12.2012
Сообщений: 3
Вопрос задача. помогите изменить формулу в коде под нужную.Логические функции переменных

препод решил поиздеваться над моими нервами.. видите ли хочет чтоб я программу с другой формулой реализовать. подвох в том - что как раз с теми элементами, с которыми не очень ладится у меня. помогите поладить с ними пожалуйста.
по заданию используется формула на картинке 1, а нужно реализовать ту, которая на картинке 2
Код:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>

int * CycleNot(int,int,int,int);  //0
int * CycleAnd(int,int,int,int);  //1
int * CycleXor(int,int,int,int);  //2
int * CycleNor(int,int,int,int);  //4
int * CycleImpl(int,int,int,int); //6

int Not(int x) // отрицание
{
	if (x)
		return 0;
	return 1;
}

int And(int x, int y) // умножение (коньъюнкция)
{
	return x*y;
}

int Xor(int x, int y) // сложение |2|
{
	if (x!=y)
		return 1;
	return 0;
}

int Nor(int x, int y) // стрелка Пирса
{
	if (x==0 && y==0)
		return 1;
	return 0;
}

int Impl(int x, int y) // импликация 
{
	if (x==1 && y==0)
		return 0;
	return 1;
}

// функции циклов обработки по формулам
int * CycleNot(int *x)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Not(x[i]);
	}
	return z;
}

int * CycleAnd(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = And(x[i],y[i]);
	}
	return z;
}

int * CycleXor(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Xor(x[i],y[i]);
	}
	return z;
}

int * CycleNor(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Nor(x[i],y[i]);
	}
	return z;
}

int * CycleImpl(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Impl(x[i],y[i]);
	}
	return z;
}

//   -=Программа=-

int main()
{
	clrscr();

	// инициализация переменных
	int  x1[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
	int  x2[16] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1};
	int  x3[16] = {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
	int  x4[16] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
	int *temp1;
	int *temp2;
	int *temp3;
	int *temp4;
	
	temp1 = CycleNot(x4);
	temp2 = CycleAnd(x3,temp1);
	temp1 = CycleNot(x2);
	temp3 = CycleAnd(x1,temp1);
	temp1 = CycleXor(temp2,temp3);
	temp2 = CycleNot(temp1);

	temp1 = CycleNot(x1);
	temp3 = CycleAnd(temp1,x2);
	temp1 = CycleNot(x4);
	temp4 = CycleAnd(x3,temp1);
	temp1 = CycleNor(temp3,temp4);

	temp3 = CycleNot(x3);
	temp4 = CycleAnd(x1,temp3);
	temp3 = CycleXor(temp1,temp4);

	temp1 = CycleImpl(temp2,temp3);

	cout << "-------------\nx1 x2 x3 x4 f\n-------------";

	for (int i = 0; i < 16; i++) // выводим функцию на экран
	{
		cout << endl << x1[i] << "  ";
		cout <<         x2[i] << "  ";
		cout <<         x3[i] << "  ";
		cout <<         x4[i] << "  ";
		cout <<         temp1[i] << endl;
	}
	cout << "-------------";

	cout << "\n\n‘„Ќ”: ";
	int f = 0;
	for (i = 0; i < 16; i++) //‘„Ќ”
	{
	  if (temp1[i]==1)
	  {
	    f++;
	    if (!x1[i]) cout << "n";
	    cout << "x1*";
	    if (!x2[i]) cout << "n";
	    cout << "x2*";
	    if (!x3[i]) cout << "n";
	    cout << "x3*";
	    if (!x4[i]) cout << "n";
	    cout << "x4";
	    if (i<15) cout << " + ";
	    if (f==4 || f==8 || f==12) cout << endl << "       ";
	  }
	}

     cout << "\n\n‘ЉЌ”: ";
     f = 0;
     for (i = 0; i < 16; i++) //‘ЉЌ”
      {
	if (temp1[i]==0)
	{
	  f++;
	  cout << "(";
	  if (x1[i]) cout << "n";
	  cout << "x1+";
	  if (x2[i]) cout << "n";
	  cout << "x2+";
	  if (x3[i]) cout << "n";
	  cout << "x3+";
	  if (x4[i]) cout << "n";
	  cout << "x4";
	  cout << ") ";
	  if (f==4 || f==8 || f==12) cout << endl << "       ";
	}
      }
Изображения
Тип файла: jpg 11111.jpg (34.4 Кб, 141 просмотров)
Тип файла: png ScreenShot 23.png (1.3 Кб, 79 просмотров)
Мишаня@ вне форума Ответить с цитированием
Старый 24.12.2013, 06:29   #2
SaLoKiN
Форумчанин
 
Аватар для SaLoKiN
 
Регистрация: 19.09.2013
Сообщений: 597
По умолчанию

Судя по тому что здесь написано и по тем проблемам которые у тебя возникают, все сразу понятно. Код не твой и ты в нем не шаришь. Препод это понял и вот поэтому дал иную формулу. На которую в этой программе нужно не более 5 минут.
Тупо написать правильный код это слишком будет просто для тебя. поэтому только наводка. вся формула заключена вот в этих строках
Код:
	temp1 = CycleNot(x4);
	temp2 = CycleAnd(x3,temp1);
	temp1 = CycleNot(x2);
	temp3 = CycleAnd(x1,temp1);
	temp1 = CycleXor(temp2,temp3);
	temp2 = CycleNot(temp1);

	temp1 = CycleNot(x1);
	temp3 = CycleAnd(temp1,x2);
	temp1 = CycleNot(x4);
	temp4 = CycleAnd(x3,temp1);
	temp1 = CycleNor(temp3,temp4);

	temp3 = CycleNot(x3);
	temp4 = CycleAnd(x1,temp3);
	temp3 = CycleXor(temp1,temp4);

	temp1 = CycleImpl(temp2,temp3);
Сделал сам, помоги другому!
Что-то работает не так? Дебаггер в помощь!!!
SaLoKiN вне форума Ответить с цитированием
Старый 03.01.2014, 23:56   #3
lisica198808
Пользователь
 
Регистрация: 06.11.2012
Сообщений: 64
По умолчанию какая круглая земля)не хочу дублировать тему

удивляешься иногда, когда видишь в сети точную копию своего задания
не хочу дублировать тему, потому что я просто переделала данный код под тот рабочий вариант что мне нужен.
но все равно данные по ручному просчету немного расходятся по данной предложеной формуле. подскажите что не так. я ведь только учусь
Код:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>

int * CycleNot(int,int,int,int);  //0// отрицание
int * CycleAnd(int,int,int,int);  //1// умножение (конъюнкция)
int * CycleXor(int,int,int,int);  //2// сложение |2|
int * CycleNor(int,int,int,int);  //4// стрелка Пирса
int * CycleNors(int,int,int,int);  //5// Шефнер
int * CycleImpl(int,int,int,int); //6// импликация 
int * CycleEkv(int,int,int,int);//7// эквиваленция

int Not(int x) // отрицание
{
	if (x)
		return 0;
	return 1;
}

int And(int x, int y) // умножение (коньъюнкция)
{
	return x*y;
}

int Xor(int x, int y) // сложение |2|
{
	if (x!=y)
		return 1;
	return 0;
}

int Nor(int x, int y) // стрелка Пирса
{
	if (x==0 && y==0)
		return 1;
	return 0;
}
int Nors(int x, int y) // Шефнер
{
	if (x==1 && y==1)
		return 0;
	return 1;
}

int Impl(int x, int y) // импликация 
{
	if (x==1 && y==0)
		return 0;
	return 1;
}
int Ekv(int x, int y) // эквиваленция
{
	if (x=y)
		return 1;
	return 0;
}
// функции циклов обработки по формулам
int * CycleNot(int *x)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Not(x[i]);
	}
	return z;
}

int * CycleAnd(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = And(x[i],y[i]);
	}
	return z;
}

int * CycleXor(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Xor(x[i],y[i]);
	}
	return z;
}

int * CycleNor(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Nor(x[i],y[i]);
	}
	return z;
}


int * CycleNors(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Nors(x[i],y[i]);
	}
	return z;
}

int * CycleImpl(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Impl(x[i],y[i]);
	}
	return z;
}
int * CycleEkv(int *x,int *y)
{
	int *z = new int[16];
	for (int i = 0; i < 16; i++)
	{
		*(z+i) = Ekv(x[i],y[i]);
	}
	return z;
}

//   -=Программа=-

int main()
{
	
	// инициализация переменных
	int  x1[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
	int  x2[16] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1};
	int  x3[16] = {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
	int  x4[16] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
	int *temp1;
	int *temp2;
	int *temp3;
	int *temp4;
	
	temp1 = CycleNot(x3);
	temp2 = CycleAnd(temp1,x4);
	
	temp1 = CycleNot(x2);
	temp3 = CycleAnd(x1,temp1);
	
	temp1 = CycleNors(temp2,temp3);
	
	temp2 = CycleAnd(x2,x3);
	temp3 = CycleNot(temp2);

	
	temp2=CycleImpl(temp1,temp3);
	
	temp1=CycleAnd(x1,x2);
	temp3=CycleAnd(x1,x4);
	temp4=CycleEkv(temp1,temp3);
	/*ekv*/
	
	temp1 = CycleXor(temp2,temp4);
	

	cout << "-------------\nx1 x2 x3 x4 f\n-------------";
	for (int i = 0; i < 16; i++) // выводим функцию на экран
	{
		cout << endl << x1[i] << "  ";
		cout <<         x2[i] << "  ";
		cout <<         x3[i] << "  ";
		cout <<         x4[i] << "  ";
		cout <<         temp1[i] << endl;
	}
	cout << "-------------";
	cout << "\n\n SDNF: ";
	int f = 0;
	for (int i = 0; i < 16; i++) //'"Ќ"
	{
	  if (temp1[i]==1)
	  {
	    f++;
	    if (!x1[i]) cout << "!";
	    cout << "x1*";
	    if (!x2[i]) cout << "!";
	    cout << "x2*";
	    if (!x3[i]) cout << "!";
	    cout << "x3*";
	    if (!x4[i]) cout << "!";
	    cout << "x4";
	    if (i<15) cout << " + ";
	    if (f==4 || f==8 || f==12) cout << endl << "       ";
	  }
	}
     cout << "\n\n SKNF: ";
     f = 0;
     for (int i = 0; i < 16; i++) //'ЉЌ"
      {
	if (temp1[i]==0)
	{
	  f++;
	  cout << "(";
	  if (x1[i]) cout << "!";
	  cout << "x1+";
	  if (x2[i]) cout << "!";
	  cout << "x2+";
	  if (x3[i]) cout << "!";
	  cout << "x3+";
	  if (x4[i]) cout << "!";
	  cout << "x4";
	  cout << ") ";
	  if (f==4 || f==8 || f==12) cout << endl << "       ";
	}
      }
      cout << "\n";
      system("pause");
	return 0;
}
кстати,формула та же, чему приятно удивлена
свой ручной просчет прилагаю - расхождений немного , но почему то они есть. видимо формулу не так както записала. помогите пожалуйста
Изображения
Тип файла: jpg !!!!!!!!!!!!!!!!!!!!!!!!!!!! - копия.jpg (48.8 Кб, 142 просмотров)
lisica198808 вне форума Ответить с цитированием
Старый 04.01.2014, 01:20   #4
BDA
МегаМодератор
СуперМодератор
 
Аватар для BDA
 
Регистрация: 09.11.2010
Сообщений: 7,429
По умолчанию

Во-первых, кто же Вас всех учит-то так? Утечки памяти налицо (потери указателей, неосвобождение памяти). Во-вторых, вот чуток поправленный код. Выдает таблицу, совпадающую с Вашей.
Код:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

using namespace std;

int Not(int x) // отрицание
{
    return !x;
}

int And(int x, int y) // умножение (коньъюнкция)
{
    return x * y;
}

int Xor(int x, int y) // сложение |2|
{
    return x != y;
}

int Nor(int x, int y) // стрелка Пирса
{
    return !(x || y);
}
int Nors(int x, int y) // Шеффер
{
    return !(x && y);
}

int Impl(int x, int y) // импликация 
{
    return !(x && !y);
}
int Ekv(int x, int y) // эквиваленция
{
    return x == y;
}
// функции циклов обработки по формулам
void
CycleNot(int *x, int *y)
{
    for (int i = 0; i < 16; i++)
        y[i] = Not(x[i]);
}

void
CycleAnd(int *x, int *y, int *z)
{
    for (int i = 0; i < 16; i++)
        z[i] = And(x[i], y[i]);
}

void
CycleXor(int *x, int *y, int *z)
{
    for (int i = 0; i < 16; i++)
        z[i] = Xor(x[i], y[i]);
}

void
CycleNor(int *x, int *y, int *z)
{
    for (int i = 0; i < 16; i++)
        z[i] = Nor(x[i], y[i]);
}


void
CycleNors(int *x, int *y, int *z)
{
    for (int i = 0; i < 16; i++)
        z[i] = Nors(x[i], y[i]);
}

void
CycleImpl(int *x, int *y, int *z)
{
    for (int i = 0; i < 16; i++)
        z[i] = Impl(x[i], y[i]);
}

void
CycleEkv(int *x, int *y, int *z)
{
    for (int i = 0; i < 16; i++)
        z[i] = Ekv(x[i], y[i]);
}

//   -=Программа=-

int
main()
{
    
    // инициализация переменных
    int  x1[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
    int  x2[16] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1};
    int  x3[16] = {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
    int  x4[16] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
    int temp1[16], temp2[16], temp3[16], temp4[16];

    CycleNot(x3, temp1);
    CycleAnd(temp1, x4, temp2);

    CycleNot(x2, temp1);
    CycleAnd(x1, temp1, temp3);

    CycleNors(temp2, temp3, temp1);

    CycleAnd(x2, x3, temp2);
    CycleNot(temp2, temp3);

    CycleImpl(temp1, temp3, temp2);

    CycleAnd(x1, x2, temp1);
    CycleAnd(x1, x4, temp3);
    CycleEkv(temp1, temp3, temp4);
    CycleNot(temp4, temp3);

    CycleXor(temp2, temp3, temp1);

    cout << "-------------\nx1 x2 x3 x4 f\n-------------";
    for (int i = 0; i < 16; i++) // выводим функцию на экран
    {
        cout << endl << x1[i] << "  ";
        cout <<         x2[i] << "  ";
        cout <<         x3[i] << "  ";
        cout <<         x4[i] << "  ";
        cout <<         temp1[i] << endl;
    }
    cout << "-------------";
    cout << "\n\n SDNF: ";
    int f = 0;
    for (int i = 0; i < 16; i++)
        if (temp1[i]==1)
        {
            f++;
            if (!x1[i]) cout << "!";
            cout << "x1*";
            if (!x2[i]) cout << "!";
            cout << "x2*";
            if (!x3[i]) cout << "!";
            cout << "x3*";
            if (!x4[i]) cout << "!";
            cout << "x4";
            if (i<15) cout << " + ";
            if (f==4 || f==8 || f==12) cout << endl << "       ";
        }
    cout << "\n\n SKNF: ";
    f = 0;
    for (int i = 0; i < 16; i++)
        if (temp1[i]==0)
        {
            f++;
            cout << "(";
            if (x1[i]) cout << "!";
            cout << "x1+";
            if (x2[i]) cout << "!";
            cout << "x2+";
            if (x3[i]) cout << "!";
            cout << "x3+";
            if (x4[i]) cout << "!";
            cout << "x4";
            cout << ") ";
            if (f==4 || f==8 || f==12) cout << endl << "       ";
        }
      cout << "\n";
      system("pause");
    return 0;
}
Вообще-то, программу можно еще красивее написать...
Пишите язык программирования - это форум программистов, а не экстрасенсов. (<= это подпись )

Последний раз редактировалось BDA; 04.01.2014 в 01:26.
BDA вне форума Ответить с цитированием
Старый 04.01.2014, 01:27   #5
BDA
МегаМодератор
СуперМодератор
 
Аватар для BDA
 
Регистрация: 09.11.2010
Сообщений: 7,429
По умолчанию

Продолжение
Например:
Код:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

using namespace std;

int Not(int x) // отрицание
{
    return !x;
}

int And(int x, int y) // умножение (конъюнкция)
{
    return x * y;
}

int Xor(int x, int y) // сложение |2|
{
    return x != y;
}

int Nor(int x, int y) // стрелка Пирса
{
    return !(x || y);
}
int Nors(int x, int y) // Шеффер
{
    return !(x && y);
}

int Impl(int x, int y) // импликация 
{
    return !(x && !y);
}
int Ekv(int x, int y) // эквиваленция
{
    return x == y;
}
// функции циклов обработки по формулам
void
CycleNot(int *x, int *y)
{
    for (int i = 0; i < 16; i++)
        y[i] = Not(x[i]);
}

void
Cycle(int *x, int *y, int *z, int (*f)(int, int))
{
    for (int i = 0; i < 16; i++)
        z[i] = f(x[i], y[i]);
}

int
main()
{
    // инициализация переменных
    int  x1[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
    int  x2[16] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1};
    int  x3[16] = {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
    int  x4[16] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
    int temp1[16], temp2[16], temp3[16], temp4[16];

    CycleNot(x3, temp1);
    Cycle(temp1, x4, temp2, And);

    CycleNot(x2, temp1);
    Cycle(x1, temp1, temp3, And);

    Cycle(temp2, temp3, temp1, Nors);

    Cycle(x2, x3, temp2, And);
    CycleNot(temp2, temp3);

    Cycle(temp1, temp3, temp2, Impl);

    Cycle(x1, x2, temp1, And);
    Cycle(x1, x4, temp3, And);
    Cycle(temp1, temp3, temp4, Ekv);
    CycleNot(temp4, temp3);

    Cycle(temp2, temp3, temp1, Xor);

    cout << "-------------\nx1 x2 x3 x4 f\n-------------";
    for (int i = 0; i < 16; i++) // выводим функцию на экран
    {
        cout << endl << x1[i] << "  ";
        cout <<         x2[i] << "  ";
        cout <<         x3[i] << "  ";
        cout <<         x4[i] << "  ";
        cout <<         temp1[i] << endl;
    }
    cout << "-------------";
    cout << "\n\n SDNF: ";
    int f = 0;
    for (int i = 0; i < 16; i++)
        if (temp1[i]==1)
        {
            f++;
            if (!x1[i]) cout << "!";
            cout << "x1*";
            if (!x2[i]) cout << "!";
            cout << "x2*";
            if (!x3[i]) cout << "!";
            cout << "x3*";
            if (!x4[i]) cout << "!";
            cout << "x4";
            if (i<15) cout << " + ";
            if (f==4 || f==8 || f==12) cout << endl << "       ";
        }
    cout << "\n\n SKNF: ";
    f = 0;
    for (int i = 0; i < 16; i++)
        if (temp1[i]==0)
        {
            f++;
            cout << "(";
            if (x1[i]) cout << "!";
            cout << "x1+";
            if (x2[i]) cout << "!";
            cout << "x2+";
            if (x3[i]) cout << "!";
            cout << "x3+";
            if (x4[i]) cout << "!";
            cout << "x4";
            cout << ") ";
            if (f==4 || f==8 || f==12) cout << endl << "       ";
        }
      cout << "\n";
      system("pause");
    return 0;
}
Может быть, можно еще что-нибудь, но думать лень
Пишите язык программирования - это форум программистов, а не экстрасенсов. (<= это подпись )
BDA вне форума Ответить с цитированием
Ответ


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



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Логические функции borovi4ok Microsoft Office Excel 1 19.12.2013 22:01
как найти нужную таблицу в HTML коде saydash Работа с сетью в Delphi 2 03.06.2011 09:21
Вычисление функции на Паскале - изменить формулу Veina Помощь студентам 3 09.04.2011 20:04
Изменить формулу olimpus Microsoft Office Excel 5 12.12.2009 05:45
Подскажите нужную формулу(С++) Olya90 Помощь студентам 4 25.06.2009 18:13