c語言怎麼創建索引,創建函數索引

本文目錄一覽:

C語言中什麼是索引??

1.索引表的類型可定義如下:

struct IndexItem

{

IndexKeyType index;

//IndexKeyType為事先定義的索引值類型

int start;

//子表中第一個元素所在的下標位置

int length;

//子表的長度域

};

2.首先根據給定的索引值K1,在索引表上查找出索引值等於K1的索引項,以確定對應子表在主表中的開始位置和長度,然後再根據給定的關鍵字K2,在對應的子表中查找出

3。關鍵字等於K2的元素。

設數組A是具有mainlist類型的一個主表,數組B是具有indexlist類型的在主表A上建立的一個索引表,m為索引表B的實際長度,即所含的索引項的個數,K1和K2分別為給定

帶查找的索引值和關鍵字,並假定每個子表採用順序存儲,則索引查找演算法為:

int Indsch(mainlist A, indexlist B, int m, IndexKeyType K1, KeyType K2)

{//利用主表A和大小為 m 的索引表B索引查找索引值為K1,關鍵字為K2的記錄

//返回該記錄在主表中的下標位置,若查找失敗則返回-1

int i, j;

for (i = 0; i m; i++)

if (K1 == B[i].index)

break;

if (i == m)

return -1; //查找失敗

j = B[i].start;

while (j B[i].start + B[i].length)

{

if (K2 == A[j].key)

break;

else

j++;

}

if (j B[i].start + B[i].length)

return j; //查找成功

else

return -1; //查找失敗

}

用c語言怎麼做索引數組

用索引做排序就可以了

void select_sort(int *a, int len) {

int Min;

int *index;

index=(int *)new int[len];

for ( int i=0;ilen;i++ ) index[i]=i;

for (int i = 0; i len – 1; i++) {

for (int j = 1 + i; j len; j++) {

if (a[index[i]] a[index[j]]) {

Min=index[i];

index[i]=index[j];

index[j]=Min;

}

}

}

for ( int i=0;ilen;i++ ) coutindex[i]’ ‘; coutendl;

for ( int i=0;ilen;i++ ) couta[index[i]]’ ‘; coutendl;

delete [] index;

}

C語言,創建了一個文件,包含一個結構體變數,按結構體中的某一項進行排序,如何創建與之相匹配的索引文件

可以用xml文件創建,xml根據需要(比如:奧運的獎牌排序和總分排序)可以定義主索引和若干輔助索引。

C語言文件中怎麼對存入文件的數據設置索引,在使用的時候可以按照索引的類別來調用它?

你可在寫入主體文件數據時,順便建立索引表寫入本文件或另一個文件,可以在讀主體數據的文件前,先讀取索性表的文件,然後根據索引表指向的位置定位文件流指針讀取主體數據文件。

c語言索引搜索如何建立索引表?

#include cstdlib

#include cstdio

#include iostream

#include cmath

#include cstring

#include algorithm

#include stdlib.h

#include direct.h

#include string.h

#include io.h

#include stdio.h

#include iostream

#include windows.h

#include time.h

using namespace std;

FILE *p;

void md(long long int m)

{

long long int shi,fen,miao,hmiao; 

hmiao=m%3600000;

m=m/1000;

if(m/360024)

{

shi = m/3600;

fen = m/60%60;

miao = m%60;

coutendl”換算等於”shi”時:”fen”分:”miao”秒:”(hmiao-miao*1000)”毫秒”endl;

else printf(“輸入數值太大”); 

}

class CBrowseDir

{

protected:

char m_szInitDir[_MAX_PATH];

public:

CBrowseDir();

bool SetInitDir(const char *dir);

bool BeginBrowse(const char *filespec);

protected:

bool BrowseDir(const char *dir,const char *filespec);

virtual bool ProcessFile(const char *filename);

virtual void ProcessDir(const char *currentdir,const char *parentdir);

};

CBrowseDir::CBrowseDir()

{

getcwd(m_szInitDir,_MAX_PATH);

int len=strlen(m_szInitDir);

if (m_szInitDir[len-1] != ‘\\’)

strcat(m_szInitDir,”\\”);

}

bool CBrowseDir::SetInitDir(const char *dir)

{

if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)

return false;

if (_chdir(m_szInitDir) != 0)

return false;

int len=strlen(m_szInitDir);

if (m_szInitDir[len-1] != ‘\\’)

strcat(m_szInitDir,”\\”);

return true;

}

