大师 • 5个月前
using namespace std;
struct Com {
int a;
int b;
Com *next;
};
Com create(int a, int b); void add(Com &head, int a, int b); Com sum(Com *head);
int main() {
Com *head = nullptr;
int a, b;
for (int i = 0; i < 10; ++i)
{
cin >> a >> b;
add(head, a, b);
}
Com result = sum(head);
cout << result.a << "+" << result.b << "i" << endl;
return 0;
}
Com *create(int a, int b) {
Com *c = new Com;
c->a = a;
c->b = b;
c->next = nullptr;
return c;
}
void add(Com *&head, int a, int b) {
if (head == nullptr)
{
head = create(a, b);
}
else
{
Com *current = head;
while (current->next != nullptr)
{
current = current->next;
}
current->next = create(a, b);
}
}
Com sum(Com *head) {
Com sum = {0, 0, nullptr};
Com *current = head;
while (current != nullptr)
{
sum.a += current->a;
sum.b += current->b;
current = current->next;
}
return sum;
}
评论:
请先登录,才能进行评论