第一题
#include<stdio.h>
int hermite(int n, int x);
int main(void) {
int h = hermite(3, 2);
printf("%d\n", h);
return 0;
}
int hermite(int n, int x) {
if (n <= 0)
return 1;
else if (n == 1)
return 2 * x;
else
return 2 * x * hermite(n - 1, x) - 2 * (n - 1) * hermite(n - 2, x);
}
第二题
#include<stdio.h>
int gcd(int M, int N);
int main(void) {
int c = gcd(142,102);
printf("%d\n", c);
return 0;
}
int gcd(int M, int N) {
int R=M%N;
if (M < 0 || N < 0)
return 0;
if (R == 0)
return N;
else
return gcd(N, R);
}
3.为下面这个函数原型编写函数定义:
int ascii._to_integer(char* string)
这个字符串参数必须包含一个或多个数字, 函数应该把这些数字字符转换为整数井返回这个整数。如果字符串参数包含了任何非数字字符,函数就返回零,不必担心算术溢出。提示:这个技巧很简单一每发现一 个数字, 就把当前值乘以10.并把这个值和新数字所代表的值相加。
#include<stdio.h>
#include<string.h>
#define SIZE 10
int ascii_to_integer(char* string);
void s_gets(char* st, int n);
int main(void) {
char string[SIZE] = { 0 };
int a;
s_gets(string, SIZE);
if (string[0] != '\0')
a = ascii_to_integer(string);
printf("%d\n", a);
return 0;
}
int ascii_to_integer(char* string) {
int i = 0;
int h = 1;
int sum = 0;
while (string[i] != '\0') {
if (string[i] <= '/' || string[i] >= ':')
return 0;
h *= 10;
i++;
}
h /= 10;
i = 0;
while (string[i] != '\0') {
sum += (string[i] - 48) * h;
i++;
h /= 10;
}
return sum;
}
void s_gets(char* st, int n) {
char* ret_val;
char* find;
ret_val = fgets(st, n, stdin);/*和gets基本一样 fgets读到换行符会存储在字符串中 gets会丢弃换行符*/
if (ret_val) {
find = strchr(st, '\n');/*查找指定字符*/
if (find)
*find = '\0';
else while (getchar() != '\n')/*筛选掉多出的字符*/
continue;
}
}
4.编写一个名叫max list 的函数,用于检查任意数目的整型参数并返回它们中的最大值。
参数列表必须以个负值结尾, 用于提示列表的结束。
#include<stdio.h>
#include<stdarg.h>
int max_list(int list, ...);
int main(void) {
int max = max_list(7, 5, 9, 8, 3, 4, 10, -1);
printf("%d\n", max);
return 0;
}
int max_list(int list, ...) {
va_list ap;
va_start(ap, list);
int max = 0;
for (int i = 0; i < list; i++) {
int a = va_arg(ap, int);
if(a>0)
max = max < a ? a : max;
}
va_end(ap);
return max;
}
5.实现一个简化的printf函数,它能够处理%d、%f %s和%c格式码.根据ANSI标准的原
则,其他格式码的行为是未定义的。可以假定已经存在函数print integer 和print float,用于打印这些类型的值。对于另外两种类型的值,使用putchar来打印。
#include<stdio.h>
#include<stdarg.h>
void printf_ifcs(char* str, ...);
void print_integer(int i);
void print_float(float f);
int main(void) {
printf_ifcs("%d %f %c %s",1314,3.14,'Y',"order");
return 0;
}
void printf_ifcs(char* str, ...) {
va_list va;
char* string;
va_start(va, str);
while (*str != '\0') {
if (*str == '%') {
printf("%c\n", *str);
str++;
switch (*str) {
case 'd':print_integer(va_arg(va,int)); break;
case 'f':print_float(va_arg(va,double)); break;
case 'c':putchar(va_arg(va, int)); break;
case 's':string = va_arg(va, char*);
while (*string != '\0') {
putchar(*string);
string++;
}
break;
}
}
else
putchar(*str);
str++;
}
va_end(va);
return;
}
void print_integer(int i) {
printf("%d", i);
}
void print_float(float f) {
printf("%f", f);
}
6.编写如下函数:
void written_amount( unsigned int amount, char *buffer)
它把amount表示的值转换为单词形式,并存储于buffer 中。这个函数可以在一个打印支票的程序中使用。例如,如果amount的值是16312,那么buffer 中存储的字符串应该是SIXTEEN THOUSAND THREE HUNDRED TWELVE
调用程序应该保证buffer 缓冲区的空间足够大
有些值可以用两种不同的方法进行打印。例如,1200 可以是ONE THOUSAND TWO HUNDRED或TWELVE HUNDRED.可以选择一种自己 喜欢的形式。
#include<stdio.h>
#include<string.h>
#define SIZE 128
char* save[] = { "","ONE ", "TWO ","THREE ","FOUR ","FIVE ","SIX ","SEVEN ","EIGHT ","NINE ","TEN ",
"ELEVEN ","TWELVE ","THIRTEEN ","FOURTEEN ","FIFTEEN ","SIXTEEN ","SEVENTEEN ","NINETEEN " };
char* saveteen[] = { "","","TWENTY ","THIRTY ","FORTY ","FIFTY ","SIXTY ","SEVENTY ","EIGHTY ","NINETY "};
char* thoun[] = {"","THOUSAND ","MILLION ","BILLION " };
void written_amount(unsigned int amont, char* buffer);
int fun(int amont);
int main(void) {
char buffer[SIZE]={0};
written_amount(100, buffer);
puts(buffer);
return 0;
}
void written_amount(unsigned int amont, char* buffer) {
int value;
int nums;
int i = 0;
int j = 0, h = 0;
if (amont == 0) {
strcat(buffer, "ZERO");
return;
}
value = amont / 1000;
while (value > 0) {
value /= 1000;
j++;
i++;
}
value = amont;
while (1) {
if (h < j) {
value /= 1000;
h++;
}
else {
nums = value;
value %= 1000;
value /= 100;
if (value > 0) {
strcat(buffer, save[value]);
strcat(buffer, "HUNDRED ");
}
value = nums % 100;
if (value >= 20) {
strcat(buffer, saveteen[value/10]);
value %= 10;
}
if (value > 0)
strcat(buffer, save[value]);
if (amont > 0) {
strcat(buffer, thoun[i]);
i--;
}
value = amont;
h = 0;
j--;
}
if (i < 0)
break;
}
}