Skip to content

Instantly share code, notes, and snippets.

@ecnelises
Created July 2, 2016 07:28
Show Gist options
  • Save ecnelises/f29c57ea2e07f7208069acac76942804 to your computer and use it in GitHub Desktop.
Save ecnelises/f29c57ea2e07f7208069acac76942804 to your computer and use it in GitHub Desktop.
2016年同济C++期末考试上机题
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
// Case-insensitive Compare
bool ci_comp(char c1, char c2)
{
return tolower(c1) == tolower(c2);
}
template<typename T, typename predicate>
bool mymismatch(const T& lhs, const T& rhs, predicate pre)
{
T::const_iterator i, j;
for (i = lhs.cbegin(), j = rhs.cbegin();
i != lhs.cend() && j != rhs.cend(); ++i, ++j) {
if (!pre(*i, *j)) {
return true;
}
}
if (i != lhs.cend() || j != rhs.cend()) {
return true;
}
return false;
}
// Here we have to pass-by-value
// Because we'll sort them but don't modify original strings
char anagram(string s1, string s2)
{
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
if (mymismatch(s1, s2, ci_comp)) {
return 'N';
} else {
return 'Y';
}
}
int main(void)
{
string lhs, rhs;
char ch;
bool is_second = false;
ifstream filein("in_1.txt");
ofstream fileout("1454001_1_out.txt");
while (filein >> ch) {
if (ch == ',') {
is_second = true;
continue;
}
if (is_second) {
rhs += ch;
} else {
lhs += ch;
}
}
fileout << anagram(lhs, rhs);
return 0;
}
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
template<typename T>
void input_fill(vector<T> vec, const string& line)
{
T tmp;
int offset = 0;
for (auto i = line.cbegin(); i != line.cend(); ++i) {
if (isdigit(*i)) {
break;
}
++offset;
}
istringstream iss(string(line.c_str() + offset));
while (iss >> tmp) {
vec.push_back(tmp);
iss.ignore();
}
}
bool my_equal(int n1, int n2)
{
return n1 == n2;
}
template<typename T, typename U>
bool my_equal(T n1, U n2)
{
const float epsi = 0.00001;
return abs(n1 - n2) < epsi;
}
template<typename T>
bool my_equal(T n1, T n2)
{
const float epsi = 0.00001;
return abs(n1 - n2) < epsi;
}
template<typename T, typename U>
auto find_same(vector<T> vec1, vector<U> vec2) -> vector<decltype(T(0) + U(0))>
{
vector<decltype(T() + U())> res;
for (auto i = vec1.begin(); i != vec1.end(); ++i) {
for (auto j = vec2.begin(); j != vec2.end(); ++j) {
if (my_equal(*i, *j)) {
res.push_back(*i);
vec2.erase(j);
break;
}
}
}
return res;
}
template<typename T>
void print(const vector<T> vec, ofstream& file)
{
for (int i = 0; i < (int)vec.size() - 1; ++i) {
file << vec.at(i) << ',';
}
file << vec.at(vec.size() - 1);
}
int main(void)
{
// sorry, input has some problem
vector<int> ivec1, ivec2;
vector<float> fvec1, fvec2;
ifstream file("in_2.txt");
ofstream out("1454001_2_out.txt");
string line1, line2;
getline(file, line1);
getline(file, line2);
bool i1 = true, i2 = true;
if (line1.find('.') == string::npos) {
i1 = false;
}
if (line2.find('.') == string::npos) {
i2 = false;
}
int offset;
if (i1) {
input_fill(ivec1, line1);
} else {
input_fill(fvec1, line1);
}
if (i2) {
input_fill(ivec2, line2);
} else {
input_fill(fvec2, line2);
}
if (i1) {
if (i2) {
print(find_same(ivec1, ivec2));
} else {
print(find_same(ivec1, fvec2));
}
} else {
if (i2) {
print(find_same(fvec1, ivec2));
} else {
print(find_same(fvec1, fvec2));
}
}
return 0;
}
#include <sstream>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdio>
#include <cctype>
using namespace std;
class image {
public:
image(ifstream& input);
void average(void);
void output(ofstream& outer);
private:
short nb_avg(const vector<short>& org, unsigned rank);
short get(const vector<short>& vec, int line, int column, unsigned& total);
vector<short> inner;
unsigned lines;
unsigned columns;
};
image::image(ifstream& input)
{
unsigned count = 0;
string str_tmp;
getline(input, str_tmp);
istringstream iss(str_tmp);
short tmp;
while (iss >> tmp) {
++count;
inner.push_back(tmp);
}
columns = count;
while (input >> tmp) {
inner.push_back(tmp);
}
lines = inner.size() / columns;
}
short image::nb_avg(const vector<short>& org, unsigned rank)
{
int line = rank / columns;
int column = rank % columns;
unsigned total_num = 0;
unsigned sum =
get(org, line - 1, column, total_num) +
get(org, line + 1, column, total_num) +
get(org, line, column + 1, total_num) +
get(org, line, column - 1, total_num) +
get(org, line - 1, column + 1, total_num) +
get(org, line - 1, column - 1, total_num) +
get(org, line + 1, column + 1, total_num) +
get(org, line + 1, column - 1, total_num);
return sum / total_num;
}
// For convenience, outside borders are set to zero.
short image::get(const vector<short>& vec, int line, int column, unsigned& total)
{
if (line >= lines || column >= columns) {
return 0;
} else if (line < 0 || column < 0) {
return 0;
} else {
++total;
return vec.at(line * columns + column);
}
}
void image::average(void)
{
vector<short> original = inner;
for (unsigned i = 0; i < inner.size(); ++i) {
inner.at(i) = nb_avg(original, i);
}
}
void image::output(ofstream& outer)
{
for (unsigned i = 0; i < lines; ++i) {
for (unsigned j = 0; j < columns; ++j) {
outer << inner.at(i * columns + j) << '\t';
}
outer << '\n';
}
}
int main(void)
{
int n;
ifstream file("in_3.txt");
string tmp_line;
getline(file, tmp_line);
int offset = 0;
for (auto i = tmp_line.cbegin(); i != tmp_line.cend(); ++i) {
if (isdigit(*i)) {
break;
}
++offset;
}
sscanf(tmp_line.c_str() + offset, "%d", &n);
cout << n;
ofstream out("1454001_3_out.txt");
image matrix(file);
for (int i = 0; i < n; ++i) {
matrix.average();
}
matrix.output(out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment