打卡cs106x(Autumn 2017)-lecture8
(以下皆使用SPL实现,非STL库,后续课程结束会使用STL实现)
Write a recursive function named reverseLines
that accepts as its parameter a reference to a file input stream (ifstream
) and prints the lines of that file in reverse order. For example, if an input file named poem.txt
contains the following text:
Roses are red, Violets are blue. All my base Are belong to you.
Then the call of reverseLines("poem.txt");
should produce the following console output:
Are belong to you. All my base Violets are blue. Roses are red,
You may assume that the input file exists and is readable.
Constraints: Your solution should read the file only once, not make multiple passes over the file data. Your function must be recursive.
解答:
#include
#include
#include
#include "console.h"
using namespace std;
void reverseLines(ifstream& input);
int main() {
ifstream input;
input.open("poem.txt");
if (input.is_open()) {
reverseLines(input);
}
input.close();
return 0;
}
void reverseLines(ifstream& input) {
string line;
if (getline(input, line)) { // recursive case
reverseLines(input);
cout << line << endl;
}
// base case: no line
// do nothing
}
Write a recursive function named printBinary
that accepts an integer and prints that number's representation in binary (base 2) to the console. For example, the call of printBinary(43);
should print 101011
. You do not need to print an endl
character at the end of your output.
If the integer is negative, print the binary representation preceded by a minus sign. For example, the call of printBinary(-6);
should print -110
.
Constraints: Do not use any loops. Do not solve this problem by converting the number into a string
. Do not solve it by using the pow
function or any form of computing exponents; that is inefficient and not needed to solve the problem. Do not declare any auxiliary collections such as a vector
or map
. Do not solve it by using the ostream
manipulation functionality such as setbase
.
解答:
#include
#include
#include "console.h"
using namespace std;
void printBinary(int n);
int main() {
printBinary(43);
return 0;
}
void printBinary(int n) {
if (n < 0) {
cout << "-";
printBinary(-n);
} else if (n <= 1) {
cout << n;
} else {
int last = n % 2;
int remind = n / 2;
printBinary(remind);
printBinary(last);
}
}
Write a recursive function named crawl
that accepts a string for a file or directory name as a parameter and prints information about that file or directory. If the name represents a normal file, just print its name. If the name represents a directory, print its name and information about every file/directory inside it, indented by 4 spaces at each level. For example, the call of crawl("course")
might print the following information about your course
directory:
course handouts lecture-schedule.xls syllabus.doc homework 1-gameoflife GameOfLife.zip life.cpp life.h
Constraints: Your function must be recursive, though you may use a loop to help you as appropriate.
解答:
#include
#include
#include "console.h"
#include "filelib.h"
#include "vector.h"
using namespace std;
void crawl(const string& filename, const string& indentation="");
int main() {
crawl("../BlankProject");
return 0;
}
void crawl(const string& filename, const string& indentation) {
if (isDirectory(filename)) {
Vector v = listDirectory(filename);
for (string vs: v){
cout << indentation << vs << endl;
string indentations = indentation + " ";
crawl(filename + '/' + vs, indentations);
}
}
}