#include <iostream>
using namespace std
int main ()
{long int num
int indiv,ten,hundred,thousand,ten_thousand,place
/*分别代表个位,十位,百位,千位,万位和位数*/
cout<<"enter an integer(0~99999):"
cin>>num
if (num>9999)
place=5
else if (num>999)
place=4
else if (num>99)
place=3
else if (num>9)
place=2
else place=1
cout<<"place="<<place<<endl
//计算各位数字
ten_thousand=num/10000
thousand=(int)(num-ten_thousand*10000)/1000
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10)
cout<<"original order:"
switch(place)
{case 5:cout<<ten_thousand<<","<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl
cout<<"reverse order:"
cout<<indiv<<ten<<hundred<<thousand<<ten_thousand<<endl
break
case 4:cout<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl
cout<<"reverse order:"
cout<<indiv<<ten<<hundred<<thousand<<endl
break
case 3:cout<<hundred<<","<<ten<<","<<indiv<<endl
cout<<"reverse order:"
cout<<indiv<<ten<<hundred<<endl
break
case 2:cout<<ten<<","<<indiv<<endl
cout<<"reverse order:"
cout<<indiv<<ten<<endl
break
case 1:cout<<indiv<<endl
cout<<"reverse order:"
cout<<indiv<<endl
break
}
return 0
}非常感谢您的耐心观看,如有帮助请采纳,祝生活愉快!谢谢!
应用的 *** 作和原理
目标Android应用的 *** 作过程是这样的:选择你的性别,然后输入你的身高,点查看计算结果的按钮就在Toast中显示你的标准体重。力求 *** 作简单,结果显示清楚。
标准体重的计算公式:
男性:(身高cm-80)×70%=标准体重
女性:(身高cm-70)×60%=标准体重
应用的源码
BMIActivity.java:
package com.lingdududu.bmiimport java.text.DecimalFormat
import java.text.NumberFormat
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.EditText
import android.widget.RadioButton
import android.widget.Toast
/*
* @author lingdududu * 该程序的功能是用户选择自己的性别和输入自己的身高,然后点击按钮,就能在Toast显示出自己的标准体重
*/
public class BMIActivity extends Activity {
/** Called when the activity is first created. */
private Button countButton
private EditText heighText
private RadioButton maleBtn, femaleBtn
String sex = ""
double height
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
//调用创建视图的函数
creadView()
//调用性别选择的函数
sexChoose()
//调用Button注册监听器的函数
setListener()
}
//响应Button事件的函数
private void setListener() {
countButton.setOnClickListener(countListner)
}
private OnClickListener countListner = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(BMIActivity.this, "你是一位"+sexChoose()+"\n"
+"你的身高为"+Double.parseDouble(heighText.getText().toString())+"cm"
+"\n你的标准体重为"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)
.show()
}
}
//性别选择的函数
private String sexChoose(){
if (maleBtn.isChecked()) {
sex = "男性"
}
else if(femaleBtn.isChecked()){
sex = "女性"
}
return sex
}
//创建视图的函数
public void creadView(){
//txt=(TextView)findViewById(R.id.txt)
countButton=(Button)findViewById(R.id.btn)
heighText=(EditText)findViewById(R.id.etx)
maleBtn=(RadioButton)findViewById(R.id.male)
femaleBtn=(RadioButton)findViewById(R.id.female)
//txt.setBackgroundResource(R.drawable.bg)
}
//标准体重格式化输出的函数
private String format(double num) {
NumberFormat formatter = new DecimalFormat("0.00")
String str = formatter.format(num)
return str
}
//得到标准体重的函数
private String getWeight(String sex, double height) {
height = Double.parseDouble(heighText.getText().toString())
String weight = ""
if (sex.equals("男性")) {
weight =format((height - 80) * 0.7)
}
else {
weight = format((height - 70) * 0.6)
}
return weight
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)