C和指針課後習題(第七章)

7.1

/*The Hermite Polynomials are defined as follows:
Write a recursive function to compute the
value 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;
	else
		return 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 128

int 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;
		else
			number = 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 one
	while((ch=*format++)!='\0')
	{
		if(ch!='%')
		{
			//not a format code
			putchar(ch);
			continue;
		}
		//format code
		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;
		}
	}
}

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 128

static 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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章