-1
我正在为学校任务编写代码,该任务应该要求用户提供限制并调用一个函数,该函数将给定数组的值生成并传递给数组回到调用方法来打印。这里是我到目前为止的代码:C++返回数组指针不能按预期工作
页眉:
/*
* series.h
*/
#ifndef SERIES_H_
#define SERIES_H_
// Direct calls for testing - DELETE AFTER
double log(int x);
double exp(int x);
double fib(int x);
double fac(int x);
// Global
enum SERIES {LOG, EXP, FIB, FAC};
void getSeries(SERIES _s, double _limit, int& _length, double *series);
double mean(double* _values, int length);
void printSeries(double* _series, int _length);
#endif /* SERIES_H_ */
CPP:
/*
* series.cpp
*/
#include "series.h"
#include <cmath>
#include <iostream>
using namespace std;
int factorial(int x) {
return (x == 1 ? x : x * factorial(x - 1));
}
double log(int x) {
return ((double)x - ((double)(pow(x,2))/(double)(2))
+ ((double)(pow(x,3))/(double)(3)));
}
double exp(int x) {
return (((double) 1) + ((double) x)
+ ((pow((double) x, 2))/((double) factorial(2)))
+ ((pow((double) x, 3))/((double) factorial(3)))
+ ((pow((double) x, 4))/((double) factorial(4))));
}
double fib(int x) {
return (x < 2 ? x : (fib(x - 1) + fib(x - 2)));
}
double fac(int x) {
return (x < 2 ? 1 : x * factorial(x - 1));
}
void getSeries(SERIES _s, double _limit, int& _length, double *series) {
_length = 0;
switch (_s) {
case LOG: {
for (int i = 0; log(i) < _limit; i++) {
series[i] = log(i);
_length++;
}
break;
}
case EXP: {
for (int i = 0; exp(i) < _limit; i++) {
series[i] = exp(i);
_length++;
}
break;
}
case FIB: {
for (int i = 0; fib(i) < _limit; i++) {
series[i] = fib(i);
_length++;
}
break;
}
case FAC: {
for (int i = 0; fac(i) < _limit; i++) {
series[i] = fac(i);
_length++;
}
break;
}
}
}
double mean(double* _values, int length) {
double sum;
for (int i = 0; i < length; i++) {
sum += _values[i];
}
return (sum/length);
}
void printSeries(double* _series, int _length) {
for (int i = 0; i < _length; i++) {
cout << "[" << i << "]" << _series[i] << endl;
}
}
主营:
/*
* main.cpp
*/
#include <iostream>
#include "series.h"
using namespace std;
int main() {
double _limit;
int _length;
double* series = new double;
cout << "Series Calculator Application" << endl;
cout << "Please enter a limit:" << endl;
cin >> _limit;
cout << "Limit is " << _limit << endl;
getSeries(LOG, _limit, _length, series);
cout << "\nLOG Length is " << _length <<endl;
cout << "------------TEST(LOG)" << endl;
for(int i = 0; i < _length; i++){
cout << "[" << i << "]" << log(i) << endl;
}
cout << "------------ACTUAL(LOG)" << endl;
printSeries(series, _length);
getSeries(EXP, _limit, _length, series);
cout << "\nEXP Length is " << _length <<endl;
cout << "------------TEST(EXP)" << endl;
for(int i = 0; i < _length; i++){
cout << "[" << i << "]" << exp(i) << endl;
}
cout << "------------ACTUAL(EXP)" << endl;
printSeries(series, _length);
}
,当我跑我的主要我得到这个从控制台:
Series Calculator Application
Please enter a limit:
300
Limit is 300
LOG Length is 11
------------TEST(LOG)
[0]0
[1]0.833333
[2]2.66667
[3]7.5
[4]17.3333
[5]34.1667
[6]60
[7]96.8333
[8]146.667
[9]211.5
[10]293.333
------------ACTUAL(LOG)
[0]0
[1]0.833333
[2]2.66667
[3]7.5
[4]2.24151e-048
[5]1.96774e-259
[6]1.02756e-259
[7]96.8333
[8]146.667
[9]211.5
[10]293.333
EXP Length is 9
------------TEST(EXP)
[0]1
[1]2.70833
[2]7
[3]16.375
[4]34.3333
[5]65.375
[6]115
[7]189.708
[8]297
------------ACTUAL(EXP)
[0]1
[1]2.70833
[2]7
[3]16.375
[4]8.60084e-043
[5]1.56125e-259
[6]1.02899e-259
[7]189.708
[8]297
这是我的问题,正如您所见,元素4,5和6的实际实现与测试值不匹配。我不知道这是怎么发生的。请帮忙!
Uugh!这么多不必要的括号。了解您的运营商优先表和数据类型促销规则! – Bathsheba 2014-09-29 12:36:38
'double * series = new double;'分配一个'double',但'series'被用作一个数组。 – 2014-09-29 12:38:59
@Bath:parens很好。这是过多的[C型]演员阵容,这是一个问题。 – 2014-09-29 12:39:04