写日志:

class LogFile
{
public:
static LogFile &instance();
operator FILE *() const { return m_file; }
private
LogFile(const char *filename)
{
m_file = fopen(filename, "a+");
}
~LogFile()
{
fclose(m_file);
}
FILE *m_file;
}; LogFile &LogFile::instance()
{
static LogFile log("AppLog.txt");
return log;
} 用的时候可以这么写:
fwrite("abc", 1, 3, LogFile::instance());

读取文件信息:

c语言实现如下功能 输入全部文件名(绝对路径加文件名)得到,文件名,扩展名,文件长度

/* MAKEPATH.C */
#include<string.h>
#include <stdlib.h>
#include <stdio.h> #define LENGTH 200 struct FILEHEAD
{
char path_buffer[LENGTH];
char filename[LENGTH];
char ext[LENGTH];
unsigned int length; }; FILEHEAD file; void getFileInformation(FILEHEAD file)
{
memset(&file,0,sizeof(file)); //_makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" );
printf( "Path created with : \n\n");
scanf("%s",file.path_buffer);
_splitpath( file.path_buffer, NULL, NULL, file.filename, file.ext ); FILE *fp= NULL;
fp=fopen(file.path_buffer,"r");
if (NULL==fp)
{
printf("cannot open the %s \n",file.path_buffer);
exit(0);
} fseek(fp,0l,SEEK_END);
file.length=ftell(fp); fclose(fp);
fp = NULL; //需要指向空,否则会指向原打开文件地址 printf( "Path extracted with _splitpath:\n" );
//printf( " Drive: %s\n", drive );
//printf( " Dir: %s\n", dir );
printf( " Filename: %s\n", file.filename );
printf( " Ext: %s\n", file.ext );
printf( " length is btye: %ld btye\n", file.length );
} int main( void )
{ getFileInformation(file); system("pause"); return 0;
}

算法排序框架:

