题目要求是两个任意精度的大数相加,但是我现在遇到的问题是该怎么在不大范围修改代码的情况下,保证小数点的位置,我现在编译出来的没有小数点
#include <iostream>
using namespace std;
class Node {
public:
int data; // 数据域
Node* next; // 指针域
Node() {
next = nullptr;
}
Node(int data) {
this->data = data;
next = nullptr;
}
Node(const Node& temp) {
this->data = temp.data;
next = nullptr;
}
};
class Link {
public:
Node* head;
int length;
Link() {
head = new Node();
length = 0;
}
~Link() {
while (head != nullptr) {
Node* p = head->next;
delete head;
head = p;
}
}
void insert(const Node& cache) {
Node* temp = new Node(cache);
temp->next = head->next;
head->next = temp;
length++;
}
};
void Creatlist(Link& link)
{
char cache;
bool decimal = false;
while (1) {
cache = getchar();
if (cache == '.') {
decimal = true;
}
else if (cache == ',') {
continue;
}
else if (cache == '\n') {
break;
}
else {
if (decimal) {
link.insert(Node(cache - '0'));
}
else {
link.insert(Node(cache - '0'));
}
}
}
}
void Add(Link& link_one, Link& link_two, Link& link_three) {
int temp = 0;
int i, m, n;
n = link_one.length;
m = link_two.length;
Node* p = link_one.head->next;
Node* q = link_two.head->next;
for (i = 0; i < n && i < m; i++) {
int data = p->data + q->data + temp;
temp = data / 10;
link_three.insert(data % 10);
p = p->next;
q = q->next;
}
for (; i < n; i++) {
int data = p->data + temp;
temp = data / 10;
link_three.insert(data % 10);
p = p->next;
}
for (; i < m; i++) {
int data = q->data + temp;
temp = data / 10;
link_three.insert(data % 10);
q = q->next;
}
if (temp != 0) {
link_three.insert(1);
}
}
void Display(const Link& link) {
Node* temp = link.head->next;
int count = 0;
while (temp != nullptr) {
if (count > 2) {
cout << ",";
}
if (count == 3) {
count = 0;
}
cout << temp->data;
temp = temp->next;
count++;
}
}
int main() {
Link link_one, link_two, link_three;
cout << "大数加法" << endl;
cout << "请输入第一个数:" << endl;
Creatlist(link_one);
cout << "请输入第二个数:" << endl;
Creatlist(link_two);
Add(link_one, link_two, link_three);
Display(link_three);
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:void f (int * p)//这里上面说过了,变量名是i,但是类型是int *,可以保存其他变量的地址。
{
*p=100;//上面也说过,*p就是i,i就是*p
}
int main()
{
int i=9;//这是一个普通的定义和赋值
f(&i);//这里要是调用函数,用&i,把i的地址传递给函数的参数p
cout<<"i"<<i;//结果应该是100
return 0;
}
对于需要计算两个任意精度的大数相加并保留小数点位置的问题,可以通过以下步骤进行解决:
首先需要对两个大数进行解析,将其分离为整数部分和小数部分,并分别存储在不同的数组中。 以字符串形式读入数据后,可以遍历字符串,将其分为整数和小数两个部分,分别存储在数组中。
解析出整数和小数后,需要对它们进行对齐,保持小数点位置不变。 可以先求出两个数的小数位数 $n_1$ 和 $n_2$ ,将小数部分分别左侧补齐 $n_2$ 和 $n_1$ 个 0 ,使得小数点位置一致。 例如,对于数值 $12.345$ 和 $678.9$ ,小数点位置一致后为 $012.345$ 和 $678.900$。
对齐完成后,可以对整数和小数部分进行相加。 从小数部分最右侧往左依次相加,若相加和超过 $10$ ,则需要将进位保存下来,加到下一位相加结果中。 对于整数部分同理,从最右侧往左依次相加,若相加和超过 $10$ ,也需要进位,并加到下一位相加结果中。
相加完成后,还需要对结果进行处理,保证小数点位置正确。 可以从右侧开始,查找第一个不是 0 的位置,将其前面加上小数点。
参考代码如下:
#include <iostream>
#include <string>
using namespace std;
const int PRECISION = 10; // 精度为10^PRECISION
string add(string num1, string num2) {
// 求出小数位数
int n1 = num1.find('.') == string::npos ? 0 : (int)num1.length() - num1.find('.') - 1;
int n2 = num2.find('.') == string::npos ? 0 : (int)num2.length() - num2.find('.') - 1;
// 将小数部分左侧补齐0,使小数点位置一致
string r1 = num1.find('.') == string::npos ? num1 + string(n2, '0') : num1;
string r2 = num2.find('.') == string::npos ? num2 + string(n1, '0') : num2;
// 将小数转为整数进行相加
string res = "";
int carry = 0;
for (int i = r1.size() - 1; i >= 0; i--) {
if (r1[i] == '.') {
res += ".";
continue;
}
int sum = (r1[i] - '0') + (r2[i] - '0') + carry;
carry = sum / 10;
sum %= 10;
res = (char)(sum + '0') + res;
}
if (carry > 0) {
res = (char)(carry + '0') + res;
}
// 处理小数点
int i = res.size() - PRECISION;
if (i > 0) {
res = res.substr(0, i) + "." + res.substr(i);
}
else {
while (i++ < 0) {
res = "0" + res;
}
res = "0." + string(-i, '0') + res;
}
// 去除多余的0和小数点
while (res.back() == '0') {
res.pop_back();
}
if (res.back() == '.') {
res.pop_back();
}
return res;
}
int main() {
string num1 = "123.45";
string num2 = "678.9";
cout << "num1 + num2 = " << add(num1, num2) << endl;
return 0;
}
任意长的数,我的理解就是Double都不能用,只能用数组来接收。之后进行字符串操作,加减其实很简单的。
对于你上面的代码看起来跟我理解的题目没有一毛钱关系。