简单的C语言编程问题 [英] simple C language programming question

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

问题描述

我有一个功课问题,有些专家可以帮我解决这个问题。


q。编写一个程序来读取文本文件并报告
的数量
整数,你可以假设一个数字被定义为一个或多个数字

由一个或空格字符或标点符号分隔。


例如,

金额为5657,变化为55.价格为123和56.


结果:

#of整数:4


到目前为止我有这个:-)


#include< stdio.h>

#include< ctype.h>


int main(无效)

{

char ch;

int count = 0;

FILE * fp;

char prev;


fp = fopen(" input.txt"," r");


prev ='''';

while((ch = fgetc(fp))!= EOF)

{


prev = ch;

}


printf( 整数数量:%d \ n, t);

fclose(fp);

返回0;

}

Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

The result:
# of integers: 4

so far I have this:-)

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

prev = '' '';
while((ch = fgetc(fp)) != EOF)
{

prev = ch;
}

printf("# of integers: %d\n", count);
fclose(fp);
return 0;
}

推荐答案

Andy写道:
Andy writes:
我有一个功课问题,有些专家可以帮我这个。

q。写一个读取文本文件并报告整数数量的程序,您可以假设一个数字被定义为由一个或空格字符或标点分隔的一个或多个数字。

例如,
金额为5657,变化为55.价格为123和56.
Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.




< snip>


在< ctype.h>中查看你可以用isdgit()执行什么样的魔术。



<snip>

See what kind of magic you can perform with isdgit() in <ctype.h>.


2004年5月5日星期三,Andy写道:
On Wed, 5 May 2004, Andy wrote:
我有一个功课问题,有些专家可以帮我解决这个问题。

q。编写一个程序来读取文本文件并报告
整数的数量,你可以假设一个数字被定义为一个或多个数字
由一个或空格字符或标点分隔。

例如,
金额为5657,变化为55.价格为123和56.结果:
整数数:4

到目前为止我有这个:-)

#include< stdio.h> ;
#include< ctype.h>

int main(无效)
{ch /> char ch;
int count = 0;
FILE * fp;
char prev;

fp = fopen(" input.txt"," r");

prev =''' ';
while((ch = fgetc(fp))!= EOF)
{

prev = ch;
}

printf("整数#:%d \ n",count);
fclose(fp);
返回0;
}
Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

The result:
# of integers: 4

so far I have this:-)

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

prev = '' '';
while((ch = fgetc(fp)) != EOF)
{

prev = ch;
}

printf("# of integers: %d\n", count);
fclose(fp);
return 0;
}



将其视为状态机。你开始处于

not_an_integer状态。获取字符并查找数字。


如果您读取的数字是数字,那么您将转到状态

might_be_an_integer。现在你开始阅读空格或标点符号。


如果你得到一个空格或标点符号,那么递增你的计数器并将

移回not_an_integer。


使用变量来确定你所处的状态,例如0 =

not_an_integer,1 = might_be_an_integer。


唯一可能出错的地方是字符串是否为PI的价值

约为3.14159或7/22。 3。可能会被读作一个整数

和14159将是第二个。写一张支票。你想要7/22

是两个整数吗?你会处理好吗?


P.S.这是许多方法之一。它不一定是最好的但是

非常适合你已经拥有的东西。


-

发送电子邮件to:darrell at cs dot toronto dot edu

不要发电子邮件给 vi ************ @ whitehouse.gov


Andy写道:
<我有一个功课问题,有些专家可以帮我解决这个问题。

q。写一个程序来读取一个文本文件并报告
整数的数量你可以假设一个数字被定义为由一个或空格字符或标点分隔的一个或多个数字。

例如,
金额为5657,变化为55.价格结果是:
整数数:4

到目前为止我有这个:-)

#包括< stdio.h>
#include< ctype.h>

int main(无效)
{zh_cn char ch;
int count = 0; FILE * fp;
char prev;

fp = fopen(" input.txt"," r");

prev =' ''';
while((ch = fgetc(fp))!= EOF)

Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

The result:
# of integers: 4

so far I have this:-)

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

prev = '' '';
while((ch = fgetc(fp)) != EOF)




comp.lang.c中的问题12.1常见问题

问题(FAQ)列表

http://www.eskimo.com/~scs/C-faq/top.html


解释了为什么这是错误的。


这里有一个提示:一旦你读完一个输入字符,

程序的未来路径取决于是否那个

字符是否为数字。有一个现成的方式

做出这个决定,坐在C库中

等着你找到它......


这是另一个提示:当你找到一个数字时,你应该计算一个新的数字吗?并非总是如此,因为

然后你会计算x1y23z中的三个数字。你怎么能告诉''2''和''''是单个

数字的一部分,而不是独立的数字?在你阅读''z'之前你能决定

吗?


-
Er ********* @ sun.com


这篇关于简单的C语言编程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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