C语言第十一章课后程序设计题

2021/11/6 22:14:39

本文主要是介绍C语言第十一章课后程序设计题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

习题11-1 输出月份英文名 

#include <stdio.h>
char *getmonth( int n );

int main()
{
    int n;
    char *s;

    scanf("%d", &n);
    s = getmonth(n);
    if ( s==NULL ) printf("wrong input!\n");
    else printf("%s\n", s);
    return 0;
}
char *getmonth( int n ){
	const char *month[12]={"January","February","March","April","May","June","July","August","September","October","November","December"}; 
    if(n>=1&&n<=12){
    	return month[n-1];
	}else{
		return (NULL);
	}

}

习题11-2 查找星期

#include<stdio.h>
char *getweek(int n);
int main()
{
	char *s;
	int n;
	scanf("%d",&n);
	s = getweek(n);
	if(s==NULL){
		printf("-1");
	}else{
		printf("%s",s);
	}
	return 0;
}
char *getweek(int n){
	const char *week[7]={"Sunday","Monday","Tuesday","Wednesday","Thurday","Friday","Saturday"};
	if(n>=0 && n<=6){
		return week[n];
	}else{
		return (NULL);
	}
	 
}

习题11-3  计算最长的字符串的长度

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 10
#define MAXS 20

int max_len( char *s[], int n );

int main()
{
    int i, n;
    char *string[MAXN] = {NULL};

    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        string[i] = (char *)malloc(sizeof(char)*MAXS);
        scanf("%s", string[i]);
    }
    printf("%d\n", max_len(string, n));

    return 0;
}

int max_len( char *s[], int n ){
	int max=0;//假设max为s[0] 
	int i,j;
	
	for(i=0;i<n;i++){
		if(strlen(s[max])<strlen(s[i])){
			max=i;	
		}
	}	
	return strlen(s[max]);
} 

习题11-4 字符串的连接

#include <stdio.h>
#include <string.h>

#define MAXS 10

char *str_cat( char *s, char *t );

int main()
{
    char *p;
    char str1[MAXS+MAXS] = {'\0'}, str2[MAXS] = {'\0'};

    scanf("%s %s", str1, str2);
    p = str_cat(str1, str2);
    printf("%s\n%s\n", p, str1);

    return 0;
}
char *str_cat( char *s, char *t ){
	
	s=strcat(s,t);
	return s;							
}

习题11-5 指定位置输出字符串

#include <stdio.h>
#define MAXS 10
char *match( char *s, char ch1, char ch2 );

int main() {
	char str[MAXS], ch_start, ch_end, *p;

	scanf("%s\n", str);
	scanf("%c %c", &ch_start, &ch_end);
	p = match(str, ch_start, ch_end);
	printf("%s\n", p);

	return 0;
}
char *match( char *s, char ch1, char ch2 ){
	char *temp="\n";
	while(*s!=ch1&&*s!='\0'){
         s++;	
	}  
	if(*s==ch1){
    temp=s;
	while(*s!='\0'){
		printf("%c",*s);
		if(*s==ch2){
			break; 
		}
		s++;
	}
	putchar('\n');
}
	return temp; 
}

11-6 查找子串

#include <stdio.h>
#include <string.h>
#define MAXS 30
 
char *search(char *s, char *t);
 
int main()
{
    char s[MAXS], t[MAXS], *pos;
 
    gets(s);
    gets(t);
    pos = search(s, t);
    if ( pos != NULL )
        printf("%d\n", pos - s);
    else
        printf("-1\n");
 
    return 0;
}
 
char *search(char *s, char *t){
	int i,j,k=0,slen,tlen;
	char *p=NULL;
 
	slen = strlen(s);
	tlen = strlen(t);
 
	for(i=0;i<slen;i++){
		j=i;
		while(s[j]==t[k]){
			k++;
			j++;
		}
		if(k>=tlen){
			p=&s[i];
			return p;
		}
		k=0;
	}
 
	return p;
}

11-7(不会写,转载的)

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
	int data;
	struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
