0% found this document useful (0 votes)
5 views

Lab 1

presualcode

Uploaded by

khanhldhe186786
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 1

presualcode

Uploaded by

khanhldhe186786
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define MAX 100

/* 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;
}

char *strupr(char *str) {


unsigned char *p = (unsigned char *)str;
while (*p) {
*p = toupper((unsigned char)*p);
p++;
}
return str;
}

/* Check if array is full */


int isFull(char name[][21], int n) {
return n == MAX;
}

/* Check if array is empty or not */


int isEmpty(char name[][21], int n) {
return n == 0;
}

/* Add an element at the end, increase number of elements */


void add(char codes[9], char names[21], char categories[9] , char units[9], double
prices, char code[][9], char name[][21], char category[][9], char unit[][9], double
price[], int *pn) {

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 mang dua tren mang dong

/* Print array in desc. order based on salary + allowance, positions are preserved
*/

// Print mang dua tren mang dong


int main() {
char code[MAX][9];
char name[MAX][21];
char category[MAX][9];
char unit[MAX][9];
double price[MAX];
int n = 0;
char codes[9];
char names[21];
char categories[9];
char units[9];
double prices;
int Choice;

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();

add(codes, names, categories, units, prices, code, name,


category, unit, price, &n);
printf("Added!\n");
printByIndex(n - 1, code, name, category, unit, price, n);
}
break;

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 3: // remove first existence


if (isEmpty(names, n)) {
printf("\nSorry, array is empty.\n");
} else {
printf("Input a code to update name: ");
scanf("%8s", code);
getchar();
strupr(code);
int pos = searchByCode(code, codes, n);
if (pos < 0) {
printf("%s is not found\n", code);
} else {
printf("Input new name: ");
scanf("%20s", name);
getchar();
strupr(name);
strcpy(names[pos], name);
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;
}

You might also like