C和指针——第七章 函数 (笔记+习题答案)

  • 第7章 函数
    • 7.6递归
      • 当函数被调用时,它的变量空间是创建运行在堆栈上的。以前调用的函数的变量仍然保留在堆栈上,但它们被新的函数变量所掩盖,因此不能访问
    • 7.7可变参数列表
      • 一个值的类型无法简单地通过检查它的位模式来判断
        • 1.这些宏无法判断实际存在的参数类型
        • 2.这些宏无法判断每个参数的类型
    • 7.8总结
      • 1。如果一个函数没有返回值,这类函数称为“过程”,返回类型应该是void
      • 2.当一个函数被调用时,编译器如果无法看到它的任何声明,就假定函数返回一个整型值。对于那些没有原型的函数,传递给函数的实参将进行缺省参数提升:char和short转换成int类型,float->double
      • 3.抽象数据类型由接口和实现两个部分组成,实现是私有的,接口是公有的
      • 可变参数列表包含可变参数数量和类型,用stdarg.h头文件中宏定义。
    • 7.9问题
        • 当存根函数被调用时,打印一条消息,显示它被调用,或者也可以打印作为参数传递给它的值。
        • 优点是它让你变得懒惰;需要编写的代码更少。其他后果,例如能够调用带有错误数字或参数类型的函数,都是缺点。
        • 该值被转换为函数指定的类型。该标准表明,这与将值赋给该类型的变量是一样的。
        • 这是不允许的;编译器应该给出一个错误消息
        • 解答:
          5.返回的值被当作一个整数来解释。
          6.实参值被解释为形式形参的类型,而不是它们的真实类型。
          7.不应该使用特定大小的数组作为形参。
          8.迭代和递归必须设置一些目标,达到就终止执行,每一次执行,都会靠近目标。
          9.a.在几个源文件中使用#include比复制原型更容易。b.原型本身只有一个副本。c.在定义函数的文件中包含原型可以确保它们是匹配的。
      • //计算指定数量的值的平均值
        //#include<stdarg.h>
        //#include<stdio.h>
        //
        //float average(int n_values, ...) {
        //	va_list var_arg;
        //	int count;
        //	float sum = 0;
        //	//准备访问可变参数
        //	va_start(var_arg, n_values);
        //	for (count = 0; count < n_values; count += 1) {
        //		sum += va_arg(var_arg, int);
        //	}
        //	//完成处理可变参数
        //	va_end(var_arg);
        //	return sum / n_values;
        //}
        //int main() {
        //	float avg = 0;
        //	avg = average(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        //	printf("%f", avg);
        //	return 0;
        //}
        //1.
        //#include<stdio.h>
        //int Hermite_Polynomials(int num,int x) {
        //	if (num <= 0) {
        //		return 1;
        //	}
        //	if (num == 1) {
        //		return 2 * x;
        //	}
        //	return 2 * x*Hermite_Polynomials(num - 1, x) - 2 * (num - 1)*Hermite_Polynomials(num - 2, x);
        //
        //}
        //int main() {
        //	int result = Hermite_Polynomials(3, 2);
        //	printf("%d", result);
        //	return 0;
        //}
        
        //2.
        //int
        //gcd(int m, int n)
        //{
        //	int  r;
        //	if(m < = 0 || n < = 0)
        //		return 0;
        //	r = m % n;
        //	return r > 0 ? g  c  d(n, r) : n;
        //}
        
        //3.
        //int ascii_to_integer(char *string) {
        //	int value = 0;
        //	while (*string >= '0'&&*string <= '9') {
        //		value *= 10;
        //		value += *string - '0';
        //		string++;
        //		
        //	}
        //	if (*string != '\0')
        //		value = 0;
        //	return value;
        //}
        
        //4.
        //#include<stdarg.h>
        //int max_list(int first_arg, ...)
        //{
        //	va_list var_arg;
        //	int  max = 0;
        //	
        //	if (first_arg >= 0) {
        //		int  this_arg;
        //		max = first_arg;
        //	
        //		va_start(var_arg, first_arg);
        //		while ((this_arg = va_arg(var_arg, int)) >=0)
        //			if (this_arg > max)
        //				max = this_arg;
        //		va_end(var_arg);
        //	}
        //	return max;
        //}
        
        //5.
        //#include <stdarg.h>
        //#include<stdio.h>
        //
        //void
        //printf(char *format, ...)
        //{
        //	va_list arg;
        //	char  ch;
        //	char  *str;
        //	va_start(arg, format);
        //	/*
        //	** Get the format characters one by one.
        //	*/
        //	while ((ch = *format++) != '\0') {
        //		if (ch != '%') {
        //			/*
        //			** Not a format code –– print the character verbatim.
        //			*/
        //			putchar(ch);
        //			continue;
        //		}
        //		/*
        //		*  * W  e g  o  t a % –  – n  o  w g  e  t t  h  e format code and use it to format
        //		** the next argument.
        //		*/
        //		switch (*format != '\0' ? *format++ : '\0') {
        //		case 'd':
        //			print_integer(va_arg(arg, int));
        //			break;
        //		case'f':
        //			print_float(va_arg(arg, float));
        //			break;
        //		case 'c':
        //			putchar(va_arg(arg, int));
        //			break;
        //		case 's':
        //			str = va_arg(arg, char *);
        //			while (*str != '\0')
        //				putchar(*str++);
        //			break;
        //		}
        //	}
        //}
        
        
        //6.
        #include<string.h>
        static const char  *digits[] = {"", "ONE ", "TWO ", "THREE ", "FOUR ", "FIVE ", "SIX ", "SEVEN ",
        "EIGHT ", "NINE ", "TEN ", "ELEVEN ", "TWELVE ", "THIRTEEN ",
        "FOURTEEN ", "FIFTEEN ", "SIXTEEN ", "SEVENTEEN ", "EIGHTEEN ",
        "NINETEEN "
        };
        static const  char  *tens[] = {
        "", "", "TWENTY ", "THIRTY ", "FORTY ", "FIFTY ", "SIXTY ", "SEVENTY ",
        "EIGHTY ", "NINETY "
        };
        static const char  *magnitudes[] = {
        "", "THOUSAND ", "MILLION ", "BILLION "
        };
        /*
        **  Convert the last 3–digit group of amount to words.  Amount is the value
        **  to be converted, buffer is where to put the words, and magnitude is the
        **  name of the 3–digit group we’re working on.
        */
        static void
        do_one_group(unsigned int amount, char *buffer, char **magnitude)
        {
        	int  value;
        	/*
        	** Get all the digits beyond the last three.  If we have any value
        	** there, process those digits first.  Note that they are in the next
        	** magnitude.
        	*/
        	value = amount / 1000;
        	if (value > 0)
        		do_one_group(value, buffer, magnitude + 1);
        	/*
        	** Now process this group of digits.  Any hundreds?
        	*/
        	amount %= 1000;
        	value = amount / 100;
        	if (value > 0) {
        		strcat(buffer, digits[value]);
        		strcat(buffer, "HUNDRED ");
        	}
        	/*
        	** Now do the rest of the value.  If less than 20, treat it as a single
        	** digit to get the teens names.
        	*/
        	value = amount % 100;
        	if (value >= 20) {
        		/*
        		** Greater than 20.  Do a tens name and leave the units to be
        		** printed next.
        		*/
        		strcat(buffer, tens[value / 10]);
        		value %= 10;
        	}
        	if (value > 0)
        		strcat(buffer, digits[value]);
        	/*
        	** If we had any value in this group at all, print the magnitude.
        	*/
        	if (amount > 0)
        		strcat(buffer, *magnitude);
        }
        void
        written_amount(unsigned int amount, char *buffer)
        {
        	if (amount == 0)
        		/*
        		** Special case for zero.
        		*/
        		strcpy(buffer, "ZERO ");
        	else {
        		/*
        		** Store an empty string in the buffer, then begin.
        		*/
        		*buffer = '\0';
        		do_one_group(amount, buffer, (char **)magnitudes);
        	}
        }

         

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值