```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ARRAY_SIZE 255
typedef struct product {
char* description;
char* name;
double price;
int quantity;
} product;
//Function prototypes
void buyProduct();
int checkInputData(char* data);
int deleteProductFromCart(FILE* fp,int row);
char* dgets();
void displayAllContent(FILE* fp);
void displayCart();
void displayCategory(char* category);
void displayCheckup(char* productName);
void displayDeleteMenu(FILE* fp);
void displayError();
void displayFormatedName(char * productName);
void displayMenu();
int findAndReplaceInCart(FILE* fp, product pdt);
char* getFileName(FILE* fp, char* str);
int getFileRowNumber(FILE *fp);
product getProductFromRow(char* rowStr, int cat);
char* getRowInFile(FILE* fp, int row);
int replaceRowInFile(FILE* fp, product pdt, int row);
int sendToCart(product pdt);
void writeFormatedName(FILE* fp, char* productName);
void buyProduct(FILE* category, int productNumber) {
}
int checkInputData(char* data) { //Checks whether the data entered is correct or not
int i;
for (i = 0; i < strlen(data); i++)
if ( !(isdigit(data[i])) )
if (data[i] != '\0')
return -1;
else
break;
return atoi(data);
}
int deleteProductFromCart(FILE* fp,int row) {
int status = 0;
int rowNum = getFileRowNumber(fp);
product pdt[rowNum-2];
int i;
int j = 0;
char* rowStr;
for (i = 1; i < rowNum;i++) {
if (i != row) {
rowStr = getRowInFile(fp,i);
pdt[i-1-j] = getProductFromRow(rowStr,1);
} else
j++;
}
fclose(fp);
FILE* newCartFile = fopen("carrito.txt","w");
fprintf(newCartFile,"Num\tProducto\t\t\tCantidad\tPrecio");
for (i = 1; i < rowNum-1;i++) {
fprintf(newCartFile,"\n%i.\t",i);
writeFormatedName(newCartFile,pdt[i-1].name);
fprintf(newCartFile,"%i\t\t$%.2f",pdt[i-1].quantity,pdt[i-1].price);
}
fclose(newCartFile);
status = 1;
return status;
}
void displayAllContent(FILE* fp) {
//fscanf(fp,"%*[^\n]\n");
fseek(fp, 0, SEEK_SET);
char c = fgetc(fp);
while (c != EOF) {
printf("%c",c);
c = fgetc(fp);
}
printf("\n");
}
void displayCart() {
FILE* cartFile = fopen("carrito.txt","r");
int opt = -1;
char* input;
do {
system("CLS");
displayAllContent(cartFile);
printf("Que desea realizar?\n0. Regresar al menu principal\n1. Proceder a compra\n");
printf("2. Eliminar elementos del carrito\n");
input = dgets();
opt = checkInputData(input);
switch(opt) {
case 0:
break;
case 1:
break;
case 2:
displayDeleteMenu(cartFile);
cartFile = fopen("carrito.txt","r");
break;
default:
displayError();
}
} while (opt != 0);
fclose(cartFile);
}
void displayCategory(char* category) {
int opt = -1;
char* input;
FILE* catMenu = fopen(category,"r");
do {
displayAllContent(catMenu);
printf("0. Regresar al menu principal\n");
input = dgets();
opt = checkInputData(input);
system("CLS");
if (opt <= 0 || opt > getFileRowNumber(catMenu) - 1) {
if (opt != 0)
displayError();
}
else {
displayCheckup(getRowInFile(catMenu, opt));
}
} while (opt != 0);
fclose(catMenu);
}
void displayCheckup(char* productInfo) {
int opt = -1;
product pdt = getProductFromRow(productInfo,0);
pdt.quantity = -1;
char* input;
do {
printf("El siguiente producto ha sido seleccionado:\n");
printf("Producto\t\t\tPrecio\t\tDescripcion\n");
displayFormatedName(pdt.name);
printf("$%.2f\t\t%s\n",pdt.price,pdt.description);
printf("Seleccione la cantidad que desea comprar o digite 0 para salir del checkup\n");
input = dgets();
pdt.quantity = checkInputData(input);
system("CLS");
if (pdt.quantity <= 0)
if (pdt.quantity != 0)
displayError();
else
opt = 0;
else {
pdt.price *= pdt.quantity;
do {
printf("Resumen de compra:\n");
printf("Producto\t\t\tCantidad\tPrecio total\n");
displayFormatedName(pdt.name);
printf("%i\t\t$%.2f\n",pdt.quantity,pdt.price);
printf("0. Descartar compra y regresar al menu\n1. Comprar\n2. Mandar al carrito de compras\n");
input = dgets();
opt = checkInputData(input);
system("CLS");
switch(opt) {
case 0:
break;
case 1:
printf("Compra realizada!\n");
break;
case 2:
if (sendToCart(pdt))
printf("Articulos enviados al carrito exitosamente!\n");
else
printf("Hubo un error al enviar los articulos al carrito, intentelo mas tarde\n");
break;
default:
displayError();
}
} while (!(opt >= 0 && opt <= 2));
break;
}
} while (opt != 0);
}
void displayDeleteMenu(FILE* fp) {
int opt = -1;
char* input;
do {
system("CLS");
displayAllContent(fp);
printf("Seleccione el producto que desea eliminar o digite 0 para salir al menu.\n");
input = dgets();
opt = checkInputData(input);
if(opt <= 0 || opt > getFileRowNumber(fp) - 1) {
if (opt != 0)
displayError();
} else {
if (deleteProductFromCart(fp,opt)) {
printf("Articulo eliminado exitosamente!\n");
fclose(fp);
opt = 0;
} else {
printf("El articulo no pudo ser eliminado, intente mas tarde\n");
fclose(fp);
}
}
} while (opt != 0);
}
void displayError() {
printf("El dato ingresado no es valido, por favor ingrese un dato valido\n");
}
void displayFormatedName(char * productName) {
int productNameLength = strlen(productName);
int i;
printf("%s",productName);
for (i = 0; i < 4 - productNameLength / 8; i++)
printf("\t");
}
void displayMenu() {
int opt = -1;
char* input;
char* fileName;
FILE* menu = fopen("Menu.txt","r");
do {
displayAllContent(menu);
input = dgets();
opt = checkInputData(input);
system("CLS");
if(opt <= 0 || opt > getFileRowNumber(menu) - 1) {
if (opt != 0)
displayError();
}
else {
fileName = getFileName(menu,input);
if (fileName != NULL) {
strcat(fileName,".txt");
displayCategory(fileName);
}
}
} while (opt != 0);
free(input);
fclose(menu);
}
char* dgets() {
char input[MAX_ARRAY_SIZE];
if (gets(input) == NULL)
return NULL;
char* str = malloc(sizeof(char));
int i;
for(i = 0; i < MAX_ARRAY_SIZE; i++) {
if (input[i] != '\0') {
str[i] = input[i];
str = realloc(str,sizeof(char) * i + 2);
} else {
str[i] = '\0';
break;
}
}
return str;
}
int findAndReplaceInCart(FILE* fp, product pdt) {
char* rowStr;
char* match;
int status = 0;
int rowNum = getFileRowNumber(fp);
int i, j;
for (i = 0; i < rowNum; i++) {
rowStr = getRowInFile(fp,i);
match = strstr(rowStr, pdt.name);
if (match == NULL)
continue;
product oldProduct = getProductFromRow(rowStr,1);
pdt.quantity += oldProduct.quantity;
pdt.price += oldProduct.price;
replaceRowInFile(fp,pdt,i);
status = 1;
break;
}
return status;
}
/*char* dgets() {
char c = getchar();
if(c == '\n')
return NULL;
char* str = malloc(sizeof(char));
int strSize = 0;
do {
printf("c:%c\n",c);
str[strSize] = c;
str = realloc(str, sizeof(char) * (++strSize) + 1);
} while (c = getchar() != '\n');
str[strSize] = '\0';
return str;
}*/
char* getFileName(FILE* fp, char* str) {
int rowNum = getFileRowNumber(fp);
int i;
char* rowStr;
char* match;
for (i = 0; i < rowNum;i++) {
rowStr = getRowInFile(fp,i);
match = strstr(rowStr, str);
if (match == NULL)
continue;
match = strstr(match, " ");
return match + 1;
break;
}
return NULL;
}
int getFileRowNumber(FILE *fp) {
fseek(fp, 0, SEEK_SET);
char c = fgetc(fp);
int num = 1;
while (c != EOF) {
if (c == '\n')
num++;
c = fgetc(fp);
}
return num;
}
product getProductFromRow(char* rowStr, int cat) {
product pdt;
int i,j;
char* name = strstr(rowStr, " ");
name++;
for(i = 0; i < strlen(name); i++)
if(name[i] == '\t')
break;
pdt.name = malloc(sizeof(char) * (i + 1));
for(j = 0; j < i; j++)
pdt.name[j] = name[j];
pdt.name[i] = '\0';
char* price = strstr(rowStr, "$");
char* description;
pdt.price = strtod(price+1, &description);
if (cat == 0) {
pdt.description = description + 2;
} else {
char* quantity = strstr(rowStr, name);
quantity += strlen(pdt.name) + 4 - strlen(pdt.name) / 8;
pdt.quantity = atoi(quantity);
}
return pdt;
}
char* getRowInFile(FILE* fp, int row) {
fseek(fp, 0, SEEK_SET);
int i;
for(i = 0; i < row; i++)
fscanf(fp,"%*[^\n]\n");
i = 0;
int strSize = 20;
char* str = malloc(sizeof(char) * strSize);
char c = fgetc(fp);
while(c != '\n' && c != EOF) {
if(i >= strSize) {
str = realloc(str, strSize * 2);
strSize *= 2;
}
str[i] = c;
c = fgetc(fp);
i++;
}
str[i] = '\0';
return str;
}
int replaceRowInFile(FILE* fp, product pdt, int row) {
fseek(fp, 0, SEEK_SET);
fprintf(fp,"Num"); // If this line is remove, the cart doesn't update anything. WTF??
int i;
for (i = 0; i < row; i++)
fscanf(fp,"%*[^\n]\n");
fprintf(fp,".\t");
writeFormatedName(fp,pdt.name);
fprintf(fp,"%i\t\t$%.2f",pdt.quantity,pdt.price);
}
int sendToCart(product pdt) {
FILE* cartFile = fopen("carrito.txt","r+");
if (cartFile == NULL)
return 0;
if (! (findAndReplaceInCart(cartFile, pdt)) ) {
fseek(cartFile, 0, SEEK_SET);
int itemNum = getFileRowNumber(cartFile);
int i;
for(i = 0; i < itemNum; i++)
fscanf(cartFile,"%*[^\n]\n");
itemNum;
fprintf(cartFile, "\n%i.\t",itemNum);
writeFormatedName(cartFile, pdt.name);
fprintf(cartFile, "%i\t\t$%.2f",pdt.quantity,pdt.price);
}
fclose(cartFile);
return 1;
}
void writeFormatedName(FILE* fp, char* productName) {
int productNameLength = strlen(productName);
int i;
fprintf(fp,"%s",productName);
for (i = 0; i < 4 - productNameLength / 8;i++)
fprintf(fp,"\t");
}
int main() {
int opt = -1;
char* input;
system("COLOR A");
do {
printf("*** No me acuerdo ***\n");
printf("Bienvenido a la cooperativa escolar \"No me acuerdo\", que desea realizar?\n");
printf("0. Salir de la aplicacion\n1. Buscar productos\n2. Consultar carrito de compras\n3. Consultar saldo\n4.\n");
//scanf("%[^\n]%*c", input);
input = dgets();
fflush(stdin);
opt = checkInputData(input);
system("CLS");
switch(opt) {
case 0:
break;
case 1:
displayMenu();
break;
case 2:
printf("*** Carrito de compras ***\n");
displayCart();
break;
case 3:
break;
case 4:
break;
default:
displayError();
}
} while (opt != 0);
free(input);
}
```