看过来...

bigtea 2003-01-11 01:15:38
#include <stdio.h>
#define MAXLINE 1000 /*最大输入行的大小*/

int getline (char line[],int maxline );
void copy (char tO[],char from []);

/*打印最长的输入行*/
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];

max = 0;

while ((len = getline (line,MAXLINE)) >0)
if (len > max){
max = len;
copy (longest,line);
}
if (max > 0 )
printf ("%s", longest);
return 0;
}
/*getline;将一行读入S中并返回其长度*/
int getline (char s[],int lim)
{
int c,i;

for (i = 0; i < lim -1 && (c = getchar ()) != '@' && c != '\n'; ++i)
s[i] = c;
if (c == '\n' ){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/*copy :从from拷贝到to,假定to足够大*/
void copy (char tO[],char from[])
{
int i;

i = 0;
while (( tO [i] = from [i]) != '\0')
++i;
}

此程序为书中例题实现打印最长输入行,课后练习提示:
对用于打印最长行的主程序main进行修改使之可以打印任意长度的输入行的长度以及文本行中尽可能多的字符.
不知道啥意思.
...全文
26 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
heartcool 2003-03-25
  • 打赏
  • 举报
回复
给分告诉你!哈哈!
楼上兄己经回答出!
bigtea 2003-03-25
  • 打赏
  • 举报
回复
malloc?
bigtea 2003-01-11
  • 打赏
  • 举报
回复
i don't know.please say more.
web_spider 2003-01-11
  • 打赏
  • 举报
回复
根据输入值,在heap中动态生成数组。
就可以实现任意长度(相对的)的打印了。
记着,最后,把分配的内存删除掉阿。
coader 2003-01-11
  • 打赏
  • 举报
回复
我想是malloc的意思
luixui 2003-01-11
  • 打赏
  • 举报
回复
hehe
erigido 2003-01-11
  • 打赏
  • 举报
回复
1000也太夸张了吧?
web_spider 2003-01-11
  • 打赏
  • 举报
回复
大体上的代码如下:
int main()
{
int len;
int max;
int maxLine=0;
printf("Please input the max lines to print:\n");
scanf("%d",&maxLine);
char *pLine= (char*)malloc(maxLine*sizeof(char));
char *pLongest=(char*)malloc(maxLine*sizeof(char));
if((!pLine)||(!pLongest))
{
if(pLine)
free(pLine);
if(pLongest)
free(pLongest);
printf("malloc error");
exit(1);
}
printf("%d",maxLine);

max = 0;

while ((len = getline (pLine,maxLine)) >0)
if (len > max)
{
max = len;
copy (pLongest,pLine);
};
if (max > 0 )
printf ("%s", pLongest);
free(pLine);
free(pLongest);

return 0;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