您的位置:首页 > 编程语言 > C语言/C++

C++学习笔记之封装程序记录

2019-03-07 21:28 351 查看
[code]//用C语言进行日期编写
#include<iostream>
using namespace  std;

struct Date   //数据的封装
{
int year;
int month;
int day;
};

/*对数据进行处理*/
void init(Date &d)
{
cout << "year month day" << endl;
cin >> d.year >> d.month >> d.day;
}

void print(Date &d)
{
cout << d.year << "/" << d.month << "/" << d.day << endl;
}

bool isLeapYear(Date &d)
{
if (d.year % 4 == 0 && d.year % 100 != 0 || d.year % 400 == 0)
return true;
else
return false;
}
int main()
{
Date d;
init(d);
print(d);
if (isLeapYear(d))
cout << d.year << "is leap year" << endl;
else
cout << d.year << "is not leap year" << endl;
return 0;
}
[code]//用C++语言类编写日期
#include<iostream>

using namespace std;

class Date
{
public:
void init()
{
cout << "year month day" << endl;
cin >> year >> month >> day;
}
void show()
{
cout << year << "/" << month << "/" << day << endl;
}
bool IsleapYear()
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
}
private:
int year;
int month;
int day;
};
int main()
{
Date d;
d.init();
d.show();
if (d.IsleapYear())
cout << "is leap year" << endl;
else
cout << "is not leap year" << endl;

return 0;
}
[code]//另一种用C++语言表达日期方式, 该写法便于进行多文件编程
#include<iostream>

using namespace std;

class Date     //类名相当于命名空间
{
public:
void init();
void show();
bool IsleapYear();
private:
int year;
int month;
int day;
};
void Date::init()
{
cout << "year month day" << endl;
cin >> year >> month >> day;
}
void Date::show()
{
cout << year << "/" << month << "/" << day << endl;
}
bool Date::IsleapYear()
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
}
int main()
{
Date d;
d.init();
d.show();
if (d.IsleapYear())
cout << "is leap year" << endl;
else
cout << "is not leap year" << endl;

return 0;
}

 

[code]//对列表进行封装
#include<iostream>

using namespace std;

typedef struct node
{
int data;
struct node * next;
}Node;

class List
{
public:
void createlist();
void insertList(int data);
void traverseList();

private:
Node * head;
};

void List::createlist()
{
head = new Node;
head->next = NULL;
}
void List::insertList(int data)
{
Node * cur = new node;
cur->data = data;

cur->next = head->next;
head->next = cur;
}
void List::traverseList()
{
Node * t = head->next;
while (t)
{
cout << t->data << endl;
t = t->next;
}
}
int main()
{
List L;
L.createlist();
L.insertList(1);
L.insertList(2);
L.insertList(3);
L.insertList(4);
L.insertList(5);
L.traverseList();

return 0;
}

 

[code]//C语言应用结构体,函数构造栈
#include<iostream>

using namespace std;

struct Stack
{
char buf[1024];
int top;
};
void init(Stack &s)
{
memset(s.buf, 0, 1024);
s.top = 0;
}
void push(Stack &s,char  ch)
{
s.buf[s.top++] = ch;
}
char pop(Stack &s)
{
return s.buf[--s.top];
}
bool IsEmpty(Stack &s)
{
return s.top == 0;
}
bool IsFull(Stack &s)
{
return s.top == 1024;
}
int main()
{
Stack s;
init(s);
if (!IsFull(s))
push(s, 'd');
if (!IsFull(s))
push(s, 'c');
if (!IsFull(s))
push(s, 'b');
if (!IsFull(s))
push(s, 'a');

while (!IsEmpty(s))
cout << pop(s) << endl;

return 0;
}
[code]//用C++类封装栈
#include<iostream>

using namespace std;

class Stack
{
public:
void init()
{
memset(buf, 0, 1024);
top = 0;
}
void push(char ch)
{
buf[top++] = ch;
}
char pop()
{
return buf[--top];
}
bool IsEmpty()
{
return top == 0;
}
bool IsFull()
{
return top == 1024;
}
private:
char buf[1024];
int top = 0;
};
int main()
{
Stack s;

s.init();

if (!s.IsFull())
s.push('e');
if (!s.IsFull())
s.push('f');
if (!s.IsFull())
s.push('g');
if (!s.IsFull())
s.push('h');

while( !s.IsEmpty())
cout << s.pop()<< endl;
return 0;
}

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: