當前位置:首頁 » 編程語言 » 搜索c語言程序
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

搜索c語言程序

發布時間: 2023-03-17 15:53:06

1. c語言題目不會在哪能搜到

如果您遇到了一些較為復雜或者比較專業的C語言編程題目,可能在一些普通的搜索引擎中不容易找到相關的答案。以下是一些可以查找C語言編程題目的網站和資源:

  1. LeetCode(https://leetcode.com):這是一個著名的面向程序員的演算法練習平台,其中包含大量難度不同的C語言編程題目,涵蓋各種數據結構和演算法問題。

  2. HackerRank(https://www.hackerrank.com):HackerRank 是一個全球性的技術人才招聘和技能測評平台,在其上也包含有豐富的C語言編程題物螞庫轎螞春。

  3. Programiz(https://www.programiz.com):Programiz 是一個面向初學者的編程教育網站,提供了許多C語言編程題目和示例代碼,適合需要初步入門的學習者。

  4. Stack Overflow(https://stackoverflow.com):Stack Overflow 是一個知名的程序員問答社區,其中包含了大量的編程問題和解答,適合於尋找具體問題的答案和解決方案。

  5. GitHub(https://github.com):GitHub 是一個全球最大的開源代碼庫,其中閉耐包含著數以億計的開源項目和代碼片段,可以通過搜索關鍵字找到相應的C語言編程題目和實現代碼。

2. 怎麼用C語言編寫一個程序,可以搜索電腦里的文件

//***************全盤搜索指定文件*******************
//**************************************************
//**使用遞歸來搜索文件,效率低,使用多線程效果更好**
//**************************************************
#include<stdio.h>
#include<windows.h>

void FindFile(char*,char*);
int count=0;//統計文件數
char fname[32];
#define BUFSIZE 256
int main(int argc,char*argv[])
{
char szLogicalDriveStrings[BUFSIZE];
DWORD iLength;
int iSub;
printf("請輸入要搜索的文件名");
scanf("%s",fname);
ZeroMemory(szLogicalDriveStrings, BUFSIZE);
iLength = GetLogicalDriveStringsA(BUFSIZE-1, szLogicalDriveStrings);
for(iSub=0;iSub<iLength;iSub+=4)
{
//如果不是固定磁碟驅動器:本地硬碟或移動硬碟,忽略
if(GetDriveType(szLogicalDriveStrings+iSub)!=3)
continue;
FindFile(szLogicalDriveStrings+iSub,"*.*");
}
printf("一共發現%d個文件..\n",count);
scanf("%*d");
return 0;
}
void FindFile(char*pfilename,char*pfilter)
{
WIN32_FIND_DATA findfiledate;
HANDLE hfind;
char filename[512];
char ipFileName[512];
char _ipFileName[512];
int i;
int result;
for (i=0;*(pfilename+i)!='\0';i++)
filename[i]=*(pfilename+i);
filename[i]='\0';
//如果最後一個字元不是『\』
if(filename[strlen(filename)-1]!='\\')
strcat(filename,"\\");//添加"\"
strcpy(ipFileName,filename);
strcat(ipFileName,pfilter);
hfind=FindFirstFile(ipFileName,&findfiledate);
if(hfind==INVALID_HANDLE_VALUE)
return;
do
{
//如果不是目錄
if(!(findfiledate.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
//如果找到指定文件
if(0==strcmp(fname,findfiledate.cFileName))
{
printf("%s%s\n",filename,findfiledate.cFileName);
count++;
}
}
//如果是目錄
else
{
//.和..不輸出
if(findfiledate.cFileName[0]!='.')
{
strcpy(_ipFileName,filename);
strcat(_ipFileName,findfiledate.cFileName);
FindFile(_ipFileName,pfilter);//遞歸
}
}
}while(FindNextFile(hfind,&findfiledate));//FindNextFile返回為真,繼續搜索
FindClose(hfind);
return;
}

非原作者

3. C語言編寫"全盤搜索一個文件"的程序

用C++遍歷目錄
河南洛陽 祝曉鷹

--------------------------------------------------------------------------------

---- 所謂遍歷目錄,就是給定一個目錄,訪問其中的所有文件(包括子目錄下的文件)。迭代是比較常用的遍歷演算法。本文利用C++面向對象的特性,通過一個類CBrowseDir,對目錄遍歷進行了封裝。用戶只需掌握該類四個成員函數的用法,就可以在自己的程序中,很方便地實現目錄遍歷。

---- 類CBrowseDir使用了迭代演算法。因為演算法不是本文重點,筆者不打算展開進一步討論,對其感興趣者可參考相關資料。

一、類成員函數說明:

---- bool SetInitDir(const char *dir);

---- 功能:設置要遍歷的目錄。

---- 參數:dir 指向要遍歷的目錄,可以使用相對路徑,比如"d:..\hawk";還可以使用網路路徑,比如"\\wf\d\hawk"(其中wf是主機名,d是共享目錄,hawk是目錄)。

---- 返回值:返回true,表示設置成功;返回false,說明目錄不可用。

---- bool BeginBrowse(const char *filespec);

---- 功能:開始遍歷目錄中由filespec指定的文件(包括隱藏文件)。

---- 參數:filespec 指定文件類型,可以使用通配符*和?,比如"*.exe"或"a?.*"都是合法參數。注意:filespec中不能包含路徑,象"hawk\*.*"是錯誤的。

---- 返回值:函數返回true,表明已順利遍歷完所有文件;返回false,遍歷過程被用戶中止。

---- virtual bool ProcessFile(const char *filename);

---- 功能:虛函數。每找到一個文件,程序就會調用ProcessFile,並把文件名作為參數傳遞給函數。如果函數返回false,則強制遍歷中止,並導致類成員函數函數BeginBrowse返回false。 用戶應該覆寫此函數,以加入自己的處理代碼。

---- 參數:filename 指向一個文件名。注意:filename使用絕對路徑。

---- 返回值:返回true,繼續遍歷;否則,中止遍歷。

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

---- 功能:虛函數。在遍歷過程中,每進入一個子目錄,程序就會調用ProcessDir,並把目錄名及其上一級目錄名作為參數傳遞給函數。如果該目錄是成員函數SetInitDir指定的初始目錄,則parentdir=NULL。用戶可以覆寫此函數,以加入自己的處理代碼。比如可以在這里統計子目錄的個數。

---- 參數:currentdir 指向一個子目錄。
---- parentdir 指向currentdir的父目錄。
---- 注意:currentdir和parentdir均使用絕對路徑。

二、使用:

---- 把類CBrowseDir的頭文件BrowseDir.h及實現文件BrowseDir.cpp加到項目(Project)中,然後派生自己的類並覆寫虛函數ProcessFile和ProcessDir。遍歷目錄時,先構造一個派生類對象,用成員函數SetInitDir指定目錄,然後調用BeginBrowse開始遍歷。

---- 本文提供了一個例子 example.cpp,它從CBrowseDir派生出子類CStatDir,通過統計函數ProcessFile及ProcessDir的調用次數,可以得知目錄中的文件及子目錄個數。程序都有注釋,這里就不再羅嗦了。

三、注意事項:

---- 1. 類CBrowseDir會改變當前工作目錄。同一個相對路徑,使用CBrowseDir前後,可能會有不同的含義。因此用戶編程時,要小心使用相對路徑。

---- 2. 如果項目(Project)是一個MFC應用程序,直接加入BrowseDir.h及BrowseDir.cpp會導致編譯出錯。這是因為預設情況下,MFC項目使用了預編譯頭(Precompiled Header),而BrowseDir.h和BrowseDir.cpp是用標准C++語句編寫的,沒用預編譯。一個解決辦法是先用類向導生成類CBrowseDir的"架子",再把相應的代碼拷貝過去。

---- 本文代碼均在Win95、Visual C++ 5.0環境下調試通過。

附源代碼:

/**************************************************
這是CBrowseDir的類定義文件 BrowseDir.h

/**************************************************
#include "stdlib.h"

class CBrowseDir
{
protected:
//存放初始目錄的絕對路徑,以'\'結尾
char m_szInitDir[_MAX_PATH];

public:
//預設構造器
CBrowseDir();

//設置初始目錄為dir,如果返回false,表示目錄不可用
bool SetInitDir(const char *dir);

//開始遍歷初始目錄及其子目錄下由filespec指定類型的文件
//filespec可以使用通配符 * ?,不能包含路徑。
//如果返回false,表示遍歷過程被用戶中止
bool BeginBrowse(const char *filespec);

protected:
//遍歷目錄dir下由filespec指定的文件
//對於子目錄,採用迭代的方法
//如果返回false,表示中止遍歷文件
bool BrowseDir(const char *dir,const char *filespec);

//函數BrowseDir每找到一個文件,就調用ProcessFile
//並把文件名作為參數傳遞過去
//如果返回false,表示中止遍歷文件
//用戶可以覆寫該函數,加入自己的處理代碼
virtual bool ProcessFile(const char *filename);

//函數BrowseDir每進入一個目錄,就調用ProcessDir
//並把正在處理的目錄名及上一級目錄名作為參數傳遞過去
//如果正在處理的是初始目錄,則parentdir=NULL
//用戶可以覆寫該函數,加入自己的處理代碼
//比如用戶可以在這里統計子目錄的個數
virtual void ProcessDir(const char
*currentdir,const char *parentdir);
};

/*********************************************/

這是CBrowseDir的類實現文件 BrowseDir.cpp

/***********************************************/
#include "stdlib.h"
#include "direct.h"
#include "string.h"
#include "io.h"

#include "browsedir.h"

CBrowseDir::CBrowseDir()
{
//用當前目錄初始化m_szInitDir
getcwd(m_szInitDir,_MAX_PATH);

//如果目錄的最後一個字母不是'\',則在最後加上一個'\'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '\\')
strcat(m_szInitDir,"\\");
}

bool CBrowseDir::SetInitDir(const char *dir)
{
//先把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);

//首先查找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);
if (!ProcessFile(filename))
return false;
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}

//查找dir中的子目錄
//因為在處理dir中的文件時,派生類的ProcessFile有可能改變了
//當前目錄,因此還要重新設置當前目錄為dir。
//執行過_findfirst後,可能系統記錄下了相關信息,因此改變目錄
//對_findnext沒有影響。
_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)
{
}

/*************************************************
這是例子example.cpp

/*************************************************
#include "stdio.h"

#include "BrowseDir.h"

//從CBrowseDir派生出的子類,用來統計目錄中的文件及子目錄個數
class CStatDir:public CBrowseDir
{
protected:
int m_nFileCount; //保存文件個數
int m_nSubdirCount; //保存子目錄個數

public:
//預設構造器
CStatDir()
{
//初始化數據成員m_nFileCount和m_nSubdirCount
m_nFileCount=m_nSubdirCount=0;
}

//返迴文件個數
int GetFileCount()
{
return m_nFileCount;
}

//返回子目錄個數
int GetSubdirCount()
{
//因為進入初始目錄時,也會調用函數ProcessDir,
//所以減1後才是真正的子目錄個數。
return m_nSubdirCount-1;
}

protected:
//覆寫虛函數ProcessFile,每調用一次,文件個數加1
virtual bool ProcessFile(const char *filename)
{
m_nFileCount++;
return CBrowseDir::ProcessFile(filename);
}

//覆寫虛函數ProcessDir,每調用一次,子目錄個數加1
virtual void ProcessDir
(const char *currentdir,const char *parentdir)
{
m_nSubdirCount++;
CBrowseDir::ProcessDir(currentdir,parentdir);
}
};

void main()
{
//獲取目錄名
char buf[256];
printf("請輸入要統計的目錄名:");
gets(buf);

//構造類對象
CStatDir statdir;

//設置要遍歷的目錄
if (!statdir.SetInitDir(buf))
{
puts("目錄不存在。");
return;
}

//開始遍歷

4. C語言如何用函數來實現搜索

#include<stdio.h>
intsearch(inta[],intb,intc,inti)
{
intx,y,z;
x=i+1;
z=b-1;
y=(x+z)/2;
while(x<=z)
{
if(a[y]>c)
{
z=y-1;
y=(x+z)/2;
continue;
}
if(a[y]<c)
{
x=y+1;
y=(x+z)/2;
continue;
}
returny+1;
}
return-1;

}

intmain()
{
inti,m,pos;
scanf("%d",&m);
inta[m];
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<m;i++)
{
pos=search(a,m,a[i],i);
if(pos!=-1)
{
printf("FOUNDa[%d]=%d,positionis%d ",i,a[i],i+1);
return0;
}
}
if(pos==-1)
{
printf("NOTFOUND ");
}
return0;
}

這種查找方法的數組必須是從小到大的,用遍歷的話就沒這個問題了。

5. C語言 單詞檢索程序

=====================================
問題補充:二樓的是死循環運行不了啊
=====================================
實在抱歉,之前疏忽了,現在已經改好了,再試一下吧:)

=====================================
問題補充:二樓的幸苦了,仔細看了一下你的,好像有點出入,不是自己輸入文章,是打開已有文章。還得麻煩你稍稍修改下。謝謝哈
=====================================
根據你的要求,又改了一版,現在已經改好了,再試一下吧:)
給:
#include<stdio.h>
#include<string.h>
#define MAX_size 1000
int flag=1,degree=0;
void Index(char str[],char word[],int position[])
{
int i,len_str,len_word,pos_str,pos_word,k=0,word_number=0;//word_number代表短文中單詞的個數
len_word=strlen(word);
len_str=strlen(str);
for(i=0;i<len_str;i++)
{
while(str[i]==' '||str[i]==','||str[i]=='.')
i++;
word_number++; //單詞個數加一
for(pos_str=i,pos_word=0;pos_str<len_str && pos_word<len_word;pos_str++,pos_word++)
{
if(str[pos_str]!=word[pos_word])
break;
}
if(pos_word==len_word && (str[pos_str]=='\0'|| str[pos_str]==' '||str[pos_str]==','||str[pos_str]=='.')) //表明找到相等的單詞
{
position[k++]=word_number;
degree++; //相等的次數加1
flag=0;
}
else
{
while(str[pos_str]!=' '&&str[pos_str]==','&&str[pos_str]=='.'&& pos_str<len_str)
pos_str++;
}
i=pos_str;
}
}
void main()
{
char str[MAX_size],word[20],ch;
int position[100],i;

int k=0;
FILE *fp;

if((fp=fopen("a.txt","r"))!=NULL)
{
while(1)
{
ch=fgetc(fp);
if(ch==EOF) break;

str[k]=ch;
k++;
}
}

printf("請輸入要檢索的單詞: \n");
gets(word);
Index(str,word,position);
if(flag)
printf("您輸入的單詞不在短文中。\n");
else
{
printf("您輸入的單詞在短文中,它共出現 %-d 次\n",degree);
printf("出現的位置為: \n");
for(i=0;i<degree;i++)
printf("第%-2d個單詞\n",position[i]);
}
fclose(fp);
}

6. 如何用C語言順序查找程序

#includex0dx0avoid main()x0dx0a{x0dx0a int a[10]={1,2,3,4,5,6,7,8,9,10};x0dx0a int i,x,y;x0dx0a printf("輸入你要查找的數:\n");x0dx0a scanf("%d",&x);x0dx0a y=0; //標記是否已找到,y=1表是找到了,y=0表示沒握拆找到x0dx0a for(i=0;i<10;i++) //循環,把x和數組中的元素一個個比較x0dx0a {x0dx0a if(x==a[i]) //如果x=a[i]說明已經找到x0dx0a {x0dx0a y=1; //把y變段埋棗成1,說明已經找到了 x0dx0a printf("你要查找的數%d在第個%d位液脊置\n",x,i+1); //輸出找到的相關信息x0dx0a break; //跳出循環x0dx0a }x0dx0a }x0dx0a if(y==0)printf("無法找到你要查找的數\n"); //y=0表示找不到x0dx0a}

7. C語言字元串查找簡單程序

#include<stdio.h>

int main(){
//str相當於保存字元的數組
char *str = "abc abc bcd bcd efg fff";
//用於保存三個連續字母出現的頻次
int tag[25] = {};
int max = 0,i;
char *ch;

ch = str;
while(*ch != '\0'){
if(*ch == ' '){
ch += 1;
}else{
if(*(ch + 1) == ' ')
{
ch += 2;
} else{
if(*ch + 1 == *(ch + 1) ){
if(*(ch + 2) == ' '){
ch += 3;
}else{
if(*(ch) + 2 == *(ch + 2))
{
tag[*ch - 'a'] ++;
ch += 1;
}else{
ch += 2;
}
}
}else{
ch += 1;
}
}
}
}
max = tag[0];
//查詢出現頻次最高的字元串
for(i = 1;i < 25;i ++){
if(tag[i] > max)
max = tag[i];
}
//輸出查詢結果
if(max == 0)
{
printf("沒有找到三個字母連續的字元串\n");
}else{
printf("出現頻次最多的有:\n");
for(i = 0;i < 25;i ++)
if(tag[i] == max)
printf("%c%c%c\n",'a' + i,'a' + i + 1,'a' + i +2);
}
return 0;
}

8. C語言順序查找程序

//順序查找
//思路:從表中最後一個記錄開始,逐個進行記錄的關鍵字和
//給定值的比較,若某個記錄的關鍵字和給定值比較相等,則
//返回返回記錄所在的位置,或查找完所有記錄後還沒有發現
//符合的記錄,則查找失敗。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define N 10

typedef int DataType;//定義比較的元素類型

//靜態查找表的順序存儲結構
typedef struct{
DataType * data;//數據元素存儲空間基址,按實際長度分配,0號單元留空
//建表時按實際長度分配,0 號單元留空
int length;//表長度
}SSTable;

//創建一個靜態表,內容為20以內的隨機數
void createST(SSTable* ST,int n){
int i;
time_t t;
if(ST!=NULL){
ST->data=(DataType*)calloc(n+1,sizeof(DataType));
if(ST->data!=NULL){
srand((unsigned) time(&t));
for(i=1;i<=n;i++){
ST->data[i]=rand() ;//產生20以內的隨機數
}
ST->length=n;
}
}
}

//創建一個靜態表,內容按從小到大排列,以便折半查找
void createST_binary(SSTable* ST,int n){
int i,j=0;
time_t t;
if(ST!=NULL){
ST->data=(DataType*)calloc(n+1,sizeof(DataType));
if(ST->data!=NULL){
for(i=1;i<=n;i++){
ST->data[i]=j;
j+=4;
}
ST->length=n;
}
}
}

//列印出靜態表的內容
void print_SSTable(SSTable* ST){
int i,n=ST->length;
if(ST!=NULL){
for(i=1;i<=n;i++){
printf("%d ",ST->data[i]);
}
printf("\n");
}
}

//順序查找(Sequential Search)
//思路:從表中最後一個記錄開始,逐個進行記錄的關鍵字和
//給定值的比較,若某個記錄的關鍵字和給定值比較相等,則
//返回返回記錄所在的位置,或查找完所有記錄後還沒有發現
//符合的記錄,則查找失敗。
//查找成功:返回記錄所在位置
//查找失敗:返回0
int search_seq(SSTable ST,DataType key){
int i;
if(ST.data==NULL)return 0;
ST.data[0]=key;//設置監視哨。目的在於免去查找過程中每一步都要檢測整
//個表是否查找完畢,是一個很有效的程序設計技巧 。監視
//哨也可以設在高下標處。
for(i=ST.length;ST.data[i]!=key;i--);
return i;
}

//折半查找(Binary Search)
//當記錄的key按關系有序時可以使用折半查找
//思路:對於給定key值,逐步確定待查記錄所在區間,每次將搜索空間減少一半(折半),
//直到查找成功或失敗為止。
int search_binary(SSTable ST,DataType key){
int low,high,mid;
low=1;
high=ST.length;
while(low<=high){//當表空間存在時
mid=(low+high)/2;
if(ST.data[mid]==key){
return mid;//查找成功,返回mid
}
if(key<ST.data[mid]){
high=mid-1;//繼續在前半區間查找
}else{
low=mid+1;//繼續在後半區間查找
}
}
return 0;//查找失敗
}

//分塊查找(只記錄思想)
//分塊查找中,設記錄表長為n,將表的n個記錄分成b=n/s個塊,每個s個記錄
//最後一個記錄數可以少於s個,且表分塊有序,即後一個塊的所有key值大於
//前一個塊的所有key值
//每塊對應一個索引項,索引項記錄了該塊記錄的最大key值和該塊第一記錄的指針(或序號)
//演算法:
//(1)由索引表確定待查找記錄所在的塊;
//(2)在塊內順序查找。

int main(){
int n=20;//在20個數中查找,方便看結果,不要設置得太大
SSTable ST,ST_binary;//分別用於順序查找和折半查找的靜態表
index indtb[n+1];//索引表,用於分塊查找
createST(&ST,n);//創建一個隨機靜態表
createST_binary(&ST_binary,n);//創建一個從小到大順序排列的靜態表

//採用順序查找
printf("原始數據:");
print_SSTable(&ST);
printf("順序查找5的結果:%d\n",search_seq(ST,5));
printf("順序查找10的結果:%d\n",search_seq(ST,10));
printf("順序查找12的結果:%d\n",search_seq(ST,12));
printf("順序查找15的結果:%d\n",search_seq(ST,15));
printf("順序查找20的結果:%d\n",search_seq(ST,20));

printf("--------------------------------------------\n");
//採用折半查找
printf("原始數據:");
print_SSTable(&ST_binary);
printf("折半查找5的結果:%d\n",search_binary(ST_binary,5));
printf("折半查找10的結果:%d\n",search_binary(ST_binary,10));
printf("折半查找12的結果:%d\n",search_binary(ST_binary,12));
printf("折半查找15的結果:%d\n",search_binary(ST_binary,15));
printf("折半查找20的結果:%d\n",search_binary(ST_binary,20));

system("pause");//暫停一下,看看結果
free(ST.data);//不要忘了釋放堆空間
return 0;
}