C#:C高精度算法移植
来源:互联网 发布:淘宝仿真金属狙击枪 编辑:程序博客网 时间:2024/06/06 01:14
** 由于参加系里比赛的需要,把C语言的算法移植到C#上了,语法基本一样,没用多久
高精度算法类:HPA(High Precision Algorithm) **
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication21{ internal class HPA { public string OP1 { get; set; } public string OP2 { get; set; } public HPA(string OP1, string OP2) { this.OP1 = OP1; this.OP2 = OP2; } public List<int> Add() { int MAX, i; List<int> a = new List<int>();//动态数组定义 List<int> b = new List<int>(); a.Add(OP1.Length);//第0位储存位数 b.Add(OP2.Length); if (a[0] >= b[0]) { MAX = a[0]; } else { MAX = b[0]; } for (i = 0; i < MAX+1; i++)//提前分配空间 { a.Add(0); b.Add(0); } for (i = 1; i <= a[0]; i++)//倒序存储 { a[i] = OP1[a[0] - i] - '0'; } for (i = 1; i <= b[0]; i++) { b[i] = OP2[b[0] - i] - '0'; } for (i = 1; i <= MAX; i++)//竖式加法 { a[i + 1] += (a[i] + b[i]) / 10;//进位 a[i] = (a[i] + b[i]) % 10; } if (a[i] > 0 && MAX == a[0])//最高位进位处理 a[0] += 1; if (a[i] > 0 && MAX == b[0]) a[0] = b[0] + 1; return a; } public List<int> Sub() { int MAX, i, f = 0; List<int> a = new List<int>();//动态数组定义 List<int> b = new List<int>(); a.Add(OP1.Length);//第0位储存位数 b.Add(OP2.Length); if (a[0] >= b[0]) { MAX = a[0]; } else { MAX = b[0]; } for (i = 0; i < MAX + 1; i++)//提前分配空间 { a.Add(0); b.Add(0); } for (i = 1; i <= a[0]; i++)//倒序存储 { a[i] = OP1[a[0] - i] - '0'; } for (i = 1; i <= b[0]; i++) { b[i] = OP2[b[0] - i] - '0'; } /* 操作数比较开始 */ if (a[0] > b[0])//依据位数比较大小 f = 1; else { if (a[0] < b[0])//依据位数比较大小 f = 0; else { for (i = a[0]; i > 0; i--)//从高位开始比较大小 { if (a[i] > b[i]) f = 1; if (a[i] < b[i]) f = -1; } } } /* 操作数比较结束 */ /* 计算开始 */ if (f == 1)//竖式减法 { for (i = 1; i <= MAX; i++) { if (a[i] < b[i])//借位 { a[i + 1] -= 1; a[i] = 10 - b[i] + a[i]; } else { a[i] = a[i] - b[i]; } } return a; } else { for (i = 1; i <= MAX; i++) { if (b[i] < a[i])//借位 { b[i + 1] -= 1; b[i] = 10 - a[i] + b[i]; } else { b[i] = b[i] - a[i]; } b[0] = -b[0];//传递负数结果 } return b; } /* 计算结束 */ } public List<int> Mul() { int MAX, i, j; List<int> a = new List<int>();//动态数组定义 List<int> b = new List<int>(); List<int> c = new List<int>(); a.Add(OP1.Length);//第0位储存位数 b.Add(OP2.Length); if (a[0] >= b[0]) { MAX = a[0]; } else { MAX = b[0]; } for (i = 0; i < MAX + 1; i++)//提前分配空间 { a.Add(0); b.Add(0); } for (i = 0; i <= a[0] + b[0]; i++) { c.Add(0); } for (i = 1; i <= a[0]; i++)//倒序存储 { if (OP1[a[0] - i] == '-') break; a[i] = OP1[a[0] - i] - '0'; } for (i = 1; i <= b[0]; i++) { if (OP2[b[0] - i] == '-') break; b[i] = OP2[b[0] - i] - '0'; } for (i = 1; i <= a[0]; i++)//无进位竖式乘法 { for (j = 1; j <= b[0]; j++) c[i + j-1] += a[i] * b[j]; } for (i = 1; i < a[0] + b[0]; i++)//进位处理 { c[i + 1] += c[i] / 10; c[i] %= 10; } while (c[i]==0 && i >= 0)//输出位数计算 i--; c[0] = i; if (OP1[0] == '-' ) { if (OP2[0] == '-') ; else c[0] = -c[0]; } else { if (OP2[0] == '-') c[0] = -c[0]; else ; } return c; } }}
界面部分
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication21{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int i; if (cz1.Text == "" | cz2.Text == "") { MessageBox.Show("您还有操作数没有输入"); } else { List<int> res = new List<int>(); HPA H = new HPA(cz1.Text, cz2.Text); res = H.Add(); rult.Text = null; for (i = res[0]; i > 0; i--) { rult.Text = rult.Text + res[i].ToString(); } } } private void button2_Click(object sender, EventArgs e) { int i; if (cz1.Text == "" | cz2.Text == "") { MessageBox.Show("您还有操作数没有输入"); } else { List<int> res = new List<int>(); HPA H = new HPA(cz1.Text, cz2.Text); res = H.Sub(); rult.Text = null; if (res[0] < 0)//如果结果为负数 { rult.Text = "-"; res[0] = -res[0]; } for (i = res[0]; i > 0; i--) { rult.Text = rult.Text + res[i].ToString(); } } } private void button3_Click(object sender, EventArgs e) { int i; if (cz1.Text == "" | cz2.Text == "") { MessageBox.Show("您还有操作数没有输入"); } else { List<int> res = new List<int>(); HPA H = new HPA(cz1.Text, cz2.Text); res = H.Mul(); rult.Text = null; if (res[0] < 0)//如果结果为负数 { rult.Text = "-"; res[0] = -res[0]; } for (i = res[0]; i > 0; i--) { rult.Text = rult.Text + res[i].ToString(); } } } private void button4_Click(object sender, EventArgs e) { MessageBox.Show("高精度加法:仅支持无符号十进制整数运算\n高精度减法:仅支持无符号十进制整数运算\n高精度乘法:仅支持带符号十进制整数运算\n数学表达式求解:不支持高精度运算"); } private void button5_Click(object sender, EventArgs e) { MessageBox.Show("这个还没有做......"); } }}
阅读全文
0 0
- C#:C高精度算法移植
- C语言 高精度算法
- 【高精度】高精度分数[c++]
- 高精度数取余(C\C++)
- 高精度算法数据结构及常用函数实现(C++)
- C/C++算法实例(五)--高精度计算
- C 语言高精度算法(一)——加法
- C 语言高精度算法(二)——乘法
- 小白C语言写高精度算法(加法)
- C语言无符号整数高精度算法试写
- 高精度运算(c程序)
- 高精度计算(C)
- 高精度运算(c程序)
- 高精度运算(c程序)
- 【C++】高精度加法
- 【C++】高精度乘法
- 【C++】高精度除法
- C语言 高精度加法
- POJ 刷题系列:1503 Integer Inquiry
- 测试
- Flatten Binary Tree to Linked List:二叉树变更结构
- 图像增强—空域滤波,椒盐噪声/高斯噪声 均值滤波器/中值滤波器
- SGD BGD MBGD各种梯度下降的处理方式
- C#:C高精度算法移植
- python抓取博客正文
- SpringMVC日期转换之JsonSerialize
- 【Linux】在Linux上安装VNC
- tornado + django + nginx + mysql网站源码分享
- 时域分析的matlab实现
- 编译Hadoop 2.9.0 提示 Could not find artifact com.amazonaws:DynamoDBLocal
- Java NIO 通道
- <你不知道的Javascript>笔记