Problem 1: Vectors
挑第二问写上来吧
void RemoveSpan(vector<eMailMsg> &eMailMsg)
{
for (long i = eMailMsg.size()-1 ; i>=0; i--) //size zai jian shao
if("SPAM" == eMailMsg[i].subject.substr(0,4))
{
//remove
}
}
Problem 2: Queues
用栈解决队列的reverse问题
cs106用的是自己的stack queue库,我用的标准库,还是有点区别的
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
using namespace std;
//problem 2
void ReverseQueue(queue<int> &q);
int main() {
queue<int> test1;
test1.push(1);
test1.push(2);
test1.push(3);
test1.push(4);
test1.push(5);
cout << test1.front()<<test1.back()<<endl;
ReverseQueue(test1);
cout << test1.front()<<test1.back()<<endl;
return 0;
}
//problem 2
void ReverseQueue(queue<int> &q)
{
stack<int> s;
for(long i=q.size(); i>0; i--){
int j= q.front();
s.push(j);
q.pop();
}
for(long i=s.size(); i>0; i--){
int j= s.top();
q.push(j);
s.pop();
}
}
晕不知道怎么用mac上传图片~
Problem 3: Using the Scanner and Stack classes
copy大神的想法,太难了有木有
#include <iostream>
#include <cstdlib>
#include <string>
#include "scanner.h"
#include "stack.h"
using namespace std;
bool ProcessOpenTag(Scanner& scanner, Stack<string>& tagStack);
bool ProcessCloseTag(Scanner& scanner, Stack<string>& tagStack);
bool ProcessTag(Scanner& scanner, Stack<string>& tagStack);
bool IsCorrectlyNested(string htmlStr);
int main()
{
string htmlStr="<html><b><i>CS106 rules!</i></b></html>";
bool corret = IsCorrectlyNested(htmlStr);
cout<<htmlStr<<endl;
cout<<corret<<endl;
return 0;
}
bool ProcessOpenTag(Scanner& scanner, Stack<string>& tagStack)
{
string tag = scanner.nextToken();
tagStack.push(tag);
return true;
}
bool ProcessCloseTag(Scanner& scanner, Stack<string>& tagStack)
{
string tag = scanner.nextToken();
if (!tagStack.isEmpty() && tag == tagStack.pop()) {
return true;
}else {
return false;
} }
bool ProcessTag(Scanner& scanner, Stack<string>& tagStack)
{
// read the next token to see if we found an
// opening or closing tag
string token = scanner.nextToken();
if (token == "/")
{
return ProcessCloseTag(scanner, tagStack);
}
else {
return ProcessOpenTag(scanner, tagStack);
}
}
bool IsCorrectlyNested(string htmlStr)
{
Scanner scanner;
scanner.setSpaceOption(Scanner::IgnoreSpaces);
Stack<string> tagStack;
scanner.setInput(htmlStr);
// start by assuming it is balanced
bool isBalanced = true;
while (scanner.hasMoreTokens())
{
string token = scanner.nextToken();
if (token == "<")
{
if (!ProcessTag(scanner, tagStack))
{
isBalanced = false;
break; }
// get rid of ">" part of tag
scanner.nextToken();
}
}
if (!tagStack.isEmpty()) isBalanced = false;
return isBalanced;
}
Problem 4: Map Warm-up
文件流总是有问题,是课程太老了嘛
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "map.h"
char MostFrequentCharacter(ifstream &in, int &numOccurrences);
using namespace std;
int main()
{
ifstream infile;
int num =0;
infile.open("me.txt");
char a =MostFrequentCharacter(infile, num);
cout<<a <<" " << num<< endl;
infile.close();
return 0;
}
char MostFrequentCharacter(ifstream &in, int &numOccurrences)
{
Map<int> table;
numOccurrences=0;
char ch;
char mostch;
while (in.get(ch) != fail) {
string ch1="";
ch1[0]=ch;
if (table.containsKey(ch1)) {
int value= table.get(ch1);
value++;
if(numOccurrences<value)
{
numOccurrences=value;
mostch=ch1[0];
}
}
else{
table.put(ch1,1);
}
}
return mostch;
}
Problem 5: Minesweeper
写的有点笨,不过学习嘛
#include <iostream>
#include <cstdlib>
#include <string>
#include "grid.h"
using namespace std;
Grid<int> MakeGridOfCounts(Grid<bool> & bombLocations);
int CalMines(Grid<bool> & bombLocations,int i,int j);
int main()
{
Grid<bool> b(4,4);
Grid<int> result(4,4);
b[0][0]=1;b[0][1]=1;b[0][2]=0;b[0][3]=1;
b[1][0]=0;b[1][1]=1;b[1][2]=1;b[1][3]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;b[2][3]=1;
b[3][0]=1;b[3][1]=0;b[3][2]=1;b[3][3]=1;
cout<<"bombs is :"<<endl;
for(int i=0;i<4;i++){
cout<<b[i][0]<<'|'<<b[i][1]<<'|'<<b[i][2]<<'|'<<b[i][3]<<endl;
}
result=MakeGridOfCounts(b);
cout<<"counts is :"<<endl;
for(int i=0;i<4;i++){
cout<<result[i][0]<<'|'<<result[i][1]<<'|'<<result[i][2]<<'|'<<result[i][3] <<endl;
}
return 0;
}
Grid<int> MakeGridOfCounts(Grid<bool> &bombLocations){
int rows=bombLocations.numRows();
int cols=bombLocations.numCols();
Grid<int> counts(rows,cols);
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
counts[i][j]=CalMines(bombLocations,i,j);
}
}
return counts;
}
int CalMines(Grid<bool> & bombLocations,int i,int j){
int num;
Grid<int> counts2 ((bombLocations.numRows())+2,(bombLocations.numCols())+2);
for(int m=0; m<bombLocations.numRows()+2; m++){
for(int n=0; n<bombLocations.numCols()+2; n++){
counts2[m][n]=0;
}
}
for(int m=0; m<bombLocations.numRows(); m++){
for(int n=0; n<bombLocations.numCols(); n++){
counts2[m+1][n+1]=bombLocations[m][n];
}
}
num=counts2[i][j]+counts2[i][j+1]+counts2[i][j+2]+counts2[i+1][j]+
counts2[i+1][j+2]+counts2[i+2][j]+counts2[i+2][j+1]+counts2[i+2][j+2];
return num;
}