平时各种小段代码的收集(二)

1.计算结果用链表存储的2000!的阶乘算法

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct _numNode{
    char num[100];
    struct _numNode* prior;
    struct _numNode* next;
}numNode,*numNodeptr;

typedef struct 
{
    bool fist_output;
    numNodeptr next;
}headNode;

int main()
{
    headNode Head={false,NULL};
    numNodeptr p=(numNodeptr)malloc(sizeof(numNode));
    memset(p->num,0,100);
    p->prior=NULL;
    p->next=NULL;
    Head.next=p;
    int temp=0;
    int c=0;//最开始作为进位申请内存标志 
    p->num[0]=1;
    for(int i=2;i<=2000;i++) //计算阶乘 
    {
        p=Head.next;
        for(;p->next;p=p->next); //从尾节点开始 
        do
        {
            if(p->prior)
                p=p->prior;
            for(int j=0;j<100;j++)
            {
                temp=(p->num[j])*i+temp;
                p->num[j]=temp%10;
                temp/=10;
                if(j==99&&temp&&!(p->prior))
                    c=1;
            }
            if(c)
            {
                numNodeptr q=(numNodeptr)malloc(sizeof(numNode));
                memset(q->num,0,100);
                q->next=p;
                q->prior=NULL;
                p->prior=q;
                Head.next=q;
                p=q;//指向新插入的节点
                c=0; 
            }
            
            /*
            if(p->prior){
                p=p->prior;

                continue; 
            }
            if(temp)
                continue;
            else
                break; 
        }while(1);*/
        
        }while((p->prior)||temp);
    }
    int count=0;
    for(p=Head.next;p;p=p->next)
    {
        if(!Head.fist_output){
            for(c=99;!(p->num[c])&&c>=0;c--);
            count+=c+1;
            for(int i=c;i>=0;i--)
                printf("%d",p->num[i]);
            Head.fist_output=true;
        }
        else{
            for(int i=99;i>=0;i--)
                printf("%d",p->num[i]);
            count+=100;
        }
    } 
    printf("\ncount=%d\n",count);
}

  

2.字符串转整数,溢出时用边界值代替

//copyright@njnu_mjn 2013   
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int my_isspace(int x)    
{    
    if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r')    
        return 1;    
    else     
        return 0;    
}    
  
int my_isdigit(int x)    
{    
    if(x<='9'&&x>='0')             
        return 1;     
    else     
        return 0;    
} 

int StrToDecInt(const char* str)    
{    
    static const int MAX = (int)((unsigned)~0 >> 1);    
    static const int MIN = -(int)((unsigned)~0 >> 1) - 1;    
    static const int MAX_DIV = (int)((unsigned)~0 >> 1) / 10;    
    static const int MIN_DIV = (int)((((unsigned)~0 >> 1) + 1) / 10);    
    static const int MAX_R = (int)((unsigned)~0 >> 1) % 10;    
    static const int MIN_R = (int)((((unsigned)~0 >> 1) + 1) % 10);    
    int n = 0;    
    int sign = 1;    
    int c;    
    
    while (my_isspace(*str))    
        ++str;    
    if (*str == '+' || *str == '-')    
    {    
        if (*str == '-')    
            sign = -1;    
        ++str;    
    }    
    while (my_isdigit(*str))    
    {    
        c = *str - '0';    
        if (sign > 0 && (n > MAX_DIV || (n == MAX_DIV && c >= MAX_R)))    
        {    
            n = MAX;    
            break;    
        }    
        else if (sign < 0 && (n > MIN_DIV || (n == MIN_DIV && c >= MIN_R)))    
        {    
            n = MIN;    
            break;    
        }    
        n = n * 10 + c;    
        ++str;    
    }    
    return sign > 0 ? n : -n;    
}    


void main()
{
    char *teststr=new char[11];
    cout<<"请输入需要测试的字符串,数值溢出时将以边界值代替!"<<endl;
    cin>>teststr;
    cout<<"刚才输入的字符串为:"<<teststr<<endl;
    cout<<"输入字符串的长度为:"<<strlen(teststr)<<endl;
    cout<<"装换后的结果为:    "<<StrToDecInt(teststr)<<endl;
}

 

 

      未完!待续········

posted @ 2013-10-08 09:47  登高行远  阅读(319)  评论(0编辑  收藏  举报