update solution of lab02

This commit is contained in:
2025-01-22 14:05:54 -05:00
parent 93388c1fe2
commit b638fdea68
8 changed files with 52 additions and 12 deletions

37
.vscode/launch.json vendored
View File

@@ -5,7 +5,7 @@
"name": "nyplaylists", "name": "nyplaylists",
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}", // 指向你的可执行文件 "program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [ "args": [
"playlist_tiny2.txt", "playlist_tiny2.txt",
"actions2.txt", "actions2.txt",
@@ -17,6 +17,41 @@
"MIMode": "gdb", "MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb", "miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "C/C++: g++ build active file" "preLaunchTask": "C/C++: g++ build active file"
},
{
"name": "nyride",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [
"drivers.txt",
"riders.txt",
"output0.txt",
"output1.txt",
"output2.txt",
"516-951-1561",
"request",
"debug"
],
"cwd": "${fileDirname}",
"environment": [],
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "C/C++: g++ build active file"
},
{
"name": "lab02-roads",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [
"input_a.txt"
],
"cwd": "${fileDirname}",
"environment": [],
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "C/C++: g++ build active file"
} }
] ]
} }

View File

@@ -22,7 +22,7 @@ double gradient(const Line &ln) {
float gradient = 100.0 * fabs(slope); float gradient = 100.0 * fabs(slope);
// take the absolute value // take the absolute value
if (gradient < 0) { if (gradient < 0) {
gradient * -1; gradient *= -1;
} }
return gradient; return gradient;
} }
@@ -37,6 +37,5 @@ bool steeper_gradient(const Line &m, const Line &n) {
return true; return true;
if (gradient_n > gradient_m) if (gradient_n > gradient_m)
return false; return false;
return false;
} }

View File

@@ -1,3 +1,5 @@
#ifndef LINE_H
#define LINE_H
#include "point.h" #include "point.h"
// A simple line class. In this simple world, we'll follow the // A simple line class. In this simple world, we'll follow the
@@ -26,4 +28,4 @@ double gradient(const Line &ln);
// (That can be used to sort a collection of roads.) // (That can be used to sort a collection of roads.)
bool steeper_gradient(const Line &m, const Line &n); bool steeper_gradient(const Line &m, const Line &n);
#endif

Binary file not shown.

View File

@@ -20,6 +20,6 @@ double compute_slope(const Point &a, const Point &b) {
double run_x = b.get_x() - a.get_x(); double run_x = b.get_x() - a.get_x();
double run_z = b.get_z() - a.get_z(); double run_z = b.get_z() - a.get_z();
double run = sqrt(run_x*run_x + run_z*run_z); double run = sqrt(run_x*run_x + run_z*run_z);
double answer = rise / run; //double answer = rise / run;
return rise / run; return rise / run;
} }

View File

@@ -1,6 +1,7 @@
#ifndef _POINT_H_
#define _POINT_H_
#include <iostream> #include <iostream>
// A simple 3D point class. In this simple world, we'll follow the // A simple 3D point class. In this simple world, we'll follow the
// convention often used in Computer Graphics. y is the vertical // convention often used in Computer Graphics. y is the vertical
// axes, "pointing" up. The x and z axes define the ground plane. // axes, "pointing" up. The x and z axes define the ground plane.
@@ -26,3 +27,5 @@ std::ostream& operator<< (std::ostream &ostr, const Point &p);
// A helper function to compute the slope between two Points. // A helper function to compute the slope between two Points.
double compute_slope(const Point &a, const Point &b); double compute_slope(const Point &a, const Point &b);
#endif

View File

@@ -2,13 +2,13 @@
#include <vector> #include <vector>
#include <cmath> #include <cmath>
#include <fstream> #include <fstream>
#include <algorithm>
#include "line.h" #include "line.h"
#include "point.h"
// A helper function to parse a collection of files from an input file. // A helper function to parse a collection of files from an input file.
std::vector<Line>& load(std::ifstream &istr) { std::vector<Line> load(std::ifstream &istr) {
std::vector<Line> roads; std::vector<Line> roads;
float x1,y1,z1,x2,y2,z2; float x1,y1,z1,x2,y2,z2;
while (istr >> x1 >> y1 >> z1 >> x2 >> y2 >> z2) { while (istr >> x1 >> y1 >> z1 >> x2 >> y2 >> z2) {
@@ -28,13 +28,14 @@ void organize(std::vector<Line> &roads) {
void print(const std::vector<Line> &roads) { void print(const std::vector<Line> &roads) {
// print each road in the current, sorted order (steepest first) // print each road in the current, sorted order (steepest first)
for (int i = 0; i < roads.size(); i++) { //for (int i = 0; i < roads.size(); i++) {
for (size_t i = 0; i < roads.size(); i++) {
std::cout << roads[i] << std::endl; std::cout << roads[i] << std::endl;
} }
// count the number of roads with gradient less than 10% // count the number of roads with gradient less than 10%
int count; int count;
for (unsigned int i = roads.size() - 1; for (int i = roads.size() - 1;
i >= 0 && gradient(roads[i]) < 10.0; i >= 0 && gradient(roads[i]) < 10.0;
i--) { i--) {
count++; count++;