Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by registered user cycki ( 15 years ago )
#define _CRT_SECURE_NO_WARNINGS
#define ILOSC 255
#include <stdio.h>
#include <stdlib.h>
#include <string.h>//do memcpy, strtok


struct item
{
 struct Wiersz *wiersz;
 struct item *next;
 struct item *prev;
};
struct Wiersz {
 int *dane;
 char *etyk; 
};

int dodajW(struct item **start, struct item **end,struct Wiersz *w)
{
 struct item *nowy = NULL;

 nowy = (struct item *)malloc(sizeof(struct item));
 if(!nowy)
 {
  fprintf(stderr, "Brak pamieci!!\n");
  return 0;
 }
 nowy->next = NULL;
 nowy->prev = NULL;
 nowy->wiersz=w;

 if(*start == NULL)
 {
  *start = nowy;
  *end = *start;
  return 1;
 }
 else
 {
  (*end)->next = nowy;
  nowy->prev = (*end);
  (*end) = nowy;
  return 2;
 }
}
void wypisz(struct item **start, struct item **end,int iloscDanych)
{
 struct item *skoczek;
 int j,i=1;
 skoczek = *start;
 while(skoczek != NULL)
 {
  printf("%d. %s ",i++, skoczek->wiersz->etyk);
  for(j=0;j<iloscDanych-1;j++)
  {
   printf("%d ",skoczek->wiersz->dane[j]);
  }
  printf("\n");
  skoczek = skoczek->next;
 }
}
struct item *szukaj(char etyk[],int lp, struct item **start, struct item **end)
{
 struct item *skoczek;
 struct Wiersz *w;
 int ileSkokow;
 ileSkokow=1;
 skoczek = *start;
 while(skoczek != NULL)
 {
  w=skoczek->wiersz;
  if((!strcmp(etyk, skoczek->wiersz->etyk)) || (lp ==ileSkokow))
  {
   return skoczek;
  }
  skoczek = skoczek->next;
  ileSkokow++;
 }
 return NULL;
}
int zamienW(struct item **start, struct item **end,int jed,int dwa){
 struct item *temp = NULL;
 struct item *pierwszy = NULL;
 struct item *drugi = NULL;

 temp=(struct item *)malloc(sizeof(struct item));
 pierwszy=szukaj("",jed,start,end);
 drugi=szukaj("",dwa,start,end);
 if(pierwszy==NULL || drugi==NULL)
 {
  fprintf(stderr, "Nie znaleziono takiego rekordu!");
  return 0;
 }
 temp->wiersz=pierwszy->wiersz;
 pierwszy->wiersz=drugi->wiersz;
 drugi->wiersz=temp->wiersz;
 free(temp);
 return 1;
}
int usun(struct item **start, struct item **end,int lp,char etyk[])
{
 struct item *szukany = NULL;

 szukany = szukaj(etyk,lp, start, end);
 if(szukany == NULL)
 {
  fprintf(stderr, "Nie znaleziono takiego rekordu!");
  return 0;
 }
 else if((szukany == *start)&&(szukany == *end))
 {
  szukany->next = NULL;
  szukany->prev = NULL;
  free(szukany->wiersz->dane);
  free(szukany);
  *start=NULL;
 }
 else if(szukany == *end)
 {
  (*end) = szukany->prev;
  szukany->prev->next = NULL;
  szukany->prev = NULL;
  free(szukany->wiersz->dane);
  free(szukany);
 }
 else if(szukany == *start)
 {
  (*start) = szukany->next;
  szukany->next->prev = NULL;
  szukany->next = NULL;
  free(szukany->wiersz->dane);
  free(szukany);
 }
 else
 {
  szukany->prev->next = szukany->next;
  szukany->next->prev = szukany->prev;
  szukany->next = NULL;
  szukany->prev = NULL;
  free(szukany->wiersz->dane);
  free(szukany);
 }
 return 1;
}
int policzPrzecinki(FILE *fp){
 int ilosc;
 int ile;
 char znak='2';
 ilosc=0;
 ile=1;
 while(znak!='\n')
 {
  if(znak==',')
  {
   ilosc++;
  }
  znak=fgetc(fp);
  ile++;
 }
 fseek(fp, -ile, 1);//cofa wskaźnik na pozycje na ktorej był przed wejsciem do funkcji
 return ilosc;
}
void UsunCalosc(struct item **start, struct item **end)
{
 struct item *skoczek;
 while(*start != NULL)
 {
  skoczek = (*start)->next;
  free(*start);
  *start = skoczek;
 }
}

void main(){
 FILE* fp;
 struct item *glowa;
 struct item *ogon;
 char **etykiety;
 int iloscKol;
 int wybor=1;


 glowa=NULL;
 do
 {
  switch(wybor){

  case 1: 
   {
    int i;
    char wczytane[ILOSC];
    struct Wiersz *w;
    char *temp;
    char znak[]=",\n";
    UsunCalosc(&glowa;,˛);
    fp=fopen&#40;"test.csv","r"&#41;;
    iloscKol=policzPrzecinki(fp)+1;
    etykiety=(char**)malloc(iloscKol*sizeof(char*));

    fgets(wczytane, ILOSC, fp);

    temp=NULL;
    i=0;

    temp = strtok( wczytane, znak );
    while( temp != NULL )
    {
     etykiety[i]=(char*)malloc(strlen(temp)*sizeof(char));
     memcpy(etykiety[i], temp, strlen(temp));
     etykiety[i][strlen(temp)]='\0';
     temp = strtok( NULL, znak );
     i++;
    }
    etykiety[i]=NULL;

    while(fgets(wczytane, ILOSC, fp) )
    {
     temp=NULL;
     w=(struct Wiersz*)malloc(sizeof(struct Wiersz));
     w->dane=(int*)malloc((iloscKol-1)*sizeof(int));

     temp = strtok( wczytane, znak );
     w->etyk=(char*)malloc(strlen(temp)*sizeof(char));

     memcpy(w->etyk, temp, strlen(temp));
     w->etyk[strlen(temp)]='\0';

     i=0;
     while( temp != NULL ) 
     {
      temp = strtok( NULL, znak );
      if(temp!=NULL)
      {
       w->dane[i]=atoi(temp);
       i++;
      }
     }
     w->dane[i]='/0';
     dodajW(&glowa;,˛,w);
    }
    break;
   }
  case 2:
   {
    int i;
    for(i=0;etykiety[i]!=NULL;i++)
    {
     printf("%s ",etykiety[i]);
    }
    printf("\n");
    wypisz(&glowa;,˛,iloscKol);
    printf("\n");
    break;
   }
  case 3:
   {
    int jed,dwa;
    printf("podaj dwie liczby");
    scanf("%d %d",&jed;,&dwa;);
    zamienW(&glowa;,˛,jed,dwa);
    printf("%s %s %s\n",etykiety[0],etykiety[1],etykiety[2]);
    wypisz(&glowa;,˛,iloscKol);
    printf("\n");
    break;
   }
  case 4:
   {
    char tab[ILOSC];
    printf("podaj co?");
    scanf("%s",tab);
    usun(&glowa;,˛,atoi(tab),tab);
    wypisz(&glowa;,˛,iloscKol);
    break;
   }
  case 5:
   {
    zapiszWykres(&glowa;,˛,iloscKol);
    wybor=0;
    break;
   }
  }
  printf("podaj: ");
  scanf("%d",&wybor;);
 }
 while(wybor!=0);
 
 UsunCalosc(&glowa;,˛);
 free(etykiety);

 system&#40;"Pause"&#41;;
}

 

Revise this Paste

Parent: 42383
Children: 42385
Your Name: Code Language: