解题思路:先创建头结点,再根据输入的n来创建for循环来建立链表,同时输入复数的两个值
注意事项:只要熟悉链表指向一些基础就行
参考代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct str
{
int a; //实数
int b; //虚数
struct str *next;
}str;
int main()
{
int n,sum1=0,sum2=0; //sum1为实数和,sum2为虚数和
str *head,*pm;
scanf("%d",&n);
head=(str*)malloc(sizeof(str)); //创建头结点
if(head==NULL) //如果创建失败,则结束当前程序
exit(0);
str *t=head; //将头结点付给t,方便循环创建链表
for(int i=0;i<n;i++)
{
pm=(str*)malloc(sizeof(str)); //创建一个结点
if(pm==NULL) //与上面解释相同
exit(0);
scanf("%d%d",&pm->a,&pm->b); //如题输入a,b
pm->next=NULL; //新结点加到链表末尾
t->next=pm; //与结点建立联系
t=pm;
pm=pm->next; //循环创建链表重要步骤
}
str *t1=head->next;
for(int j=0;j<n;j++)
{
sum1+=t1->a;
sum2+=t1->b;
t1=t1->next;
}
printf("%d+%di\n",sum1,sum2);
return 0;
}
0.0分
1 人评分
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:701 |
C语言训练-立方和不等式 (C语言代码)浏览:751 |
数组输出 (C语言代码)--此题的题目描述有问题浏览:1818 |
成绩转换 (C语言代码)浏览:1012 |
简单的a+b (C语言代码)浏览:344 |
简单的a+b (C语言代码)浏览:636 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:800 |
模拟计算器 (C++代码)浏览:814 |
Tom数 (C语言代码)浏览:725 |
蛇行矩阵 (C语言代码)浏览:508 |