C++基础题目_SECE_OJ_C++_2019级_001

6 篇文章 0 订阅

1.int整数变化

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm>
//输入一个int型整数,按照从右到左的顺序阅读,返回一个不含重复数字的新的整数
//例如:输入37472338   输出83274
using namespace std;
int main()
{
	int n, temp;
	vector<int> num;
	while (cin >> n) {
		int sum = 0;
		while (n) {
			temp = n % 10;
			if (find(num.begin(), num.end(), temp) == num.end()) {
				num.push_back(temp);
				sum = sum * 10 + temp;
			}
			n = n / 10;
		}
		cout << sum << endl;
	}
	system("pause");
}

2.输出双栏列表

在这里插入图片描述
在这里插入图片描述

3.类与对象,拷贝构造函数,运算符重载

在这里插入图片描述
在这里插入图片描述

//默认构造函数
Str::Str() {
	length = 0;
	data = {};
	//this->data=new char('\0');
}

Str::Str(const char* c_str) {   //important
	//data = c_str;
	//length = strlen(c_str) + 1;  //Don't write code like this!!!
	data = new char[strlen(c_str)+1];
	//for (int i = 0; i < length; i++) {
	//	data[i] = c_str[i];
	//}
	strcpy(data, c_str);		//It's your problem!!!
}

Str::Str(const Str& s) {
	//length = s.length;
	//data = s.data;
	length = s.length;
	data = new char[strlen(s.data)+1];
	strcpy(data, s.data);
	/*for (int i = 0; i < length; i++) {
		data[i] = s.data[i];
	}*/
}

Str::~Str() {
	delete data;
	data = nullptr;
}

int Str::len()const {
	return length;
}

Str& Str::operator=(const Str& s) {
	/*length = s.length;
	data = new char[length+1];
	for (int i = 0; i < length; i++) {
		data[i] = s.data[i];
	}
	return *this;*/
	length = s.length;
	data = new char[length + 1];
	data = s.data;
	return *this;
}

Str Str::operator+(const Str& s) const {
	Str a;
	a.data = new char[strlen(this->data)+strlen(s.data)+1];
	a.length = this->length + s.length;
	strcpy(a.data, this->data);
	a.data=strcat(a.data, s.data);
	return a;
}

ostream& operator<<(ostream& out, const Str& s) {
	out << s.data;
	return out;
}

