重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
#include stdio.h
創新互聯公司基于分布式IDC數據中心構建的平臺為眾多戶提供眉山聯通機房 四川大帶寬租用 成都機柜租用 成都服務器租用。
#include string.h
int count(int x)
{
int i;
int n = 0;
for(i = 1; ix; i++)
if(x % i == 0)
n ++;
return n;
}
int main(void)
{
int x;
int num;
scanf("%d",x);
num = count(x);
printf("num = %d\n",num);
}
可以參考下面的代碼:#include stdio.hintmain(){inta,b,c,ch;a=b=c=0;//計數器初始化為0.while((ch=getchar())!='\n')//循環讀取字符,到換行結束。{if(ch='0' ch='9')//數字a++;else if((ch='a' ch='z')||(ch='A' ch='Z'))//字母b++;else//其它c++;}printf("%d%d%d\n",a,b,c);//輸出結果。return0;}擴展資料:printf()函數函數printf()函數是格式化輸出函數, 一般用于向標準輸出設備按規定格式輸出信息。在編寫程序時經常會用到此函數。函數的原型為:int printf(const char *format, ...);函數返回值為整型。若成功則返回輸出的字符數,輸出出錯則返回負值,printf()函數的調用格式為:printf("格式化字符串", 參量表);while語句的一般表達式為:while(表達式){循環體}。參考資料來源:百度百科-printf()參考資料來源:百度百科-while (循環語句及英文單詞)
他們的區別在于,第一種方法中,count是個局部變量,只能在main函數中使用,第二種方法里count是個全局變量,比如下面的兩段程序
int
count=0;
int
main()
{}
int
a()
{}
-------------
int
main()
{
int
count=0;
........}
int
a()
{}
--------------
第一段里函數a里可以使用count
第二段里的函數a則無法使用count
#include?bits/stdc++.h
#define?LDB?long?double
using?namespace?std;
LDB?count(int?a[]){
LDB?tot=0;
for?(int?i=0;i10;i++)?tot+=a[i];
tot/=10;
return(tot);
}
int?main(){
int?arr[10];
printf("%.3Lf\n",count(arr));
}
C語言輸入輸出函數有很多,標準I/O函數中包含了如下幾個常用的函數:
scanf,printf,getc,putc,getchar,putchar,gets,puts,fgets,fputs,fgetc,fputc,fscanf,fprintf等.
int
scanf(const
char
*format,
arg_list)
scanf主要從標準輸入流中獲取參數值,format為指定的參數格式及參數類型,如scanf("%s,%d",str,icount);
它要求在標準輸入流中輸入類似"son
of
bitch,1000"這樣的字符串,同時程序會將"son
of
bitch"給str,1000給icount.
scanf函數的返回值為int值,即成功賦值的個數,在上例中如果函數調用成功,則會返回2,所以我們在寫程序時,可以通過
語句if(scanf("%s,%d",str,icount)
!=
2){...}來判斷用戶輸入是否正確.
int
printf(const
char
*format,
arg_list)
printf主要是將格式化字符串輸出到標準輸出流中,在stdio.h頭文件中定義了標準的輸入和輸出,分別是stdin,stdout.
arg_list可以是變量名,也可以是表達式,但最終都會以值的形式填充進format中.
int
getc(FILE
*fp)
getc主要是從文件中讀出一個字符.常用的判斷文件是否讀取結束的語句為:(ch
=
getc(fp))
!=
EOF.EOF為文件結束標志,
定義在stdio.h中,就像EXIT_SUCCESS,EXIT_FAILURE定義在stdlib.h中一樣,文件也可以被理解為一種流,所以當fp為stdin
時,getc(stdin)就等同于getchar()了.
int
putc(int
ch,FILE
*fp)
putc主要是把字符ch寫到文件fp中去.如果fp為stdout,則putc就等同于putchar()了.
int
getchar(void)
getchar主要是從標準輸入流讀取一個字符.默認的標準輸入流即stdio.h中定義的stdin.但是從輸入流中讀取字符時又
涉及到緩沖的問題,所以并不是在屏幕中敲上一個字符程序就會運行,一般是通過在屏幕上敲上回車鍵,然后將回車前的字符
串放在緩沖區中,getchar就是在緩沖區中一個一個的讀字符.當然也可以在while循環中指定終止字符,如下面的語句:
while
((c
=
getchar())
!=
'#')這是以#來結束的.
int
putchar(int
ch)
putchar(ch)主要是把字符ch寫到標準流stdout中去.
char
*
gets(char
*str)
gets主要是從標準輸入流讀取字符串并回顯,讀到換行符時退出,并會將換行符省去.
int
puts(char
*str)
puts主要是把字符串str寫到標準流stdout中去,并會在輸出到最后時添加一個換行符.
char
*fgets(char
*str,
int
num,
FILE
*fp)
str是存放讀入的字符數組指針,num是最大允許的讀入字符數,fp是文件指針.fgets的功能是讀一行字符,該行的字符數
不大于num-1.因為fgets函數會在末尾加上一個空字符以構成一個字符串.另外fgets在讀取到換行符后不會將其省略.
int
fputs(char
*str,
file
*fp)
fputs將str寫入fp.fputs與puts的不同之處是fputs在打印時并不添加換行符.
int
fgetc(FILE
*fp)
fgetc從fp的當前位置讀取一個字符.
int
fputc(int
ch,
file
*fp)
fputc是將ch寫入fp當前指定位置.
int
fscanf(FILE
*fp,
char
*format,...)
fscanf按照指定格式從文件中出讀出數據,并賦值到參數列表中.
int
fprintf(FILE
*fp,
char
*format,...)
fprintf將格式化數據寫入流式文件中.
#include stdio.h
int count(char* str);
int main(void)
{
char s1[10000] = { '\0' }, s2[10000] = { '\0' };
printf("輸入字符串 s1:");
scanf("%s", s1);
printf("輸入字符串 s2:");
scanf("%s", s2);
printf("s1中小寫字母個數:%d\ns2中小寫字母個數:%d", count(s1), count(s2));
return 0;
}
//
int count(char* str)
{
int count = 0;
while (*str)
{
if ((*str = 'a') (*str = 'z'))
count++;
str++;
}
return count;
}