<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Linux Forum &mdash; Сортировка строк по количеству слов (C)]]></title>
	<link rel="self" href="https://linuxforum.ru/extern.php?action=feed&amp;tid=14208&amp;type=atom" />
	<updated>2011-06-08T03:52:41Z</updated>
	<generator>PunBB</generator>
	<id>https://linuxforum.ru/viewtopic.php?id=14208</id>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=186026#p186026" />
			<content type="html"><![CDATA[<p>man 3 qsort<br /></p><div class="codebox"><pre><code> qsort (pt, total_lines, sizeof (structure), compare);</code></pre></div><p>structure наверно всётаки {char*str; int words;}.</p>]]></content>
			<author>
				<name><![CDATA[Fat-Zer]]></name>
				<uri>https://linuxforum.ru/profile.php?id=11911</uri>
			</author>
			<updated>2011-06-08T03:52:41Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=186026#p186026</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=186007#p186007" />
			<content type="html"><![CDATA[<p>Елки-палки, с qsort&#039;ом разобраться не могу(<br /></p><div class="codebox"><pre><code>struct structure                                  
    {
    char c;
    int words;
    };

structure *pt = (structure*) malloc (total_lines * sizeof(structure));            //разместил массив структур в куче




int compare (const void *p1, const void *p2)       // функция сравнения
    {
    return  ((*(structure*)p1).words - (*(structure*)p2).words);
    }



 ... 


for (int i = 0; i &lt; total_lines; ++i)                    
        {
        qsort (&amp;(pt[i].words), total_lines*sizeof(int), sizeof (int), compare);
        printf (&quot;%d \n&quot;, pt[i].words);
        
        }</code></pre></div><p>На выводе &quot;мусор&quot; и несколько значений words (неотсортированных)</p><p>Что делаю не так?</p>]]></content>
			<author>
				<name><![CDATA[Pek1neZ]]></name>
				<uri>https://linuxforum.ru/profile.php?id=14531</uri>
			</author>
			<updated>2011-06-07T20:12:35Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=186007#p186007</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185775#p185775" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Pek1neZ пишет:</cite><blockquote><p>Если qsort&#039;ом отсортирую массив из количества слов, порядок строк ведь не изменится?<br />Или я что-то не так понял?</p></blockquote></div><p>а вы сделайте не несколько массивов, а один массив структур</p><p>struct Word{<br />char *string;<br />int count;<br />};</p><p>а потом его отсортируйте.</p><p>Это кстати куда как грамотнее и быстрее связанного списка.</p>]]></content>
			<author>
				<name><![CDATA[emulek]]></name>
				<uri>https://linuxforum.ru/profile.php?id=17759</uri>
			</author>
			<updated>2011-06-07T07:59:46Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185775#p185775</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185772#p185772" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Pek1neZ пишет:</cite><blockquote><p>На словах вроде понял, но слабо себе представляю, как это реализовать(</p></blockquote></div><p>список?<br /></p><div class="codebox"><pre><code>struct List{
List *next;
int data;
};

// последний эл-т указывает на NULL
// первый эл-т
List *first = NULL;// список вначале пуст

//вставка в нужную позицию
// например список такой 10 20 30 40
// вставляем 27 после 20
void insert(int d)
{
List *p = first;
while(*p &amp;&amp; p-&gt;next &amp;&amp; p-&gt;data &lt; d &amp;&amp; p-&gt;data-&gt;next &lt; d)
 p = p-&gt;next;
List *n = malloc(sizeof(List));
if(*p)
{
if(p-&gt;next)
{
n-&gt;next = p-&gt;next;
p-&gt;next = n;
}
else
// дальше сами, лениво писать</code></pre></div>]]></content>
			<author>
				<name><![CDATA[emulek]]></name>
				<uri>https://linuxforum.ru/profile.php?id=17759</uri>
			</author>
			<updated>2011-06-07T07:57:32Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185772#p185772</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185771#p185771" />
			<content type="html"><![CDATA[<p>Если qsort&#039;ом отсортирую массив из количества слов, порядок строк ведь не изменится?<br />Или я что-то не так понял?</p>]]></content>
			<author>
				<name><![CDATA[Pek1neZ]]></name>
				<uri>https://linuxforum.ru/profile.php?id=14531</uri>
			</author>
			<updated>2011-06-07T07:56:31Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185771#p185771</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185770#p185770" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Pek1neZ пишет:</cite><blockquote><p>Нужно отсортировать строки по количеству слов. Как это можно сделать?</p></blockquote></div><p><strong>man qsort</strong></p>]]></content>
			<author>
				<name><![CDATA[emulek]]></name>
				<uri>https://linuxforum.ru/profile.php?id=17759</uri>
			</author>
			<updated>2011-06-07T07:49:32Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185770#p185770</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185761#p185761" />
			<content type="html"><![CDATA[<p>На словах вроде понял, но слабо себе представляю, как это реализовать(</p>]]></content>
			<author>
				<name><![CDATA[Pek1neZ]]></name>
				<uri>https://linuxforum.ru/profile.php?id=14531</uri>
			</author>
			<updated>2011-06-07T07:38:13Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185761#p185761</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185729#p185729" />
			<content type="html"><![CDATA[<p>точнее, пожалуй, будет сказать не массив, а связанный список</p>]]></content>
			<author>
				<name><![CDATA[voral]]></name>
				<uri>https://linuxforum.ru/profile.php?id=841</uri>
			</author>
			<updated>2011-06-07T06:43:55Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185729#p185729</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185728#p185728" />
			<content type="html"><![CDATA[<p>массив. в каждом элементе строка и кол-во слов&nbsp; в ней.<br />при получении очередной строки вставляете ее в нужную позицию в массиве.</p>]]></content>
			<author>
				<name><![CDATA[voral]]></name>
				<uri>https://linuxforum.ru/profile.php?id=841</uri>
			</author>
			<updated>2011-06-07T06:43:09Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185728#p185728</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Сортировка строк по количеству слов (C)]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=185717#p185717" />
			<content type="html"><![CDATA[<p>Функция считает слова и символы в строке. Затем количество слов и строк заносится в массив. Строка вводится из файла или консоли. <br /></p><div class="codebox"><pre><code>for (int i = 0; i &lt; total_lines; ++i)
        {
                
            int c, words, chars, condition;                
            c = words = chars = 0;                                    
            condition = OUT;
            while ((c = getc (input)) != &#039;\n&#039; &amp;&amp; &#039;\r&#039;) 
        
            {
                ++chars;
                if (c == &#039; &#039; || c == &#039;\n&#039; || c == &#039;\t&#039;)
                    condition = OUT;
                else if (condition == OUT) 
                    {
                    condition = IN;
                    ++words;
                    }
            }
            printf (&quot;words:%d chars:%d\n&quot;, words, chars);

            words_base[i] = words;
            printf (&quot;%d\n&quot;, words_base[i]);
        }</code></pre></div><br /><p>Пример работы:</p><p>total lines:3<br />words:3 chars:9<br />words:1 chars:6<br />words:5 chars:3</p><br /><p>Нужно отсортировать строки по количеству слов. Как это можно сделать? </p><br /><p>Заранее спасибо.</p>]]></content>
			<author>
				<name><![CDATA[Pek1neZ]]></name>
				<uri>https://linuxforum.ru/profile.php?id=14531</uri>
			</author>
			<updated>2011-06-07T06:24:44Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=185717#p185717</id>
		</entry>
</feed>
