Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ C++ Primer Plus 6th answers (by **PytLab**)

C++ Primer Plus(第六版) 编程练习答案(全部本人所写,仅供参考)

###Table of Content
### Table of Content

- [第三章 处理数据](https://github.com/PytLab/Cpp-Primer-Plus/tree/master/ch03)
- [第四章 复合类型](https://github.com/PytLab/Cpp-Primer-Plus/tree/master/ch04)
Expand Down
2 changes: 1 addition & 1 deletion ch07/7_9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int getinfo(student * pstu, int n)
cout << "fullname: ";
// detect blank line
cin.getline(pt->fullname, Slen);
if(!pt->fullname)
if(!*pt->fullname)
break;
cout << "hobby: ";
cin.getline(pt->hobby, Slen);
Expand Down
22 changes: 17 additions & 5 deletions ch11/11_1/randwalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ int main()
// write to file
ofstream fout;
fout.open("walk.txt");

if (!fout.is_open()) {
cout << "Could not write to file \n";
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
Expand All @@ -29,29 +33,37 @@ int main()
break;

// write to file
fout << "Target Distance: " << target << ", ";
fout << "Step Size: " << dstep << endl;

fout << "Target Distance: " << target << ", Step Size: " << dstep << endl
<< steps << ": " << result << endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
fout << steps << ": " << result << endl;
steps++;
fout << steps << ": " << result << endl;
}
cout << "After " << steps << " steps, the subject "
"has the following location:\n";
cout << result << endl;
fout << "After " << steps
<< " steps, the subject "
"has the following location:\n" << result << endl;
result.polar_mode();
cout << " or\n" << result << endl;
cout << "Average outward distance per step = "
<< result.magval()/steps << endl;
<< result.magval()/steps << endl;
fout << " or\n"
<< result << endl
<< "Average outward distance per step = " << result.magval() / steps
<< endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
fout.close();

return 0;
}