※ 引述《Godfrey0216 (Godfrey)》之銘言:
: 哈囉大家好唷,我想問一下大家,如果我現在手上有一筆存在矩陣中
: 的資料.我想用利用鏈結串列讓資料印出來,分別是正向跟反向
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <cmath>
using namespace std ;
int isPrime(int p)
{
int i;
for (i=2;i<=sqrt((double)p);i++)
{
if(p%i==0)
return 0 ;
}
return 1 ;
}
struct friendData {
int ans[1024];
};
struct linkedListNode {
struct friendData data;
struct linkedListNode *nextPtr;
};
typedef struct linkedListNode LinkedListNode;
int main()
{
LinkedListNode *startPtr, *newPtr, *currentPtr ;
startPtr = NULL;
int n , c ; int m ; int j ; int r ;
char ch ;
int num[1024] ;
cout << "請輸入N值 : " << endl ;
cin >> n ;
newPtr = (LinkedListNode *)malloc(sizeof(LinkedListNode));
for ( int i = 1 ; i <= n ; i++)
{
if( n%i ==0)
{
c++ ;
num[c] = i ;
cout << "第" << c << "個因數 : " << num[c] << endl ;
}
}
cout << "因數總個數為 : " << c << "個" << endl ;
cout << "====================================================" << endl;
for( int i = 1 ; i <=c ; i++ )
{
if(isPrime(num[i]))
cout << num[i] << "為質數" << endl;
else
cout << num[i] << "不為質數" << endl;
}
cout << "====================================================" << endl;
cout << "請選擇列印因數方法" << endl;
cout << "1) 正向列印出所有因數 " << endl;
cout << "2) 反向列印出所有因數 " << endl;
cout << "3) 離開 " << endl;
startPtr == NULL;
newPtr->data.ans;
newPtr->nextPtr = NULL;
if (startPtr == NULL) {
startPtr = newPtr;
}
else {
currentPtr = startPtr;
while (currentPtr != NULL) {
if (currentPtr->nextPtr == NULL) {
currentPtr->nextPtr = newPtr;
break;
}
currentPtr = currentPtr->nextPtr;
}
}
while(1){
ch = getchar();
switch(ch){
case '1' :
{
currentPtr = startPtr;
while (currentPtr != NULL)
cout << currentPtr-> data.ans <<endl;
currentPtr = currentPtr->nextPtr;
}
break ;
case '2' :
for (int i=c; i>0; i