Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <math.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 100
- #define MAX_C 50
- long convertNumber(char c[MAX_C]){
- int i = 0;
- long res = c[0] - 48;
- for(i=1; i<strlen(c); i++)
- res = res*10 + c[i]-48;
- return res;
- }
- void validateISBN(char c[MAX_C]) {
- // keep input in array to keep 0 before number
- // canbe use ` typedef char[MAX_C] String ` for conventience :)
- int flag = 1;
- do {
- flag = 1;
- scanf("%s",c);
- int i = 0;
- for(i=0; i<strlen(c); i++){
- if (c[i]<'0' || c[i]>'9'){
- flag = 0;
- if (c[0] != '-')
- printf("Trailing character meet\nTry again : ");
- else
- printf("Negative is unacceptable\nTry again : ");
- break;
- }
- }
- } while (flag == 0);
- }
- double validatePrice(){
- int flag=1;
- char c[20];
- memset(c,0,sizeof(c));
- do{
- flag =1;
- scanf("%s",c);
- /*
- * loop to validate and count number of dot
- * if more than one dot --> error
- */
- int j = 0, dot = 0;
- for(j=0;j<strlen(c);j++){
- if(c[j]<'0' || c[j]>'9' ){
- if (dot == 0){
- dot++;
- continue;
- }
- flag = 0;
- if (c[j] == '.')
- printf("Wrong format only 1 dot (.) allow\nTry again :");
- else
- printf("Just contain number.\nTry again:");
- }
- }
- /* prevent someone fucking */
- if(strlen(c)==0)
- flag=0;
- }while(flag == 0);
- /*
- * output : divide two phrase:
- * phrase 1 : integer part
- * phrase 2: fraction part
- */
- double res = 0.0;
- /* phrase one*/
- /* first step : find location of dot */
- int i = 0;
- for (i=0;i<strlen(c);i++){
- if(c[i] =='.')
- break;
- }
- /* second step : sum of integer part */
- int k;
- res = 1.0*c[0] - 48;
- for (k=1;k<i;k++)
- res = res* 10 + c[k] - 48;
- /* phrase two */
- int m =k;
- k++;
- for(k;k<strlen(c);k++)
- res += (c[k]-48) * pow((float)10,-(k-m));
- printf("%res:%f",res);
- return res;
- }
- int validateQuantity() {
- int flag=1;
- int i = 0;
- char c[MAX_C];
- memset(c,0,sizeof(c));
- do {
- flag = 1;
- // input
- fflush(stdin);
- scanf("%s",c);
- // prevent someone fucking
- if (strlen(c)<=0 || strlen(c)>7){
- printf("Too big or too small !!!\nTry again : ");
- flag = 0;
- }
- // check if number ?
- for(i=0; i<strlen(c); i++){
- if (!isdigit(c[i])){
- flag = 0;
- if (c[0] != '-')
- printf("Trailing character meet\nTry again : ");
- else
- printf("Negative is unacceptable\nTry again : ");
- break;
- }
- }
- } while (flag == 0);
- return convertNumber(c);
- }
- int input(char barcode[MAX][MAX_C], long barcodeV[MAX], double price[MAX], int quantity[MAX], int n) {
- long bcode;
- double p;
- int quan, i;
- char c[30];
- memset(c,0,sizeof(c));
- printf("\nISBN : ");
- validateISBN(c);
- bcode = convertNumber(c);
- for (i = 0; i < n; i++) {
- if (barcodeV[i] == bcode) {
- printf("Item exits\n");
- return n;
- }
- }
- printf("Price: ");
- p = validatePrice();
- printf("Quantity: ");
- quan = validateQuantity();
- barcodeV[n] = bcode;
- strcpy(barcode[n],c);
- price[n] = p;
- quantity[n] = quan;
- n++;
- return n;
- }
- int sort(char barcode[MAX][MAX_C], long barcodeV[MAX], double price[MAX], int quantity[MAX], int n) {
- /* temporary variable for sorting */
- long _bcodeV;
- double _price;
- char _bcode[MAX_C];
- int _quantity;
- // implement bubble sort here
- int i=0, j=0;
- for (i = 0; i < n - 1; i++) {
- for (j = i + 1; j < n; j++) {
- if (barcodeV[i] > barcodeV[j]) {
- _bcodeV = barcodeV[i];
- _price = price[i];
- _quantity = quantity[i];
- strcpy(_bcode, barcode[i]);
- barcodeV[i] = barcodeV[j];
- strcpy(barcode[i], barcode[j]);
- price[i] = price[j];
- quantity[i] = quantity[j];
- barcodeV[j] = _bcodeV;
- strcpy(barcode[j], _bcode);
- price[j] = _price;
- quantity[j] = _quantity;
- }
- }
- }
- double sum = 0;
- printf("\n------------------------------------------------\n");
- printf("ISBN----Price-------Quantity--------Total---\n");
- for (i = 0; i < n; i++) {
- sum += price[i] * quantity[i];
- printf("%s\t %.3lf %d %.3lf\n", barcode[i], price[i], quantity[i], price[i] * quantity[i]);
- }
- printf("Total is : %lf\n", sum);
- return 1;
- }
- void main() {
- char barcode[MAX][MAX_C]; // keep barcode (still remain 0 before number). MAX_C : maxinum number of barcode
- long barcodeV[MAX]; // barcode under integer type (keep this array for convenience when sorting)
- double price[MAX];
- int quantity[MAX];
- int n = 0;
- char c;
- // make all array to zero by memset() in <string.h>
- memset(barcodeV,0,sizeof(barcodeV));
- memset(barcode,0,sizeof(barcode));
- memset(price,0,sizeof(price));
- memset(quantity,0,sizeof(quantity));
- // just normal menu
- do {
- printf("=================\n");
- printf("1.Enter new\n");
- printf("2.Sort\n");
- printf("3.Exit");
- printf("Choice : ");
- do {
- c = _getch();
- printf("%c\n",c);
- if (c != '1' && c != '2' && c != '3')
- printf("\nInvalid choice : ");
- } while (c != '1' && c != '2' && c != '3');
- if (c == '1')
- n = input(barcode, barcodeV, price, quantity, n);
- else if (c == '2')
- n = sort(barcode, barcodeV, price, quantity, n);
- } while (c != 3);
- }
Advertisement
Add Comment
Please, Sign In to add comment