AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

采用循环双向链表, 能实现多个长整型进行加法运算的源代码

51自学网 2015-08-30 http://www.wanshiok.com

/*

* 文件名: 1_2.c

* 实验环境: Turbo C 2.0

* 完成时间: 2003年2月17日

*--------------------------------------------------------------------

* 改进说明: 采用循环双向链表, 能实现多个长整型进行加法运算.

*/



#include <math.h>

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>



#define TRUE 1

#define FALSE 0

#define OPERAND_NUM 2

#define POSITIVE 1

#define NEGATIVE 0



typedef int ElemType;

typedef int status;

typedef struct NodeType

{

ElemType data;

struct NodeType *prior;

struct NodeType *next;

} NodeType, *LinkType;



status CreateOpHeadBuff(LinkType **, int);

status CreateOperandList(LinkType *, int);

void CreateResultList(LinkType *, LinkType *, int);

status DeleteZero(LinkType *);

status PushDataToList(LinkType *, int, int);

status AppendNodeToList(LinkType *, LinkType);

LinkType ComputePNList(LinkType, LinkType, int);

LinkType ComputePPNNList(LinkType, LinkType, int);

status MakeNode(LinkType *, ElemType);

status PrintList(LinkType);

status ClearMemory(LinkType *, int);

status DeleteList(LinkType);

status ErrorProcess(char[], int);



int main(void)

{

int iCounter,

iOpNum = 2;/* 操作数的个数, 默认为2 */

char strNum[10], cOrder[5];

LinkType ResultList = NULL, /* 结果链表的头指针 */

*ListHeadBuff = NULL; /* 指向操作数头指针缓冲 */



do

{

printf("请输入需要的操作数的个数, 注意至少为2: ");

gets(strNum);

iOpNum = atoi(strNum);

} while (iOpNum < 2);

/* 构造操作数链表的头指针缓冲区 */

CreateOpHeadBuff(&ListHeadBuff, iOpNum);

/* 提示用户输入数据,并构造操作数链表 */

while (!CreateOperandList(ListHeadBuff, iOpNum))

{

printf("/n出现非法输入, 需要退出吗?/n");

printf("键入Y则退出, 键入N重新输入(Y/N):");

gets(cOrder);

if (cOrder[0] == 'Y' || cOrder[0] == 'y')

{

ClearMemory(ListHeadBuff, iOpNum);

return 0;

}

}

printf("打印输入情况:/n");

for (iCounter = 0; iCounter < iOpNum; iCounter++)

{

printf("- - - 第%d个操作数 - - -/n", iCounter + 1);

DeleteZero(ListHeadBuff + iCounter);

PrintList(*(ListHeadBuff + iCounter));

}



/* 相加所有操作数链表的结果,并存放在ResultList中*/

CreateResultList(&ResultList, ListHeadBuff, iOpNum);

printf("打印结果:/n");

PrintList(ResultList);



ClearMemory(ListHeadBuff, iOpNum);

DeleteList(ResultList);

printf("运算完毕!");

getch();



return 0;

}



status CreateOpHeadBuff(LinkType **pBuff, int size)

{

int iCounter;



*pBuff = (LinkType *)malloc(sizeof(LinkType) * size);

if (!*pBuff)

{

printf("Error, the memory is overflow!/n");

return FALSE;

}

for (iCounter = 0; iCounter < size; iCounter++)

*(*pBuff + iCounter) = NULL;



return TRUE;

}

<

 

 

 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
上一篇:常见Datagrid&nbsp;错误  下一篇:用RealPlayer控件制作的播放器