BinaryArt 10 發表於 September 29, 2008 檢舉 Share 發表於 September 29, 2008 如題 我剛開始自學C++,用的是dev 想先寫個解二次方程式的程式我寫的如下:問題在他無法接受 (-b+(b^2+4*a*c)^0.5)/2*a 裡的 ^ (次方) 運算 x^y x,y為小數時會顯試錯誤; "^" 運算結果也不正確 (我測試2^3 結果為1)---------------------------------------------------------------------------------------------------------------#include <iostream>#include <math.h>#include <stdlib.h>using namespace std;int main(){ int a; int b; int c; int d; int e; cout << "此為解二次方程式程式!\n" << endl; cout << "請輸入二次項係數:"; cin >> a; cout << "您輸入的二次項係數為:" << a <<"\n"<< endl; cout << "請輸入一次項係數:"; cin >> b; cout << "您輸入的一次項係數為:" << b <<"\n"<< endl; cout << "請輸入常數項:"; cin >> c; cout << "您輸入的常數項為:" << c <<"\n"<< endl; d=(-b+(b^2+4*a*c)^0.5)/2*a; e=(-b-(b^2+4*a*c)^0.5)/2*a; cout << "您輸入的二次方程式之二根分別為:" << d << "," << e << endl; system("pause") ; return 0;}-----------------------------------------------------------------------------------------------------------------------我知道這個遇到非有理數或非實數解時會有問題 我只是想先試試看簡單的麻煩各位高手指點! 是哪裡出錯了呢? 或是有更好的寫法嗎? 鏈接文章 分享到其他網站
TerryW 10 發表於 September 30, 2008 檢舉 Share 發表於 September 30, 2008 '^'在C++中是邏輯運算中的XOR而非乘冪若要用乘冪請使用pow()這個函數可參考這裡http://www.cplusplus.com/reference/clibrary/cmath/pow.html 鏈接文章 分享到其他網站
ryan10328 10 發表於 October 26, 2008 檢舉 Share 發表於 October 26, 2008 '^'在C++中是邏輯運算中的XOR而非乘冪若要用乘冪請使用pow()這個函數可參考這裡http://www.cplusplus.com/reference/clibrary/cmath/pow.html=。。=建議先翻翻書再說如果要平方你用 b*b會比較好 也比較方便 鏈接文章 分享到其他網站
Recommended Posts
請登入後來留意見
在登入之後,您才能留意見
立即登入