4.多态

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//以Array为基类,设计派生类Vector和Matrix,并在Vector和Matrix里重写print()虚函数,以实现多态
class Vector: public Array {//表示向量
	int weidu, vector[100];
public:
	virtual void print();
	Vector(int n, int *arr);
};
Vector::Vector(int n, int *arr) {
	weidu = n;
	for (int i = 0; i < n; i++) {
		vector[i] = *arr;
		arr++;
	}
}
inline void Vector::print() {
	cout << "(";
	for (int i = 0; i < weidu-1; i++) {
		cout << vector[i] << ",";
	}
	cout << vector[weidu - 1] << ")" << endl;
}
class Matrix: public Array {//矩阵
	int row, col, matrix[100];
public:
	Matrix(int r, int c, int *mat);
	virtual void print();
};
Matrix::Matrix(int r, int c, int *mat) {
	row = r;
	col = c;
	for (int i = 0; i < r*c; i++) {
		matrix[i] = *mat;
		mat++;
	}
}
inline void Matrix::print() {
	cout << "[";
	for (int i = 0; i < row-1; i++) {
		cout << "[";
		for (int j = 0; j < col-1; j++) {
			cout << matrix[i*col + j] << ",";
		}
		cout << matrix[i*col + col - 1] << "]" << endl;
	}
	cout << "[";
	for (int k = 0; k < col - 1; k++) {
		cout << matrix[(row - 1)*col + k] << ",";
	}
	cout << matrix[(row - 1)*col + col - 1] << "]]";
}
#include <iostream>//重写虚函数实现多态  2020/12/29
using namespace std;
class Array {//已经设计好了一个数组类,名为Array
protected:
	int *data;
public:
	virtual void print() = 0;//纯虚函数
	~Array() { delete[] data; }
};
//以Array为基类,设计派生类Vector和Matrix
//并在Vector和Matrix里重写print()虚函数,以实现多态
class Vector: public Array {//表示向量
	int weidu, vector[100];
public:
	virtual void print();
	Vector(int n, int *arr);
};
Vector::Vector(int n, int *arr) {
	weidu = n;
	for (int i = 0; i < n; i++) {
		vector[i] = *arr;
		arr++;
	}
}
inline void Vector::print() {
	cout << "(";
	for (int i = 0; i < weidu-1; i++) {
		cout << vector[i] << ",";
	}
	cout << vector[weidu - 1] << ")" << endl;
}
class Matrix: public Array {//矩阵
	int row, col, matrix[100];
public:
	Matrix(int r, int c, int *mat);
	virtual void print();
};
Matrix::Matrix(int r, int c, int *mat) {
	row = r;
	col = c;
	for (int i = 0; i < r*c; i++) {
		matrix[i] = *mat;
		mat++;
	}
}
inline void Matrix::print() {
	cout << "[";
	for (int i = 0; i < row-1; i++) {
		cout << "[";
		for (int j = 0; j < col-1; j++) {
			cout << matrix[i*col + j] << ",";
		}
		cout << matrix[i*col + col - 1] << "]" << endl;
	}
	cout << "[";
	for (int k = 0; k < col - 1; k++) {
		cout << matrix[(row - 1)*col + k] << ",";
	}
	cout << matrix[(row - 1)*col + col - 1] << "]]";
}
void print(Array *p) {
	p->print();
}
int main() {
	int n, r, c;
	//n是向量的维数,矩阵有r行c列
	cin >> n >> r >> c;
	int* arr = new int[n];
	int* mat = new int[r*c];

	for (int i = 0; i < n; i++)
		cin >> arr[i];

	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
			cin >> mat[i*c + j];

	Array *v = new Vector(n, arr);
	Array *m = new Matrix(r, c, mat);

	delete[] arr;
	delete[] mat;

	print(v);
	print(m);

	delete v;
	delete m;
	system("pause");
}

5.文件IO

在这里插入图片描述

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    char c;
    int count=0;
    freopen("in.txt", "r", stdin);   //输入重定向,输入数据将从in.txt文件中读取
    freopen("out.txt", "w", stdout); //输出重定向,输出数据将保存在out.txt文件中
    while (cin >> c)
    {
        if(c=='a'){
         count++; 
        }
    }
    cout<<count;
    fclose(stdin);  //关闭文件
    fclose(stdout); //关闭文件
    return 0;
}

6.Stack with Template

//test.cpp:
//C++第12次作业Stack with Template
#include <iostream>
#include <string>
#include <sstream>
#include <exception>

using namespace std;

class STLForbidden : public exception {
  virtual const char *what() const throw() {
    return "Please do not use std::sort or std::list or std::vector .....";
  }
};

class Job {
    public:
        explicit Job(int pri = 0) {
            id = number++;
            priority = pri;
        }
        string toString() {
            stringstream ss;
            ss << "[" << id << ":" << priority << "]";
            return ss.str();
        }
    private:
        static int number;
        int id;
        int priority;
};

int Job::number = 0;

//stack.hpp && stack.cpp:
template<typename T>
class Stack {
    public:
        Stack() {
            top_node = NULL;
            node_num = 0;
        }
        Stack(const Stack &stack) {
            top_node = NULL;
            node_num = 0;
            Node* temp = stack.top_node;
            int N = stack.node_num;
            T arr[N];
            for (int i = 0; i < N; i++) {
                arr[N - i - 1] = temp->element;
                temp = temp->next;
            }
            for (int i = 0; i < N; ++i) {
                push(arr[i]);
            }
        }
        ~Stack() {clear();}
        bool empty() {
            return node_num == 0;
        }

        int size() const {return node_num;}

        T top() const {return top_node->element;}

