帮助一个程序 [英] Help with a progam

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

问题描述

大家好!


我正在尝试从我的C书中做一个练习,恰好是非常类似于最近的一本书。 ;程序"线程。


任务:编写一个程序,使用重定向接受来自磁盘的输入

文件,

计算数量乘以字母表的前三个字母(a,b,

c,以及A,B,C)

出现在文件中。


以下是我的尝试:

================================= =========

#include< stdio.h>

#include< stdlib.h>

#定义MAX 1000 / *文件中的最大字符数* /


int main(无效)

{

char * ptr;

int count,

ctra = 0,

ctrb = 0,

ctrc = 0;


ptr = malloc(1000 * sizeof(char));

得到(ptr);

for(count = 0; count< MAX; count ++){

if(ptr [count] ==''a''|| ptr [count] ==''A'')

ctra ++;

if(ptr [count] ==''b''|| ptr [count] ==''B'')

ctrb ++;

if(ptr [count] == 'c''|| ptr [count] ==''C'')

ctrc ++;

}

printf(" ctra =%d \ n",ctra);

printf(" ctrb =%d \ n",ctrb);

printf(" ctrc =% d \ n",ctrc);

免费(ptr);

返回0;

}

==========================================

问题:只有当已知(b 输入.txt

)中的最大字符数(小于1000)时才能正常工作。


我试过包括:


=======================

while((ch = getchar())!= EOF){

ctr ++;

}

======= =================


这样做的原因是我可以在malloc中使用ctr来

分配足够的内存来获取文件中的所有字符。


ie。 ptr = malloc(ctr * sizeof(char));

得到(ptr);


但是,因为getchar *得到了来自stdin的所有字符,声明

得到(ptr)从stdin获得*没有*。


我已经为此工作了2天。它正在推动我NUTTTSSSSS !!!

有人可以给我一个提示或2吗?


先谢谢你们!热爱你的工作!


巴克。


-

使用M2,Opera的革命性e-邮件客户端: http://www.opera.com/m2/

Hi guys!

I am trying to do an excercise from my C book, which coincidently is very
similar to the one in the recent "program" thread.

Task: Write a program that uses redirection to accept input from a disk
file,
counts the number of times the first three letters of the alphabet (a, b,
c, and A, B, C)
occur in the file.

Below is my attempt:
==========================================
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 /*Maximum number of characters in the file */

int main( void)
{
char *ptr;
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;

ptr = malloc(1000 * sizeof(char));
gets(ptr);
for (count = 0; count < MAX; count++) {
if( ptr[count] == ''a'' || ptr[count] == ''A'')
ctra++;
if( ptr[count] == ''b'' || ptr[count] == ''B'')
ctrb++;
if( ptr[count] == ''c'' || ptr[count] == ''C'')
ctrc++;
}
printf("ctra = %d\n", ctra);
printf("ctrb = %d\n", ctrb);
printf("ctrc = %d\n", ctrc);
free(ptr);
return 0;
}
==========================================
Problem: This works fine only if the maximum number of characters in
input.txt
is known(less than 1000).

I''ve tried including:

=======================
while ((ch = getchar()) != EOF) {
ctr++;
}
========================

The reason for this is so that I can use ctr in a malloc to
allocate enough memory to get all the characters in the file.

ie. ptr = malloc(ctr * sizeof(char));
gets(ptr);

However, because getchar *got* all the characters from stdin, the statement
gets(ptr) gets *nothing* from stdin.

I''ve been working on this for 2 days. It''s driving me NUTTTSSSSS!!!
Can someone please give me a hint or 2?

Thanks in advance guys! Love your work!

Buck.

--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/

推荐答案



[..]

[..]
#include< stdio.h> ;
#include< stdlib.h>


#include< ctype.h>

#define MAX 1000 / *文件中的最大字符数* /
int main(void)
{char * ptr;
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;

ptr = malloc(1000 * sizeof(char));


if(!ptr)

{

perror(" Malloc");

退出(EXIT_FAILURE);

}

得到(ptr);


gets()很危险。改为使用fgets(3)。

for(count = 0; count< MAX; count ++){
if(ptr [count] ==''a''|| ptr [count] ==''A'')
ctra ++;
if(ptr [count] ==''b''|| ptr [count] ==''B'')
ctrb ++;
if(ptr [count] ==''c''|| ptr [count] ==''C'')
ctrc ++;
}


我更喜欢这个:


/ *你说重定向? * /

while(fgets(ptr,MAX,stdin))

{

char * a;


a = ptr;

while(* a)

{

if(toupper(* a)==''A' ')

ctra ++;


if(toupper(* a)==''B'')

ctrb ++;


if(toupper(* a)==''C'')

ctrc ++;


a ++;

}

} printf(" ctra =%d \ n",ctra);
printf(" ctrb =%d \ n" ,ctrb);
printf(" ctrc =%d \ n",ctrc);
免费(ptr);
返回0;
}
= =========================================
问题:这很好用只有当
input.txt
中的最大字符数已知时(小于1000)。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 1000 /*Maximum number of characters in the file */

int main( void)
{
char *ptr;
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;

ptr = malloc(1000 * sizeof(char));
if ( !ptr )
{
perror ( "Malloc" );
exit ( EXIT_FAILURE );
}
gets(ptr);
gets() is dangerous. Use fgets(3), instead.
for (count = 0; count < MAX; count++) {
if( ptr[count] == ''a'' || ptr[count] == ''A'')
ctra++;
if( ptr[count] == ''b'' || ptr[count] == ''B'')
ctrb++;
if( ptr[count] == ''c'' || ptr[count] == ''C'')
ctrc++;
}
I would prefer this instead:

/* You said redirection? */
while ( fgets ( ptr, MAX, stdin ) )
{
char *a;

a = ptr;
while ( *a )
{
if ( toupper ( *a ) == ''A'' )
ctra++;

if ( toupper ( *a ) == ''B'' )
ctrb++;

if ( toupper ( *a ) == ''C'' )
ctrc++;

a++;
}
} printf("ctra = %d\n", ctra);
printf("ctrb = %d\n", ctrb);
printf("ctrc = %d\n", ctrc);
free(ptr);
return 0;
}
==========================================
Problem: This works fine only if the maximum number of characters in
input.txt
is known(less than 1000).




我想这是因为获取()!


[..]

-

Vijay Kumar R Zanvar

我的主页 - http://www.geocities.com/vijoeyz/




巴克罗杰斯 < wh*@cares.com.au>在消息中写道

news:op ************** @ news.ozemail.com.au ...

"Buck Rogers" <wh*@cares.com.au> wrote in message
news:op**************@news.ozemail.com.au...
大家好!

我正在尝试从我的C书中做一个练习,这个练习非常类似于最近的程序中的那本书。线程。

任务:编写一个程序,使用重定向接受来自磁盘
文件的输入,
计算字母表的前三个字母的次数(a,b ,
c,以及A,B,C)
发生在文件中。

以下是我的尝试:
========== ================================
#include< stdio.h>
#include < stdlib.h>
#define MAX 1000 / *文件中的最大字符数* /

int main(无效)
{
char * ptr ;
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;

ptr = malloc(1000 * sizeof(char)获得(ptr);
for(count = 0; count< MAX; count ++){
if(ptr [count] ==''a''|| ptr [ count] ==''A'')
ctra ++;
if(ptr [count] ==''b''|| ptr [count] ==''B'')
ctrb ++;
if(ptr [count] ==''c'' || ptr [count] ==''C'')
ctrc ++;
}
printf(" ctra =%d \ n",ctra);
printf (ctrb =%d \ n,ctrb);
printf(" ctrc =%d \ n",ctrc);
免费(ptr);
返回0 ;
}
======================================== ==
问题:只有当
input.txt
中的最大字符数已知(小于1000)时才能正常工作。

我已经尝试包括:

=======================
while((ch = getchar())!= EOF ){
ctr ++;
}
========================

这样做的原因是我可以在malloc中使用ctr来分配足够的内存来获取文件中的所有字符。

即。 ptr = malloc(ctr * sizeof(char));
得到(ptr);

但是,因为getchar *得到了来自stdin的所有字符,所以
语句得到( ptr)从标准输入中得到*没有*。

我已经为此工作了2天。它正在推动我NUTTTSSSSS !!!
有人可以给我一个提示还是2?

先谢谢你们!热爱你的工作!

巴克。
Hi guys!

I am trying to do an excercise from my C book, which coincidently is very
similar to the one in the recent "program" thread.

Task: Write a program that uses redirection to accept input from a disk
file,
counts the number of times the first three letters of the alphabet (a, b,
c, and A, B, C)
occur in the file.

Below is my attempt:
==========================================
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 /*Maximum number of characters in the file */

int main( void)
{
char *ptr;
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;

ptr = malloc(1000 * sizeof(char));
gets(ptr);
for (count = 0; count < MAX; count++) {
if( ptr[count] == ''a'' || ptr[count] == ''A'')
ctra++;
if( ptr[count] == ''b'' || ptr[count] == ''B'')
ctrb++;
if( ptr[count] == ''c'' || ptr[count] == ''C'')
ctrc++;
}
printf("ctra = %d\n", ctra);
printf("ctrb = %d\n", ctrb);
printf("ctrc = %d\n", ctrc);
free(ptr);
return 0;
}
==========================================
Problem: This works fine only if the maximum number of characters in
input.txt
is known(less than 1000).

I''ve tried including:

=======================
while ((ch = getchar()) != EOF) {
ctr++;
}
========================

The reason for this is so that I can use ctr in a malloc to
allocate enough memory to get all the characters in the file.

ie. ptr = malloc(ctr * sizeof(char));
gets(ptr);

However, because getchar *got* all the characters from stdin, the statement gets(ptr) gets *nothing* from stdin.

I''ve been working on this for 2 days. It''s driving me NUTTTSSSSS!!!
Can someone please give me a hint or 2?

Thanks in advance guys! Love your work!

Buck.




这里有一些值得深思的东西:


1 )为什么不一次一个地获得每个炭。这可能不会很快,但是它可以工作。


2)如果你想一次加载一个块,请使用fread()