#include <stdio.h>
#include <stdlib.h>
#include <time.h> #define TRUE 1
#define FALSE 0
#define MAX 10000 typedef int KeyType;
typedef int OtherType; typedef struct
{
KeyType key;
OtherType other_data;
}RecordType; // All kinds of seek.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "headfile.h"
#include "windows.h"
#include "conio.h " #include"WinBase.h"
#include "Psapi.h" #pragma once
#pragma message("Psapi.h --> Linking with Psapi.lib")
#pragma comment(lib,"Psapi.lib") int Data[MAX]={0}; void produceData(int a[],int length) //给数组生成数据,用于随即查找
{
time_t t;
srand(time(&t));
for (int i=0;i<length;i++)
{
a[i]=rand()%length;
} } void printData(int a[],int length) //打印数字,到控制台,每五个换一行
{
for (int i=0;i<length;i++)
{
printf("%8d",a[i]);
if (0==i%5)
{
printf("\n");
}
} } double showMemoryInfo()
{ double MemorySize; //单位MB
HANDLE handle=GetCurrentProcess(); PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(handle,&pmc,sizeof(pmc));
MemorySize=pmc.WorkingSetSize/1024; printf("内存使用: %8lf \n",MemorySize); //WorkingSetSize The current working set size, in bytes. return MemorySize; } void writeRecordtime(unsigned rTime)//将程序结果运行时间写入文件
{
FILE *fpRecord=NULL; char *s="your programm running time is: ";
char *c="ms "; if((fpRecord=fopen("record.txt","wt+"))==NULL) { printf("Cannot open file strike any key exit!"); getchar(); exit(1); } fprintf( fpRecord, "%s", s);
fprintf( fpRecord, "%d", rTime);
fprintf( fpRecord, "%s", c); fprintf( fpRecord, "\n");
fprintf( fpRecord, "your programm use %fMB size of memory!!!", showMemoryInfo()); fclose(fpRecord); } int _tmain(int argc, _TCHAR* argv[])
{
produceData(Data,MAX);
printData(Data,MAX); getchar(); return 0;
}

快速求积分办法:

// Integral-romberg方法求积分.cpp : 定义控制台应用程序的入口点。
//
/*
romberg方法求积分
方法也称为逐次分半加速法。它是在梯形公式,simpson公式和newton-cotes公式之间的关系的基础上,
构造出一种加速计算积分的方法。作为一种外推算法,它在不增加计算量的前提下提高了误差的精度。
在等距基点的情况下,用计算机计算积分值通常都采用吧区间逐次分半的方法进行。
这样,前一次分割得到的函数值在分半以后仍然可以被利用,并且易于编程。 运行结果如下:
输入:
0
3.14159
输出:Romberg- -12.0703
增加迭代次数或提高精度时,程序运行
得到的结果几乎没有什么变化。可以看到,
和Simpson方法运行的结果基本一致,但
Romberg法速度更快、精度更高 */ #include "stdafx.h" #include<iostream>
#include<math.h>
#define epsilon 0.00001
#define COUNT 100
using namespace std; double fun(double x)
{
return x*x;
} double Romberg(double a,double b)
{
int m ,n;
double h,x,s,q,ep;
double p,*R =new double[COUNT]; h=b-a;
R[0]= h*(fun(a)+ fun(b))/2.0;
m=1;
n=1;
ep=epsilon+1.0;
while ((ep >= epsilon)&& (m <COUNT))
{
p = 0.0;
{
for(int i=0;i<n;i++)
{
x = a+ (i+0.5)*h ;
p= p + fun(x);
}
p= (R[0]+ h*p)/2.0;
s = 1.0;
for(int k=1;k<=m;k++)
{
s = 4.0*s;
q= (s*p-R[k-1])/(s-1.0);
R[k-1]= p;
p =q;
}
p=fabs(q -R[m-1]);
m =m + 1;
R[m-1]= q;
n = n + n;
h = h/2.0;
}
return (q);
}
} int _tmain(int argc, _TCHAR* argv[])
{
double a,b;
cout<<"Input a,b:a为下限,b为上限"<<endl;
cin>>a>>b;
cout<<"Romberg="<<Romberg(a,b)<<endl;
system("pause");
return 0;
}

【编程练习】收集的一些c++代码片,算法排序,读文件,写日志,快速求积分等等的更多相关文章

  1. 之前写的收集的一些c++代码片,算法排序,读文件,写日志,快速求积分等等

    写日志: class LogFile { public: static LogFile &instance(); operator FILE *() const { return m_file ...

  2. 使用函数式编程消除重复无聊的foreach代码(Scala示例)

    摘要:使用Scala语言为例,展示函数式编程消除重复无聊的foreach代码. 难度:中级 概述 大多数开发者在开发生涯里,会面对大量业务代码.而这些业务代码中,会发现有大量重复无聊的 foreach ...

  3. Win 32 编程之按钮消息响应(代码小错误修复)

    最近不想用MFC写东西了,有没有安装Qt和其他图形化开发环境,只能捣鼓API了.于是乎,就有了以下的学习-- 首先,老套的创建个Windows窗口,由于自己有点小懒,就直接用Hello Word的源码 ...

  4. 018-并发编程-java.util.concurrent.locks之-ReentrantReadWriteLock可重入读写锁

    一.概述 ReentrantLock是一个排他锁,同一时间只允许一个线程访问,而ReentrantReadWriteLock允许多个读线程同时访问,但不允许写线程和读线程.写线程和写线程同时访问.相对 ...

  5. VS2010/MFC编程入门之四十五(MFC常用类:CFile文件操作类)

    上一节中鸡啄米讲了定时器Timer的用法,本节介绍下文件操作类CFile类的使用. CFile类概述 如果你学过C语言,应该知道文件操作使用的是文件指针,通过文件指针实现对它指向的文件的各种操作.这些 ...

  6. Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)

    Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1.       内联样式 ...

  7. 阿里 EasyExcel 7 行代码优雅地实现 Excel 文件生成&下载功能

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  8. 【优雅代码】04-1行代码完成多线程,别再写runnable了

    [优雅代码]04-1行代码完成多线程,别再写runnable了 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮 ...

  9. 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件

    1.背景: 学习java时,教材中关于如题问题,只有一小节说明,而且要自己写麻烦的配置文件,最终结果却只能转换为jar文件.实在是心有不爽.此篇博客教你如何方便快捷地把java代码,打包成jar文件以 ...

随机推荐

  1. android面试手册

    1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚 ...

  2. ActiveMQ入门示例

    1.ActiveMQ下载地址 http://activemq.apache.org/download.html 2.ActiveMQ安装,下载解压之后如下目录

  3. 【移动开发】SharedPreferences的兼容版本

    public class SharedPreferencesCompat { private static final String TAG = SharedPreferencesCompat.cla ...

  4. 07 总结ProgressDialog 异步任务

    1,ProgressDialog     >        //使用对象  设置标题             progressDialog.setTitle("标题");   ...

  5. 【Unity Shaders】Vertex & Fragment Shader入门

    写在前面 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Shader中实现描边效果的弊端,也就是只对表面平缓的模型有效.这是因为我们是依赖法线和视角的点乘结果来进行描边判 ...

  6. 02-Git简单使用

    Git安装(windows) https://code.google.com/p/msysgit/downloads/list 我们使用版本Git-1.7.9版本 百度网盘下载:链接:http://p ...

  7. 1049. Counting Ones (30)

    题目如下: The task is simple: given any positive integer N, you are supposed to count the total number o ...

  8. 秒懂ASP.NET中的内置对象

    上篇博客,小编主要简单的介绍了一下ASP.NET中的控件,这篇博客,小编主要简单总结一下ASP.NET中的内置对象,七个内置对象分别是:Request.Response.Application.Coo ...

  9. [Error]Can't install RMagick 2.13.4. You must have ImageMagick 6.4.9 or later.

    gem 安装ruby插件的时候 出现了一个错误 Installing rmagick 2.13.4 with native extensions Gem::Installer::ExtensionBu ...

  10. Uva - 804 - Petri Net Simulation

    Input: petri.in A Petri net is a computational model used to illustrate concurrent activity. Each Pe ...