void printlist( struct ListNode *L ) {
	struct ListNode *p = L;
	while (p) {
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

int main() {
	struct ListNode *L, *Odd;
	L = readlist();
	Odd = getodd(&L);
	printlist(Odd);
	printlist(L);

	return 0;
}
struct ListNode *readlist() {
	struct ListNode *head=NULL,*p,*last;
	int n;
	last=head; //last指针用于移动表示链表末尾
	do {
		scanf("%d",&n); //输入结点数值
		if(n!=-1) {
			p=(struct ListNode *)malloc(sizeof(struct ListNode));  //申请新节点
			p->data=n; //新节点赋值
			p->next=NULL; //新节点指空
			if(last==NULL) //若为头结点时
				head=p;
			else
				last->next=p; //非头结点时,尾接
			last=p;	//标志位移动到新节点处
		} else
			break;
	} while(n!=-1);

	return head;

}

struct ListNode *getodd( struct ListNode **L ) {
	//奇数分离构成新链表,返回删除奇数的链表的头结点
	struct ListNode *odd=NULL,*last,*p,*q,*k;
	last=odd;  //用于奇数链表链表 插入新节点
	p=*L;    //表示原链表移动指针
	q=NULL; //q初始化,作为原链表移动指针前一个节点的标记
	while(p) {
		if((p->data)%2) { //data取余2为 1 则为奇数,删除节点
			if(q&&p!=*L) { //非L收个节点就是奇数的情况
				k=p; //保存奇数节点
				q->next=p->next; //删除奇数节点

				if(last!=NULL)    //插入odd链表
					last->next=k;
				else
					odd=k;
				last=k;
			} else { //L的第一个就是奇数的情况
				k=p;              //保存奇数节点
				*L=p->next;       //头结点向后移动,删除奇数节点

				if(last!=NULL)    //插入odd链表
					last->next=k;
				else
					odd=k;
				last=k;
			}
		} else
			q=p; //未找到奇数节点,则用q保留当前位置
		p=p->next; //p指针继续向下移动
		if(last) //odd链表非空则
			last->next=NULL;  //odd末尾指针指空
	}
	return odd;
}

11-8(不会写,转载的)

// 11-8
// 删除结点
 
#include <stdio.h>
#include <stdlib.h>
 
struct ListNode {
	int data;
	struct ListNode *next;
}; 
 
struct ListNode *readlist();
struct ListNode *deletem(struct ListNode *L, int m);
 
void printlist(struct ListNode *L)
{
	struct ListNode *p = L;
	while (p)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}
 
int main()
{
	int m;
	struct ListNode *L = readlist();
	scanf("%d", &m);
	L = deletem(L,m);
	printlist(L);
	
	return 0;
}
 
struct ListNode *readlist()
{
	int data;
	int size = sizeof(struct ListNode);
	struct ListNode *head, *tail, *p;
	head = tail = NULL;
	
	scanf("%d", &data);
	while (data!=-1)
	{
		p = (struct ListNode *) malloc(size);
		p->data = data;
		p->next = NULL;
		if (head==NULL)
		{
			head = p;
		}
		else
		{
			tail->next = p;
		}
		tail = p;
		
		scanf("%d", &data);
	}
	
	return head; 
}
 
struct ListNode *deletem(struct ListNode *L, int m)
{
	struct ListNode *ptr1, *ptr2;
	
	//要被删除结点为表头结点 
	while (L!=NULL && L->data==m)
	{
		ptr2 = L;
		L = L->next;
		free(ptr2);
	}
	
	// 链表空 
	if (L==NULL)
	{
		return NULL;
	}
	
	// 要被删除结点为非表头结点
	ptr1 = L;
	ptr2 = L->next;		// 从表头的下一个结点搜索所有符合删除要求的结点 
	while (ptr2!=NULL)
	{
		if (ptr2->data==m)	// ptr2所指结点符合删除要求 
		{
			ptr1->next = ptr2->next;
			free(ptr2);
		}
		else
		{
			ptr1 = ptr2;	// ptr1后移一个结点 
		}
		ptr2 = ptr1->next;	// ptr2指向ptr1的后一个结点 
	}
	
	return L;
}



这篇关于C语言第十一章课后程序设计题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程