#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int code;
char name;
float amount;
int shares;
float c_price;
}DATA;
typedef struct stockList
{
DATA stock;
stockList* link;
}STOCK;
STOCK* insertStock(STOCK* pList, STOCK* pPre, DATA item);
STOCK* buildList(char* fileID);
void printList(STOCK* pList);
void fprintList(STOCK* pList);
bool searchList(STOCK* pList, STOCK** pPre, STOCK** pCur, int target);
void modifyList(STOCK* pList,int pre_code, int code, char name, float amount, int shares, float c_price);
void display_List(STOCK* pList, STOCK** pPre, STOCK** pCur, int target);
int main (void)
{
STOCK* pList;
STOCK* pPre;
STOCK* pCur;
DATA stock;
int pre_code, code, name, amount, shares, c_price;
int more = 1;
int num;
printf("Stock List contains\n\n");
pList = buildList("Stock1.txt");
if(!pList)
{
printf("Error opening stock file\n");
exit(210);
}
printList(pList);
printf("Please select the task preferred\n");
printf("=========================================================\n");
printf("1 for modifying existing stock\n");
printf("2 for searching for a stock and printing the stock data\n");
printf("3 for printing data back to file\n\n");
printf("0 for Exit\n\n");
printf("Please enter number = ");
scanf("%d", &num);
printf("\n\n");
switch(num)
{
case 1:
{
printf("Please enter the stock code to be modified = ");
scanf("%d", &pre_code);
if(searchList(pList, &pPre, &pCur, pre_code))
{
printf("Stock exists\n\n");
printf("Enter new stock code = ");
scanf("%d", &code);
printf("Enter new stock name = ");
scanf("%s", &name);
printf("Enter new stock amount = ");
scanf("%f", &amount);
printf("Enter new stock shares = ");
scanf("%d", &shares);
printf("Enter new stock current price = ");
scanf("%f", &c_price);
modifyList(pList,pre_code, code, name, amount, shares, c_price);
}
else
printf("Stock does not exist\n\n");
break;
}
case 2:
{
printf("Please enter the stock code to be viewed = ");
scanf("%d", &code);
if (searchList(pList, &pPre, &pCur, code) ==1)
printf("Stock exists\n\n");
else
printf("Stock does not exist\n\n");
display_List(pList, &pPre, &pCur, code);
break;
}
case 3:
{
fprintList(pList);
printf("Summary.txt can be printed out\n");
break;
}
default:
{
printf("Error in number inserted. Please try again\n");
break;
}
}
return 0;
}
STOCK* buildList(char* fileID)
{
DATA stock1;
STOCK* pList;
STOCK* pPre;
STOCK* pCur;
FILE* fStock;
pList = NULL;
fStock = fopen(fileID,"r");
if(!fStock)
{
printf("Error reading from file\n");
exit(104);
}
while((fscanf(fStock,"%d %s %f %d %f",&stock1.code, stock1.name, &stock1.amount, &stock1.shares, &stock1.c_price))==1)
{
searchList(pList, &pPre, &pCur, stock1.code);
pList = insertStock(pList, pPre, stock1);
}
return pList;
}
STOCK* insertStock(STOCK* pList, STOCK* pPre, DATA item)
{
STOCK* pNew = NULL;
pNew = (STOCK*) malloc( sizeof( STOCK ));
if(!(pNew = (STOCK*)malloc(sizeof(STOCK))))
{
printf("\aMemory overflow in insert\n");
exit(100);
}
pNew->stock = item;
if(pPre == NULL)
{
pNew->link = pList;
pList = pNew;
}
else
{
pNew->link = pPre->link;
pPre->link = pNew;
}
return pList;
}
void modifyList(STOCK* pList,int pre_code, int code, char name, float amount, int shares, float c_price)
{
STOCK* pCur = NULL;
STOCK* pPre = NULL;
if ((searchList(pList,&pCur,&pPre,pre_code)) == true)
{
pCur->stock.code = code;
pCur->stock.name = name;
pCur->stock.amount = amount;
pCur->stock.shares = shares;
pCur->stock.c_price = c_price;
}
else
{
printf("No such data!!\n");
}
return;
}
bool searchList(STOCK* pList, STOCK** pPre, STOCK** pCur, int target)
{
bool found = false;
*pCur = pList;
*pPre = NULL;
while(pCur != NULL && target > (*pCur)->stock.code)
{
*pPre = *pCur;
*pCur = (*pCur)->link;
}
if(*pCur && target == (*pCur)->stock.code)
found = true;
return found;
}
void printList(STOCK* pList)
{
STOCK* pWalker;
pWalker = pList;
printf("Stock contains\n");
printf("Code\tName\tAmount\tShares\tCurrent Price\n");
printf("====\t====\t======\t======\t=============\n");
while(pWalker)
{
printf("%d\t%s\t%.2f\t%d\t%.2f\n",pWalker->stock.code, pWalker->stock.name, pWalker->stock.amount, pWalker->stock.shares, pWalker->stock.c_price);
pWalker = pWalker->link;
}
printf("\n");
return;
}
void fprintList(STOCK* pList)
{
STOCK* pWalker;
FILE* flist;
flist = fopen("Summary.txt", "w");
if (!flist)
{
printf("Error in creating file\n");
exit (106);
}
pWalker = pList;
fprintf(flist,"Stock contains\n");
fprintf(flist,"Code\tName\tAmount\tShares\tCurrent Price\n");
fprintf(flist,"====\t====\t======\t======\t=============\n");
while(pWalker)
{
fprintf(flist,"%d\t%s\t%.2f\t%d\t%.2f\n",pWalker->stock.code, pWalker->stock.name, pWalker->stock.amount, pWalker->stock.shares, pWalker->stock.c_price);
pWalker = pWalker->link;
}
fprintf(flist,"\n");
return;
}
void display_List(STOCK* pList, STOCK** pPre, STOCK** pCur, int target)
{
*pCur = pList;
*pPre = NULL;
while(pCur != NULL && target > (*pCur)->stock.code)
{
*pPre = *pCur;
*pCur = (*pCur)->link;
}
if(*pCur && target == (*pCur)->stock.code)
{
printf("Code\tName\tAmount\tShares\tCurrent Price\n");
printf("====\t====\t======\t======\t=============\n");
printf("&d\t%s\t%.2f\t%d\t%.2f\n",(*pCur)->stock.code, (*pCur)->stock.name, (*pCur)->stock.amount, (*pCur)->stock.shares, (*pCur)->stock.c_price);
}
return;
}
i can't read the data in my file.. COmpiled no error.. but, can't display output...
Hoping someone could tell me the problem... thanks in advance...=)