※ 引述《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