#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
void new_item();
void list_item();
void search_item();
void buy_item();
void list_buy_item();
struct item
{
float price;
char item_name[20];
int n_item;
struct item *ptrnext;
};
struct item*headptr, *newptr, *currentptr,*previousptr,*headptr2, *newptr2, *currentptr2,*previousptr2;
void main()
{
char ch;
int choice=TRUE;
headptr=(struct student*)NULL;
while(choice==TRUE)
{
printf("\n\n1 - Enter new item");
printf("\n2 - List all item");
printf("\n3 - Search item");
printf("\n4 - buy_item");
printf("\n5 - Calculation");
printf("\n6 - Exit\n");
printf("\nEnter choice: ");
scanf(" %c",&ch);
switch(ch)
{
case '1':new_item();break;
case '2':list_item();break;
case '3':search_item();break;
case '4':buy_item();break;
case '5':list_buy_item();break;
case '6': choice=FALSE; break;
default: printf("\nEnter only one from the above");
}
}
}
void new_item()
{
newptr=(struct item *)malloc(sizeof (struct item));
printf("\nEnter new Item: ");
scanf("%s",&newptr->item_name);
printf("\nEnter item price : ");
scanf("%f",&newptr->price);
if (headptr==(struct item *)NULL)
{headptr=newptr;
newptr->ptrnext=(struct item *)NULL;
}
else
{
currentptr=headptr;
while(currentptr->ptrnext !=(struct item *)NULL)
{
currentptr=currentptr->ptrnext;
}
currentptr->ptrnext=newptr;
newptr->ptrnext= NULL;
}
}
void list_item()
{ printf("\nItem \tprice");
if (headptr==(struct item*)NULL)
{
printf("\nEmpty list");
return;
}
currentptr=headptr;
do
{
printf("\n%s \t%.2f",currentptr->item_name,currentptr->price);
currentptr=currentptr->ptrnext;
}while(currentptr !=(struct item *)NULL);
}
void search_item()
{
item *temp;
char n[10];
int same;
printf("\n Enter the item:");
scanf("%s",&n);
temp=headptr;
if(headptr==NULL)
{
printf("\n Empty List");
return;
}
else
{
while (temp != NULL)
{
same=strcmpi(n,temp->item_name);
if(same==0)
{
printf("\n item price is = %.2f",temp->price);
break;
}
temp=temp->ptrnext;
}
if (temp == NULL)
{
printf("\n item &s not in the list",n);
}
}
}//end search
void buy_item()
{
item *temp;
char n[10];
int same;
printf("\n Item name:");
scanf("%s",&n);
temp=headptr;
if(headptr==NULL)
{
printf("\n no such item in the List");
return;
}
else
{
while (temp != NULL)
{
same=strcmpi(n,temp->item_name);
if(same==0)
{
newptr2=(struct item *)malloc(sizeof (struct item));
printf("\nEnter item price : ");
scanf("%f",&newptr2->price);
printf("\nEnter item quantity : ");
scanf("%d",&newptr2->n_item);
if (headptr2==(struct item *)NULL)
{
headptr2=newptr2;
newptr2->ptrnext=(struct item *)NULL;
}
else
{
currentptr2=headptr2;
while(currentptr2->ptrnext !=(struct item *)NULL)
{
currentptr2=currentptr2->ptrnext;
}
currentptr2->ptrnext=newptr2;
newptr2->ptrnext= NULL;
}
break;
}
temp=temp->ptrnext;
}
if (temp == NULL)
{
printf("\n item %s not in database",n);
}
}
}
void list_buy_item()
{ float total,a,chance;
printf("\nItem ");
if (headptr2==(struct item*)NULL)
{
printf("\nEmpty list");
return;
}
currentptr2=headptr2;
if(currentptr2 !=(struct item *)NULL){
total = (currentptr2->price*currentptr2->n_item);//<-----here what should i do so it can take the value from newptr2(node) and plust all the value from newptr2(node)
total = total + (currentptr2->price);
currentptr2=currentptr2->ptrnext;
printf("\ntotal price%.2f",total);
printf("\nenter customer money :");
scanf("%f",&a);
chance = a - total;
if( chance< 0)
{
printf("\nnot enough money");
}else if( chance == 0 )
{
printf("no chance");
}else
{
printf("\nthe chance is %f",chance);
}
} }
konata_89 0 Newbie Poster
konata_89 0 Newbie Poster
Luckychap 68 Posting Pro in Training
konata_89 0 Newbie Poster
Luckychap 68 Posting Pro in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.