bool CBrowseDir::BeginBrowse(const char *filespec)

{

ProcessDir(m_szInitDir,NULL);

return BrowseDir(m_szInitDir,filespec);

}

bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)

{

_chdir(dir);

long hFile;

_finddata_t fileinfo;

if ((hFile=_findfirst(filespec,fileinfo)) != -1)

{

do

{

if (!(fileinfo.attrib  _A_SUBDIR))

{

char filename[_MAX_PATH];

strcpy(filename,dir);

strcat(filename,fileinfo.name);

puts(filename);

fputs(filename,p); 

fprintf(p,”\n”); 

if (!ProcessFile(filename))

return false;

}

} while (_findnext(hFile,fileinfo) == 0);

_findclose(hFile);

}

_chdir(dir);

if ((hFile=_findfirst(“*.*”,fileinfo)) != -1)

{

do

{

if ((fileinfo.attrib  _A_SUBDIR))

{

if (strcmp(fileinfo.name,”.”) != 0  strcmp

(fileinfo.name,”..”) != 0)

{

char subdir[_MAX_PATH];

strcpy(subdir,dir);

strcat(subdir,fileinfo.name);

strcat(subdir,”\\”);

ProcessDir(subdir,dir);

if (!BrowseDir(subdir,filespec))

return false;

}

}

} while (_findnext(hFile,fileinfo) == 0);

_findclose(hFile);

}

return true;

}

bool CBrowseDir::ProcessFile(const char *filename)

{

return true;

}

void CBrowseDir::ProcessDir(const char 

*currentdir,const char *parentdir)

{

}

class CStatDir:public CBrowseDir

{

protected:

long long int m_nFileCount;   

long long int m_nSubdirCount; 

public:

CStatDir()

{

m_nFileCount=m_nSubdirCount=0;

}

int GetFileCount()

{

return m_nFileCount;

}

int GetSubdirCount()

{

return m_nSubdirCount-1;

}

protected:

virtual bool ProcessFile(const char *filename)

{

m_nFileCount++;

return CBrowseDir::ProcessFile(filename);

}

virtual void ProcessDir

(const char *currentdir,const char *parentdir)

{

m_nSubdirCount++;

CBrowseDir::ProcessDir(currentdir,parentdir);

}

};

int main()

