Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1330893
  • 博文数量: 115
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 5785
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-28 09:13
文章分类
文章存档

2009年(23)

2008年(92)

我的朋友

分类: C/C++

2008-03-06 10:11:08

这个是修正后的版本,解决了-(-(1+2))的问题,这个问题是如果出现这种情况就要吸收一个符号,使之减少,然后按照加减法规则来作。
具体代码如下:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#define N 20

typedef struct LNode{
  int start;
  int end;
  int distance;
  int pre;
  struct LNode *next;
}*List, LNode;


void input(int a[])
{
  printf("Please input :\n");
  int c, i;
  i = 0;
  while((c = getchar()) != '\n')
  {
    if(!isspace(c))
    {
      a[i++] = c;
    }
  }
  a[i] = '\0';
}

void getrid(int a[])
{
  int i, j;
  i = 0;
  while(a[i] != '\0')
  {
    if(a[i] == '#')
    {
      // printf("a[i] = %c \n", a[i]);

      for(j = i; a[j] != '\0'; j++)
      {
        a[j] = a[j + 1];
      }
      a[j - 1] = a[j];
    }
    else
      i++;
  }
  i = 0;
  if(a[i] == '+')
  {
    while(a[i] != '\0')
    {
      a[i] = a[i + 1];
      i++;
    }
  }
}

void output(int a[])
{
  int i = 0;
  while(a[i] != '\0')
  {
    // if(a[i] == '#')

    // i++;

    // else

    getrid(a);
    printf("%c", a[i++]);
  }
}

void Link_insert(List list, List node)
{
  List p = list;
  if(p->next == NULL)
    p->next = node;
  else{
    while(p->next->distance < node->distance)
    {
      p = p->next;
    }
    node->next = p->next;
    p->next = node;
  }
  // printf("start %d end %d", node->start, node->end);


}
void getList(List list, int a[])
{
  List p = list;
  int i = 0;
  int count = 0;
  while(a[i] != '\0')
  {
    List node;
    if(a[i] == '(')
    {
      node = (List)malloc(sizeof(LNode));
      node->start = i;
      int j = i;
      while(a[j] != '\0')
      {
        if(a[j] == '(')
        {
          count++;
        }
        else if(a[j] == ')')
        {
          count--;
        }
        if(count == 0)
        {
          node->end = j;
          node->distance = j - i;
          node->next = NULL;
          node->pre = i - 1;
          Link_insert(list, node);
          break;
        }
        else
          j++;
      }
    }
    i++;
  }
}

void outList(List list)
{
  List p = list;
  while(p->next != NULL)
  {
    p = p->next;
    // printf("\nstart %d end %d pre %d\n", p->start, p->end, p->pre);

  }
}


void change(List list, int a[])
{
  int pos;
  List p = list;
  List q = p->next;
  for(q; q != NULL; q = q->next)
  {
    if(a[q->pre] == '-')
    {
     int i;
     for(i = q->start; i < q->end; i++)
     {
        if(a[i] == '+')
         a[i] = '-';
        else if(a[i] == '-')
         a[i] = '+';
     }
    }
    a[q->start] = '#';
    pos = q->start + 1;
    for(; a[pos] == '#'; pos++);
    if(a[pos] == '+' || a[pos] == '-')
    {
      a[q->pre] = '#';
    }
    a[q->end] = '#';
  }
}

int main()
{
  int a[N];
  List list = (List)malloc(sizeof(LNode));
  list->next = NULL;
  input(a);
  output(a);
  getList(list, a);
  outList(list);
  change(list, a);
  printf("\n");
  output(a);
  return 0;
}


下面给出了一下测试的结果:
Please input :
1-(-2-(-(1+2)))
1+2-1-2
Please input :
1-(+2-(-(1+2)))
1-2-1-2
Please input :
1-(2-1)
1-2+1
Please input :
2-(2-(2-2))
2-2+2-2
Please input :
-(2-(2-2))
-2+2-2
Please input :
+(2-(-2-1))
2+2+1
Please input :
+(2-(+2-(-2-2)))
2-2-2-2

阅读(1215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~