科学计数法是科学家

8.科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位,该数字及其指数部分的正负号即使对正数也必定明确给出。现以科学计数法的格式给出实数A,请编写程序按普通数字表示法输出A,并保证所有有效位都被保留。

输入描述:

每个输入包含1个测试用例,即一个以科学计数法表示的实数A。该数字的存储长度不超过9999字节,且其指数的绝对值不超过9999。

输出描述:

对每个测试用例,在一行中按普通数字表示法输出A,并保证所有有效位都被保留,包括末尾的0。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
    char data;
    struct node *next;
    struct node *prev;
}Node;
typedef struct head{
    struct node *first;
    struct node *last;
    int num;
}Head;
int numflage;

Head *creat_head(void);
void add_data_node(Head *list, Node *p);
void treat_list(Head *list, int count);
Node *get_point_node(Head *list,int *n);
void print_list(Head *list);

int main()
{
    char number[10000];
    int count,flage=0;
    Head *list = creat_head();
    while(scanf("%s",number) != EOF)
    {
        int i=0;
        numflage = 0;
        if(number[0] == '+')
        {
            numflage = 1;
        }
        if(number[0] == '-')
        {
            numflage = -1;
        }
        while(number[i] != 'E')
        {
            if((number[i] >= '0'&& number[i] <='9')||number[i] == '.')
            {
                Node *p = (Node *)malloc(sizeof(*p));
                p->data = number[i];
                p->next = NULL;
                p->prev = NULL;
                add_data_node(list,p);
            }
            i++;
            if(number[i] == '\0')
            {
                print_list(list);
                return 0;
            }
        }
        i++;
        if(number[i] == '-')
        {
            flage = -1;
            i++;
        }
        else if(number[i] == '+')
        {
            flage = 1;
            i++;
        }
        else
        {
            print_list(list);
            return 0;
        }
        count = 0;
        while(number[i] != '\0')
        {
            count = count*10 + number[i]-'0';
            i++;
        }
        treat_list(list, flage*count);
    }
    return 0;
}
Head *creat_head(void)
{
    Head *list = (Head *)malloc(sizeof(list));
    list->first = NULL;
    list->last = NULL;
    list->num = 0;

    return list;
}
void add_data_node(Head *list, Node *p)
{
    if(list == NULL||p == NULL)
        return ;
    if(list->first == NULL)
    {
        list->first = p;
        list->last = p;
        list->num++;
    }
    else
    {
        list->last->next = p;
        p->prev = list->last;
        list->last = p;
        list->num++;
    }
}
void treat_list(Head *list, int count)
{
    if(count == 0)
        return ;
    int n;
    Node *point_node = get_point_node(list,&n);
    if(count < 0)
    {
        for(int i = (-1*count) - n+1;i > 0;i--)
        {
            Node *p = (Node *)malloc(sizeof(*p));
            p->data = '0';
            p->next = NULL;
            p->prev = NULL;

            list->first->prev = p;
            p->next = list->first;
            list->first = p;
            //list->num++;
        }
        Node *r = point_node;
        for(int i = (-1*count);i > 0;i--)
        {
            r = r->prev;
        }
        point_node->prev->next = point_node->next;
        point_node->next = point_node->prev;
        r->prev->next = point_node;
        point_node->prev = r->prev;
        point_node->next = r;
        r->prev = point_node;

        print_list(list);
    }
    else if(count >= 0)
    {
        for(int i = 0;i < (n+count+1-list->num);i++)
        {
            Node *p = (Node *)malloc(sizeof(*p));
            p->data = '0';
            p->next = NULL;
            p->prev = NULL;

            list->last->next = p;
            p->prev = list->last;
            list->last = p;
            //list->num++;
        }
        Node *r = point_node;
        for(int i = count;i >= 0;i--)
        {
            r = r->next;
        }
        point_node->prev->next = point_node->next;
        point_node->next = point_node->prev;
        if(n+count-list->num < 0)
        {
            r->prev->next = point_node;
            point_node->prev = r->prev;
            point_node->next = r;
            r->prev = point_node;
        }

        print_list(list);
    }
}
Node *get_point_node(Head *list,int *n)
{
    Node *p = list->first;
    int i=0;
    while(p != NULL)
    {
        if(p->data == '.')
        {
            *n = i;
            return p;
        }
        p = p->next;
        i++;
    }
    Node *r =(Node *)malloc(sizeof(*r));
    r->data = '.';
    r->next=NULL;

    list->last->next = r;
    r->prev = list->last;
    list->last = r;

    return list->last;
}
void print_list(Head *list)
{
    Node *p = list->first;
    if(numflage == -1)
        printf("-");
    while(p!=NULL)
    {
        printf("%c",p->data);
        p = p->next; 
    }
    printf("\n");
    list->first = NULL;
    list->last = NULL;
    list->num = 0;
}
全部评论

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务