0
First of all, I want to thank everyone who reads my call for help. I’m pretty new to the C language, and I’m pretty sure I made a mistake, and I’m pretty sure I didn’t miss that code. I spent all day trying to write it but I can’t find the mistake. I think it’s some inappropriate use of pointers in the swap() function or some other silly thing I did. Anyway, there it is. Please forgive the noobagens, I did everything with my best. The program even accepts some moves, but most do not. I am using Ubuntu in its latest version.
Edit1: The problem with jugs is the following: You have three jars on a table in front of you. The first holds three litres, the second five litres and the last eight litres, and is the only one that is full. Your goal is to separate four liters in the b and c jugs, and the only move you can make is to exchange the liquid from one jug to the other until it reaches the limit of the jug you receive, or until the liquid in the vase you pour runs out.
Edit2: I made some small modifications and the program hits some answers precisely, for example:
State:
a = 0, b = 0, c = 8
>3 1
State:
a = 3, b = 0, c = 5
But transferring from c to b does not return the expected:
State:
a = 3, b = 0, c = 5
>3 2
State:
a = 3, b = 3, c = 2
Thanks in advance.
Edit3: Commented code for better understanding.
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#define MAXA 3 //Defino o maximo da variável a, e etc;
#define MAXB 5
#define MAXC 8
int a = 0, b = 0, c = 8; //Defino que só a terceira está cheia.
void swap(int *po, int *pd) //Função de troca, aceita dois ponteiros para inteiros,
{ // Mas não sei se estou a usar corretamente;
int maxor, maxdes; //Defino duas variáveis que comportarão o máximo da origem e destino.
if(po != &a && po != &b && po != &c || pd != &a && pd != &b && pd != &c || po == pd) //Caso for inválido,
return; //quebre.
if(po == &a) //Se o ponteiro recebido apontar para a, máximo da origem é MAXA, etc...
maxor = MAXA;
if(po == &b)
maxor = MAXB;
if(po == &c)
maxor == MAXC;
if(pd == &a)
maxdes = MAXA;
if(pd == &b)
maxdes == MAXB;
if(pd == &c)
maxdes == MAXC;
int d = maxdes - *pd; //Variável que define o quanto o destino comporta, e o quanto a origem fornecerá.
*pd += d;
*po -= d;
}
int main()
{
int *pa = &a; //Defino os ponteiros para as respectivas variáveis.
int *pb = &b;
int *pc = &c;
while(b != 4 && c != 4) //Enquanto não houver quatro litros em b e c;
{
int (*alfa), (*beta); //Ponteiros que eventualmente apontarão pra escolha do usuário.
int cho, chos; //Inteiros de escolha de origem e destino.
printf("State:\na = %i, b = %i, c = %i\n>", a, b, c); //Imprime o estado atual.
scanf("%i %i", &cho, &chos);
if(cho == 1) //Caso cho seja 1, o ponteiro alfa aponta para a, e etc.
alfa = &a;
if(cho == 2)
alfa = &b;
if(cho == 3)
alfa = &c;
if(chos == 1)
beta = &a;
if(chos == 2)
beta = &b;
if(chos == 3)
beta = &c;
swap(alfa, beta); //Realize a troca entre alfa e beta;
}
}
In your swap function, first if, do you really want to compare the address of the variables? I see no sense in it, I think I should compare the contents of these variables.
– anonimo