scorici

proiectDani

Nov 23rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1. #include <stdio.h> //ascending name + descending code odd
  2. #include <string.h>
  3. #include <stdlib.h>
  4. // maybe test with original file lab2_3.data
  5. // have to clean up the code, remove the many declarations of i and j
  6. // have to remove useless code like file parameter in main...
  7. #define SIZE 5000
  8. int i;
  9.  
  10. typedef struct product product_t;
  11. typedef int (*std_cmp)(product_t *,product_t *b);
  12.  
  13. struct product{
  14.     char name[21];    // product name
  15.     int code;         // product code
  16. };
  17.  
  18. char* generatename(char* name){
  19.     char *string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  20.     int l=21,i;
  21.     char n[21];
  22.     for(i=0;i<l;i++)
  23.         n[i]=string[rand()%53];
  24.     n[21]='\0';
  25.     strcpy(name, n);
  26.     return name;
  27. }
  28.  
  29. void generatefile(char* file)
  30. {
  31.   int i;
  32.   char name[21];
  33.   FILE* filep = fopen(file, "w");
  34.   for(i=0;i<SIZE;i++)
  35.     fprintf(filep, "%s\n%d\n", generatename(name), rand());
  36.   fclose(filep);
  37. }
  38.  
  39. void printWhole(product_t products[], int cnt){
  40.     for(i=0; i<cnt; i++){ // print product names and codes
  41.         printf("%s\n%d\n", products[i].name, products[i].code);
  42.     }
  43. }
  44.  
  45. void print_codes(product_t products[], int cnt){
  46.     for(i=0; i<cnt; i++){ // print product code
  47.         printf("%d\n",products[i].code);
  48.     }
  49. }
  50.  
  51. void print_odd_codes(product_t products[], int cnt){
  52.     for(i=0; i<cnt; i++){ // print product odd codes
  53.         if(products[i].code %2 == 1)
  54.             printf("%d\n",products[i].code);
  55.     }
  56. }
  57.  
  58. void print_product_names(product_t products[], int cnt){
  59.     for(i=0; i<cnt; i++){ // print product names
  60.         printf("%s\n",products[i].name);
  61.     }
  62. }
  63. int cmp_code(product_t *a,product_t *b){
  64.     return b->code-a->code;
  65. }
  66.  
  67. int cmp_name(product_t *a,product_t *b){
  68.     return strcmp(b->name,a->name);
  69. }
  70.  
  71. void bubble_sort(product_t v[],int cnt,std_cmp cmpf){
  72.     for(int i=0;i<cnt;i++){
  73.         for(int j=1;j<(cnt-i);j++){
  74.             if(cmpf(&v[j-1],&v[j])<0){
  75.                 product_t tmp=v[j-1];
  76.                 v[j-1]=v[j];
  77.                 v[j]=tmp;
  78.             }
  79.         }
  80.     }
  81. }
  82. //=====================
  83. void bubble_sort_descend(product_t v[],int cnt,std_cmp cmpf){
  84.     for(int i=0;i<cnt;i++){
  85.         for(int j=1;j<(cnt-i);j++){
  86.             if(cmpf(&v[j-1],&v[j])>0){
  87.                 product_t tmp=v[j-1];
  88.                 v[j-1]=v[j];
  89.                 v[j]=tmp;
  90.             }
  91.         }
  92.     }
  93. }
  94. //=====================
  95. void bubble_sort_odd(product_t v[],int cnt){
  96.     for(int i=0;i<cnt;i++){
  97.         for(int j=0;j<cnt;j++){
  98.             if(v[i].code%2==0 || v[j].code%2==0)
  99.                 continue;
  100.             if(v[i].code<v[j].code){
  101.                 product_t tmp=v[j];
  102.                 v[j]=v[i];
  103.                 v[i]=tmp;
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109.  
  110. void binarysearch(product_t v[],int cnt,int x){
  111.     int left=0,mid,right=cnt-1,found=0;
  112.     while ((left<=right)&&(!found)){
  113.         mid=(left+right)/2;
  114.         if(v[mid].code==x)
  115.             found=1;
  116.         else if(v[mid].code<x)
  117.             left=mid+1;
  118.         else
  119.             right=mid-1;
  120.     }
  121.     if(!found)
  122.         puts("Not found");
  123.     else
  124.         printf("Found:\n%s\n%d\n",v[mid].name,v[mid].code);
  125. }
  126.  
  127. void menu(product_t* products){
  128.  
  129.     int num;
  130.     printf("\n1. Print list of product name and code\n");
  131.     printf("2. Order by ascending name\n");
  132.     printf("3. Order by ascending odd code\n");
  133.     printf("4. Search by code\n");
  134.     printf("5. Print odd product codes\n");
  135.     printf("6. Print product codes\n");
  136.     printf("7. Print product names\n");
  137.     printf("8. Order by descending name\n");
  138.     printf("9. Exit\n\n");
  139.  
  140.     scanf("%d", &num);
  141.     switch(num){
  142.     case 1: printWhole(products, SIZE);
  143.         break;
  144.     case 2:{
  145.         bubble_sort(products,SIZE,cmp_name);
  146.         break;
  147.     }
  148.     case 3:{
  149.         bubble_sort_odd(products, SIZE);
  150.         break;
  151.     }
  152.     case 4:{
  153.         int c;
  154.         printf("Type the code you want to find inside the product list: \n");
  155.         scanf("%d", &c);
  156.         bubble_sort(products, SIZE, cmp_code);
  157.         binarysearch(products, SIZE, c);
  158.         break;
  159.     }
  160.     case 5:{
  161.         print_odd_codes(products, SIZE);
  162.         break;
  163.     }
  164.     case 6:{
  165.         print_codes(products, SIZE);
  166.         break;
  167.     }
  168.     case 7:{
  169.         print_product_names(products, SIZE);
  170.         break;
  171.     }
  172.     case 8: {
  173.         bubble_sort_descend(products,SIZE,cmp_name);
  174.         break;
  175.     }
  176.     case 9: return;
  177.     default:
  178.         exit(0);
  179.     }
  180.     menu(products);
  181. }
  182.  
  183. int main(int argc,char **argv){
  184.     /*if(argc<2){
  185.         fprintf(stderr,"Usage:%s [file]",argv[0]);
  186.         return 1;
  187.     }*/
  188.     //generatefile(argv[1]);
  189.     generatefile("random.txt");
  190.     //FILE *input = fopen(argv[1],"r");
  191.     FILE *input = fopen("random.txt","r");
  192.     if(!input){
  193.         perror("Error reading file");
  194.         return 1;
  195.     }
  196.     product_t *products=malloc(SIZE*sizeof(product_t));
  197.     if(!products){
  198.         perror("Malloc error");
  199.         return 1;
  200.     }
  201.     for(i=0; i<SIZE; i++){
  202.         fscanf(input, "%s%d", products[i].name, &products[i].code);
  203.     }
  204.     fclose(input);
  205.     menu(products);
  206.     free(products);
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment