Giter Site home page Giter Site logo

cs-a150's People

Contributors

trung0246 avatar

Watchers

 avatar  avatar

cs-a150's Issues

h09

#include "h09.h"
#include BOOST_MODULE(array)

std::string STUDENT = "";

#define FUNC (const std::string& msg) { prssPrmt(msg);

void prssPrmt(const std::string& msg) {
	std::cout << msg;
	if (!msg.empty() && msg.at(msg.size() - 1) != ' ') std::cout << ' ';
}

std::string getData(bool isStr) {
	std::string data, prev;

	while (getline(std::cin, data)) std::swap(prev, data);

	if (!data.empty() && (isStr || (std::isdigit(data.at(0)) || !std::isalpha(data.at(0)))))
		return data;

	return prev;
}

std::string getLine FUNC
	std::string data;
	getline(std::cin, data);
	return data;
}

int getInt FUNC
	return std::stoi(getData(false));
}

double getReal FUNC
	return std::stod(getData(false));
}

const boost::array<std::string, 6> TRUTHY {{
	"y", "yes", "yep", "ok", "fine", "true",
}};

bool getYN FUNC
	std::string data = getData(true);

	data.erase(std::remove_if(data.begin(), data.end(), ::isspace), data.end());
	std::transform(data.begin(), data.end(), data.begin(), ::tolower);

	for (const std::string& str: TRUTHY) {
		if (data == str) return true;
	}
	return false;
}

Here's my solution for that stupid hw08

h08.cpp

#define HW
#include "../../tools.hpp"

//ONLY for example purpose, please don't copy the code directly :)

using namespace std;

string STUDENT = "";

#include "h08.h"

std::string dataSets(const std::string & fileName) {
	double sum = 0, count = 0;
	int real = 0, set = 1;
	string rel;
	ifstream istrm(fileName, ios_base::binary);

	if (!istrm.is_open()) {
		cout << "Failed to open " << fileName << '\n';
		return fileName + " cannot be opened.";
	}

	string data;
	char ch, prev = ' ';
	while (istrm.get(ch)) {
		if (prev == ' ' && ch == '0' && istrm.peek() == ' ') {
			ostringstream temp;
			temp << "data set " << set << ": total values = " << count <<
				"\naverage value = " << fixed << setprecision(4) << sum / count << "\n\n";
			rel += temp.str();
			count = 0;
			sum = 0;
			set++;
			istrm.get(ch);
			prev = ch;
		}
		if (!(isdigit(ch) || ch == '.') && isdigit(prev)) {
			if (real > 0) {
				sum += real * atof(data.c_str());
				real = 0;
			} else {
				real = atoi(data.c_str());
				count += real;
			}
			data = "";
		} else {
			data += ch;
		}
		prev = ch;
	}
	istrm.close();
	if (rel.size() > 0) {
		rel.erase(rel.size() - 1, 1);
	}
	return rel;
}

tools.hpp

#define _USE_MATH_DEFINES

#include <string>
#include <cmath> //https://stackoverflow.com/questions/10694255/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <functional>

#if (__cplusplus == 201103L) //https://stackoverflow.com/questions/2324658/
//remove_all to guarantee for always pure type https://stackoverflow.com/questions/14522496
#include <type_traits>
#endif

//#pragma warning(push)
//#pragma warning(disable: 3338)
template<class T> struct remove_all { typedef T type; };
template<class T> struct remove_all<T*> : remove_all<T> {};
template<class T> struct remove_all<T&> : remove_all<T> {};
//template<class T> struct remove_all<T&&> : remove_all<T> {};
template<class T> struct remove_all<T const> : remove_all<T> {};
template<class T> struct remove_all<T volatile> : remove_all<T> {};
template<class T> struct remove_all<T const volatile> : remove_all<T> {};
//#pragma warning(pop)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.