{

int DiskCount = 0;

DWORD DiskInfo = GetLogicalDrives();

while(DiskInfo)

{

if(DiskInfo1)

{

++DiskCount;

}

DiskInfo = DiskInfo  1;

}

cout”邏輯磁碟數量:”DiskCountendl;

int DSLength = GetLogicalDriveStrings(0,NULL);

char* DStr = new char[DSLength];

GetLogicalDriveStrings(DSLength,(LPTSTR)DStr);

int DType;

int si=0;

BOOL fResult;

unsigned long long int i64FreeBytesToCaller;

unsigned long long intTotalBytes;

unsigned long long int i64FreeBytes;

unsigned long long int i64TotalBytes;

int g=0,k=0;

char name[100];

for(int i=0; iDSLength/4; ++i)

{

char dir[5]= {DStr[si],’:’,’\\’};

coutdir;

DType = GetDriveType(DStr+i*4);

if(DType == DRIVE_FIXED)

{

cout”硬碟”;

name[k]=DStr[si];

k++;

g++;

}

else if(DType == DRIVE_CDROM)

{

cout”光碟機”;

}

else if(DType == DRIVE_REMOVABLE)

{

cout”可移動式磁碟”;

}

else if(DType == DRIVE_REMOTE)

{

cout”網路磁碟”;

}

else if(DType == DRIVE_RAMDISK)

{

cout”虛擬RAM磁碟”;

}

else if (DType == DRIVE_UNKNOWN)

{

cout”未知設備”;

}

fResult = GetDiskFreeSpaceEx (dir,(PULARGE_INTEGER)i64FreeBytesToCaller,(PULARGE_INTEGER)i64TotalBytes,(PULARGE_INTEGER)i64FreeBytes);

if(fResult)

{

if((i64TotalBytes/1024/1024)1024)

  cout” 磁碟大小:”(float)i64TotalBytes/1024/1024/1024″ GB”;

 else

   cout” 磁碟大小:”(float)i64TotalBytes/1024/1024″ MB”;

if((i64FreeBytesToCaller/1024/1024)1024)

  cout” 剩餘空間:”(float)i64FreeBytesToCaller/1024/1024/1024″ GB”;

 else

   cout” 剩餘空間:”(float)i64FreeBytesToCaller/1024/1024″ MB”;

}

else

{

cout” 設備未準備好或啟用了系統加密!”;

}

if(!fResult)

  {

   k–;

  }

coutendl;

si+=4;

}

cout”共有硬碟”g”塊”endl;

g=k+1;

cout”共有未加密的硬碟”g”塊”endl;

cout”正在啟動索引程序”endl; 

int i;

p=fopen(“list.txt”,”a+”);

if(g==1)

  {

   cout”您只有一個c盤”endl;

   cout”檢測C盤可能會讓程序崩潰”endl;

  } 

clock_t start,finish;

    start=clock();

for(i=1;ig;i++)

   {

    fprintf(p,”%c盤內的文件如下\n”,name[i]);

char buf[6] = {name[i],’:’,’\\’};

    CStatDir statdir;

statdir.SetInitDir(buf);

        statdir.BeginBrowse(“*.*”);

        fprintf(p,”文件總數: %d\n子目錄總數:%d\n”,statdir.GetFileCount(),statdir.GetSubdirCount());

        printf(“文件總數: %d\n子目錄總數:%d\n”,statdir.GetFileCount(),statdir.GetSubdirCount());

   }

cout”全部完成”endl;

finish=clock();

cout”用時”(finish-start)”毫秒”;

md(finish-start); 

return 0;

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/158525.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-19 00:42
下一篇 2024-11-19 00:42

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python中capitalize函數的使用

    在Python的字元串操作中,capitalize函數常常被用到,這個函數可以使字元串中的第一個單詞首字母大寫,其餘字母小寫。在本文中,我們將從以下幾個方面對capitalize函…

    編程 2025-04-29
  • Python中set函數的作用

    Python中set函數是一個有用的數據類型,可以被用於許多編程場景中。在這篇文章中,我們將學習Python中set函數的多個方面,從而深入了解這個函數在Python中的用途。 一…

    編程 2025-04-29
  • 單片機列印函數

    單片機列印是指通過串口或並口將一些數據列印到終端設備上。在單片機應用中,列印非常重要。正確的列印數據可以讓我們知道單片機運行的狀態,方便我們進行調試;錯誤的列印數據可以幫助我們快速…

    編程 2025-04-29
  • 三角函數用英語怎麼說

    三角函數,即三角比函數,是指在一個銳角三角形中某一角的對邊、鄰邊之比。在數學中,三角函數包括正弦、餘弦、正切等,它們在數學、物理、工程和計算機等領域都得到了廣泛的應用。 一、正弦函…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變數時顯示的指定變數類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python實現計算階乘的函數

    本文將介紹如何使用Python定義函數fact(n),計算n的階乘。 一、什麼是階乘 階乘指從1乘到指定數之間所有整數的乘積。如:5! = 5 * 4 * 3 * 2 * 1 = …

    編程 2025-04-29
  • Python定義函數判斷奇偶數

    本文將從多個方面詳細闡述Python定義函數判斷奇偶數的方法,並提供完整的代碼示例。 一、初步了解Python函數 在介紹Python如何定義函數判斷奇偶數之前,我們先來了解一下P…

    編程 2025-04-29

發表回復

登錄後才能評論