C和指针课后习题(第七章)
来源:互联网 发布:东莞网络推广 编辑:程序博客网 时间:2024/05/02 03:12
7.1
/*The Hermite Polynomials are defined as follows:Write a recursive function to compute thevalue of Hn(x). Your function should match this prototype:int hermite( int n, int x );*/#include <stdio.h>#include <stdlib.h>int hermite(int n,int x){if(n<=0)return 1;else if(n==1)return 2*x;elsereturn 2*x*hermite(n-1,x)-2*(n-1)*hermite(n-2,x); }int main(){int n,x;printf("please input a n and x:\n");scanf("%d%d",&n,&x);printf("the result of hermite is %d\n",hermite(n,x));return 0;}
7.2
递归
#include <stdio.h>#include <stdlib.h>#include <assert.h>int gcd(int m,int n){int a,b;int R;if(m>n){a = m;b = n;}else{a = n;b = m;}if(a<=0||b<=0)return 0;else if(0==a%b)return (b);else{R = a%b;while(R>0){a = b;b = R; return gcd(a,b);}}}int main(){int a,b;printf("please input 2 numbers:\n");scanf("%d%d",&a,&b);printf("the result is %d\n",gcd(a,b));return 0;}
7.3
#include <stdio.h>#include <string.h>#include <malloc.h>#define MAX_LEN 128int ascii_to_integer(char *string){int string_len = 0;int number = 0;int i=0;//printf("string = %s\n",string);//printf("hello world!\n");while(string[i++]!='\0')string_len++;printf("the length of string is %d\n",string_len);for(i=0;i<string_len;i++)if(!isdigit(string[i]))return 0;elsenumber = number*10 + string[i]-'0';printf("number is %d\n",number);return number;}int main(){char *string;//memset(string,0,10);int k;string = (char *)malloc(sizeof(char)*MAX_LEN);memset(string,0,MAX_LEN);printf("please input a string:");scanf("%s",string);printf("the input string is %s\n",string);printf("the string means number is:\n ");k = ascii_to_integer(string);printf("%d\n",k);return 0;}
7.4
#include <stdio.h>#include <stdarg.h>#include <stdlib.h> /* 说明: 本题目中没有表示长度的第一个参数,但是使用stdarg需要在列表前有一个参数。 解决方法:将列表的第一个元素 作为使用stdarg需要的第一个参数。 而且,由于列表以负数结尾,即如果第一个元素为负数时,表示列表中没有元素; */ int max_list( int first, ... ); int main(){ /* 测试max_list函数 */ int max = max_list(2, 4, 5, 6, 7, 1, -1); printf("%d\n", max); max = max_list(-1); printf("%d\n", max); max = max_list(2, -10); printf("%d\n", max); getchar(); return EXIT_SUCCESS; } int max_list( int first, ... ){ va_list arg; int max; /* 用来保存结果 */ /* first为负数时,说明列表为空,返回0 */ if( first<0 ){ return 0; } /* 用va_start初始化arg, 第一个参数为va_list的名字,第二个为省略号前的最后一个变量 */ va_start( arg, first ); /* 寻找最大数 */ max = first; int temp = va_arg( arg, int ); while( temp>=0 ){ if( temp>max ){ max = temp; } temp = va_arg(arg, int); } /* 结束时要调用va_end */ va_end(arg); return max; }
7.5
#include <stdio.h>#include <stdarg.h>#include <stdlib.h>void print(char *format,...){va_list arg;char ch;char *str;va_start(arg,format);//get the format one by onewhile((ch=*format++)!='\0'){if(ch!='%'){//not a format codeputchar(ch);continue;}//format codeswitch(*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;}}}int main(){//int number;//printf("please input a number:\n");//scanf("%d",&number);//printf("the number is %d\n",number);return EXIT_SUCCESS;}上面这个程序没有写完,感觉自己对变参数理解的不到位
7.6
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h>#define MAX_LEN 128static char *digits[] = {"","ONE ","TWO ","THREE ","FOUR ","FIVE ","SIX ","SEVEN ","EIGHT ","NINE ","TEN ","ELEVEN ", "TWELVE ", "THIRTEEN ","FOURTEEN ", "FIFTEEN ", "SIXTEEN ","SEVENTEEN ", "EIGHTEEN ","NINETEEN"};static char *tens[] = {"", "", "TWENTY ", "THIRTY ", "FORTY ","FIFTY ", "SIXTY ", "SEVENTY ","EIGHTY ", "NINETY "};static 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;value = amount / 1000;if(value>0)do_one_group(value,buffer,magnitude+1);amount = amount % 1000;value = amount / 100;if(value > 0){strcat(buffer,digits[value]);strcat(buffer,"HUNDRED ");}value = amount % 100;if(value >= 20){strcat(buffer,tens[value / 10]);value = value % 10;}if(value>0)strcat(buffer,digits[value]);if(amount>0)strcat(buffer,*magnitude);}void written_amount(unsigned int amount,char *buffer){if(amount == 0)strcpy(buffer,"ZERO ");else{do_one_group(amount,buffer,magnitudes);}printf("the string of number is %s\n",buffer);}int main(){unsigned int number;printf("please input a number:\n");scanf("%d",&number);char *buffer = (char *)malloc(sizeof(char)*MAX_LEN);memset(buffer,0,MAX_LEN);written_amount(number,buffer);return EXIT_SUCCESS;}
0 0
- C和指针课后习题(第七章)
- C和指针课后习题(第四章)
- C和指针课后习题(第五章)
- C和指针课后习题(第六章)
- C和指针课后习题(第八章)
- C和指针课后习题
- C++Primer(第七章课后习题程序题源代码)
- C和指针第九章课后习题编程练习
- C++Primer 中文版 第五版 第七章课后习题答案
- 《C语言程序设计教程》(主编黄迪明、余勤)第七章课后习题答案
- C++程序设计课后习题及解答(第七章)
- C和指针读书笔记(第七章)
- Java程序设计2 第七章课后习题
- 算法导论课后习题解析 第七章
- 第4章 数组和指针 课后习题
- c 和 指针 第七章
- c 和 指针 第七章
- 《C和指针》第一章课后
- 先写一篇吧。
- 顺序查找
- Adobe Gaming SDK 内置ProductStore详解
- web.xml权限配置
- 使用Chrome DevTools的Timeline和Profiles提高Web应用程序的性能
- C和指针课后习题(第七章)
- Linux多线程中使用信号-1
- 字符串中查找字符串的位置
- Cygwin学习笔记之wget
- 使用Logstash + Elasticsearch作为大数据索引、分析工具
- Storyboard会加载很多系统类库
- 理解MySQL——索引与优化
- 初学cocos2dx 写了个2048 跟大家一起分享下
- 黑马程序员-银行业务调度系统(自己写的)目前还有异常,不知道怎么处理?