之前写的收集的一些c++代码片,算法排序,读文件,写日志,快速求积分等等
写日志:
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++代码片,算法排序,读文件,写日志,快速求积分等等的更多相关文章
- 【编程练习】收集的一些c++代码片,算法排序,读文件,写日志,快速求积分等等
写日志: class LogFile { public: static LogFile &instance(); operator FILE *() const { return m_file ...
- Spring-boot+Spring-batch+hibernate+Quartz简单批量读文件写数据用例
本文程序集成了Spring-boot.Spring-batch.Spring-data-jpa.hibernate.Quartz.H2等.完整代码在Github上共享,地址https://github ...
- 读文件/写文件。http请求。读取文件列表。
package transfor; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import j ...
- spark读文件写mysql(java版)
package org.langtong.sparkdemo; import com.fasterxml.jackson.databind.ObjectMapper; import org.apach ...
- C++高并发场景下读多写少的解决方案
C++高并发场景下读多写少的解决方案 概述 一谈到高并发的解决方案,往往能想到模块水平拆分.数据库读写分离.分库分表,加缓存.加mq等,这些都是从系统架构上解决.单模块作为系统的组成单元,其性能好坏也 ...
- C++高并发场景下读多写少的优化方案
概述 一谈到高并发的优化方案,往往能想到模块水平拆分.数据库读写分离.分库分表,加缓存.加mq等,这些都是从系统架构上解决.单模块作为系统的组成单元,其性能好坏也能很大的影响整体性能,本文从单模块下读 ...
- 【优雅代码】04-1行代码完成多线程,别再写runnable了
[优雅代码]04-1行代码完成多线程,别再写runnable了 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮 ...
- 写出形似QML的C++代码
最开始想出的标题是<Declarative C++ GUI库>,但太标题党了.只写了两行代码,连Demo都算不上,怎么能叫库呢……后来想换掉“库”这个字,但始终找不到合适词来替换.最后还是 ...
- [置顶] 自己写代码生成器之生成Dal层代码(获取数据库所有表名称)
自己写代码生成器之生成Dal层代码(获取数据库所有表名称) --得到数据库birthday所有表名称 select name from sysobjects where [type]='U' --se ...
随机推荐
- 洛谷 P1990 覆盖墙壁
P1990 覆盖墙壁 题目描述 你有一个长为N宽为2的墙壁,给你两种砖头:一个长2宽1,另一个是L型覆盖3个单元的砖头.如下图: 0 0 0 00 砖头可以旋转,两种砖头可以无限制提供.你的任务是计算 ...
- spring揭秘 读书笔记 二 BeanFactory的对象注冊与依赖绑定
本文是王福强所著<<spring揭秘>>一书的读书笔记 我们前面就说过,Spring的IoC容器时一个IoC Service Provider,并且IoC Service Pr ...
- 更改linux文件的拥有者及用户组(chown和chgrp)
.使用chown命令更改文件拥有者 在 shell 中,能够使用chown命令来改变文件全部者.chown命令是change owner(改变拥有者)的缩写.须要要注意的是,用户必须是已经存在系统中的 ...
- win7笔记本设置wifi热点
1.打开cmd 输入netsh wlan set hostednetwork mode=allow ssid=ACE-PC key=12345678 2.等待1-2分钟后,网络连接里会出现一个&quo ...
- 查看spark是否有僵尸进程,有的话,先杀掉。可以使用下面命令
查看spark是否有僵尸进程,有的话,先杀掉.可以使用下面命令yarn application -listyarn application -kill <jobid>
- ORM中基于对象查询与基于queryset查询
感谢老男孩~ 一步一步走下去 前面是视图函数 后面是表结构models.py from django.shortcuts import render, HttpResponse from djang ...
- Hadoop框架基础(五)
** Hadoop框架基础(五) 已经部署了Hadoop的完全分布式集群,我们知道NameNode节点的正常运行对于整个HDFS系统来说非常重要,如果NameNode宕掉了,那么整个HDFS就要整段垮 ...
- 项目中访问controller报错:HTTP Status 500 - Servlet.init() for servlet spring threw exception
直接访问controller路径http://localhost:8080/index报错: HTTP Status 500 - Servlet.init() for servlet spring t ...
- MyBatis、JDBC、Hibernate区别
从层次上看,JDBC是较底层的持久层操作方式,而Hibernate和MyBatis都是在JDBC的基础上进行了封装使其更加方便程序员对持久层的操作. 从功能上看, JDBC就是简单的建立数据库连接,然 ...
- ajax无刷新翻页后,jquery失效问题的解决
例如 $(".entry-title a").click(function () { 只对第一页有效, 修改为 $(document).on('click', ".e ...