打印一隻魚
#include<stdio.h>
int main()
{
printf("\n\
** *\n\
******* **\n\
************* ***\n\
****************** *****\n\
*******@******************\n\
****************** *****\n\
************* ***\n\
******* **\n\
** *\n");
return 0;
}
運行結果
知識補充
\n 換行符
\ 告訴編譯器要把一行代碼分成多行寫,不然編譯器會報錯
計算同目錄下所有文件中代碼行數
程序要求:統計當前目錄及所有子目錄下,C 語言源文件的代碼總行數
代碼
#include<io.h>
#include<direct.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 256
long total;
int countlines(const char *filename);
void findAllCodes(const char *path);
void findAllFiles(const char *path);
int countLines(const char *filename)
{
FILE *fp;
int count=0;
int temp;
if((fp=fopen(filename,"r"))==NULL)
{
fprintf(stderr,"Can not open the file:%s\n",filename);
return 0;
}
while((temp=fgetc(fp))!=EOF)
{
if(temp=='\n')
{
count++;
}
}
fclose(fp);
return count;
}
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath[MAX],target[MAX];
strcpy(thePath,path);
if((handle=_findfirst(strcat(thePath,"/*.cpp"),&fa))!=-1L)//和小甲魚給的不同因爲我的c文件後綴是.cpp
{
do
{
sprintf(target,"%s/%s",path,fa.name);
total +=countLines(target);
}while (_findnext(handle,&fa)==0);
}
_findclose(handle);
}
void findAllDirs(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath[MAX];
strcpy(thePath,path);
if((handle=_findfirst(strcat(thePath,"/*"),&fa))==-1L)
{
fprintf(stderr,"The path %s is wrong!\n",path);
return;
}
do
{
if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
continue;
if(fa.attrib==_A_SUBDIR)
{
sprintf(thePath,"%s/%s",path,fa.name);
findAllCodes(thePath);
findAllDirs(thePath);
}
}while(_findnext(handle,&fa)==0);
_findclose(handle);
}
int main()
{
char path[MAX]=".";
printf("計算中...\n");
findAllCodes(path);
findAllDirs(path);
printf("目前你總共寫了%ld行代碼!\n\n",total);
system("pause");
return 0;
}
運行結果
寫一個 printf 函數,要求在屏幕上打印下邊內容:
你知道以下程序打印的是什麼內容嗎?
#include <stdio.h>
int main()
{
printf("Hello world\b\b\b\b\bFishC\n");
return 0;
}