3)如果你想立即加载完整的文件,那么看看fseek()

和ftell()来获取文件大小。我相信这对于非二进制文件来说只是可靠的便携式,但我可能会误会。


HTH

Allan



Here is some food for thought:

1) why dont you get each char, one at a time. This may not be fast, but it
will work.

2) If you want to load in a block at a time, use fread()

3) If you want to load the complete file in at once, then look at fseek()
and ftell() to get the file size. I believe that this is only reliably
portable for non-binary files, but I could be mistaken.

HTH
Allan


Buck Rogers写道:
Buck Rogers wrote:
大家好!

我正在努力做一个练习我的C书,与最近的程序相似,非常类似。线程。

任务:编写一个程序,使用重定向接受来自磁盘
文件的输入,
计算字母表前三个字母的次数(a,
b,c和A,B,C)
发生在文件中。

以下是我的尝试:
========== ================================
#include< stdio.h>
#include < stdlib.h>
#define MAX 1000 / *文件中的最大字符数* /

int main(无效)
{
char * ptr ;


你最好在这里有一个int,

int c; / *好的,可以选择更好的名字* /

int count,
ctra = 0,
ctrb = 0,
ctrc = 0;

ptr = malloc(1000 * sizeof(char));
得到(ptr);


只需浏览文件:

while((c = fgetc(stdin))!= EOF){

开关(c)/ *使用toupper或tolower会减少

案件数量* /

{

case''a'':

case''A'':ctra ++;

break;

case''b'':

case ''B'':ctrb ++;

break;

case''c'':

case''C'':ctrc ++;

休息;

默认:休息;

}

for(count = 0; count< MAX; count ++){
if(ptr [count] ==''a''|| ptr [count] ==''A'')
ctra ++;
if(ptr [count] ==''b''|| ptr [count] ==''B'')
ctrb ++;
if(ptr [count] ==''c''|| ptr [count] ==''C'')
ctrc ++;
}
printf(" ctra =%d \ n",ctra);
printf(" ctrb =% d \ n,c trb);
printf(" ctrc =%d \ n",ctrc);
免费(ptr);
返回0;
}
== ========================================
问题:这只能正常工作如果已知
input.txt
中的最大字符数(小于1000)。


见上文。

我试过包括:

============ ===========
while((ch = getchar())!= EOF){
ctr ++;
}
====== ==================

这样做的原因是我可以在malloc中使用ctr来分配足够的内存来获取文件中的所有字符。

即。 ptr = malloc(ctr * sizeof(char));
得到(ptr);


即使你想要文件中的所有字符,使用gets()也不是

的方法。首先,gets()本质上是邪恶的。至少使用fgets()

。但是gets()不会得到文件中的所有文本,而只是

第一行。你仍然需要一个循环。 fread()是填充

缓冲区的方法,但对于练习来说是不必要的,因为无论如何输入流都将被缓冲。
<但是,因为getchar *得到了来自stdin的所有字符,所以语句
得到(ptr)从stdin得到* nothing *。
Hi guys!

I am trying to do an excercise from my C book, which coincidently is very
similar to the one in the recent "program" thread.

Task: Write a program that uses redirection to accept input from a disk
file,
counts the number of times the first three letters of the alphabet (a,
b, c, and A, B, C)
occur in the file.

Below is my attempt:
==========================================
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 /*Maximum number of characters in the file */

int main( void)
{
char *ptr;
You are better off just have an int here,
int c; /* OK, a better name could be chosen */
int count,
ctra = 0,
ctrb = 0,
ctrc = 0;

ptr = malloc(1000 * sizeof(char));
gets(ptr);
just go through the file:
while ((c = fgetc(stdin)) != EOF) {
switch (c) /* using toupper or tolower would reduce
the number of cases */
{
case ''a'':
case ''A'': ctra++;
break;
case ''b'':
case ''B'': ctrb++;
break;
case ''c'':
case ''C'': ctrc++;
break;
default: break;
}
for (count = 0; count < MAX; count++) {
if( ptr[count] == ''a'' || ptr[count] == ''A'')
ctra++;
if( ptr[count] == ''b'' || ptr[count] == ''B'')
ctrb++;
if( ptr[count] == ''c'' || ptr[count] == ''C'')
ctrc++;
}
printf("ctra = %d\n", ctra);
printf("ctrb = %d\n", ctrb);
printf("ctrc = %d\n", ctrc);
free(ptr);
return 0;
}
==========================================
Problem: This works fine only if the maximum number of characters in
input.txt
is known(less than 1000).
See above.

I''ve tried including:

=======================
while ((ch = getchar()) != EOF) {
ctr++;
}
========================

The reason for this is so that I can use ctr in a malloc to
allocate enough memory to get all the characters in the file.

ie. ptr = malloc(ctr * sizeof(char));
gets(ptr);
Even if you wanted all the characters in the file, using gets() is not the
way to do it. First, gets() is inherently evil. At least use fgets()
instead. But gets() will not get all the text in the file, but just the
first line. You would still need a loop. fread() is the the way to fill a
buffer, but is unnecessary for your exercise, since the input stream will
be buffered, anyway.

However, because getchar *got* all the characters from stdin, the statement
gets(ptr) gets *nothing* from stdin.




见以上。

-

Martin Ambuhl



See above.
--
Martin Ambuhl


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

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