        void push(T element) {
            if (top_node == NULL) {
                top_node = new Node(element);
                node_num++;
            } else {
                Node* current = new Node(element);
                current->next = top_node;
                top_node = current;
                node_num++;
            }
        }

        void pop() {
            if (top_node) {
                Node* temp = top_node;
                top_node = top_node->next;
                delete temp;
                node_num--;
            }
        }

        void swap(Stack& stack) {
            Node *temp = top_node;
            top_node = stack.top_node;
            stack.top_node = temp;
        }
        /*reverse the elements' order in the stack*/
        void reverse() {
            int N = node_num;
            T arr[N];
            for (int i = 0; i < N; ++i) {
                arr[i] = top();
                pop();
            }
            for (int i = 0; i < N; ++i) {
                push(arr[i]);
            }
        }

        void clear() {
            while (node_num) {
                pop();
            }
        }
    private:
        struct Node {
            T element;
            Node* next;
            Node(T ele, Node *n = NULL) {
                element = ele;
                next = n;
            }
        };
        Node *top_node;
        int node_num;
};

template<typename T>
void print(Stack<T> stack) {
    while (!stack.empty()) {
        cout << stack.top() << " ";
        stack.pop();
    }
    cout << endl;
}

int main() {

// ignore it
//#if defined(_GLIBCXX_ALGORITHM) || defined(_GLIBCXX_LIST) || \
    defined(_GLIBCXX_VECTOR) || defined(_GLIBCXX_DEQUE) || \
    defined(_GLIBCXX_STACK)
    // throw AlgorithmnForbidden();
    //throw STLForbidden();
//#endif

    // testing -> integer..
    Stack<int> stk;
    int m, n;
    cin >> m >> n;
    for (int i = 0; i < m; i++) stk.push(i + 0.01);
    for (int i = 0; i < n; i++) stk.pop();

    Stack<int> stk0(stk);

    if (!stk.empty()) cout << stk.top() << endl;
    cout << "The size is: " << stk.size() << endl;
    if (stk.empty()) cout << "The stack is empty!" << endl;
    else cout << "The stack is NOT empty!" << endl;

    // testing -> Stack(const &stack);
    if (!stk0.empty()) cout << "The top of Stack is: " << stk0.top() << endl;
    cout << "The size is: " << stk0.size() << endl;
    print(stk0);
    if (!stk0.empty()) cout << "The top of Stack is: " << stk0.top() << endl;
    cout << "The size is: " << stk0.size() << endl;

    // testing -> reverse()
    stk0.reverse();
    cout << "Result of reversing is: ";
    print(stk0);

    // testing -> double..
    Stack<double> stk1;
    cin >> m >> n;
    for (int i = 0; i < m; i++) stk1.push(i + 0.01);
    for (int i = 0; i < n; i++) stk1.pop();
    if (!stk1.empty()) cout << stk1.top() << endl;

    cout << "The size is: " << stk1.size() << endl;
    stk1.clear();
    cout << "The size is: " << stk1.size() << endl;

    if (stk1.empty()) cout << "The stack is empty!" << endl;
    else cout << "The stack is NOT empty!" << endl;

    // testing -> user defined class..
    cin >> m >> n;
    Stack<Job> stk2;
    for (int i = 0; i < m; i++) stk2.push(Job(i));
    for (int i = 0; i < n; i++) stk2.pop();

    if (!stk2.empty()) cout << stk2.top().toString() << endl;
    cout << "The size is: " << stk2.size() << endl;
    if (stk2.empty()) cout << "The stack is empty!" << endl;
    else cout << "The stack is NOT empty!" << endl;

    // testing -> swap function..
    Stack<int> stk3, stk4;
    for (int i = 0; i < m; i++) stk3.push(i);
    for (int i = 0; i < m; i++) stk4.push(m - i);

    cout << "Before swap...." << endl;
    print(stk3);
    print(stk4);

    stk3.swap(stk4);

    cout << "After swap...." << endl;
    print(stk3);
    print(stk4);

	system("pause");
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SmallC1oud

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值