把握命运,追逐梦想

对自己所做的事要有兴趣,同时还要能够坚持不懈

统计

留言簿(1)

阅读排行榜

评论排行榜

刚刚上课两天,记录点小代码(求素数的)

#include<stdio.h>
#include
<memory.h>
#include
<math.h>
struct PrimeBlock
{
    PrimeBlock();
    
void addPrime(int );
    
bool isFull();
    
int primes[20];
    
int numOfPrimes;
    
int current;
    PrimeBlock 
*pNext;
}
;
PrimeBlock::PrimeBlock()
{
    memset(primes,
0,20*sizeof(int));
    numOfPrimes 
= 0;
    pNext 
= 0;
    current 
= 0;
}


void PrimeBlock::addPrime(int prime)
{
    
if(!isFull())
    
{
        
this->primes[numOfPrimes] = prime;
        numOfPrimes
++;
    }

}


bool PrimeBlock::isFull()
{
    
if(this->numOfPrimes == 20)
        
return true;
    
else
        
return false;
}





struct LinkedPrime
{
    LinkedPrime();
    
~LinkedPrime();
    
void addPrime(int);
    
int getNextPrime();
    
bool hasNext();
    
int getFirstPrime();
    PrimeBlock
*    pHead;
    PrimeBlock
*  pCurrent;
    PrimeBlock
*    pLast;
    PrimeBlock
*    pEnd;
}
;
LinkedPrime::LinkedPrime()
{
    pHead 
= new PrimeBlock();
    pLast 
= pHead;
    pEnd 
= pHead;
    pCurrent 
= pHead;
}

LinkedPrime::
~LinkedPrime()
{
    PrimeBlock 
*pCurrent = pHead;
    PrimeBlock 
*pTemp = pCurrent;
    
while(pCurrent != 0)
    
{
        pTemp 
= pCurrent->pNext;
        delete pCurrent;
        pCurrent 
= pTemp;
    }

}


bool LinkedPrime::hasNext()
{
    
if(pCurrent->pNext!=0)
    
{
        
return true;
    }

    
else
    
{
        
if(pCurrent->current<pCurrent->numOfPrimes-1)
            
return true;
        
else
            
return false;
    }

}

int LinkedPrime::getFirstPrime()
{
    pCurrent 
= pHead;
    pCurrent
->current = 0;
    
return pCurrent->primes[pCurrent->current];
}


int LinkedPrime::getNextPrime()
{
    
if(this->hasNext())
    
{
        
if(pCurrent->current == 19)
        
{
            
if(pCurrent->pNext!= 0)
            
{
                pCurrent 
= pCurrent->pNext;
                pCurrent
->current= 0;
            }

        }

        
else
        
{
            pCurrent
->current++;
        }

    }

    
int ret = pCurrent->primes[pCurrent->current];
    
    
return ret;
}

void LinkedPrime::addPrime(int prime)
{
    
if(!pLast->isFull())
    
{
        pLast
->addPrime(prime);
    }

    
else
    
{
        pLast
->pNext = new PrimeBlock();
        pEnd 
= pLast->pNext;
        pLast
=  pLast->pNext;
        pLast
->addPrime(prime);
    }

}

int main()
{
    printf(
"请输入判断素数的数字,请输入2以上的数字:");
    
int value = 0;
    scanf(
"%d",&value);

    LinkedPrime primes;
    
    primes.addPrime(
2);
    
int i =primes.getFirstPrime();

    
while(i*i<=value)
    
{
        
if(value%== 0)
        
{
            printf(
"不是素数");
            
break;
        }

        
else
        
{
            i
++;
            
int j = primes.getFirstPrime();
            
while(1)
            
{
                
if(i%j==0)
                
{
                    i
++;
                    j 
= primes.getFirstPrime();
                    
continue;
                }

                
else if(primes.hasNext())
                
{
                    j 
= primes.getNextPrime();
                }

                
else
                
{
                    primes.addPrime(i);
                    
break;
                }

            }

        }

    }

    
if(i*i>value)
    
{
        printf(
"是素数");
    }


    
return 0;
}

posted on 2009-07-28 16:26 把握命运 阅读(310) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理