本文目錄一覽:
- 1、C語言中什麼是索引??
- 2、用c語言怎麼做索引數組
- 3、C語言,創建了一個文件,包含一個結構體變數,按結構體中的某一項進行排序,如何創建與之相匹配的索引文件
- 4、C語言文件中怎麼對存入文件的數據設置索引,在使用的時候可以按照索引的類別來調用它?
- 5、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