add solution of lab01

This commit is contained in:
2025-01-20 02:18:25 -05:00
parent 0dd22a2682
commit 8b451ff045
5 changed files with 141 additions and 11 deletions

28
labs/classes/Animal.h Normal file
View File

@@ -0,0 +1,28 @@
#include <string>
class Animal {
public:
Animal(const std::string &name);
std::string getName() const;
void setWeight(double new_weight);
double getWeight() const;
void setCanSurviveOnLand(bool can_land);
bool canSurviveOnLand() const;
void setCanSurviveInWater(bool can_water);
bool canSurviveInWater() const;
void setEatsMeat(bool meat);
bool eatsMeat() const;
void setEatsPlants(bool plants);
bool eatsPlants() const;
bool isOmnivore() const;
bool isAmphibious() const;
private:
std::string name_;
double weight_;
bool can_survive_on_land_;
bool can_survive_in_water_;
bool eats_meat_;
bool eats_plants_;
};

30
labs/classes/fileio.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream inputFile("input.txt");
if (!inputFile.is_open()) {
std::cerr << "Failed to open the user data file." << std::endl;
exit(1);
}
std::string name, gender, number, profession, school;
int age;
double latitude, longitude;
while(inputFile >> name
>> age
>> gender
>> number
>> profession
>> school
>> latitude
>> longitude){
std::ofstream outputFile("output.txt", std::ios::app);
outputFile << name << "," << number << std::endl;
outputFile.close();
}
inputFile.close();
return 0;
}

View File

@@ -23,15 +23,13 @@ int main() {
// UNCOMMENT THESE TESTS AS YOU WORK THROUGH CHECKPOINT 3 // UNCOMMENT THESE TESTS AS YOU WORK THROUGH CHECKPOINT 3
/*
std::cout << "testing print" << std::endl; std::cout << "testing print" << std::endl;
a.PrintAMPM(); a.PrintAMPM();
b.PrintAMPM(); b.PrintAMPM();
c.PrintAMPM(); c.PrintAMPM();
std::cout << std::endl; std::cout << std::endl;
*/
/*
std::cout << "testing modifiers" << std::endl; std::cout << "testing modifiers" << std::endl;
a.setHour(4); a.setHour(4);
a.setMinute(32); a.setMinute(32);
@@ -42,9 +40,7 @@ int main() {
assert (a.getSecond() == 1); assert (a.getSecond() == 1);
a.PrintAMPM(); a.PrintAMPM();
std::cout << std::endl; std::cout << std::endl;
*/
/*
std::cout << "more testing print" << std::endl; std::cout << "more testing print" << std::endl;
Time noon(12,0,0); Time noon(12,0,0);
Time midnight(0,0,0); Time midnight(0,0,0);
@@ -56,10 +52,7 @@ int main() {
std::cout << "midnight2 "; std::cout << "midnight2 ";
midnight2.PrintAMPM(); midnight2.PrintAMPM();
std::cout << std::endl; std::cout << std::endl;
*/
/*
std::vector<Time> times; std::vector<Time> times;
times.push_back(Time(0,0,1)); times.push_back(Time(0,0,1));
@@ -81,6 +74,4 @@ int main() {
for (int i = 0; i < times.size(); i++) { for (int i = 0; i < times.size(); i++) {
times[i].PrintAMPM(); times[i].PrintAMPM();
} }
*/
} }

61
labs/classes/time.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "time.h"
#include <iostream>
#include <iomanip>
//init hour, minute, second to 0
Time::Time() {
hour = 0;
minute = 0;
second = 0;
}
Time::Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const {
return second;
}
void Time::setHour(int h) {
hour = h;
}
void Time::setMinute(int m) {
minute = m;
}
void Time::setSecond(int s) {
second = s;
}
void Time::PrintAMPM() const {
int hh = hour;
std::string ampm = "am";
if (hh == 0) {
hh = 12;
} else if (hh == 12) {
ampm = "pm";
} else if (hh > 12) {
hh -= 12;
ampm = "pm";
}
std::cout << hh << ":";
if (minute < 10) {
std::cout << "0" << minute << ":";
} else {
std::cout << minute << ":";
}
std::cout << std::setfill('0') << std::setw(2) << second;
std::cout << " " << ampm << std::endl;
}

20
labs/classes/time.h Normal file
View File

@@ -0,0 +1,20 @@
class Time {
private:
int hour;
int minute;
int second;
public:
Time();
Time(int h, int m, int s);
int getHour() const;
int getMinute() const;
int getSecond() const;
void setHour(int h);
void setMinute(int m);
void setSecond(int s);
void PrintAMPM() const;
};