c语言数字字符一起读,c语言:将文件内容读取为数字并将它们一起添加

本文介绍了一个简单的C++逻辑代码实现的解析器,适用于初学者。通过使用字符数组和状态标志来读取文本文件中的数字,并将其转换为浮点数进行加法运算。文章详细解释了代码逻辑,包括如何初始化变量、清理数组以及处理不同状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

它在c ++抱歉:(我不知道c

对于简单的minde来说这是一个非常简单的逻辑代码:我也是一个初学者,我还没有测试过这个编程,如果出现问题但是确切地说对不起

在同样的原则上工作我的解析器,它工作正常。所以这是一个真正的方法。不是很有效但是......

不要马上使用这个程序,了解它的逻辑,这将帮助你很多。复制不会给你任何东西

...解析器导师是如此罕见....

int x = 0;

char ch ='r'; //我用这个方法来避免ch的第一次ckeck出错。

程序启动时必须用东西填充。

char bigch [10];

int checknumber = 0;

float firstnumber = 0;

float secondnumber = 0;

float result = 0;

void clearar(char frombigar [10],int xar)//这个函数获取bigch作为引用,这意味着eny

这里所做的改变,将直接影响到bigch本身。

函数获取数组的实际长度并放置空格

在bigch的每个元素中都将数字归零。我们需要明确

任何以前的数字都很重要。低于你,看看为什么我需要这个。

'xar'是来自main函数的x。它在这里告诉我们清洁工

填充bigar元素的真实长度。

{

for(int i = 0; i

}

}

int main()

{

< ------------------- //在这里添加文件打开和读取命令

while(!myfile.eof())//但尚未到达txt文件的结尾

{

CH = myfile.get(); //将每个字母输入ch,并将光标移动一步

在txt文件中转发以供进一步阅读。

get()自动执行游标转发

if (ch!= " ") //i used space as an indicator where one number ends

//so while space havent been reahced, read letters.

{ bigch[x] = ch; //get read letter into bigch array.

x++; //icrement bigch array step

}

else

if(ch == " ") //if space is reached that means one number has ended and

{ im trying to set a flag at that moment. it will be used further.

checknumber++; the flag is simple number. first space will set checknumber to 1

second space will set it to 2. thats all.

}

if (checknumber == 1) //if our checknumber is 1, wich means that reading

of first number is done, lets make one whole float

from that bigch array.

{firstnumber = atof(bigch); //这里我们得到bigch,atof(array to float)命令转换

bigch数组成一个整数浮点数。

clearar(bigch,x); //here we send bigch and its element step into function where

bigch gets cleaned because we dont want some ghost numbers in it.

abviously clearar function cleans bigch int main function aswell,

not only in it's teritory. its a global cleaning :)

}

else if (checknumber ==2) //here we do the same but if flag is 2 this means that two spaces

had been passed and its time to convert bigch into secondnumber.

{ secondnumber = atof(bigch); //same method of converting array into float (it hates other

not number letters, i mean if its a number its fine. if in your text

was 'a' or 's' in that case atof will panic hehe.. )

clearar(bigch,x); //same thing, we send bigch to cleaner function to kill any numbers

it, we get one space letter ( " " ) into each element of bigch.

}

checknumber = 0;如果两个数字都已被读出并转换。我们需要重置

太空旗手。并从0开始计算;对于下一对数字。

result = firstnumber + secondnumber;一切都很清楚。

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值