发新话题
打印

[TIPS]数据结构学习札记

[TIPS]数据结构学习札记

  信息来源:邪恶八进制

请不要在这里灌水 老帖地址
http://eviloctal.bbnl.org/forum/read.php?tid=1165
http://www.eviloctal.com/forum/read.php?tid=1165

以后就记在这里吧.

win32多线程学习手记
/*
*循序渐进学Windows编程(学习笔记).cpp
*simple code for "multithreading applicationgs in win32"
*
*Demonstrate how to use Windows编程
*demo:正在学习多线程程序设计,如果您对此感兴趣,请和我交流。http://wmjie.51.net/swords
*/
下载http://wmjie.51.net/swords/blog/attachment.php?id=4
引用:
#include<iostream.h>
#include<time.h>

int seqsearch(int a[],const int n,const int x){
     for(int i=1;i<=n;i++)
          if(a==x)
              break;
     if(i>n)
          return -1;
     else
          return n;
}

int main(){
     int a[1000001],n;
     long start,stop;
     for(int j=1;j<=1000000;j++)
          a[j]=j;
     cin>>n;
     time(&start);
     int k=seqsearch(a,n,0);
     time(&stop);
     long runTime=stop-start;
     cout<<" "<<n<<" "<<runTime<<endl;
     return 0;
}
编译运行出现time()函数异常?
QQ:838468959

TOP

//swords tips
//2005/3/14
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>


int main(int argc,char **argv,char **env){
    printf("process id:%x\n",getpid());
    if(!system("net user")) ;
    if(!system("set")) ;
    printf("press any key to continue...");
    while(!kbhit()) ;//等候按键
    return 0;
}
QQ:838468959

TOP

#include<stdio.h>
#include<conio.h>
#include<string.h>


int main(int argc,char **argv,char **env){
    printf("Env:\n\n");
    while(*env)
        printf("%s\n",*env++);//相当于dos中的set命令,输出环境变量
    printf("press any key to continue...");
    while(!kbhit()) ;//等候按键
    return 0;
}
QQ:838468959

TOP

//a simple list contains all file in current directory
//write by swords
//2005.3.14

#include<stdio.h>
#include<dirent.h>
#include<alloc.h>
#include<string.h>

void main(int argc,char *argv[])
{
    DIR *directory;
    struct dirent *entry;
    struct FileList
    {
        char filename[64];
        struct FileList *next;
    }start,*node;
   
    if((directory=opendir(argv[1]))==NULL)
        printf("Error opening %s\n",argv[1]);
    else{
        start.next=NULL;
        node=&start;
        while(entry=readdir(directory))
        {
            node->next=(struct FileList *)malloc(sizeof(struct FileList));
            node=node->next;
            strcpy(node->filename,entry);
            node->next=NULL;
        }
        closedir(directory);
        //display the list
        node=start.next;
        while(node)
        {
            printf("%s ",node->filename);
            node=node->next;
        }
    }
}
QQ:838468959

TOP

//child.c
//write by swords
//2005.3.14

#include<stdio.h>
#include<stdio.h>

void main(int argc,char *argv[],char *env[])
{
    printf("command line:\n ");
    while(*argv)
        puts(*argv++);
    printf("Environment entries:\n");
    while(*env)
        puts(*env++);
}

//调用child.exe
#include<process.h>
#include<stdio.h>

void main()
{
    printf("about to call child process\n\n");
    spawnl(P_WAIT,"child.exe","child","aaa","bbb","ccc",NULL);
    printf("\n\nback from child process!!!!!!!\n");
}
/*一、system(char *command)函数。功能是执行一个MS-DOS命令,它通过MS-DOS的命令解释程序COMMAND.COM,
来执行参数command字串中要求的命令,所以内存中会重新加载一份COMMAND.COM。例如可以在程序中用语句:
system(2command.com2)来实现MS-DOS的shell功能。
  二、exec类函数。这类函数执行成功后子进程将覆盖父进程,这样在自己的程序中调用外部程序执行成
功后,无法返回自己的程序中,将退回到DOS状态,一般较少用到。
  三、spawn类函数。此类函数功能较多,它能够由子进程返回父进程,接着运行父进程,相比之下用途
多些。

  函数申明: int spawnl(int mode, char *path, char *arg0, ...)
int spawnle(int mode, char *path, char *arg0, ...)
int spawnlp(int mode, char *path, char *arg0, ...)
int spawnlpe(int mode, char *path, char *arg0, ...)
int spawnv(int mode, char *path, char *argv[])
int spawnve(int mode, char *path, char *argv[], char **env)
int spawnvp(int mode, char *path, char *argv[])
int spawnvpe(int mode, char *path, char *argv[], char **env)



函数用途: 在一个程序中调用另外一个程序  
头 文 件: process.h
输入参数: path:被调用程序路径;arg:调用的参数
mode:调用模式,具体如下:
   P_WAIT    0   将父过程挂起,知道子过程执行完毕
   P_NOWAIT  1   父子过程同时执行,Turboc不支持
   P_OVERLAY 2   子过程覆盖父过程


输出参数:  
返 回 值:  
使用说明: -1:调用失败,0:调用成功  
*/
QQ:838468959

