Lab 1
Lab 1
h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
/* Prototypes
int menu(void);
char *strlwr(char *str);
char *strupr(char *str);
int isFull(char names[][21], int n);
int isEmpty(char names[][21], int n);
void add(char code[9], char name[21], double salary, double allowance, char codes[]
[9], char names[][21], double salaries[], double allowances[], int *pn);
int printByName(char value[21], char codes[][9], char names[][21], double
salaries[], double allowances[], int n);
void printByIndex(int i, char codes[][9], char names[][21], double salaries[],
double allowances[], int n);
int searchByCode(char value[9], char codes[][9], int n);
int removeOne(int pos, char codes[][9], char names[][21], double salaries[], double
allowances[], int *pn);
void printAsc(char codes[][9], char names[][21], double salaries[], double
allowances[], int n);
void printDesc(char codes[][9], char names[][21], double salaries[], double
allowances[], int n);
*/
int menu(void) {
int choice;
printf("\nQuan li vat lieu");
printf("\n1 - Them vat lieu");
printf("\n2 - Chinh gia (code)");
printf("\n3 - Chinh ten (code)");
printf("\n4 - Xoa thong tin(code)");
printf("\n5 - Sap xep theo thu tu giam dan theo loai vat lieu");
printf("\n6 - Sap xep theo thu tu tang dan theo gia");
printf("\n7 - Xuat toan bo danh sach");
printf("\n8 - Xuat danh sach theo ma vat lieu");
printf("\n9 - Xuat danh sach trong khoang gia nao do");
printf("\n10 - Xuat gia tien trung binh ,gia cao nhat ,gia thap nhat");
printf("\nOthers - Quit");
printf("\nPlease choose: ");
scanf("%d%*c", &choice); // remove ENTER key
return choice;
}
char *strlwr(char *str) {
unsigned char *p = (unsigned char *)str;
while (*p) {
*p = tolower((unsigned char)*p);
p++;
}
return str;
}
strcpy(code[*pn], codes);
strcpy(name[*pn], names);
strcpy(category[*pn], categories);
strcpy(unit[*pn], units);
price[*pn] = prices;
*pn = *pn + 1;
}
/* Print by index */
void printByIndex(int i, char code[][9], char name[][21], char category[][9], char
unit[][9], double price[], int n) {
printf("Material(code: %s, name: %s, category: %s, unit: %s, price: %lf)\n",
code[i], name[i], category[i], unit[i], price[i]);
}
/* Search by code */
int searchByCode(char value[9], char codes[][9], int n) {
int i;
int pos = -1; // fail
for (i = 0; i < n && pos < 0; i++) {
if (strcmp(value, codes[i]) == 0) pos = i; // pos found
}
return pos;
}
/* Print array in asc. order based on salary + allowance, positions are preserved
*/
/* Print array in desc. order based on salary + allowance, positions are preserved
*/
do {
Choice = menu();
switch (Choice) {
case 1: // add a value
if (isFull(names, n)) {
printf("\nSorry, array is full!\n");
} else {
printf("Nhap nhan vien thu %d/%d: \n", n + 1, MAX);
printf("Code: ");
scanf("%8[^\n]", &codes);
getchar();
strupr(code);
printf("Name: ");
scanf("%20[^\n]", &names);
getchar();
strupr(name);
printf("Category: ");
scanf("%8[^\n]", &categories);
getchar();
printf("Units : ");
scanf("%8[^\n]", &units);
getchar();
printf("Price : ");
scanf("%lf", &prices);
getchar();
case 2:
if (isEmpty(names, n)) {
printf("\nSorry, array is empty.\n");
} else {
printf("Input a code to update PRICE: ");
scanf("%8s", code);
getchar();
strupr(code);
int pos = searchByCode(code, codes, n);
if (pos < 0) {
printf("%s is not found\n", code);
}else{
scanf("%lf",&prices);
price[pos]=prices;
printf("%s changed\n", code);
}
}
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
default:
printf("Goodbye!\n");
break;
}
} while (Choice > 0 && Choice < 11);
getchar();
return 0;
}