它在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;一切都很清楚。
}
}