
C++
爱听音乐的孩纸
ITtochat.com
展开
-
使用goto语句构成的循环求1~100之间的所有奇数之和,即求解表达式1+3+5+...+99之和。
#include using namespace std;int main(){ int sum,a; sum = 0; a = 1;loop: sum = sum+a; a = a+2; if (a100) goto loop;//goto语句跳转 cout"1~100内的所有奇数之和为:"原创 2014-03-01 11:27:14 · 6609 阅读 · 1 评论 -
参数类型不同的函数重载(C++)
#include using namespace std;int max(int,int);float max(float,float);double max(double,double);int main(){coutint ia=10,ib=20,iresult;iresult=max(ia,ib);coutcoutcoutflo原创 2014-03-12 21:33:05 · 4951 阅读 · 0 评论 -
参数个数不同的函数载体(C++)
/*参数个数不同的函数重载是指,具有不同参数个数的函数可以使用同一个函数名,编译器在调用该函数的根据实际参数的个数判断应该调用*/ #include using namespace std;int max(int a,int b);/*声明带有2个参数的函数max()*/int max(int a,int b,int c);/*声明带有3个参数的函数max()*/原创 2014-03-14 21:24:02 · 2533 阅读 · 0 评论 -
动态内存分配(C++)
/*在C++中,通过关键字new和delete来实现程序的动态内存分配和回收。其中关键字new实现内存分配,如果需要对分配出的内存进行初始化,则在类型后面加上一个括号,并带有初始值。因此,C++中动态分配内存的一般形式如下:类型标示符 *指针名 = new 类型标示符(初始值)*/#include using namespace std;int main()原创 2014-03-17 09:28:39 · 1093 阅读 · 0 评论 -
自定义拷贝构造函数(C++语言)
/*拷贝构造函数的作用是:用一个已经存在的对象来初始化该类型的新对象。用户可以根据需要,自定义拷贝构造函数。自定义的拷贝构造函数的形式为:类名(类名 &对象名){ 函数体}其中,&对象名表示对一个对象的引用。例如,下面程序实现一个用户自定义的拷贝构造函数。代码如下:*/#includeusing namespace std;cl原创 2014-03-27 15:41:47 · 1935 阅读 · 0 评论 -
继承的访问控制_私有继承(C++语言)
/*在私有继承中,派生类以私有方式继承*/原创 2014-04-19 18:10:56 · 1073 阅读 · 0 评论 -
继承的访问控制_公有继承(C++语言)
/*在公有继承中,基类成员的*/原创 2014-04-18 16:43:19 · 925 阅读 · 0 评论 -
继承的访问控制_保护继承_保护成员的声明(C++语言)
/*保护成员的声明,保护成员用关键 */原创 2014-04-20 21:31:34 · 1279 阅读 · 0 评论 -
继承的访问控制_保护继承_保护继承(C++语言)
在保护继承中,基类的原创 2014-04-21 11:34:41 · 1203 阅读 · 0 评论 -
C++中随机函数rand()和srand()的用法(函数讲解)
一、rand() 函数名: rand 功 能: 随机数发生器 用 法: int rand(void); 所在头文件: stdlib.h 函数说明 : rand()的内部实现是用线性同余法做的,它不是真的随机数,因其周期特别长,故在一定 的范围里可看成转载 2014-05-10 22:38:53 · 6815 阅读 · 0 评论 -
MFC获取当前时间
void CTest17GetTimeDlg::OnGetTime() { // TODO: 在此添加控件通知处理程序代码 //UpdateData(true); CTime m_time; m_time=CTime::GetCurrentTime(); //获取当前时间日期 m_strDate=m_time.转载 2014-11-25 19:20:29 · 1998 阅读 · 0 评论 -
windows下利用文件映射实现共享内存
windows下利用文件映射实现共享内存的办法比较简单,下面是实现代码,细节用注释说明.调用类似linux下shm的操作.该类没有进行太多的测试,欢迎提出问题和bug~~:)#include #include #include #include using std::string;using std::cout;using std::endl;#pragma warning(di转载 2014-12-01 09:59:22 · 2371 阅读 · 0 评论 -
vc++,MFC 中,用ado执行sql server语句时,并且对返回值真假判断的例子,即bool类型
MFC VC++ 中的对数据库中的表进行查询,判断SQL语句是否执行成功若存在返回bool值true(1),若失败返回bool值false(0)。简单bool类型代码如下:CString sql,Result;sql.Format(_T("select * from [dbo].[%s]"), Name);// Name是所要查找数据库中表的名字BOOL bool_temp =原创 2014-12-06 10:50:12 · 3517 阅读 · 1 评论 -
WideCharToMultiByte与MultiByteToWideChar转化
#include "stdafx.h"int UnicodeToChar(char *Unicode, char *ASCText, unsigned short UnicodeLength){ int nwlen = 0; int tlength = WideCharToMultiByte(CP_ACP,0,LPCWSTR(Unicode),UnicodeLength,0,0,0,FA原创 2014-12-06 12:34:17 · 1001 阅读 · 0 评论 -
实现当前文件的路径(C++语言)
#include using namespace std;int main(int argc,char *argv[]){ int i;coutfor(i=1;i{cout}return 0; }原创 2014-03-07 12:41:19 · 1199 阅读 · 1 评论 -
判断用户输入的年份是否是闰年,并在主函数main()中调用该函数(C++语言)
#include <iostream>using namespace std;bool isLeapYear(int year){ if((0==year%4)&&(0!=year%100)) { return true; } if(0==year%400) { return true; } return 0;} int main(){ int year; bool flag; cout<<"--判断润年--"<<endl; cout<<原创 2014-03-05 20:59:44 · 10100 阅读 · 0 评论 -
折半查找的函数实现(C++)
/*折半查找的前提条件是数组中的元素是顺序的,即数组元素须按值升序排列或降序排列*/ #include //折半查找的函数实现 using namespace std;int search(int array[],int n,int key){int mid,low=0,high=n-1;//定义初始位置和结束为止并初始化 while(low//开始折半查找{mid原创 2014-03-12 13:57:49 · 2157 阅读 · 0 评论 -
控制输出精度
#include #include using namespace std;/*对比A1和A2、B1和B2、C1和C2的输出结果,观察出“fixed”和“setprecision(int x)”的用法*//*“fixed”和“setprecision(int x)”用一次,对下面的程序都起作用*/int main(){ const double PI=3.原创 2014-02-27 20:12:00 · 1018 阅读 · 0 评论 -
使用格式控制符进行输出格式的设置(讲解)
#include #include using namespace std;int main(){ cout"第一章" cout" ";//起始空格,以便下边函数的使用(向左对齐,好看) cout.setf(ios::left);//设置对齐方式为left(范围为7的宽度,只对"cout.width(7原创 2014-02-26 20:57:23 · 2583 阅读 · 0 评论 -
杨辉三角的输出(C++语言)
#include using namespace std;int main(){ int n; cout"请输入行数:"; cin>>n; int a[20]={0},b[20]={0}; for (int i=0; i { b[0]=1; b[i]=1; for (int原创 2014-03-01 21:10:18 · 2211 阅读 · 0 评论 -
输入5个同学的姓名,将这些姓名按照字符串的大小进行升序排列并输出(C++语言)
#include using namespace std;int main(){ char str[20],name[5][20]; int i,j,p; cout"亲输入5个学生的姓名:" for (i=0; i5; i++) { gets(name[i]); } cout"姓名升序排列如下:"原创 2014-03-01 21:37:50 · 15203 阅读 · 2 评论 -
控制不同进制的输出
#include #include using namespace std;int main(){ int a; cout"请输入一个十进制数:"; cin>>a; coutendl"输出:" cout"系统默认进制:" cout"十进制:"endl; cout"八进制:"endl原创 2014-02-26 21:17:21 · 975 阅读 · 0 评论 -
打印一个九九乘法表
#include using namespace std;int main(){ int bcs,cs; for (bcs = 1; bcs9; bcs++) { for (cs = 1; cs { cout'*''='' '; } coutendl;原创 2014-03-01 12:08:10 · 1392 阅读 · 0 评论 -
顺序结构的中和应用(讲解)
#include #include using namespace std;int main(){ const double PI=3.1415926; cout.flags(ios::right);//cout.flags取当前状态标志向左还是向右 coutsetw(10)endl; cout.fill('*'); cout.wi原创 2014-02-27 21:36:58 · 818 阅读 · 0 评论 -
菱形的输出(C++语言)
#include using namespace std;int main(){ int i,j,m,n; cout" *"endl; for (i=1; i7; i++) { for (j = 0; j6-i; j++) { cout" "; }原创 2014-03-01 19:39:57 · 5295 阅读 · 0 评论 -
用辗转相除法求两个数的最大公约数和最小公倍数(什么是辗转相除法,讲解)(C++语言)
/*什么是辗转相除法呢?比如求x,y的最大公约数,先讨论(x%y)的余数b是否等于0,如果是,则y为最大公约数;否则将y的值赋给x,把b的值赋给y然后再继续对b讨论,一直循环下去直到b=0时,y值就是所求的最大公约数了。其实现代码如下:*/#include using namespace std;int main(){ int a,原创 2014-03-01 15:59:39 · 4298 阅读 · 0 评论 -
简单程序结构体实例的展现(C++语言)
#include #include using namespace std;int main() {int i;struct student_rec{int number;float scores[5];}; struct student_rec student1,student2;coutcin>>student原创 2014-03-03 17:19:21 · 1543 阅读 · 0 评论 -
传递多维数组参数(C++)
#include using namespace std;float average(float array[][3],int n,int m);int main(){float array[3][3],aver;coutaver=average(array,3,3);return 0;} float average(float array[][3],int原创 2014-03-11 09:30:24 · 1056 阅读 · 0 评论 -
实例化函数模版(C++)
#include using namespace std;template T max(T a,S b) //定义函数模块 {return a>b?a:b; //返回两个数中较大的一个 }int main(){coutint ia=10,ib=20,iresult;ir原创 2014-03-12 16:32:33 · 914 阅读 · 0 评论 -
用递归函数求Fibonacci数列中的第n个数(C++语言)
/*(描述)无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为F(n)=1 ...........(n=1或n=2)F(n)=F(n-1)+F(n-2).....(n>2)现要你来求第n个斐波纳奇数。(第1个、第二个都为1)(输入)第一行是一个整数m(m每次测试数据只有一行,且只有一个整形数n(n(输出)对每组输入n,原创 2014-03-06 21:40:17 · 12098 阅读 · 0 评论 -
C++文件简单的读写操作实例
#include #include using namespace std;void main(){ // 写操作 ofstream in; in.open("com.txt", ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建 if (in.fail()) { cout << "ERROR!"; } in <原创 2014-12-06 15:41:33 · 1205 阅读 · 0 评论