C语言题目14

IT小小龙 posted @ 2014年4月06日 16:30 in C with tags 链表 大整数加法 , 607 阅读
//实现输入任意两个若干位的整数,进行加法,计算并返回加和后的结果。
//(注意两个整数可以足够大,以至于double都会溢出。只要系统内存够用,
//此加法都要正常运行)
#include"stdio.h"
#include"stdlib.h"
typedef struct node{
  int x;
  struct node *next;
  struct node *before;
}llnode;

int charToNumber(char c)
{
  return c-48;
}

void getNumber(llnode* tail)
{
  char c;
  llnode *p;
  llnode *q;
  q = tail;
  printf("请输入一个整数, 输入完成后敲击回车:");
  while(1)
    {
      c = getchar();
      if(c == '\n')
	{
	  break;
	}
      else
	{
	  p = (llnode *)malloc(sizeof(llnode));
	  p->x = charToNumber(c);
	  p->before = NULL;
	  if(tail->x == 0)
	    {
	      tail->before = p;
	      p->next = tail;
	      tail->x = tail->x+1;
	    }
	  else
	    {
	      q = tail->before;
	      q->next = p;
	      p->before = q;
	      p->next = tail;
	      tail->before = p;
	      q = p;
	      tail->x = tail->x+1;
	    }
	}
    }
}

void addNumber(llnode* ctail, llnode* atail, llnode* btail)
{
  int i=0;
  int alength = atail->x;
  int blength = btail->x;
  llnode *pshort;
  llnode *plong;
  llnode *ps;
  llnode *pl;
  llnode *p;
  llnode *q;
  int a;
  int b;
  int nextc;
  int thisc;
  int d;
  if(alength>blength)
    {
      pshort = btail;
      plong = atail;
    }
  else
    {
      pshort = atail;
      plong = btail;
    }
  ps = pshort->before;
  pl = plong->before;
  while(i<pshort->x)
    {
      thisc = nextc;
      if(ps->x + pl->x >=10)
	{
	  nextc = 1;
	  d = (ps->x + pl->x)%10;
	}
      else
	{
	  nextc = 0;
	  d = (ps->x + pl->x);
	}
      ps = ps->before;
      pl = pl->before;
      p = (llnode *)malloc(sizeof(llnode));
      p->x = d+thisc; 
      p->before = NULL;
      if(ctail->x == 0)
	{
	  ctail->before = p;
	  p->next = ctail;
	  ctail->x = ctail->x+1; 
	  q = p;
	}
      else
	{
	  p->next = q;
	  q->before = p;
	  q = p;
	  ctail->x = ctail->x+1;
	}
      i = i+1;
    }
  while(i<plong->x)
    {
      if(i == pshort->x)
	{
	  p = (llnode *)malloc(sizeof(llnode));
      	  p->x = (pl->x)+nextc; 
      	  p->before = NULL;
      	}
      else
      	{
	  p = (llnode *)malloc(sizeof(llnode));
      	  p->x = pl->x;
	  p->before = NULL;
      	}
      p->next = q;
      q->before = p;
      q = p;
      ctail->x = ctail->x+1;
      pl = pl->before;
      i = i+1;
    }
}

void showResult(llnode* ctail)
{
  llnode *p;
  p = ctail;
  while(p->before != NULL)
    {
      p = p->before;
    }
  while(p != ctail)
    {
      printf("%d",p->x);
      p = p->next;
    }
  printf("\n");
}

int main()
{
  llnode *atail = (llnode *)malloc(sizeof(llnode));
  llnode *btail = (llnode *)malloc(sizeof(llnode));
  llnode *ctail = (llnode *)malloc(sizeof(llnode));
  llnode *p=atail;
  atail->x = 0;
  btail->x = 0;
  ctail->x = 0;
  atail->next = NULL;
  btail->next = NULL;
  ctail->next = NULL;
  getNumber(atail);
  getNumber(btail);
  addNumber(ctail, atail, btail);
  showResult(ctail);
  return 1;
}
Avatar_small
HDFC Life Insurance 说:
2022年8月07日 12:22

HDFC Life is one of the top insurance providers in India which helps many subscribers with their benefited plans, and customers get easy avenues that make their online payment and other options very easy, where HDFC was established in 2000 and it has been growing in service that it provides to customers. HDFC Life Premium Payment The insurance policy through HDFC Life does carry in every different stream which ensures customers get their desired policy for their purpose, and customers can get more information about the HDFC Life Insurance from the official website of HDFC and here we will let you know how to get the installments for the policy to be paid.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter