高级语言期末2006级A卷(计算机学院)

1.编写函数,数列的前n项和。例如当n等于5时s(5) = 1 - 1/2 + 1/3 - 1/4 + 1/5

#include <stdio.h>

float func(int n) {
	float sum=0;
	int sign=1;
	for(int i=0; i<=n; i++) {
		sum+=1.0*sign/i;
		sign*=-1;
	}
	return sum;
}

2.编程序判断10阶整数方阵是否关于主对角线对称

#include <stdio.h>
 
int issym(int maxt[][10]) {
	for(int i=0; i<10; i++)
		for(int j=0; j<10; j++)
			if(maxt[i][j]!=maxt[j][i])
				return 0;
	return 1;
}

3.编写函数比较两个字符串的大小,当s1>s2时,返回1,s1<s2时,返回-1,否则s1=s2时,返回0

#include <stdio.h>

int strcmp(char *str1,char *str2) {
	int i=0;
	while(str1[i]!='\0'&&str2[i]!='\0') {
		if(str1[i]>str2[i])
			return 1;
		else if(str1[i]<str2[i])
			return -1;
		else
			i++;
	}
	if(str1[i]=='\0'&&str2[i]=='\0')
		return 0;
	else if (str1[i]=='\0')
		return -1;
	else
		return 1;
}

4.编写函数计算如下多项式的第n项值

#include <stdio.h>
 
int func(int n,int x) {
	if(n==0)
		return 1;
	else if(n==1)
		return 2*x;
	return 2*x*func(n-1,x)-2*(n-1)*func(n-2,x);
}

5.把一个由数字和字母构成的字符串用单链表存储,每一个结点存储一个字符,请完成如下功能:
①:请给出节点的数据结构的定义
②:编写一个将链表中所有数字串到前面,而字母串到后面,且数字与数字,字母与字母相对顺序不变。

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

typedef struct node {
	char ch;
	struct node *next;
} node;

struct node* resort(struct node* head) {
	struct node *arghead=(struct node *)malloc(sizeof(struct node));
	struct node *dighead=(struct node *)malloc(sizeof(struct node));
	arghead->next=NULL;
	dighead->next==NULL;
	struct node *rear1=arghead,*rear2=dighead,*p=head;
	while(p!=NULL) {
		struct node *temp=p->next;
		if(p->ch>='a'&&p->ch<='z'||p->ch>='A'&&p->ch<='Z') {
			p->next=rear1->next;
			rear1->next=p;
			rear1=p;
		} else {
			p->next=rear2->next;
			rear2->next=p;
			rear2=p;
		}
		p=temp;
	}
	rear2->next=arghead->next;
	return dighead->next;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值