/**********************************************************
成都創(chuàng)新互聯(lián)網(wǎng)站建設由有經(jīng)驗的網(wǎng)站設計師、開發(fā)人員和項目經(jīng)理組成的專業(yè)建站團隊,負責網(wǎng)站視覺設計、用戶體驗優(yōu)化、交互設計和前端開發(fā)等方面的工作,以確保網(wǎng)站外觀精美、成都網(wǎng)站建設、做網(wǎng)站易于使用并且具有良好的響應性。
讀入一個整數(shù),統(tǒng)計并輸出該數(shù)中某個數(shù)的個數(shù)?要求定義并調(diào)
用函數(shù)countdigit(number,digit),它的功能是統(tǒng)計整數(shù)number中
數(shù)字digit的個數(shù)?例如,countdigit(10090,0)的返回值是3?【輸入
輸出樣例1】(下劃線部分表示輸入)
Enter an number:21252
Enter an digit:2
Number of digit 2: 3
************************************************************/
#includestdio.h
void main()
{?
int?countdigit(int number,int digit);
int num,dig;
printf("Enter a number:");
scanf("%d",num);
printf("Enter a digit:");
scanf("%d",dig);
printf("Number of digit %d:%d\n",dig,countdigit(num,dig));
}
int countdigit(int number,int digit)
{
int z=0,s;
while(number != 0)
{
s=number%10;
if(s == digit)
{
z++;
}
number=number/10;
}
return z;
}
看用什么方法輸入數(shù)據(jù),每成功輸入1個數(shù)據(jù),你用累加器加1。
常用輸入數(shù)據(jù)函數(shù)是 scanf(), 這個函數(shù)能返回成功讀入的數(shù)據(jù)個數(shù)。
例如: n = scanf("%d %f %lf %s", k, a, x, s);
成功讀入4個,n得4,成功讀入3個,n得3,。。。成功讀入1個,n得1,
一個也沒成功,n得 0。
如果循環(huán)讀入:
int sum=0, i=0;
while(...){
n = scanf("%d %f %lf %s", k[i], a[i], x[i], s[i]);
sum = sum + n;
i++;
}
printf("成功讀入的數(shù)據(jù)個數(shù)是%d\n",sum);
第一種方法,設置一個結(jié)構(gòu)體變量的成員為某個具體的常量,進行遍歷尋找得出變量的數(shù)量
第二種方法,在輸入時計算
第三種,建立一個有指針域的動態(tài)鏈表
用第三種方法實現(xiàn)的一個例子,可用來學籍管理系統(tǒng)
#include
stdio.h
#include
stdlib.h
#include
conio.h
typedef
struct
student
{
int
num;
char
name[20];
char
sex[3];
struct
student
*next;
}LIST;
LIST
*CreateList()
{
int
i,n;
LIST
*head=NULL,*pre,*cur;
printf("請輸入學生的總個數(shù):");
scanf("%d",n);
for(i=1;i=n;i++)
{
printf("正在輸入第%d個學生數(shù)據(jù)!\n",i);
pre=(LIST
*)malloc(sizeof(LIST));
pre-next=NULL;
printf("請輸入該學生的姓名:");
scanf("%s",pre-name);
printf("請輸入該學生的學號:");
scanf("%d",pre-num);
printf("請輸入該學生的性別:");
scanf("%s",pre-sex);
if(NULL==head)
head=pre;
else
cur-next=pre;
cur=pre;
}
return
head;
}
int
GetNum(LIST
*head)
{
int
i=1;
while(head-next!=NULL)
{
i++;
head=head-next;
}
return
i;
}
void
Disp(LIST
*head)
{
int
i=1;
printf("正在輸出學生信息!\n");
while(head-next!=NULL)
{
printf("正在輸入第%d個學生的數(shù)據(jù)!\n",i++);
printf("學生姓名:\t%s\n學生學號:\t%d\n學生性別:\t%s\n",head-name,head-num,head-sex);
head=head-next;
}
printf("正在輸入第%d個學生的數(shù)據(jù)!\n",i++);
printf("學生姓名:\t%s\n學生學號:\t%d\n學生性別:\t%s\n",head-name,head-num,head-sex);
printf("學生信息輸出結(jié)束!\n");
}
void
DestroyList(LIST
*head)
{
LIST
*pre=head;
while(head-next!=NULL)
{
pre=pre-next;
free(head);
head=pre;
}
}
int
main(void)
{
LIST
*head;
head=CreateList();
printf("一共有%d個學生數(shù)據(jù)!\n",GetNum(head));
Disp(head);
DestroyList(head);
getch();
return
0;
}
滿意采納,不滿意追問
#includestdio.h
#includestring.h
main()
{
int?i,j=0,k;
char?a[1000];//長度自己根據(jù)實際情況調(diào)整
printf("請輸入一串字符串:\n");
gets(a);
k=strlen(a);
for(i=0;ik;i++)
if('0'=a[i]='9')
j++;
printf("這串字符串中數(shù)字字符有%d個!\n",j);
}
#include?stdio.h
#include?string.h
int?conNumfromStr(char?*,int);
int?main()
{
char?str[21];
printf("輸入20以內(nèi)的字符:");
scanf("%s",str);
printf("字符串中數(shù)字字符個數(shù)為:%d",conNumfromStr(str,strlen(str))?);
return?0;
}
int?conNumfromStr(char?*p,int?len)//計數(shù)字符串中數(shù)字字符的個數(shù)
{
int?i,con=0;
for(i=0;ilen;i++)
{
if(p[i]='0'??p[i]='9')
con++;
}
return?con;
}
分享標題:c語言計算數(shù)字個數(shù)函數(shù),c語言統(tǒng)計數(shù)字個數(shù)函數(shù)
標題網(wǎng)址:http://muchs.cn/article40/hciceo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、企業(yè)網(wǎng)站制作、微信公眾號、移動網(wǎng)站建設、全網(wǎng)營銷推廣、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)