第一篇:西北農(nóng)林科技大學(xué) c語言上機(jī) 實(shí)習(xí)6答案
實(shí)習(xí)六答案
1、整數(shù)交換函數(shù)設(shè)計(jì)
/* exer 6-1 交換2個(gè)整數(shù) */ #include
void swap(int *, int *);
/* 函數(shù)聲明 */
void main(void){
int a,b;
//printf(“please enter 2 data:”);
scanf(“%d%d”,&a,&b);
/* 輸入2個(gè)整數(shù) */
//printf(“a=%d,b=%dn”,a,b);
swap(&a,&b);
/* 調(diào)用函數(shù)進(jìn)行交換 */
printf(“%d %dn”,a,b);}
void swap(int *p1, int *p2)
/* 交換函數(shù) */ {
int temp;
temp = *p2;
/* 交換 */
*p2 = *p1;
*p1 = temp;}
2、數(shù)字字符個(gè)數(shù)統(tǒng)計(jì)函數(shù)設(shè)計(jì)
/* exer 6-2 統(tǒng)計(jì)一串字符中數(shù)字字符的個(gè)數(shù) */ #include
/* 函數(shù)聲明 */ void main(void){
char pstr[80];
//printf(“please enter string:”);
gets(pstr);
/* 輸入字符串 */
printf(“%dn”,count(pstr));/* 調(diào)用函數(shù)進(jìn)行統(tǒng)計(jì) */ }
int count(char *p)
/* 統(tǒng)計(jì)函數(shù) */ {
int num=0;
while(*p!='