问题2和3可以共用一个函数,代码如下:
// File Name: bbs_main.cpp
// Author : olive zhang
// Date : Sep. 21. 05
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <iomanip.h>
int MultiplyFactor(int digit); // function of the first question
int GetPrimeNumber(int Num, int *primeNum); //function of the second and third question
int Light(int lightNum);
int main()
{
// Q1:
int factor;
int multiResult;
cin >> factor;
multiResult = MultiplyFactor(factor);
cout << "multiResult = " << multiResult << endl;
// Q2 and Q3:
int num;
int n;
int primeNum[100];
cin >> num;
n = GetPrimeNumber(num, primeNum);
cout << "prime number is: " << endl;
for (int i=0; i<n; i++)
{
cout <<setw(4) << primeNum[i];
if ((i+1)%10 == 0)
cout << endl;
}
cout << endl;
cout << "n:" << n << endl;
return 0;
}
//------------------------------------------------------------------------//
// Function: MultiplyFactor()
// Input : digit
// Output : return the result of caculation
//------------------------------------------------------------------------//
int MultiplyFactor(int digit)
{
int result = 1;
int n = 1;
int base = 1;
if (digit == 0)
return 0;
if (digit < 0)
digit = -digit;
while (digit)
{
result *= digit%10;
digit /= 10;
}
return result;
}
//------------------------------------------------------------------------//
// Function: GetPrimeNumber()
// Input : Num -- a digit for caculating prime number
// primeNum -- a pointer for saving prime numbers
// Output : return the counts of prime number
//------------------------------------------------------------------------//
int GetPrimeNumber(const int Num, int *primeNum)
{
int number = Num;
int n = 0;
int flag = 1;
if (number < 0)
number = -number;
for (int i=3; i<number; i++)
{
int temp = (int)sqrt(i+1);
for (int j=2; j<=temp; j++)
{
if (i%j == 0)
{
flag = 0;
break;
}
}
if (flag)
{
primeNum[n] = i;
//cout << primeNum[n] << " ";
n++;
}
flag = 1;
}
return n;
}