标签:names getc lan bre lse 视频教程 面向对象 任务 问题:
学习Java黑马系列Java视频教程31-60集。
完成面向对象程序设计小学期任务9:整数链表
代码如下:
#include <stdio.h>
#include<iostream>
using namespace std;
class Num
{
public:
int data;
Num* next;
Num()
{
next = nullptr;
}
Num(int data)
{
this->data = data;
}
Num(const Num& temp)
{
this->data = temp.data;
}
};
class Number
{
public:
Num* head;
int length = 0;
Number()
{
head = new Num();
}
~Number()
{
while (head != nullptr)
{
Num* p = head->next;
free(head);
head = p;
}
}
void insert(const Num& cache)
{
Num* temp = new Num(cache);
temp->next = head->next;
head->next = temp;
length++;
}
};
void Creatlist(Number& Number)
{
char cache;
while (1)
{
cache = getchar();
if (cache == ‘\n‘)
break;
Number.insert(Num(cache - ‘0‘));
}
}
void Add(Number& Number_one, Number& Number_two, Number& Number_three)
{
int temp = 0, i, m, n;
n = Number_one.length;
m = Number_two.length;
Num* p = Number_one.head->next;
Num* q = Number_two.head->next;
for (i = 0; i < n && i < m; i++)
{
int data = p->data + q->data + temp;
temp = data / 10;
Number_three.insert(data % 10);
p = p->next;
q = q->next;
}
for (; i < n; i++)
{
int data = p->data + temp;
temp = data / 10;
Number_three.insert(data % 10);
p = p->next;
}
for (; i < m; i++)
{
int data = q->data + temp;
temp = data / 10;
Number_three.insert(data % 10);
q = q->next;
}
if (temp != 0)
Number_three.insert(1);
}
void Sub(Number& Number_one, Number& Number_two, Number& Number_three)
{
int temp = 0, i, m, n;
n = Number_one.length;
m = Number_two.length;
Num* p = Number_one.head->next;
Num* q = Number_two.head->next;
for (i = 0; i < m; i++)
{
int cache;
int z = p->data;
int y = q->data;
cache = (p->data) - (q->data);
if (cache < 0)
{
p->next->data -= 1;
cache += 10;
}
Number_three.insert(cache);
p = p->next;
q = q->next;
}
for (; i < n; i++)
{
int cache = p->data;
if (cache < 0)
{
p->next->data--;
cache += 10;
}
if (p->next == nullptr && p->data == 0)
break;
Number_three.insert(cache);
p = p->next;
}
}
void Display(const Number& Number)//输出
{
Num* temp = Number.head->next;
while (temp != nullptr)
{
cout << temp->data;
temp = temp->next;
}
cout << endl;
}
int main()
{
while (true) {
cout << "1 两个大整数求和\n2 两个大整数求差\n3 储存并显示一个大整数\n0或其它任意数字 退出" << endl;
cout << "请选择:";
int a;
cin >> a;
char c = getchar();
if (a == 1) {
Number Number_one, Number_two, Number_three;
cout << "请输入第一个整数:" << endl;
Creatlist(Number_one);
cout << "请输入第二个整数:" << endl;
Creatlist(Number_two);
Add(Number_one, Number_two, Number_three);
cout << "结果为:" << endl;
Display(Number_three);
cout << endl;
}
else if (a == 2) {
Number Number_one, Number_two, Number_three;
cout << "请输入第一个整数:" << endl;
Creatlist(Number_one);
cout << "请输入第二个整数:" << endl;
Creatlist(Number_two);
Sub(Number_one, Number_two, Number_three);
cout << "结果为:" << endl;
Display(Number_three);
cout << endl;
}
else if (a == 3) {
Number Number_one, Number_two, Number_three;
cout << "请输入一个整数:" << endl;
Creatlist(Number_one);
Number_two.head->data = 0;
Add(Number_one, Number_two, Number_three);
cout << "该数为:" << endl;
Display(Number_three);
cout << endl;
}
else if (a == 0) {
exit(0);
}
else {
cout << "无此选项!" << endl;
cout << endl;
}
}
return 0;
}
标签:names getc lan bre lse 视频教程 面向对象 任务 问题:
原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/13269988.html