| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 978 人关注过本帖
标题:帮帮忙,是新手,这个怎么做??
只看楼主 加入收藏
Osman
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-3-8
收藏
 问题点数:0 回复次数:9 
帮帮忙,是新手,这个怎么做??
Assignment 3 is an exercise in handling of dynamic arrays and strings. Submit (upload) a file with your source code. The file's name must be assgn3.c and it must be an ASCII text file for the upload to work.

For reading of any user's input, use the code from Assignment 2 to read a word of at most 20 characters (i.e. it should not allow the user to enter more than 20 characters, not including the terminating '\n' and using the while-loop to clean up if the user does so). Thus, you need to read into a buffer of size 21. Turn the input word into a string by replacing '\n' with '\0'. Unlike in assignment 2, we must disregard empty input (when the user only strikes <enter> key).

When memory is being allocated (for the dynamic array, and then for each name), the allocation is performed using the standard function malloc() and it must be checked whether it worked or no. For that end use the macro merror()to simplify the program a bit:

Put at the top of your program a line

    #define merror()   {printf("memory allocation problem\n");exit(1);}

Then to test if allocation worked:

    p = (char*) malloc(strlen(buffer)+1));
    if (p == 0) merror();

Hence you will need to include three header files in your program

   1. stdio.h   for printf(), getchar() or fgetc(), fflush(stdout)
   2. stdlib.h  for exit() and atoi()
   3. string.h  for strlen() and strcpy()

If a number is to be read, read it as a word into the buffer, and then use the standard function atoi(buffer) to turn it into the appropriate number. For instance, if size is my variable for size, and buffer is my input buffer, size=atoi(buffer); will do the trick, of course, only after buffer was filled with the user's input.

   1. The user is prompted for how many names he/she will enter.
   2. Then a (dynamic) array of the requested size is created. It is an array of strings.
   3. In a loop of the requested size
         1. prompt the user to enter a name of at most 20 characters.
         2. read the name and turn into a string.
         3. allocate  memory for a copy of the string and store its address in the appropriate item of the  dynamic array.
         4. copy the input string into the memory allocated.
   4. When the loop is over, display the strings stored in the dynamic array.
   5. Use bubble sort the sort the strings in the lexico-graphic order (like in Assignment 2, however we will just swap pointers rather than swapping strings by copying them).
   6. Display the (sorted) strings.

how many names will be entered :
empty input
how many names will be entered :123456789012345678902134556677
input too long
how many names will be entered :3
you will enter 3 names
enter a name:
empty input
enter a name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
input too long
enter a name: bill
enter a name: adam
enter a name: aaron
names as entered:
bill
adam
aaron
sorted names:
aaron
adam
bill
搜索更多相关主题的帖子: including exercise reading dynamic 
2008-03-08 13:06
xfcyjhb
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:116
专家分:0
注 册:2008-2-26
收藏
得分:0 
我晕!

多C多智慧,将C进行到底.........
2008-03-08 18:50
xianshizhe111
Rank: 1
等 级:新手上路
帖 子:1451
专家分:0
注 册:2007-12-8
收藏
得分:0 
不可能不晕,先做翻译,再做编程.
2008-03-08 20:14
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
我也来晕晕。。。不想看。。。。

学习需要安静。。海盗要重新来过。。
2008-03-08 20:15
mqh21364
Rank: 1
等 级:新手上路
帖 子:642
专家分:0
注 册:2008-2-28
收藏
得分:0 
上个星期公司举行英语笔试,我得了16.5。好受打击哦!!
再次我也是过6级的人啊!!!
看来东西老不用是会忘记的!!
2008-03-09 10:00
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
六级效果不大啊..我也过了..但是觉得收效不大啊....

学习需要安静。。海盗要重新来过。。
2008-03-09 10:06
yt414204458
Rank: 2
等 级:论坛游民
帖 子:260
专家分:55
注 册:2008-3-1
收藏
得分:0 
刚开始学了,有些代码原来是见过的,不过还不知道是什么意思,得好好学习一下了
2008-03-09 16:38
zhuwei168
Rank: 1
来 自:东软信息学院
等 级:新手上路
帖 子:180
专家分:0
注 册:2008-2-13
收藏
得分:0 
汗楼主是中国人么
那么多的英语看得脑袋都大了~~~
2008-03-09 18:19
hjkgame
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-3-2
收藏
得分:0 
新手也应该有新手的态度  更何况是问问题了  LZ显然不懂这个道理
2008-03-09 19:20
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:0 
看来LZ也是不会英文
看来LZ的英文也不太好,不然不会整篇地COPY上来,他肯定是自己也翻译不过来,才弄来让大家翻译再弄代码滴。

呵呵~~~有意思。
2008-03-09 19:55
快速回复:帮帮忙,是新手,这个怎么做??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041110 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved