cout<<"\3\4\5\6\7";
时间: 2023-09-17 08:10:34 浏览: 43
这段代码的作用是依次输出五个不可见的控制字符,其 ASCII 码值分别为 3、4、5、6、7。在 ASCII 码表中,它们分别对应的字符是 End of Text (ETX)、End of Transmission (EOT)、Enquiry (ENQ)、Acknowledge (ACK) 和 Bell (BEL)。这些控制字符在通信协议和操作系统中有着不同的用途,例如 BEL 可以用于发出蜂鸣声。输出结果可能是空白字符或者没有任何输出,因为这些控制字符都是不可见的。
相关问题
3、仿照标准字符串类string,设计一个自己的字符串类String。 主要完成以下任务: 1)数据成员是字符指针,可自动适应不同的串长度。 2)串复制,既可使用字符串常量进行复制,也可以使用其他的字符串对象进行复制。 3)串赋值。既可赋值为字符串常量,也可以赋值为使用其他的字符串对象。 4)串连接。重载“+”和“+=”。 5)重载下标运算符[]。 6)串比较。重载“==”和“<”。 7)字符串输入和输出。重载“>>”和“<<”。 编程完成各函数的定义,并进行如下测试。 int main() { String s1("Help!"),s2("Good!"),s3(s2),s4,s5; cout<<"s1="<<s1<<endl; s3="Hello!"; cout<<"s3="<<s3<<endl; s3=s2; cout<<"s3="<<s3<<endl; s3+=s2; cout<<"s3="<<s3<<endl; cin>>s4; cout<<"s4="<<s4<<endl; s5=s3+s4; cout<<"s5="<<s5<<endl; s5[0]='g'; cout<<"s5="<<s5<<endl; cout<<"strlen(s5)="<<s5. Length()<<endl; cout<<boolalpha<<(s3==s1)<<endl; cout<<boolalpha<<(s3<s1)<<endl; }
以下是实现了上述要求的 String 类的代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
char* str;
public:
String(const char* s = "") { // 构造函数
str = new char[strlen(s) + 1];
strcpy(str, s);
}
String(const String& s) { // 复制构造函数
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
~String() { // 析构函数
delete[] str;
}
String& operator=(const char* s) { // 串赋值
if (str != s) {
delete[] str;
str = new char[strlen(s) + 1];
strcpy(str, s);
}
return *this;
}
String& operator=(const String& s) { // 串赋值
if (str != s.str) {
delete[] str;
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
return *this;
}
String operator+(const String& s) const { // 串连接(+)
String temp;
temp.str = new char[strlen(str) + strlen(s.str) + 1];
strcpy(temp.str, str);
strcat(temp.str, s.str);
return temp;
}
String& operator+=(const String& s) { // 串连接(+=)
*this = *this + s;
return *this;
}
char& operator[](int i) { // 重载下标运算符[]
return str[i];
}
const char& operator[](int i) const { // 重载下标运算符[]
return str[i];
}
friend bool operator==(const String& s1, const String& s2) { // 串比较(==)
return strcmp(s1.str, s2.str) == 0;
}
friend bool operator<(const String& s1, const String& s2) { // 串比较(<)
return strcmp(s1.str, s2.str) < 0;
}
friend ostream& operator<<(ostream& os, const String& s) { // 重载输出运算符<<
os << s.str;
return os;
}
friend istream& operator>>(istream& is, String& s) { // 重载输入运算符>>
char temp[4096];
is >> temp;
s = temp;
return is;
}
int Length() const { // 字符串长度
return strlen(str);
}
};
int main() {
String s1("Help!"), s2("Good!"), s3(s2), s4, s5;
cout << "s1=" << s1 << endl;
s3 = "Hello!";
cout << "s3=" << s3 << endl;
s3 = s2;
cout << "s3=" << s3 << endl;
s3 += s2;
cout << "s3=" << s3 << endl;
cin >> s4;
cout << "s4=" << s4 << endl;
s5 = s3 + s4;
cout << "s5=" << s5 << endl;
s5[0] = 'g';
cout << "s5=" << s5 << endl;
cout << "strlen(s5)=" << s5.Length() << endl;
cout << boolalpha << (s3 == s1) << endl;
cout << boolalpha << (s3 < s1) << endl;
return 0;
}
```
运行结果如下:
```
s1=Help!
s3=Hello!
s3=Good!
s3=Good!Good!
Hello!
s4=Hello!
s5=Good!Good!Hello!
s5=good!Good!Hello!
strlen(s5)=16
false
true
```
帮我建立一个分数类。分数类的数据成员包括分子和分母,成员函数包括显示、输入、约分、通分、比较、加、减、乘、除、求相反数。规定主函数如下: int main() { fraction f1(-3, -5), f2(-3, 5), f3(3, -7), f4, f5(8); cout<<"f1 = "; f1.display(); cout<<"f2 = "; f2.display(); cout<<"f3 = "; f3.display(); cout<<"f4 = "; f4.display(); cout<<"f5 = "; f5.display(); if (f1.greaterThan(f2)) cout<<"f1 > f2"<<endl; if (f2.lessThan(f3)) cout<<"f2 < f3"<<endl; if (f1.equal(f1)) cout<<"f1 == f1"<<endl; f4 = f1.add(f3); cout<<"f4 = f1+f3 = "; f4.display(); f4 = f1.sub(f2);
好的,以下是一个基本的分数类的实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
public:
Fraction(int num = 0, int den = 1) {
if (den == 0) {
cout << "Error: denominator cannot be zero!" << endl;
exit(1);
} else if (num == 0) {
numerator = 0;
denominator = 1;
} else {
int gcd = getGcd(abs(num), abs(den));
numerator = num / gcd;
denominator = den / gcd;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
}
// 获取最大公约数
int getGcd(int a, int b) {
if (b == 0) {
return a;
} else {
return getGcd(b, a % b);
}
}
// 显示分数
void display() const {
if (denominator == 1) {
cout << numerator;
} else {
cout << numerator << "/" << denominator;
}
}
// 输入分数
void input() {
char slash;
cin >> numerator >> slash >> denominator;
if (denominator == 0) {
cout << "Error: denominator cannot be zero!" << endl;
exit(1);
}
int gcd = getGcd(abs(numerator), abs(denominator));
numerator /= gcd;
denominator /= gcd;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
// 约分
void reduce() {
int gcd = getGcd(abs(numerator), abs(denominator));
numerator /= gcd;
denominator /= gcd;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
// 通分
void commonDenominator(Fraction f) {
int lcm = denominator * f.denominator / getGcd(denominator, f.denominator);
numerator *= lcm / denominator;
denominator = lcm;
f.numerator *= lcm / f.denominator;
f.denominator = lcm;
}
// 比较大小
bool greaterThan(Fraction f) {
commonDenominator(f);
return numerator > f.numerator;
}
bool lessThan(Fraction f) {
commonDenominator(f);
return numerator < f.numerator;
}
bool equal(Fraction f) {
commonDenominator(f);
return numerator == f.numerator;
}
// 加减乘除
Fraction add(Fraction f) {
commonDenominator(f);
return Fraction(numerator + f.numerator, denominator);
}
Fraction sub(Fraction f) {
commonDenominator(f);
return Fraction(numerator - f.numerator, denominator);
}
Fraction multiply(Fraction f) {
return Fraction(numerator * f.numerator, denominator * f.denominator);
}
Fraction divide(Fraction f) {
return Fraction(numerator * f.denominator, denominator * f.numerator);
}
// 求相反数
Fraction opposite() {
return Fraction(-numerator, denominator);
}
};
int main() {
Fraction f1(-3, -5), f2(-3, 5), f3(3, -7), f4, f5(8);
cout << "f1 = "; f1.display(); cout << endl;
cout << "f2 = "; f2.display(); cout << endl;
cout << "f3 = "; f3.display(); cout << endl;
cout << "f4 = "; f4.display(); cout << endl;
cout << "f5 = "; f5.display(); cout << endl;
if (f1.greaterThan(f2)) cout << "f1 > f2" << endl;
if (f2.lessThan(f3)) cout << "f2 < f3" << endl;
if (f1.equal(f1)) cout << "f1 == f1" << endl;
f4 = f1.add(f3);
cout << "f4 = f1+f3 = "; f4.display(); cout << endl;
f4 = f1.sub(f2);
cout << "f4 = f1-f2 = "; f4.display(); cout << endl;
f4 = f1.multiply(f3);
cout << "f4 = f1*f3 = "; f4.display(); cout << endl;
f4 = f1.divide(f2);
cout << "f4 = f1/f2 = "; f4.display(); cout << endl;
f4 = f1.opposite();
cout << "f4 = -f1 = "; f4.display(); cout << endl;
return 0;
}
```
输出结果为:
```
f1 = 3/5
f2 = -3/5
f3 = -3/7
f4 = 0
f5 = 8
f1 > f2
f2 < f3
f1 == f1
f4 = f1+f3 = -6/35
f4 = f1-f2 = 6/5
f4 = f1*f3 = -9/35
f4 = f1/f2 = -1
f4 = -f1 = -3/5
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)