写日志:

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. Spring-boot+Spring-batch+hibernate+Quartz简单批量读文件写数据用例

    本文程序集成了Spring-boot.Spring-batch.Spring-data-jpa.hibernate.Quartz.H2等.完整代码在Github上共享,地址https://github ...

  3. 读文件/写文件。http请求。读取文件列表。

    package transfor; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import j ...

  4. spark读文件写mysql(java版)

    package org.langtong.sparkdemo; import com.fasterxml.jackson.databind.ObjectMapper; import org.apach ...

  5. C++高并发场景下读多写少的解决方案

    C++高并发场景下读多写少的解决方案 概述 一谈到高并发的解决方案,往往能想到模块水平拆分.数据库读写分离.分库分表,加缓存.加mq等,这些都是从系统架构上解决.单模块作为系统的组成单元,其性能好坏也 ...

  6. C++高并发场景下读多写少的优化方案

    概述 一谈到高并发的优化方案,往往能想到模块水平拆分.数据库读写分离.分库分表,加缓存.加mq等,这些都是从系统架构上解决.单模块作为系统的组成单元,其性能好坏也能很大的影响整体性能,本文从单模块下读 ...

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

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

  8. 写出形似QML的C++代码

    最开始想出的标题是<Declarative C++ GUI库>,但太标题党了.只写了两行代码,连Demo都算不上,怎么能叫库呢……后来想换掉“库”这个字,但始终找不到合适词来替换.最后还是 ...

  9. [置顶] 自己写代码生成器之生成Dal层代码(获取数据库所有表名称)

    自己写代码生成器之生成Dal层代码(获取数据库所有表名称) --得到数据库birthday所有表名称 select name from sysobjects where [type]='U' --se ...

随机推荐

  1. kubernetes 项目

    1:CI/CD Docker: Harbor Git Jenkins 2:微服务 istio

  2. Linux初学习

    Linux Linux运行与关闭 Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 UNIX 的多用户.多任务.支持多线程和多 CPU 的操作系统. Linux ...

  3. Swift环境下实现UILabel居上 居中 居下对齐

    首先在Xcode中新建.h文件,将下面代码复制进去 // // myUILabel.h // // // Created by yexiaozi_007 on 3/4/13. // Copyright ...

  4. Android-Universal-Image-Loader 的使用说明

    这个图片异步载入并缓存的类已经被非常多开发人员所使用,是最经常使用的几个开源库之中的一个,主流的应用,随便反编译几个火的项目,都能够见到它的身影. 但是有的人并不知道怎样去使用这库怎样进行配置,网上查 ...

  5. nj11--http

    概念:Node.js提供了http模块.其中封装了一个高效的HTTP服务器和一个建议的HTTP客户端. http.server是一个基于事件的HTTP服务器.内部有C++实现.接口由JavaScrip ...

  6. codeforces#281 A

    脑子有点秀逗 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...

  7. Getting started with Kentico

    https://docs.kentico.com/k10tutorial 主面板按照功能分成两行排版 https://docs.kentico.com/k10tutorial/getting-star ...

  8. Android--ViewPager-Fragment

    package com.cnn.viewpager02; import java.util.ArrayList; import java.util.List; import android.os.Bu ...

  9. 版本控制器:SVN(精讲)

    版本控制器:SVN 1 开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目 ...

  10. 003.ES2015和ES2016新特性--类.md

    JavaScript使用的是基于原型的OO模型,用对象字面量或者函数来实例化对象,用原型链来实现继承. 这样对于数据传统C++.Java的OO范式的开发者来说,会感到比较困惑,于是从ES2015开始逐 ...