[ Android ] Ứng dụng Giải phương trình bậc 2

Vi dụ: Xây dựng ứng dụng giải phương trình: ax^2+bx+c=0 (trong đó a, b, c được nhập vào qua EditText)


>> Thực hiện: 

- Create Android Project: GiaiPhuonTrinhBac2



- Thiết lập và code:

+ Res\layout\main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Giai PT bac 2"
/>
<TextView
    android:text="He so a:"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">  
</TextView>
<EditText
    android:id="@+id/a"
    android:numeric="integer"
    android:layout_width="200sp"
    android:layout_height="wrap_content"
  android:hint="@string/edit_hint"  
    android:inputType="text|textAutoCorrect|textCapWords|number"
    android:textSize="18sp"
 
    />
<TextView
    android:text="He so b:"
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">  
</TextView>
<EditText
    android:id="@+id/b"
    android:numeric="integer"
    android:layout_width="200sp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_hint"
    android:inputType="text|textAutoCorrect|textCapWords|number"
    android:textSize="18sp"
    />
<TextView
    android:text="He so c:"
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 
</TextView>
<EditText
    android:id="@+id/c"
    android:numeric="integer"
    android:layout_width="200sp"
    android:layout_height="wrap_content"
  android:hint="@string/edit_hint"  
    android:inputType="text|textAutoCorrect|textCapWords|number"
    android:textSize="18sp"
 
    />
<Button
    android:id="@+id/calculate"
    android:text="Giai"
    android:layout_width="wrap_content"
    android:layout_height="83dp"/>
 
<TextView
    android:id="@+id/result"
    android:layout_height="200sp"
    android:layout_weight="0.25"
    android:layout_width="match_parent"/>
</LinearLayout>

+ Res\value\string.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Mời bạn nhập hệ số</string>
    <string name="app_name">Giai phuong trinh bac 2</string>
<string name="edit_hint">Input</string>
</resources>

+ rsc\...

package txt.Begin.GiaiPhuongTrinhBac2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class GiaiPhuongTrinhBac2Activity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.calculate);
final TextView result = (TextView) findViewById(R.id.result);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String sa = ((EditText) findViewById(R.id.a)).getText().toString();
String sb = ((EditText) findViewById(R.id.b)).getText().toString();
String sc = ((EditText) findViewById(R.id.c)).getText().toString();
try
{
double a = Double.parseDouble(sa);
double b = Double.parseDouble(sb);
double c = Double.parseDouble(sc);
if (a == 0)
{
result.setText("Phuong trinh bac I: ");
if (b == 0)
{
if (c == 0)
result.setText(result.getText() + "PT co' vo so nghiem");
else
result.setText(result.getText() + "PT vo nghiem");
}
else
{
result.setText(result.getText() + "x=" + (-c/b));
}
}
else
{
double delta = b*b - (4*a*c);
if (delta < 0.0)
{
result.setText("PT vo nghiem \n");
}
else
if (delta == 0)
{
result.setText("PT co nghiem kep: " + (-b/(2*a)));
}
else
{
double delta_sqrt = Math.sqrt(delta);
result.setText("x1 = " + ((b*b + delta_sqrt)/(2 * a)) +"\n" + "x2 = " + ((b*b - delta_sqrt)/(2 * a)));
}
}
} catch (Exception ex)
{
result.setText(ex.toString());
}
}
    });
}
}
>> Kết quả:


[Xem thêm]

*******

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Giáo trình: Lập Trình Android [Click để xem]

# Khoá học online:  Lập trình Android toàn tập [Click để xem]

-----------------------------------------

Tìm kiếm nội dung khác: