3.3 形参和实参
1、定义一个函数increment,它的作用是将传进来的参数加1,然后在main函数中用increment函数来增加变量的值:
void increment(int x)
{
x = x + 1;
}
int main(void)
{
int i = 1, j = 2;
increment(i); /* now i becomes 2 */
increment(j); /* now j becomes 3 */
return 0;
}
这个increment函数能奏效吗?为什么?
答:不能,因为在函数increment()内部,是形参x自加1,并没有将x赋给i或j,而在函数increment()调用完后,函数的栈就自行释放,x就算自加1,也是被释放掉,所以该自加1就没有任何意义,注意:变量i,j和形参x是不同的量。
正确的做法:
#include <stdio.h>
int increment(int x)
{
x=x+1;
return x;
}
int main(void)
{
int i=1,j=2;
i=increment(i);
j=increment(j);
printf("i=%d,j=%d\n",i,j);
return 0;
}
4.1 if语句
1、以下是程序段编译能通过,执行也不出错,但是执行结果不正确(根据第 3节“程序的调试”,这是一个语义错误),请分析一下哪里错了。还有,既然错了为什么编译能通过呢?
int x = -1;
if (x > 0);
printf("x is positive.\n");
错在if后面的条件表达式后多了个;号。能编译成功是因为这不是一个语义错误,而是一个逻辑错误。
4.2 if/else语句
1、写两个表达式,分别取整数x的个位和十位。
2、写一个函数,参数是整数x,功能是打印参数x的个位和十位。
解:个位u=a%10;
十位d=((a%100)-u)/10
/*Example:
* please enter a integer:
* 34
* the tens is 3,the units is 4.
* */
/*Type Contract
* (number)->(string)
* */
#include <stdio.h>
/*This programme will give the units and tens of the integer we enter*/
int main(void)
{
int a,u,d;
/* int m=0;*/
printf("please enter a integer.\n");
scanf("%d",&a);
u=a%10;
d=((a%100)-u)/10;
printf(" the tens is %d,the units is %d.\n",d,u);
/*
int i=0;
for(i=0;;i++)
{
if(a/(10^i)==0)
m=i;
printf("this number is a %d-digit integer.\n",m);
break;
}
*/
return 0;
}
5.1 return语句
1、编写一个布尔函数int is_leap_year(int year),判断参数year是不是闰年。如果某一年的年份能被4整除,但不能被100整除,那么这一年就是闰年,此外,能被400整除的年份也是闰年。
/*This program will determine whether a entered integer reprents a leap year.
* If it can be devided by 4 but can't be devided by 100,it is a leap year;
* besides, it can be devided by 400,it is also a leap year.
* Also,this program will list the situation of whether it is a leap year from 1990 to 2020.
* */
#include <stdio.h>
int is_leap_year(int year)
{
if((year%4==0)&&(year%100!=0))
{
return 1;
}
else if(year%400==0)
{
return 1;
}
else
return 0;
}
/*Function name:print_year
* Input parameter:
* b:int,can only be 0 or 1,indecates the controlling expression
* intyear:int,represents the number of year we entered after the terminal has reminded us
* Output:a string printed by c-function 'printf'
* Function:print whether the year is a leap year
*
* */
void print_year(int b,int intyear)
{
if (b==0)
printf("%d is not a leap year.\n",intyear);
else
printf("%d is a leap year.\n",intyear);
}
int main(void)
{
int a,b;
printf("please enter an integer to judge whether it is a leap year.\n");
scanf("%d",&a);
b=is_leap_year(a);
print_year(b,a);
int i=1990;
for(i=1990;i<=2020;i++)
{
int b=is_leap_year(i);
print_year(b,i);
}
return 0;
}
5.3递归
1、编写递归函数求两个正整数a和b的最大公约数(GCD,GreatestCommon Divisor),使用Euclid算法:
1.如果a除以b能整除,则最大公约数是b。
2.否则,最大公约数等于b和a%b的最大公约数。
/*
* This program will get the Greatest Common Divisor(GCD) of two positive integers, with the applying of Euclid arithmetic:
* 1.If a devides b with no reminder,the GCD is a
* 2.Else,GCD equles the GCD of b and (a%b)
* */
#include <stdio.h>
int GCD(int a,int b)
{
if(a%b==0)
return b;
else
{
int recurse=GCD(b,a%b);
return recurse;
}
}
int main(void)
{
int n1,n2,gcd;
printf("please enter 2 different integer.\n");
scanf("%d%d",&n1,&n2);
gcd=GCD(n1,n2);
printf("The Greatest Common Divisor if %d and %d is %d.\n",n1,n2,gcd);
return 0;
}
2、编写递归函数求Fibonacci数列的第n项,这个数列是这样定义的:
fib(0)=1
fib(1)=1
fib(n)=fib(n-1)+fib(n-2)
/*
*This program will find out the n th item of the Fibonacci Series.
fib(0)=1
fib(1)=1
fib(n)=fib(n-1)+fib(n-2)
What is Fibonacci Series?
every item,except the 1st and the 2nd, equals the summary of it's prvent two items.
1,1,2,3,5,8,13,21,34,33,89,144...
* */
#include <stdio.h>
int fibonacci(int n)
{
if(n==0)
return 1;
else if(n==1)
return 1;
else
{
int recurse1=fibonacci(n-1);
int recurse2=fibonacci(n-2);
int result=recurse1+recurse2;
return result;
}
}
int main(void)
{
int a,b;
printf("which item of the Fibonacci Series whould you like to know,please enter it.\n");
scanf("%d",&a);
b=fibonacci(a);
printf("the %d .th item of Fibonacci Series is %d.\n",a,b);
/*List the previous n items of the F series,n can be get from the keyboard*/
int c;
int i=1;
printf("Would you like to get the previous n items of the Fibonacci Series?give us a number.\n");
scanf("%d",&c);
for(i=1;i<=c;i++)
{
int d=fibonacci(i);
printf("No.%d item is:%d.\n",i,d);
}
return 0;
}