它在 c++ 中对不起:(我不知道 c
这是一个非常简单的逻辑代码,用于简单的头脑:我也是初学者,我还没有测试过这个程序,如果出现问题,很抱歉,但完全正确
我的解析器以同样的原则工作,它工作得很好。所以这是一个真正的方法。不是很有效,但是...
不要马上使用这个程序,了解它的逻辑这将对你有很大帮助。复制不会给你任何东西
...解析器导师是如此罕见....
int x=0;
字符 ch = 'r'; //我已经使用这个等式来避免第一次 ckeck 的错误。
它必须在程序启动时被填充。
字符比奇[10];
int checknumber = 0;
float firstnumber = 0;
浮动秒数 = 0;
浮动结果=0;
void clearar(char frombigar[10], int xar) //这个函数获取 bigch 作为引用,这意味着 eny
这里所做的更改,将直接影响 bigch 本身。
ths 函数获取数组的实际长度并放置空格
在 bigch 的每个元素中将数字归零。我们需要清除
以前任何数字的大人物。在下面你会明白为什么我需要这个。
'xar' 是来自主函数的 x。它在这里告诉我们的清洁工
填充的 bigar 元素的真实长度。
{
对于 (int i=0; 我
}
}
int main()
{
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(数组到浮点数)命令转换
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.
}
校验码 = 0;如果两个数字都被读出并转换。我们需要重置
空间标志。并从0开始计数;下一对号码。
结果=第一个数字+第二个数字;那么这里一切都清楚了。
}
}