对输入进行排序 [英] sorting the input

查看:65
本文介绍了对输入进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1我想创建一个大小为100的指针数组,因为这是我打算采取的最大输入值。我可以创建一个固定大小的数组,但在

结尾我希望我的数组在运行时扩展以适应输入的大小。我是

无法想出所有的事情和做法:


char * arr_of_pointers [100];


似乎是一个完全错误的想法,因为这是一个静态数组。我想

接受输入,然后决定我需要多少内存,然后malloc那个大小的

数组。我提出了K& R2的想法:

1.)读取并保存输入行,直到用户点击EOF

2.)对它们进行排序

3.)打印它们


我对于从什么开始感到困惑。我可以在

解决这个问题吗?我能想到的是:

使用标准库中的排序算法对行进行排序。

使用%s作为printf()中的参数打印行。 />

-
http://lispmachine.wordpress .com /

我的电子邮件ID在以上地址

1st I think of creating an array of pointers of size 100 as this is the
maximum input I intend to take. I can create a fixed size array but in the
end I want my array to expand at run-time to fit the size of input. I am
not able to come up with anyting all all and doing:

char* arr_of_pointers[100];

seems like a completely wrong idea as this is a static array. I want to
take the input and then decide how much memory I need and then malloc the
array of that size. I came up with K&R2 idea:
1.) read and save the input lines till the user hits the EOF
2.) sort them
3.) print them

I am pretty much confused on what to start with. Can I have your ideas in
solving this problem ? All I can think of is:
use sort algorithm from the standard library to sort the lines.
print the lines using %s as argument in printf().

--
http://lispmachine.wordpress.com/
my email ID is at the above address

推荐答案

在星期二,2008年4月22日23:59:49 +0500,arnuld写道:
On Tue, 22 Apr 2008 23:59:49 +0500, arnuld wrote:

1st我想创建一个大小为100的指针数组,因为这是

我打算采取的最大输入。我可以创建一个固定大小的数组,但在

结尾我希望我的数组在运行时扩展以适应输入的大小。我是

无法想出所有的事情和做法:


char * arr_of_pointers [100];
1st I think of creating an array of pointers of size 100 as this is the
maximum input I intend to take. I can create a fixed size array but in the
end I want my array to expand at run-time to fit the size of input. I am
not able to come up with anyting all all and doing:

char* arr_of_pointers[100];


... [SNIP] ....
...[SNIP]....



我想出了这个函数的静态版本有1个错误而且我不能找到一种方法来删除该错误或将其转换为

动态版本程序:

/ *写一个程序从输入读取一组行并对它们进行排序

*然后打印它们。

*

*版本1.0

* /

#include< stdio.h>

#include< stdlib .h>

#include< string.h>


enum MAXLINES {ARRSIZE = 100};


char * arr_of_ptr [ARRSIZE];

char arr_of_lines [ARRSIZE];


int readlines(char **,char *,const int max);

void printlines(char **);

/ * main()只需调用其他函数来完成工作* /


int main(void)

{

if(readlines(arr_of_ptr,arr_of_lin) es,ARRSIZE)0)

{

qsort(arr_of_ptr,ARRSIZE,sizeof(char *)); / *第26行* /

printlines(arr_of_ptr);

}

其他

{

fprintf(stderr,错误:out of memory\\\
);

}


返回0;

}

/ * 1)读取行直到我们得到NULL,

* 2)将这些行存储到一个字符数组中< arr_of_lines>,

* 3)指针arry指针< arr_of_ptr将指向

*字符数组的各个元素< arr_of_lines>,

* < br $>
* /


int readlines(char * arr_of_ptr [],char arr_of_lines [],int max)

{

int num_lines;


char temp_arr [ARRSIZE];


num_lines = 0;


while(fgets(temp_arr,max,stdin))

{

strcpy(arr_of_lines,temp_arr);

* arr_of_ptr ++ = arr_of_lines ++;

++ num_lines;

}


返回num_lines;

}

/ *它会si请打印

*指针数组< arr_of_ptr>元素所指向的线条。

*

* /

void printlines(char * arr_of_ptr [])

{

while(* arr_of_ptr!=''\ 0'')

{

puts(* arr_of_ptr ++);

}

}

======= =========输出====================

[arnuld @ raj C]


I came up with the Static Version of this function with 1 error and I
can''t seem to find a way either to remove that error or to convert it to a
dynamic version of the program:
/* write a program to read a set of lines from input and sort them
* and then print them.
*
* version 1.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum MAXLINES { ARRSIZE = 100 };

char* arr_of_ptr[ARRSIZE];
char arr_of_lines[ARRSIZE];

int readlines( char**, char*, const int max );
void printlines( char** );
/* main() will simply call the other functions to do the job */

int main( void )
{
if( readlines( arr_of_ptr, arr_of_lines, ARRSIZE ) 0 )
{
qsort( arr_of_ptr, ARRSIZE, sizeof( char* ) ); /* Line 26 */
printlines( arr_of_ptr );
}
else
{
fprintf( stderr, "error: out of memory\n" );
}

return 0;
}
/* 1) read lines till we get the NULL,
* 2) store those lines into an array of characters <arr_of_lines>,
* 3) pointer of arry of pointers <arr_of_ptrwill point to the
* individual elements of array of characters <arr_of_lines>,
*
*/

int readlines( char* arr_of_ptr[], char arr_of_lines[], int max )
{
int num_lines;

char temp_arr[ARRSIZE];

num_lines = 0;

while( fgets(temp_arr, max, stdin) )
{
strcpy( arr_of_lines, temp_arr );
*arr_of_ptr++ = arr_of_lines++;
++num_lines;
}

return num_lines;
}
/* it will simply print the lines pointed to by the elements of
* arrays of pointers <arr_of_ptr>.
*
*/
void printlines( char* arr_of_ptr[] )
{
while( *arr_of_ptr != ''\0'' )
{
puts( *arr_of_ptr++ );
}
}
================ OUTPUT ====================
[arnuld@raj C]


gcc -ansi -pedantic -Wall -Wextra 5-7.c

5-7.c:在函数main中:

5- 7.c:27:错误:函数`qsort'的参数太少了'

[arnuld @ raj C]
gcc -ansi -pedantic -Wall -Wextra 5-7.c
5-7.c: In function `main'':
5-7.c:27: error: too few arguments to function `qsort''
[arnuld@raj C]





我知道qsort需要比较功能,我在FAQ中找到了这个:

http://www.c-faq.com/lib/qsort1.html

但声明如下:*(char * const *)p1


完全超出我的能力。



-
http://lispmachine.wordpress.com/

我的电子邮件ID在上述地址



I know that qsort needs a compare function and I found this in FAQ:

http://www.c-faq.com/lib/qsort1.html
but a statement like: *(char * const *)p1

is totally beyond my capability.


--
http://lispmachine.wordpress.com/
my email ID is at the above address


这篇关于对输入进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