开学到现在写了一堆代码 然而懒得写博客发2333333
AHU今年最长寒假40多天简直爽cry
话说今晚C++实验期末考试啊= =
最近一直在写各科实验的代码 还有自己论文的东西。。。这本书的题目都没怎么写= =
//第一题
//main.cpp
#include "Cow.h"
int main()
{
Cow a("Bob", "Fuck", 100);
a.ShowCow();
Cow b;
b = a;
b.ShowCow();
system("PAUSE");
return 0;
}
//Cow.h
#ifndef COW_H_
#define COW_H_
#include <iostream>
#include <cstring>
class Cow {
private:
char name[20];
char *hobby;
double weight;
public:
Cow();
Cow(const char *nm, char *ho, double wt);
Cow(const Cow &c);
~Cow();
Cow &operator=(const Cow &c);
void ShowCow() const;
};
#endif
//Cow.cpp
#include "Cow.h"
Cow::Cow()
{
name[0] = '\0';
hobby = nullptr;
weight = 0;
}
Cow::Cow(const char * nm, char * ho, double wt)
{
strcpy_s(name, nm);
hobby = ho;
weight = wt;
}
Cow::Cow(const Cow & c)
{
strcpy_s(name, c.name);
hobby = c.hobby;
weight = c.weight;
}
Cow::~Cow()
{
}
Cow & Cow::operator=(const Cow & c)
{
if (this == &c)
return *this;
strcpy_s(name, c.name);
hobby = c.hobby;
weight = c.weight;
return *this;
}
void Cow::ShowCow() const
{
std::cout << "name: " << name << std::endl;
std::cout << "hobby: " << hobby << std::endl;
std::cout << "weight: " << weight << std::endl;
}
//第二题
//main.cpp
#include "string2.h"
int main()
{
String s1(" and I am a C++ student.");
String s2 = "Please enter your name: ";
String s3;
std::cout << s2;
std::cin >> s3;
s2 = "My name is " + s3;
std::cout << s2 << ".\n";
s2 = s2 + s1;
s2.Stringup();
std::cout << "The string\n" << s2 << "\ncontains " << s2.has('A') << " 'A' characters in it.\n";
s1 = "read";
String rgb[3] = { String(s1), String("green"), String("blue") };
std::cout << "Enter the name of a primary color for mixing light: ";
String ans;
bool success = false;
while (std::cin >> ans)
{
ans.Stringlow();
for (int i = 0; i < 3; ++i)
{
if (ans == rgb[i])
{
std::cout << "That's right!\n";
success = true;
break;
}
}
if (success)
break;
else
std::cout << "Try again!\n";
}
std::cout << "Bye\n";
return 0;
}
//string2.h
#ifndef STRING2_H_
#define STRING2_H_
#include <iostream>
class String {
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char *s);
String();
String(const String &);
~String();
int length() const { return len; }
String &operator=(const String &);
String &operator=(const char *);
char &operator[](int i);
const char &operator[](int i) const;
friend bool operator<(const String &st1, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend std::ostream &operator<<(std::ostream &os, const String &st);
friend std::istream &operator>>(std::istream &is, String &st);
static int HowMany();
friend String operator+(const String &st1, const String &st2);
void Stringlow();
void Stringup();
int has(char c);
};
#endif
//string2.cpp
#include "string2.h"
#include <cstring>
int String::num_strings = 0;
int String::HowMany()
{
return num_strings;
}
void String::Stringlow()
{
const int num = 'a' - 'A';
for (int i = 0; i < len; ++i)
{
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] += num;
}
}