Results 1 to 4 of 4

Thread: :

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    gibona's Avatar
    Join Date: Feb:2005
    Location: Sofia
    Posts: 3,074

    :

    :
    ( ++), :
    - ( ).
    - .
    - ,
    .
    5:
    - .
    - .
    6:
    - .
    - .
    ,
    .
    - .
    , ,
    .
    - .
    - .

    :
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int *a,size,avail;
    void inita()
    {
    	avail=8;
    	a=(int *)malloc(sizeof(int)*avail);
    	size=0;
    }
    void adda(int k)
    {
    	if (avail==(size+1))
    	{
    		avail*=2;
    		a=(int *)realloc(a, (sizeof(int) * avail));
    	}
    	a[size]=k;
    	size++;
    }
    void printall()
    {
    	int i;
    	printf("Razpechatvane na masiva \n");
    	for(i=0;i<size;i++) printf("%d, ",a[i]);
    	printf("\n\n");
    }
    void input_kb()
    {
    	inita();
    	char temp[80];
    	int t,n,i,j,f;
    	printf("Kolko elementa: ");
    	scanf("%d",&n);
    	gets(temp);
    	for(i=0;i<n;i++)
    	{
    		printf("Vavedete [%d] element: ",i);
    		gets(temp);
    		f=0;
    		for(j=0;j<(int)strlen(temp);j++)
    		if (temp[j]>'9' || temp[j]<'0') f=1;
    		if (strlen(temp)>9) f=1;
    		if (f==0)
    		{
    			t=atoi(temp);
    			adda(t);
    		}
    		else
    		{
    			i--;
    		}
    	}
    	return;
    }
    void input_file()
    {
    	FILE * pFile;
    	char temp[100], filename[80];
    	pFile=NULL;
    	while (pFile == NULL)
    	{
    		printf("Vavedete imeto na faila: ");
    		gets(filename);
    		inita();
    		pFile = fopen (filename , "r");
    		if (pFile == NULL) printf("Greshka pri otvarianeto na faila!\n");
    	}
    	int t,j,f;
    	while (fgets(temp,80,pFile))
    	{
    		f=0;
    		for(j=0;j<(int)strlen(temp);j++)
    		if ((temp[j]>'9' || temp[j]<'0') && temp[j]!=10) f=1;
    		if (strlen(temp)>100) f=1;
    		if (f==0)
    		{
    			t=atoi(temp);
    			adda(t);
    		}
    	}
    	printf("Uspeshno procheteni %d chisla\n",size);
    	fclose (pFile);
    	return;
    }
    void swap(int *a, int *b)
    {
    	int t=*a; *a=*b; *b=t;
    }
    
    void sort()
    {
    	int i,j;
    	for(i=0;i<size;i++)
    	for(j=i+1;j<size;j++)
    	if (a[i]>a[j]) swap(&a[i],&a[j]);
    }
    
    void tripple_search()
    {
    	int l,r,m1,m2,f=-1,j,t,res;
    	char temp[80];
    	while(f!=0)
    	{
    		printf("Koe e tarsenoto chislo: ");
    		gets(temp);
    		f=0;
    		for(j=0;j<(int)strlen(temp);j++)
    		if(temp[j]>'9' || temp[j]<'0') f=1;
    		if (strlen(temp)>9) f=1;
    		if (f==0)
    		{
    			t=atoi(temp);
    		}
    	}
    	l=0;
    	r=size-1;
    	res=-1;
    	while (l<=r)
    	{
    		m1=l+(r-l)/2;
    		m2=m1+(r-l)/2;
    		if (a[l]==t)
    		{
    			res=l;
    			break;
    		}
    		if (a[r]==t)
    		{
    			res=r;
    			break;
    		}
    		if (a[m1]==t)
    		{
    			res=m1;
    			break;
    		}
    		if (a[m2]==t)
    		{
    			res=m2;
    			break;
    		}
    		if (a[l]<t && t<a[m1])
    		{
    			l++;
    			r=m1-1;
    		}
    		else
    		{
    			if (a[m1]<t && t<a[m2])
    			{
    				l=m1+1;
    				r=m2-1;
    			}
    			else
    			{
    				l=m2+1;
    				r=r-1;
    			}
    		}
    	}
    	if (res==-1) printf("Chisloto ne e namereno!\n");
    	else printf("Chisloto namereno na poziciq %d\n",res);
    	return;
    }
    int main()
    {
    	char ch[10];
    	while(1)
    	{
    		printf("1. Vavejdane na masiv\n");
    		printf("2. Chetene ot Fail\n");
    		printf("3. Sortirane\n");
    		printf("4. Tarsene\n");
    		printf("5. Otpechatvane na masiva\n");
    		printf("0. Exit\n");
    		gets(ch);
    		switch (ch[0])
    		{
    			case '1':
    			{
    				input_kb();
    				break;
    			}
    			case '2':
    			{
    				input_file();
    				break;
    			}
    			case '3':
    			{
    				sort();
    				break;
    			}
    			case '4':
    			{
    				tripple_search();
    				break;
    			}
    			case '5':
    			{
    				printall();
    				break;
    			}
    			case '0':
    			{
    				return 0;
    			}
    		}
    	}
    	return 0;
    }
    /Hidden:

    :
    -- gets, malloc, realloc.
    -- .

    (list)
    , .
    :
    • 4?
    • ( , ) malloc/realloc?
    • 6- ?

  2. #2
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    , .

    malloc()/realloc() ? , - (list) ? , .

    malloc()/realloc() calloc()

  3. #3
    gibona's Avatar
    Join Date: Feb:2005
    Location: Sofia
    Posts: 3,074
    , ( ).

  4. #4
    Bombera's Avatar
    Join Date: Jul:2001
    Location: 4EVA
    Posts: 13,833
    , , ?
    C++ malloc .
    Last edited by Bombera; 5th September 2009 at 16:50. Reason: ->
    EVGA X299 FTW K|i9-7960X@4.7|4x8 Patriot Viper Steel 4000|GTX 1660 Ti|970 EVO 1 TB|Seasonic Focus GX-1000|Xigmatek Elysium|
    Rampage IV Extreme BE|E5-1680v2@4.7|4x4 HyperX 1866|Cougar Aqua 240|GTX 1050 Ti|970 EVO 1/4 TB|CM 850 SilentPro|HAF-X|

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |