1. c语言题目不会在哪能搜到
如果您遇到了一些较为复杂或者比较专业的C语言编程题目,可能在一些普通的搜索引擎中不容易找到相关的答案。以下是一些可以查找C语言编程题目的网站和资源:
LeetCode(https://leetcode.com):这是一个着名的面向程序员的算法练习平台,其中包含大量难度不同的C语言编程题目,涵盖各种数据结构和算法问题。
HackerRank(https://www.hackerrank.com):HackerRank 是一个全球性的技术人才招聘和技能测评平台,在其上也包含有丰富的C语言编程题物蚂库轿蚂春。
Programiz(https://www.programiz.com):Programiz 是一个面向初学者的编程教育网站,提供了许多C语言编程题目和示例代码,适合需要初步入门的学习者。
Stack Overflow(https://stackoverflow.com):Stack Overflow 是一个知名的程序员问答社区,其中包含了大量的编程问题和解答,适合于寻找具体问题的答案和解决方案。
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语言顺序查找程序
#include
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;
}