因为作者平时不使用C++进行编程,但是课程刷分无奈又学习一遍,这样其实只要跟随老师的脚步一步一步进行学习和实验就可以了(ps:第一次没好好学,只拿了73分)。所以初期关于C++的学习大多OJ编程作业上进行,所以内容比较关键但却又比较浅显,有需要的读者凑合着看即可。
//
// main.cpp
// CppCP3_1
//
// Created by Chenjun Xiong on 2017/3/16.
// Copyright © 2017年 Chenjun Xiong. All rights reserved.
//
//题目描述
//(Circle类)一个圆形的旱冰场地,场地内抹水泥,造价为每平方米20元,围栏用木条围成,每米造价35元。设计一个Circle类,可以求出圆的面积和边长,进而支持求出旱冰场的等价。请在下面提示的基础上完成程序,其中需要做的工作包括:(1)在类声明中声明相关的公共成员函数;(2)在类声明和main()函数之间,定义声明的成员函数。PI采用3.1415926
//
//输入描述
//请输入旱冰场的半径
//
//输出描述
//旱冰场的造价,小数点后保留2位
//
//输入样例
//5.5
//
//输出样例
//3110.18
#include
#include
using namespace std;
double PI=3.1415926;
class Circle{
private:
double radio;
public:
Circle(double r=0);
void SetRadio(double r=0);
double GetPerimeter();
double GetArea();
double GetPrice();
};
Circle::Circle(double r){
radio=r;
}
void Circle::SetRadio(double r){
radio=r;
};
double Circle::GetArea(){
return PI*radio*radio;
}
double Circle::GetPerimeter(){
return PI*radio*2;
}
double Circle::GetPrice(){
return 20*GetArea()+35*GetPerimeter();
}
int main(int argc, const char * argv[]) {
double radio;
Circle circle;
while(cin>>radio){
circle.SetRadio(radio);
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << circle.GetPrice() << endl;
}
return 0;
}
#include
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << circle.GetPrice() << endl;
//
// main.cpp
// CppCP3_2
//
// Created by Chenjun Xiong on 2017/3/16.
// Copyright © 2017年 Chenjun Xiong. All rights reserved.
//
//输入描述
//分别构造两个空栈,再读入若干对整数v、x; 1<=v<=2; 将元素x入第v个栈 。
//
//输出描述
//最后将两个栈中元素出栈,并输出;每个栈中元素占一行,元素间以空格分隔。
//
//输入样例
//1 100
//2 200
//1 300
//2 400
//1 50
//1 60
//2 80
//
//输出样例
//60 50 300 100
//80 400 200
#include
#include
using namespace std;
typedef struct linknode{
int num;
linknode *next;
}linknode;
class stack{
private:
struct linknode *top;
int size;
public:
stack(){
top=new struct linknode;
top->num=-1;
top->next=NULL;
size=0;
};
//stack(const stack& s);
void Push(int data){
linknode *newnode=new struct linknode;
newnode->num=data;
newnode->next=top;
size++;
top=newnode;
};
void Pop(){
if(size==0){
return;
}
linknode *prenode=top->next;
delete top;
top=prenode;
size--;
};
int GetTop(){
return top->num;
};
bool IsEmpty(){
if(top->next==NULL){
return 1;
}
return 0;
}
};
int main(int argc, const char * argv[]) {
int v,x;
stack s1,s2;
//int n;
//cin>>n;
//for(int i=0;i
while(cin>>v>>x){
if(v==1){
s1.Push(x);
}else{
s2.Push(x);
}
}
while(!s1.IsEmpty())
{
cout<" " ;
s1.Pop();
}
cout<while(!s2.IsEmpty())
{
cout<" ";
s2.Pop();
}
cout<return 0;
}
linknode *newnode=new struct linknode;
delete top;
//
// main.cpp
// CppCP3_3
//
// Created by Chenjun Xiong on 2017/3/16.
// Copyright © 2017年 Chenjun Xiong. All rights reserved.
//
//题目描述
//请设计实现集合类,元素类型为整形。该集合类支持集合元素增加、删除、查询;并支持集合并、交、差运算;利用你设计的集合类,实现本题要求。程序应体现面向对象程序设计思想,结构合理。为保证结果唯一,集合元素递增排列。假设集合元素个数不超100个
//
//输入描述
//开始为两个正整数m,n;后续m个整数构成集合A,再后续n个整数构成集合B
//
//输出描述
//集合A、B和他们的并、交、差集;每个集合元素间以,分隔;不同集合显示在不同行
//
//输入样例
//3 5
//1 2 3
//3 5 8 2 1
//
//输出样例
//{1,2,3}
//{1,2,3,5,8}
//{1,2,3,5,8}
//{1,2,3}
//{}
#include
using namespace std;
class Vector{
private:
int *num;
int length;
public:
Vector();
void Insert(int ele);
void Delete(int ele);
int GetElement(int pos);
bool ExistElement(int ele);
void Print();
Vector operator+(Vector &b);
Vector operator-(Vector &b);
Vector operator*(Vector &b);
};
Vector::Vector(){
num=new int[100];
length=0;
}
void Vector::Insert(int ele){
if(length>=100)
return;
else if(length==0){
num[0]=ele;
length++;
}else if(ele>=num[length-1]){
num[length]=ele;
length++;
}else{
for(int i=0;iif(num[i]>=ele){
for(int j=length;j>i;j--){
num[j]=num[j-1];
}
num[i]=ele;
length++;
return;
}
}
}
}
bool Vector::ExistElement(int ele){
for(int i=1;i<=length;i++){
if(ele==GetElement(i))
return true;
}
return false;
}
void Vector::Print(){
cout<<'{';
for(int i=1;i<=length;i++){
if(i==1){
cout<else{
cout<<','<cout<<'}'<void Vector::Delete(int ele){
if(length==0){
return;
}else{
for(int i=0;iif(num[i]==ele){
for(int j=i;j<=length-2;j++){
num[j]=num[j+1];
}
length--;
}
}
}
}
int Vector::GetElement(int pos){
if(pos>length||pos<1)
return NULL;
else
return num[pos-1];
}
Vector Vector::operator+(Vector &b){
Vector c;
int n=1,m=1;
while(n<=length&&m<=b.length){
if(GetElement(n)else if(GetElement(n)>b.GetElement(m)){
c.Insert(b.GetElement(m));
m++;
}else{
c.Insert(GetElement(n));
n++;m++;
}
}
while(n<=length){
c.Insert(GetElement(n));
n++;
}
while(m<=b.length){
c.Insert(b.GetElement(m));
m++;
}
return c;
}
Vector Vector::operator-(Vector &b){
Vector c;
for(int i=1;i<=length;i++){
if(!b.ExistElement(GetElement(i)))
c.Insert(GetElement(i));
}
return c;
}
Vector Vector::operator*(Vector &b){
Vector c;
for(int i=1;i<=length;i++){
if(b.ExistElement(GetElement(i)))
c.Insert(GetElement(i));
}
return c;
}
int main(int argc, const char * argv[]) {
int n,m,k;
Vector c,a,b;
while(cin>>n>>m){
while(n--){
cin>>k;
a.Insert(k);
}
while(m--){
cin>>k;
b.Insert(k);
}
a.Print();
b.Print();
c=a+b;
c.Print();
c=a*b;
c.Print();
c=a-b;
c.Print();
}
}
//类内声明
class Vector{
private:
int *num;
int length;
public:
Vector();
void Insert(int ele);
void Delete(int ele);
int GetElement(int pos);
bool ExistElement(int ele);
void Print();
Vector operator+(Vector &b);
Vector operator-(Vector &b);
Vector operator*(Vector &b);
//定义重载函数
Vector Vector::operator+(Vector &b){
Vector c;
int n=1,m=1;
while(n<=length&&m<=b.length){
if(GetElement(n)<b.GetElement(m)){
c.Insert(GetElement(n));
n++;
}else if(GetElement(n)>b.GetElement(m)){
c.Insert(b.GetElement(m));
m++;
}else{
c.Insert(GetElement(n));
n++;m++;
}
}
while(n<=length){
c.Insert(GetElement(n));
n++;
}
while(m<=b.length){
c.Insert(b.GetElement(m));
m++;
}
return c;
}
Vector Vector::operator-(Vector &b){
Vector c;
for(int i=1;i<=length;i++){
if(!b.ExistElement(GetElement(i)))
c.Insert(GetElement(i));
}
return c;
}
Vector Vector::operator*(Vector &b){
Vector c;
for(int i=1;i<=length;i++){
if(b.ExistElement(GetElement(i)))
c.Insert(GetElement(i));
}
return c;
}
};
//运算符重载后的使用
b.Print();
c=a+b;
c.Print();
c=a*b;
c.Print();
c=a-b;
c.Print();