高级语言期末2014级A卷(软件学院)

1.编写函数 int delarr(int a[] ,int n),删除有n个元素的正整型数组a中所有素数,要求:
1)数组a中剩余元素保持原来次序;
2)将处理后的数组输出;
3)函数值返回剩余元素个数;
4)不能额外定义新数组。

#include <stdio.h>

int prime(int n) {
	if(n==1)
		return 0;
	for(int i=2; i<n/2; i++)
		if(n%i==0)
			return 0;
	return 1;
}

int delarr(int a[],int n) {
	for(int i=0; i<n; i++) {
		if(prime(i)) {
			for(int j=i; j<n-1; j++)
				a[j]=a[j+1];
			i--;
			n--;
		}
	}
	for(int i=0; i<n; i++)
		printf("%4d",a[i]);
	return n;
}

2.编写函数bool cmpstr(char *s),判定一个给定字符串s是否对称,对称字符串也成为回文,是满足从左到右和从右到左均相同的字符序列(不考虑默认字符串结尾’\0’)

#include <stdio.h>
#include <stdbool.h>

bool cmpstr(char *s) {
	int count=0;
	while(s[count]!='\0')
		count++;
	int low=0,high=num-1;
	while(low<high) {
		if(s[low]!=s[high])
			return false;
		low++;
		high--;
	}
	return true;
}

3.编写递归函数float comp(float a[],int n),计算给定n个元素的float型数组中所有元素的算术平均值。

#include <stdio.h>

float comp(float a[],int n) {
	if(n==1)
		return a[0];
	return (a[n-1]+(n-1)=comp(a,n-1))/n;
}

4.每个学生的信息卡片包括学号、姓名、年龄三项。定义存储学生信息的单向链表的节点类型;编写函数,由键盘依次输入n个学生的信息,创建一个用于管理学生信息的单向链表。

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

typedef struct student {
	int id;
	char name[10];
	int age;
	struct student *next;
} student;

struct student *create(int n) {
	struct student *head=NULL,*p1,*p2;
	for(int i=0; i<n; i++) {
		p1=(struct student *)malloc(sizeof(struct student));
		scanf("%d%s%d",&(p1->id),&(p1->name),&(p1->age));
		if(i==0)
			head=p1;
		else
			p2->next=p1;
		p2=p1;
	}
	p2->next=NULL;
	return head;
}

5.编写函数,把上中创建的单向链表中所有年龄为z的结点删除(z的指由用户键盘输入,且年龄为z的结点可能有多个),将处理后的单向链表的所有学生信息存储到名为output.txt的文本文件中。

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

typedef struct student {
	int id;
	char name[10];
	int age;
	struct student *next;
} student;

void save(struct student *head,int z) {
	FILE *file;
	if((file=fopen("output.txt","w"))==NULL) {
		printf("open error");
		exit(0);
	}
	while(head!=NULL&&head->age==z)
		head=head->next;
	struct student *p=head,*q=NULL;
	while(p!=NULL) {
		if(p->age!=z)
			q=p;
		else
			q->next=p->next;
		p=p->next;
	}
	while(head!=NULL) {
		fprintf(file,"%5d\n%10s%5d\n",head->id,head->name,head->age);
		head=head->next;
	}
	fclose(file);
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值