Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> //ascending name + descending code odd
- #include <string.h>
- #include <stdlib.h>
- // maybe test with original file lab2_3.data
- // have to clean up the code, remove the many declarations of i and j
- // have to remove useless code like file parameter in main...
- #define SIZE 5000
- int i;
- typedef struct product product_t;
- typedef int (*std_cmp)(product_t *,product_t *b);
- struct product{
- char name[21]; // product name
- int code; // product code
- };
- char* generatename(char* name){
- char *string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
- int l=21,i;
- char n[21];
- for(i=0;i<l;i++)
- n[i]=string[rand()%53];
- n[21]='\0';
- strcpy(name, n);
- return name;
- }
- void generatefile(char* file)
- {
- int i;
- char name[21];
- FILE* filep = fopen(file, "w");
- for(i=0;i<SIZE;i++)
- fprintf(filep, "%s\n%d\n", generatename(name), rand());
- fclose(filep);
- }
- void printWhole(product_t products[], int cnt){
- for(i=0; i<cnt; i++){ // print product names and codes
- printf("%s\n%d\n", products[i].name, products[i].code);
- }
- }
- void print_codes(product_t products[], int cnt){
- for(i=0; i<cnt; i++){ // print product code
- printf("%d\n",products[i].code);
- }
- }
- void print_odd_codes(product_t products[], int cnt){
- for(i=0; i<cnt; i++){ // print product odd codes
- if(products[i].code %2 == 1)
- printf("%d\n",products[i].code);
- }
- }
- void print_product_names(product_t products[], int cnt){
- for(i=0; i<cnt; i++){ // print product names
- printf("%s\n",products[i].name);
- }
- }
- int cmp_code(product_t *a,product_t *b){
- return b->code-a->code;
- }
- int cmp_name(product_t *a,product_t *b){
- return strcmp(b->name,a->name);
- }
- void bubble_sort(product_t v[],int cnt,std_cmp cmpf){
- for(int i=0;i<cnt;i++){
- for(int j=1;j<(cnt-i);j++){
- if(cmpf(&v[j-1],&v[j])<0){
- product_t tmp=v[j-1];
- v[j-1]=v[j];
- v[j]=tmp;
- }
- }
- }
- }
- //=====================
- void bubble_sort_descend(product_t v[],int cnt,std_cmp cmpf){
- for(int i=0;i<cnt;i++){
- for(int j=1;j<(cnt-i);j++){
- if(cmpf(&v[j-1],&v[j])>0){
- product_t tmp=v[j-1];
- v[j-1]=v[j];
- v[j]=tmp;
- }
- }
- }
- }
- //=====================
- void bubble_sort_odd(product_t v[],int cnt){
- for(int i=0;i<cnt;i++){
- for(int j=0;j<cnt;j++){
- if(v[i].code%2==0 || v[j].code%2==0)
- continue;
- if(v[i].code<v[j].code){
- product_t tmp=v[j];
- v[j]=v[i];
- v[i]=tmp;
- }
- }
- }
- }
- void binarysearch(product_t v[],int cnt,int x){
- int left=0,mid,right=cnt-1,found=0;
- while ((left<=right)&&(!found)){
- mid=(left+right)/2;
- if(v[mid].code==x)
- found=1;
- else if(v[mid].code<x)
- left=mid+1;
- else
- right=mid-1;
- }
- if(!found)
- puts("Not found");
- else
- printf("Found:\n%s\n%d\n",v[mid].name,v[mid].code);
- }
- void menu(product_t* products){
- int num;
- printf("\n1. Print list of product name and code\n");
- printf("2. Order by ascending name\n");
- printf("3. Order by ascending odd code\n");
- printf("4. Search by code\n");
- printf("5. Print odd product codes\n");
- printf("6. Print product codes\n");
- printf("7. Print product names\n");
- printf("8. Order by descending name\n");
- printf("9. Exit\n\n");
- scanf("%d", &num);
- switch(num){
- case 1: printWhole(products, SIZE);
- break;
- case 2:{
- bubble_sort(products,SIZE,cmp_name);
- break;
- }
- case 3:{
- bubble_sort_odd(products, SIZE);
- break;
- }
- case 4:{
- int c;
- printf("Type the code you want to find inside the product list: \n");
- scanf("%d", &c);
- bubble_sort(products, SIZE, cmp_code);
- binarysearch(products, SIZE, c);
- break;
- }
- case 5:{
- print_odd_codes(products, SIZE);
- break;
- }
- case 6:{
- print_codes(products, SIZE);
- break;
- }
- case 7:{
- print_product_names(products, SIZE);
- break;
- }
- case 8: {
- bubble_sort_descend(products,SIZE,cmp_name);
- break;
- }
- case 9: return;
- default:
- exit(0);
- }
- menu(products);
- }
- int main(int argc,char **argv){
- /*if(argc<2){
- fprintf(stderr,"Usage:%s [file]",argv[0]);
- return 1;
- }*/
- //generatefile(argv[1]);
- generatefile("random.txt");
- //FILE *input = fopen(argv[1],"r");
- FILE *input = fopen("random.txt","r");
- if(!input){
- perror("Error reading file");
- return 1;
- }
- product_t *products=malloc(SIZE*sizeof(product_t));
- if(!products){
- perror("Malloc error");
- return 1;
- }
- for(i=0; i<SIZE; i++){
- fscanf(input, "%s%d", products[i].name, &products[i].code);
- }
- fclose(input);
- menu(products);
- free(products);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment