计算器的新功能

当你学一些可视化程序设计语言时,老师经常会让你设计并且编程做出一个计算器,这时也许你会仿照windows系统自带的计算器外观和功能去设计,但是现在老师要你多做出一个有新功能的计算器,实现当输入一个数时,能够将这个数分解成一个或多个素因子乘积的形式,并按素因子的大小排列显示出来。大家对计算器中数的表示应该很清楚的。下面显示出了0 — 9这十个数字的表示形式。每个数字都占据5 * 3大小的字符区域


输入

输入有多组测试数据,每组包括一个正整数n(1 < n <= 1000000)。

输出

对于每个数,将它分解成若干个素数乘积的形式,并按从小到大的顺序输出,素因子之间用“ * ”的形式连接。

样例输入

10

2

样例输出

 -     -

  |   |

 -  *  -

|       |

 -     -

 

 -

  |

 -

|

 -


C++源码如下

#include <iostream>
#include <vector>
using namespace std;

int getch(int num, char ch[5][10000], int n)
{
	int tmp = 1;
	while (num / tmp / 10 != 0) {
		tmp *= 10;
	};
	if (n != 0)
	{
		ch[0][n] = ' ';
		ch[1][n] = ' ';
		ch[2][n] = '*';
		ch[3][n] = ' ';
		ch[4][n++] = ' ';
	}
	while (tmp != 0) {
		int t = num / tmp % 10;
		tmp = tmp / 10;
		switch (t) {
		case 0:
			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = '-';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 1:
			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 2:
			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = '-';
			ch[3][n] = ' ';
			ch[4][n++] = '-';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';
			break;
		case 3:
			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = '-';
			ch[3][n] = ' ';
			ch[4][n++] = '-';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 4:
			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = '-';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 5:
			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = '-';
			ch[3][n] = ' ';
			ch[4][n++] = '-';

			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 6:
			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = '-';
			ch[3][n] = ' ';
			ch[4][n++] = '-';

			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 7:
			ch[0][n] = ' ';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 8:
			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = '-';
			ch[3][n] = ' ';
			ch[4][n++] = '-';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		case 9:
			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = ' ';
			ch[4][n++] = ' ';

			ch[0][n] = '-';
			ch[1][n] = ' ';
			ch[2][n] = '-';
			ch[3][n] = ' ';
			ch[4][n++] = '-';

			ch[0][n] = ' ';
			ch[1][n] = '|';
			ch[2][n] = ' ';
			ch[3][n] = '|';
			ch[4][n++] = ' ';
			break;
		}
	}
	return n;
}

void print(vector <int> data)
{
	char ch[5][10000];
	int n = 0;
	for (decltype(data.size())i = 0; i != data.size(); i++)
	{
		int tmp=getch(data[i], ch , n);
		n = tmp;
	}
	ch[0][n] = '\0';
	ch[1][n] = '\0';
	ch[2][n] = '\0';
	ch[3][n] = '\0';
	ch[4][n] = '\0';
	cout << ch[0] << endl;
	cout << ch[1] << endl;
	cout << ch[2] << endl;
	cout << ch[3] << endl;
	cout << ch[4] << endl;
}

int main()
{

	int num;
	while (cin >> num)
	{
		int i = 2;
		vector <int> data;
		while (num != 1)
		{
			if (num%i == 0)
			{
				data.push_back(i);
				num /= i;
			}
			else {
				i++;
			}
		}
		print(data);
	}
	
	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个 Android 计算器需要以下步骤: 1. 创建一个的 Android 项目,并在 layout 文件夹中创建一个布局文件,用于显示计算器的界面。界面可以包括数字按钮、运算符按钮、清除按钮、等于号按钮和文本框等。 2. 在 MainActivity.java 中编写代码,实现计算器的逻辑功能。可以使用 Java 中的数学运算符和方法来实现计算器的加减乘除等功能。 3. 在 MainActivity.java 中编写代码,实现按钮的点击事件。可以使用 OnClickListener 接口来处理按钮的点击事件。 4. 在 AndroidManifest.xml 文件中添加权限,以允许应用程序访问互联网。 以下是一个简单的 Android 计算器示例代码: 1. 布局文件 activity_main.xml: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/result_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/button_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/button_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/button_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/button_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/button_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/button_subtract" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/button_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/button_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/button_multiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="×" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="C" /> <Button android:id="@+id/button_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" /> <Button android:id="@+id/button_equal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" /> <Button android:id="@+id/button_divide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="÷" /> </LinearLayout> </LinearLayout> ``` 2. MainActivity.java 文件: ```java public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView resultTextView; private Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0; private Button buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEqual, buttonClear; private String currentNumber = ""; private String lastNumber = ""; private String lastOperator = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); resultTextView = findViewById(R.id.result_textview); button1 = findViewById(R.id.button_1); button2 = findViewById(R.id.button_2); button3 = findViewById(R.id.button_3); button4 = findViewById(R.id.button_4); button5 = findViewById(R.id.button_5); button6 = findViewById(R.id.button_6); button7 = findViewById(R.id.button_7); button8 = findViewById(R.id.button_8); button9 = findViewById(R.id.button_9); button0 = findViewById(R.id.button_0); buttonAdd = findViewById(R.id.button_add); buttonSubtract = findViewById(R.id.button_subtract); buttonMultiply = findViewById(R.id.button_multiply); buttonDivide = findViewById(R.id.button_divide); buttonEqual = findViewById(R.id.button_equal); buttonClear = findViewById(R.id.button_clear); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); button5.setOnClickListener(this); button6.setOnClickListener(this); button7.setOnClickListener(this); button8.setOnClickListener(this); button9.setOnClickListener(this); button0.setOnClickListener(this); buttonAdd.setOnClickListener(this); buttonSubtract.setOnClickListener(this); buttonMultiply.setOnClickListener(this); buttonDivide.setOnClickListener(this); buttonEqual.setOnClickListener(this); buttonClear.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_0: currentNumber += "0"; break; case R.id.button_1: currentNumber += "1"; break; case R.id.button_2: currentNumber += "2"; break; case R.id.button_3: currentNumber += "3"; break; case R.id.button_4: currentNumber += "4"; break; case R.id.button_5: currentNumber += "5"; break; case R.id.button_6: currentNumber += "6"; break; case R.id.button_7: currentNumber += "7"; break; case R.id.button_8: currentNumber += "8"; break; case R.id.button_9: currentNumber += "9"; break; case R.id.button_add: lastOperator = "+"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_subtract: lastOperator = "-"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_multiply: lastOperator = "*"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_divide: lastOperator = "/"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_equal: double result = calculateResult(); resultTextView.setText(String.valueOf(result)); currentNumber = String.valueOf(result); lastOperator = ""; lastNumber = ""; break; case R.id.button_clear: currentNumber = ""; lastNumber = ""; lastOperator = ""; resultTextView.setText(""); break; } resultTextView.setText(currentNumber); } private double calculateResult() { double result = 0.0; double current = Double.parseDouble(currentNumber); double last = Double.parseDouble(lastNumber); switch (lastOperator) { case "+": result = last + current; break; case "-": result = last - current; break; case "*": result = last * current; break; case "/": result = last / current; break; } return result; } } ``` 在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 这样,就可以实现一个简单的 Android 计算器了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值