TOP

//testfileio.cpp
//Practise only by swords

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>

void main()
{
    //包含有文件名和值的文本文件
    ifstream fin;

    //标志符存放于name中
    char name[30];
    double value;

    //打开names.dat文件用作输入,并确保其存在
    fin.open("names.dat",ios::in|ios::nocreate);
    if(!fin)
    {
        cerr<<"could not open &#39;names.dat&#39;"<<endl;
        exit(1);
    }

    //读入名字及对立值,并用格式&#39;name:value&#39;写到outs中
    while(fin>>name)
    {
        fin>>value;
        cout<<name<<"="<<value<<endl;
    }
}

/*
<文件&#39;names.dat&#39;>

  start 55
  breaktop 255.39
  stop 33
  */
QQ:838468959

TOP

win32多线程学习手记
/*
*循序渐进学Windows编程(学习笔记).cpp
*simple code for "multithreading applicationgs in win32"
*
*Demonstrate how to use Windows编程
*demo:正在学习多线程程序设计,如果您对此感兴趣,请和我交流。http://wmjie.51.net/swords
*/
下载http://wmjie.51.net/swords/blog/attachment.php?id=4
QQ:838468959

TOP

引用:
下面是引用swords于2005-03-03 11:18发表的[TIPS]数据结构学习札记:
  信息来源:邪恶八进制

请不要在这里灌水 老帖地址
http://eviloctal.bbnl.org/forum/read.php?tid=1165
http://www.eviloctal.com/forum/read.php?tid=1165
.......
好像楼主打字的时候少了一个东西
seqsearch里应该是
引用:
if(a==x)
吧....^^
引用:
#include<iostream.h>
#include<time.h>


int seqsearch(int a[],const int n,const int x){
   for(int i=1;i<=n;i++)
      if(a==x)
     break;
   if(i>n)
      return -1;
   else
      return n;
}

int main(){
     
   int a[1000001],n;
   long start,stop;
   for(int j=1;j<=1000000;j++)
      a[j]=j;
   cin>>n;
   time(&start);
   int k=seqsearch(a,n,0);
   time(&stop);
   long runTime=stop-start;
   cout<<" "<<n<<" "<<runTime<<endl;
   return 0;
}

TOP

全排列算法,组合算法
//全排列算法 by sowrds
//05.3.22

#include<iostream.h>

//排列元素最大数
const int UpperLimit=5;

//从数组y中拷贝n个元素到数组x中
void copy(int x[],int y[],int n)
{
for(int i=0;i<n;i++)
x=y;
}

void permute(int permlist[],int start,int n)
{
int temparr[UpperLimit];
int temp,i;

//终止条件,已到数组的最后一个元素
if(start==n-1)
{
//输出本次排列结束
for(i=0;i<n;i++)
cout<<permlist<<" ";
cout<<endl;
}
else
//递归步骤:交换元素permlist[start]和permlist的值,
//将数组拷贝到temparr,然后从start+1开始到数组结束对
//temparr进行全排列
for(i=start;i<n;i++)
{
//交换元素permlist[start]和permlist的值
temp=permlist;
permlist=permlist[start];
permlist[start]=temp;
//产生一个新表后调用permute
copy(temparr,permlist,n);
permute(temparr,start+1,n);
}
}



void main()
{
int permlist[UpperLimit];
int n,i;
cout<<"Enter a number &#39;n&#39; between 1 and "<<UpperLimit<<":";
cin>>n;

//初始化permlist为{1,2,3,……,n}
for(i=0;i<n;i++)
permlist=i+1;
cout<<endl;
//从下标 到n-1输出permlist数组中的排列结果
permute(permlist,0,n);
}
/*
Enter a number &#39;n&#39; between 1 and 5:3

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/

//组合算法 by sowrds
//05.3.22

#include<iostream.h>

int comm(int n,int k)
{
if(k>n)
return 0;
else if(n==k||k==0)
return 1;
else
return comm(n-1,k)+comm(n-1,k-1);
}


void main()
{
int n,k;
cout<<"Enter n ,k for c(n,k):"<<endl;
cin>>n>>k;
cout<<"c("<<n<<","<<k<<"): ";
cout<<comm(n,k)<<endl;
}



Submitted by 菠菜 on 2005, April 27, 9:12 AM
QQ:838468959

TOP

发新话题