11 2011 档案
C++的cin/cout高阶格式化操作
摘要:这篇文章主要讲解如何在C++中使用cin/cout进行高级的格式化输出操作,包括数字的各种计数法(精度)输出,左或右对齐,大小写等等。通过本文,您可以完全脱离scanf/printf,仅使用cin/cout来完成一切需要的格式化输入输出功能(从非性能的角度而言)。更进一步而言,您还可以在<sstream>、<fstream>上使用这些格式化操作,从而代替sscanf/sprintf和fscanf/fprintf函数。为方便描述,下文仅以cin/cout为例进行介绍。一、综述cin/cout是STL库提供的一个iostream实例,拥有ios_base基类的全部函数和成员 阅读全文
posted @ 2011-11-27 15:19 Gavin Dai 阅读(663) 评论(0) 推荐(0)
用循环链表解决Josehus问题
摘要:#include <iostream>using namespace std;struct CirLinkNode{int data;struct CirLinkNode *link;CirLinkNode(CirLinkNode *next = NULL):link(next) { }CirLinkNode(int d, CirLinkNode *next = NULL):data(d), link(next) { }};class CircList{public:CircList() { head = NULL; }void create();CirLinkNode* getH 阅读全文
posted @ 2011-11-26 17:37 Gavin Dai 阅读(266) 评论(0) 推荐(0)
输放输出格式控制
摘要:#include <iostream>using namespace std;int main(){int a=21;cout.setf(ios::showbase);cout<<"dec:"<<a<<endl;cout.unsetf(ios::dec);cout.setf(ios::hex);cout<<"hex:"<<a<<endl;cout.unsetf(ios::hex);cout.setf(ios::oct);cout<<"oct:&quo 阅读全文
posted @ 2011-11-15 18:42 Gavin Dai 阅读(231) 评论(0) 推荐(0)
自加运算符的前置与后置的重载
摘要:#include <iostream>using namespace std;class Time{public: Time(){minute=0;sec=0;} Time(int m,int s):minute(m),sec(s){} Time operator++(); Time operator++(int); void display(){cout<<minute<<":"<<sec<<endl;} private: int minute; int sec;};Time Time::operator++() 阅读全文
posted @ 2011-11-15 16:57 Gavin Dai 阅读(362) 评论(0) 推荐(0)
运算符重载
摘要:#include <iostream>#include <string>using namespace std;class String{public: String(){p=NULL;} String(char *str); friend bool operator>(String &string1,String &string2); friend bool operator<(String &string1,String &string2); friend bool operator==(String &strin 阅读全文
posted @ 2011-11-15 16:32 Gavin Dai 阅读(271) 评论(0) 推荐(0)
子类对象赋值给父类引用
摘要:#include <iostream>using namespace std;class Point{public:Point(float=0,float=0);void setPoint(float,float);float getX() const {return x;}float getY() const {return y;}friend ostream & operator<<(ostream &,const Point &);protected:float x,y;};Point::Point(float a,float b){x=a 阅读全文
posted @ 2011-11-14 19:46 Gavin Dai 阅读(2097) 评论(0) 推荐(0)
字符串比较
摘要:#include <iostream>#include <string>using namespace std;int main(){ void max_string(char str[][30],int i);int i;char country_name[3][30];for(i=0;i<3;i++)cin>>country_name[i];max_string(country_name,3);return 0;}void max_string(char str[][30],int n){int i;char string[30];strcpy(s 阅读全文
posted @ 2011-11-13 22:37 Gavin Dai 阅读(188) 评论(0) 推荐(0)
二分法解方程
摘要:#include <cmath>#include <iostream>using namespace std;float f(float x){return x * x * x - 5 * x *x + 16 * x - 80;}void main(void){float x1, x2, x0, f0, f1, f2;do {cout<<"Input x1, x2\n";cin>>x1>>x2;f1 = f(x1);f2 = f(x2);} while (f1 * f2 > 0);do {x0 = ( x1 阅读全文
posted @ 2011-11-13 22:01 Gavin Dai 阅读(264) 评论(0) 推荐(0)
一个类的成员函数做为另一个类的友元函数
摘要:#include <iostream>using namespace std;class Date;class Time{public:Time(int,int,int);void display(const Date&);private:int hour;int minute;int sec;};class Date{public:Date(int,int,int);friend void Time::display(const Date &);private:int month;int day;int year;};Time::Time(int h,int m, 阅读全文
posted @ 2011-11-13 21:36 Gavin Dai 阅读(3418) 评论(0) 推荐(0)
指向类成员函数的指针
摘要:#include <iostream>using namespace std;class Time{public:Time(int,int,int);int hour;int minute;int sec;void get_time();};Time::Time(int h,int m,int s){hour=h;minute=m;sec=s;}void Time::get_time(){cout<<hour<<":"<<minute<<":"<<sec<<endl;}i 阅读全文
posted @ 2011-11-13 20:27 Gavin Dai 阅读(275) 评论(0) 推荐(0)
函数的默认参数与内联函数
摘要:函数分声明和定义,但是如果声明和定义是在一些起的,相当于没有声明的话,默认参数与inline就只要写在定义中就可以了。但是如果函数如果先声明后定义的话,默认参数只能写在声明中,定义时不能再写上默认参数。inline在声明中写,定义时可写可不写. 阅读全文
posted @ 2011-11-13 15:10 Gavin Dai 阅读(293) 评论(0) 推荐(0)
数据结构(殷人琨版)学习笔记之单链表
摘要:/**************************************************************************** file name : LinkList.h* created : 2011/11/03* description : * author : Gavin Dai XLX* update :****************************************************************************/#ifndef __LINKLIST_H#define __LINKLIST_H#include &l 阅读全文
posted @ 2011-11-03 20:16 Gavin Dai 阅读(376) 评论(0) 推荐(0)
数据结构之单链表
摘要:/**************************************************************************** file name : LinkNode.h* created : 2011/11/02* description : * author : Gavin Dai XLX* update :****************************************************************************/#ifndef __LINKNODE_H#define __LINKNODE_H#include &l 阅读全文
posted @ 2011-11-03 11:12 Gavin Dai 阅读(249) 评论(0) 推荐(0)
数据结构(殷人琨版)学习笔记之顺序表
摘要:把书上的伪代码整理了一下:/**************************************************************************** file name : LinearList.h* created : 2011/11/01* description : * author : Gavin Dai XLX* update :****************************************************************************/#ifndef __LINEARLIST_H#define __LINE 阅读全文
posted @ 2011-11-02 12:41 Gavin Dai 阅读(424) 评论(0) 推荐(0)
数据结构之顺序表
摘要:最近在找工作,复习一些基础知识,所以想把以前学过的东西总结一下。在华为面试的时候被问到顺序表的优点,当然主要是与链表相对而言:1)随机访问,存储密度大。2)逻辑上相邻的物理上也相邻.顺序表的缺点:1)插入删除时不方便,要移动大量元素.实现代码:/**************************************************************************** file name : SeqList.h* created : 2011/11/01* description : * author : Gavin Dai XLX* update :******* 阅读全文
posted @ 2011-11-01 18:38 Gavin Dai 阅读(1225) 评论(0) 推荐(0)