int input_error(fstream i){
if (!i){
cerr << endl << "Fail to open the file!" << endl << endl;
return -1;
}
else
return 0;
}
int main(){
int n;
fstream input("input.txt");
if (input_error(input) == -1){
fstream input;
string filename;
cout << "Please input the absolute path of the file:";
getline(cin, filename, '\n');
input.open(filename);
if (input_error(input) == -1)return -1;
};
以下为源代码,希望能够得到各位大神的解决,谢谢!
#include<iostream>
#include <iomanip>
#include<fstream>
#include<string>
using namespace std;
long long Fibo[90] = { 0 };
unsigned long long fibo(long n){
if (n <= 1)
return Fibo[n] = n;
else if
(Fibo[n] != 0) return Fibo[n];
else
return Fibo[n] = fibo(n - 1) + fibo(n - 2);
}
int input_error(fstream i){
if (!i){
cerr << endl << "Fail to open the file!" << endl << endl;
return -1;
}
else
return 0;
}
int main(){
int n;
fstream input("input.txt");
if (input_error(input) == -1){
fstream input;
string filename;
cout << "Please input the absolute path of the file:";
getline(cin, filename, '\n');
input.open(filename);
if (input_error(input) == -1)return -1;
};
input >> n;
input.close();
if (n<0 || n>90){
cout << endl << "The integer is illegal!" << endl << endl;
return -1;
}
else
cout << endl << "n: " << n << endl;
cout << endl << "Result: " << fibo(n) << endl;
cout << endl << "—————Process—————" << endl;
for (int i = 0; i <= n; i++){
cout << setw(2) << setfill('0') << i << ".....";
cout << setw(19) << setfill('.') << Fibo[i] << endl;
}
cout << endl;
return 0;
}
再次感谢!
//:~
/*Fibonacci 1410729 许木胜 */
#include<iostream>
#include <iomanip>
#include<fstream>
#include<string>
using namespace std;
long long Fibo[90] = { 0 };
unsigned long long fibo(long n){
if (n <= 1)
return Fibo[n] = n;
else if
(Fibo[n] != 0) return Fibo[n];
else
return Fibo[n] = fibo(n - 1) + fibo(n - 2);
}
int input_error(fstream i){
if (!i){
cerr << endl << "Fail to open the file!" << endl << endl;
return -1;
}
else
return 0;
}
int main(){
int n;
fstream input("input.txt");
if (input_error(input) == -1){
fstream input;
string filename;
cout << "Please input the absolute path of the file:";
getline(cin, filename, '\n');
input.open(filename);
if (input_error(input) == -1)return -1;
};
input >> n;
input.close();
if (n<0 || n>90){
cout << endl << "The integer is illegal!" << endl << endl;
return -1;
}
else
cout << endl << "n: " << n << endl;
cout << endl << "Result: " << fibo(n) << endl;
cout << endl << "—————Process—————" << endl;
for (int i = 0; i <= n; i++){
cout << setw(2) << setfill('0') << i << ".....";
cout << setw(19) << setfill('.') << Fibo[i] << endl;
}
cout << endl;
return 0;
}
以上才是源代码,第一次提问,错误请原谅= =
int main(){
int n;
fstream input("input.txt");
if (input_error(input) == -1){
fstream input;
,,,
这里input你定义了2次,后面你用的是里面那个