_13th_Dragon na razie ma działać. Jak dobrze zrozumiem zasadę działania to wtedy można optymalizować, ale dziękuję za wskazówkę. Na pewno się zastanowię nad tym co napisałeś. A tymczasem skoro temat nie został zamknięty, mam jeszcze jedno pytanko...
Mój program delikatnie urósł:
#include <stdlib.h>
#include <string.h>
#include "stdafx.h"
typedef struct product
{
unsigned short int productPriority;
float productPrice;
struct product *nextProduct;
char productName[50];
}product;
float money;
void addProductToList(product **firstOnList, unsigned short int productPriority, float productPrice, char productName)
{
product *temp, *ptr;
if (!(*firstOnList))
{
temp = (product*) malloc(sizeof(struct product));
temp->productPriority = productPriority;
temp->productPrice = productPrice;
strcpy(temp->productName,&productName);
temp->nextProduct = NULL;
*firstOnList = temp;
printf("Nazwa: %s\n",&temp->productName);
}
else
{
if ((*firstOnList)->nextProduct)
{
if (productPriority<(*firstOnList)->productPriority)
{
temp = (product*) malloc(sizeof(struct product));
temp->productPriority = productPriority;
temp->productPrice = productPrice;
ptr = *firstOnList;
temp->nextProduct = ptr;
*firstOnList = temp;
}
else
{
ptr = *firstOnList;
while ((ptr->nextProduct)&&((ptr->nextProduct)->productPriority<productPriority))
{
ptr = ptr->nextProduct;
}
temp = (product*) malloc(sizeof(struct product));
temp->productPriority = productPriority;
temp->productPrice = productPrice;
temp->nextProduct = ptr->nextProduct;
ptr->nextProduct = temp;
}
}
else
{
if (productPriority<(*firstOnList)->productPriority)
{
temp = (product*) malloc(sizeof(struct product));
temp->productPriority = productPriority;
temp->productPrice = productPrice;
ptr = *firstOnList;
temp->nextProduct = ptr;
*firstOnList = temp;
}
else
{
temp = (product*) malloc(sizeof(struct product));
temp->productPriority = productPriority;
temp->productPrice = productPrice;
temp->nextProduct = NULL;
(*firstOnList)->nextProduct = temp;
}
}
}
};
void writeProductList(product **firstOnList)
{
product *temp = *firstOnList;
if(!temp)
{
printf("Lista jest pusta!\n");
}
else
{
printf("Nazwa: %s\tPriorytet: %hu\tCena: %.2f\n",&temp->productName,temp->productPriority,temp->productPrice);
while(temp->nextProduct != NULL)
{
temp = temp->nextProduct;
printf("Priorytet: %hu\tCena: %.2f\n",temp->productPriority,temp->productPrice);
}
}
};
void makeShopping(product **firstOnList, float *money)
{
product *temp = *firstOnList;
unsigned short int counter;
counter = 0;
if(!temp)
{
printf("Lista jest pusta!\n");
}
else
{
while ((*money-((temp)->productPrice))>=0)
{
counter++;
*money = *money-(temp)->productPrice;
}
printf("Zakupiono: %hu\tCZEGO\n",counter);
while(temp->nextProduct != NULL)
{
temp = temp->nextProduct;
counter = 0;
while ((*money-((temp)->productPrice))>=0)
{
counter++;
*money = *money-(temp)->productPrice;
}
printf("Zakupiono: %hu\tCZEGO\n",counter);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
product *firstOnList = NULL;
char nazwa[50] = "dupa";
money = 10;
addProductToList(&firstOnList,2,1.19,*nazwa);
addProductToList(&firstOnList,3,0.99,*nazwa);
addProductToList(&firstOnList,1,3.20,*nazwa);
writeProductList(&firstOnList);
//makeShopping(&firstOnList,&money);
free(firstOnList);
getchar();
return 0;
}
A problem mam z tym, że nie za bardzo wiem w jaki sposób wpisać do każdego elementu jego nazwę productName, a potem to wyświetlić... Szczerze mówiąc nie wiem nawet jak to przekazać. Niby mi się udaje już w funkcji addProductToList i jak tam wypisuję to działa, ale w funkcji writeProductList wypisuje jakieś śmieci. Jakaś wskazówka? Pewnie coś podobnego jak z przekazaniem wskaźnika - błąd w przekazywaniu tablicy char-ów do funkcji?