计算机二级题目之选择结构练习学习

1.、在考生文件夹下,给定程序FILL.C的功能是: 统计整数n的各个位上出现数字1、2、3的次数,并通过外部(全局)变量c1、c2、c3返回主函数。   例如,当n=123114350时,结果应该为:c1=3 c2=1 c3=2。

#include   
int  c1,c2,c3;
void fun(long  n)
{  c1 = c2 = c3 = 0;while (n) {
/**********found**********/switch(___1___){
/**********found**********/case 1:    c1++;___2___;
/**********found**********/case 2:    c2++;___3___;case 3:    c3++;}n /= 10;}
}
main()
{  long  n=123114350L;fun(n);printf("\nThe result :\n");printf("n=%ld  c1=%d  c2=%d  c3=%d\n",n,c1,c2,c3);
}

2.函数fun的功能是:从三个形参a,b,c中找出中间的那个数,作为函数值返回。例如,当a=3,b=5,c=4时,中数为4。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。注意:源程序存放在考生文件夹下的BLANKl.C中。不得增行或删行,也不得更改程序的结构!

#include    
int fun(int  a, int  b, int  c)
{int  t;
/**********found**********/t = (a>b) ? (b>c? b :(a>c?c:___1___)) : ((a>c)?___2___ : ((b>c)?c:___3___));return  t;
}
main()
{  int  a1=3, a2=5, a3=4, r;r = fun(a1, a2, a3);printf("\nThe middle number is  :  %d\n", r);
}

 

 3.编写一个程序,统计一个英文句子中各小写的元音字母出现的次数。

#include 
#include 
int  vv[26];
void Vs( char* s )
{for( ; *s; s++ )
/************FOUND**********/switch( s );{  case( 'a' ):case( 'e' ):case( 'i' ):case( 'o' ):case( 'u' ):vv[ *s - 'a' ]++;}
}
main()
{char str[81], ww[6] = { "aeiou" };int i;printf( "\nPlease enter a string:\n" );gets( str );Vs( str );
/************FOUND**********/for( i = 0; i < 6; i++ )printf( "\n%c ==> %2d", ww[i], vv[ ww[i] - ww[0] ] );
}

 4.判断整数x是否为同构数,若是同构数,函数返回1,否则返回0。x的值由主函数从键盘读入,要求不大于1

#include 
#include 
int  fun ( int x )
{if(x<10)return!((x*x-x)%10);elsereturn!((x*x-x)%100);}
NONO( )
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */int i, x, y ;FILE *rf, *wf ;rf = fopen("bc1.in", "r") ;wf = fopen("bc1.out", "w") ;for(i = 0 ; i < 10 ; i++) {fscanf(rf, "%d", &x) ;y = fun( x ) ;if ( y ) fprintf (wf, "%d  YES\n", x);else fprintf (wf, "%d  NO!\n", x);}fclose(rf) ;fclose(wf) ;
}
main( )
{int  x, y;printf( "\nPlease enter a integer numbers:  " ); scanf ( "%d", &x );if ( x >100 || x < 0 ){ printf ("data error !\n"); exit(0); }y = fun ( x );if ( y ) printf ("%d  YES\n", x);else printf ("%d  NO!\n", x);NONO( ) ;
}

 

5.计算n!的阶乘;

#include int fun(int j)
{}//声明函数
void NONO ();int main( )
{int i,n;scanf("%d",&n);for(i=0;i<=n;i++)printf("%d!=%d\n",i,fun(i));NONO ();return 0;
}void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */FILE *wf ;int i,t;wf = fopen("out.dat","w") ;for(i = 6 ; i < 11 ; i++) {t=fun(i) ;fprintf(wf, "%d\n", t) ;}fclose(wf) ;
}

6.企业发放的奖金根据企业的当年利润决定。当利润I低于或等于100000元时,奖金可提成10%;利润大于100000元,小于200000元(1000001000000)时,超过1000000元的部分按1%提成。从键盘输入当年利润I,求应发放奖金总数。要求:
(1)用if语句编程序;
(2)用switch语句编程序。

 

#include 
/*计算应发放奖金的总数并返回给主函数。i为利润单位为元*/
int  fun(int i)
{}void NONO (int i)
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */FILE *wf ;int a;wf = fopen("out.dat","w") ;a=fun(150000) ;fprintf(wf, "%d\n", a) ;fclose(wf) ;
}int main( )
{int i;scanf("%d",&i);printf("bonus=%d",fun(i));NONO (i);return 0;
}

 

7.

#include 
#include 
/*fun函数完成的功能为:再加上某天的天数,并判断是否为闰年,如果是闰年且月份大于2,总天数应该加一天,并返回;
否则返回总天数*/int  fun(int sum,int day,int month,int year)
{}void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */FILE *wf ;int a;wf = fopen("out.dat","w") ;a=fun(151,18,6,2015) ;fprintf(wf, "It is the %dth day.", a) ;
}int main( )
{int day,month,year,sum;printf("\nplease input year,month,day\n");scanf("%d,%d,%d",&year,&month,&day);switch(month)        /* 先计算某月以前月份的总天数*/{case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("data error");break;}printf("It is the %dth day.",fun(sum,day,month,year));NONO ();return 0;
}

 8.输入某年某月某日,判断这一天是这一年的第几天

#include 
#include 
/*fun函数完成的功能为:再加上某天的天数,并判断是否为闰年,如果是闰年且月份大于2,总天数应该加一天,并返回;
否则返回总天数*/int  fun(int sum,int day,int month,int year)
{}void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */FILE *wf ;int a;wf = fopen("out.dat","w") ;a=fun(151,18,6,2015) ;fprintf(wf, "It is the %dth day.", a) ;
}int main( )
{int day,month,year,sum;printf("\nplease input year,month,day\n");scanf("%d,%d,%d",&year,&month,&day);switch(month)        /* 先计算某月以前月份的总天数*/{case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("data error");break;}printf("It is the %dth day.",fun(sum,day,month,year));NONO ();return 0;
}

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部