Skip to content

Instantly share code, notes, and snippets.

@pythongo1
Created June 22, 2019 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythongo1/c0c7a1c7497fe1e642af74542aa30f18 to your computer and use it in GitHub Desktop.
Save pythongo1/c0c7a1c7497fe1e642af74542aa30f18 to your computer and use it in GitHub Desktop.
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<tchar.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
//template<typename T,size_t N>
//template<size_t N>
//void catalan(int (&ans)[], int size)
void catalan(unsigned long long ans[], int size)
{
int k;
if(size==2)
{
ans[1]=1;
}
else
{
ans[1]=1;
for(k=1;(k+1)!=size;k++)
{
ans[k+1]=ans[k]*(4*k+2)/(k+2);
}
}
}
TCHAR szMsg[]=TEXT("");
int main(int argc, char *argv[])
{
HANDLE hMapFile,hFile;
LPCTSTR pBuf;
hFile = CreateFile("temp.txt",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
hMapFile = CreateFileMapping(
hFile, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
1024, // maximum object size (low-order DWORD)
TEXT("SharedObject")); // name of mapping object
if (hMapFile == NULL)
{
printf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
0);
if (pBuf == NULL)
{
printf(TEXT("Could not map view of file (%d).\n"), GetLastError());
CloseHandle(hMapFile);
return 1;
}
int number = atoi(argv[1]);
printf("the number is %d\n", number);
// unsigned long long answer[number];
int fact_size = number+1; // 只使用 index 1 之後的array, 所以 設一個 fact_size
unsigned long long answer[fact_size];
int i;
catalan(answer, fact_size); // Use to calculate
char answer_string[100];
char space[2]=" ";
for(i=1;i<fact_size;i++)
{
itoa(answer[i],answer_string,10);
strcat(szMsg,answer_string);
if(i==(fact_size-1))
{
break;
}
strcat(szMsg,space);
}
CopyMemory((PVOID)pBuf, szMsg, (strlen(szMsg) * sizeof(CHAR)));
printf("enter any thing to exit\n");
getch(); // 按任意鍵 結束程式
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment