Microsoft Excel Add-In Updated

Today I got this small add-in updated.
Add-in page at APP Page

This update including several main changes as following:

1, Change UI from Office Ribbon to Windows Form which is more organized.

2, Title insertion adds a new function and now users can get the range by a simple click rather than brain taken inputs.

3, Adds a function that can compare data in two ranges.

Get Excel Selected Range via VSTO with C#

These days I have been working on the development of an Excel Add-in and learnt a way to get the currently selected range in Excel.
Take it for example that your Project namespace is ExcelAddIn. Firstly, you should refer the interop by following code:

using Microsoft.Office.Interop.Excel;

and you can get a string consistent with the Range you selected by following code:

ExcelAddIn.Globals.ThisAddIn.Application.Selection.Address;

which would return a string in format like “$A$1:$B$2″ for rectangles,”$1:$2” for rows and “$A:$B” for columns.

Dynamic Pointer and Dynamic Memory Allocation in C++

Days ago a mate asked me a favor in his code and I got trouble in dynamic pointer and dynamic memory allocation.
Well, one-dimensional pointer is easy to use, and you can easily expand it’s occupied memory size by using realloc() as following:

#include <iostream>
using namespace std;
int n=2,* pint;
void Initialization()
{
	pint=new int[n];
}
void DoSomething(){}
int main()
{
	Initialization();
	DoSomething();
	pint=(int*)realloc(pint,(++n)*sizeof(int));
	pint[n-1]=123456;
	cout<<pint[n-1]<<endl;
	return 0;
}
</pre>
But when it comes to the two-dimensional pointer, it nolonger works if you write some code like this:
<pre lang="cpp" line="1" file="code2.cpp" colla="+">
#include <iostream>
using namespace std;
int n=2,len=20;
char** ppchar;
void Initialization(){
	ppchar=new char*[n];
	for(int i=0;i<n;i++){
		ppchar[i]=new char[len];
	}
}
void DoSomething(){}
int main()
{
	Initialization();
	DoSomething();
	ppchar=(char**)realloc(ppchar,(++n)*len*sizeof(char));
	ppchar[n-1]="this is a new one";
	cout<<ppchar[n-1]<<endl;
	return 0;
}

That is because you failed to notify the system, or in other words declare the address of ppchar[n-1].
And here is a correct version of the same function by adding a statement between line 16 and 17:

ppchar[n-1]=new char[len];

Excel Add-in for Excel 2007 and 2010

Tonight I made great effort to this project which is an excel add-in for easy adding titles to the mass content. The add-in is developed under Visual Studio 2010 with Visual Studio Tools for Office in C# (CSharp), and it may need .Net Framework 4 Client Profile and Visual Studio Tools for Office Runtime.

Download the Excel Add-in

Here’s the situation. You have the table like this:

Title B1 Title C1 Title D1
Title B2 Title C2 Title D2
Content Index 1 various content various content various content
Content Index 2 various content various content various content
Content Index n various content various content various content

and you would like it to be something like this:

Title B1 Title C1 Title D1
Title B2 Title C2 Title D2
Content Index 1 various content various content various content
Title B1 Title C1 Title D1
Title B2 Title C2 Title D2
Content Index 2 various content various content various content
Title B1 Title C1 Title D1
Title B2 Title C2 Title D2
Content Index n various content various content various content

Then this Excel Add-in would help you a lot from copying n pasting n coping n pasting…

Hope you enjoy it! Good night world!

 

Enable the Gzip Compression

Have been struggling on how to turn on the gzip compression. I mean the Super cache plugin enabled compression but not always works. So I searched the Internet to find solutions, and got nothing.

Until then, someone mentioned it can be enabled by modifying the .htaccess and here follows the code,

 SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript 

It works! Thank god!

And good night the world.

Calculate the Determinant of A Square Matrix

This is a code that I wrote weeks ago, mainly to calculate the determinant of a squae matrix.

It’s written in c++. Enjoy it!

 #include <iostream>
using namespace std;
template<typename T>
class Matrix{
	T **m;
	int n;
	void rot90(){
		T ** tmp=new T*[n];
		for(int i=0;i<n;i++){
			tmp[i]=new T[n];
			for(int j=0;j<n;j++){
				tmp[i][j]=m[i][j];
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				m[i][j]=tmp[n-1-j][i];
			}
		}
	}
	Matrix<t> M(int i,int j){//get the matrix corresponds with specific minor Mij
		Matrix<t> TMP(n-1);
		for(int a=0;a<n-1;a++){
			for(int b=0;b<n-1;b++){
				int c=0,d=0;
				if(a<i) c=a;
				else c=a+1;
				if(b<j) d=b;
				else d=b+1;
				TMP.m[a][b]=m[c][d];
			}
		}
		return TMP;
	}
	T C(int i,int j){//get cofactor Cij
		if((i+j)%2==0) return M(i,j).Det();
		else return -1*M(i,j).Det();
	}
	T A(int i,int j){
		return m[i][j]*C(i,j);
	}
public :

	Matrix(int para){
		n=para;
		m=new T*[n];
		for(int i=0;i<n;i++){
			m[i]=new T[n];
			for(int j=0;j<n;j++) m[i][j]=0;
		}
	}
	Matrix(){
		int itmp=0;
		do{
			cout<<"Please input the square matrix's  n, and n has to be larger than 1"<<endl;
			cin>>itmp;
		}
		while(itmp<2);
		n=itmp;
		m=new T*[n];
		for(int i=0;i<n;i++){
			m[i]=new T[n];
			for(int j=0;j<n;j++) m[i][j]=0;
		}
	}
	void Input(){
		cout<<"Please initialize the matrix"<<endl;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++) cin>>m[i][j];
		}
		cout<<"Initialization done"<<endl;
	}
	void Display(){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<m[i][j];
				if(j!=n-1) cout<<" ";
			}
			cout<<endl;
		}
	}
	void Display(int w){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<setw(w)<<m[i][j];
			}
			cout<<endl;
		}
	}
	void Display(int i,int j){
		cout<<m[i][j]<<endl;
	}
	void Rotation(int times=1){
		for(;times>3;times-=4){}
		for(;times<0;times+=4){}
		if(times!=0){
			if(times==1) rot90();
			else{
				rot90();
				Rotation(times-1);
			}
		}
	}
	T Det(){
		if(n==2) return m[0][0]*m[1][1]-m[0][1]*m[1][0];
		else{
			T sum=0;
			for(int i=0;i<n;i++) sum+=A(0,i);
			return sum;
		}
	}
}; 

Find the Perfect Number

First of all, we have the conclusion from Euclid that define an integer n, and if 2^n – 1 is a prime, the number 2^(n-1)*(2^n – 1) must be a Perfect Number.

Here follows my source code on finding 5 the smallest Perfect Number.It’s written by C++.

using namespace std;
bool IsPrime (long long s)
{
	for(long long i=2;i<sqrt((long double)s);i++)
	{
		if(s%i==0) return false;
	}
	return true;
}
long long Power(int n)
{
	if(n!=0) return 2*Power(n-1);
	else return 1;
}
int main()
{
	int cnt=0;
	for(int i=2;cnt<9;i++)
	{
		if(IsPrime(Power(i)-1)) cout<<++cnt<<") "<<power(i-1)*(Power(i)-1)<<endl;
	}
	return 0;
}