其在C++對不起:(我不知道的C
這是一個簡單的民德一個非常簡單的邏輯代碼:D我是一個begineer過,如果出了問題我還沒有測試這種前衛很抱歉,但是,究竟 一個相同的原則是我的解析器工作,它工作得很好,所以這是一個真正的方法,不是很有效,但... 不要立即使用這個程序,理解它的邏輯,這將幫助你很多,複製,不會給你任何東西 ...解析器導師是如此罕見... ...
int x = 0; char ch ='r'; // i'v用這個equasion來避免呃在第一個cckck ror。 程序啓動時,它必須由某些東西填充。 char bigch [10]; int checknumber = 0;
float firstnumber = 0; float secondnumber = 0; float result = 0; (char frombigar [10],int xar)//此函數獲取bigch作爲參考,這意味着eny 在此處所做的更改將直接影響bigch本身。 該函數獲取數組的實際長度,並將空間 放在bigch的每個元素中以將數字清零。我們需要清除 任何以前的數字bigch。下面你會明白爲什麼我需要這個。 'xar'是主函數的x。它在這裏告訴我們更清潔的 填充bigar元素的真實長度。 { 對(INT I = 0;我 }
}
INT主() { < -------------------//在這裏添加文件打開和讀取命令 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(數組到浮點)命令將 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;在這裏,一切都很清楚。 } }
來源
2010-12-01 06:01:52
ili
這是作業嗎? – 2010-12-01 01:14:52