高级语言期末2014级唐班B卷(软件学院)

本文展示了C语言中的四个编程示例:1)函数获取字符串指定位置的字符;2)递归计算整数数组的和;3)寻找并输出数组中最小元素;4)从文件中搜索学生信息并删除链表中的数字字符。
摘要由CSDN通过智能技术生成

1.编写函数,输出给定的字符串s中从m字符开始后的r个字符(假设s中有m字符且后有r个字符)。

#include <stdio.h>

void getstr(char *str,int r) {
	int i=0;
	while(str[i]!='m')
		i++;
	for(int j=i+1;j<i+1+r;j++)
	printf("%c",str[j]);
}

int main() {
	char str[]="abcdefjmhigklwn";
	getstr(str,3);
}

2.有n个整数放在数组a中,编写递归函数,求整数之和。

#include <stdio.h>

int sum(int *a,int n) {
	if(n==1)
		return a[0];
	return sum(a,n-1)+a[n-1];
}

3.编写函数,对任意给定的int类型数组A,求最小元素值并输出。求最小元素用函数编写,以指针做参数。函数形式如下 int min_data(int *p, int n)
其中:指针p为数组首地址,n为数组元素个数

#include <stdio.h>

int min_data(int *p,int n){
	int min=p[0];
	for(int i=1;i<n;i++)
	if(min>p[i])
	min=p[i];
	return min;
}

int main(){
	int arr[]={4,2,3,45,6,7,2,2,5,6,1};
	printf("%d",min_data(arr,11));
}

4.在D盘根目录下有文件source.txt,在该文件中存放某班学生的学号及成绩,每个学生的学号是唯一的,学号和成绩是整数,每个数据宽度为6位,编写程序求:
从键盘输入学生的学号,在文件中检索该学生:如找到,显示该学生的学号及成绩;不存在显示-1.

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

void main() {
	FILE *file;
	int search,no,score;
	if((file=fopen("D:\\source.txt","r"))==NULL) {
		printf("open error");
		exit(0);
	}
	scanf("%d",&search);
	while(!feof(file)) {
		fscanf(file,"%6d%6d",no,score);
		if(no==search) {
			printf("%6d%6d\n",no,score);
			return;
		}
	}
	printf("-1\n");
	fclose(file);
}

5.已知如图所示一个链表,链表的每节是一个字符,要求:
1)根据给定的链表定义结点的结构体类型
2)编写函数删除链表中所有数字字符的结点

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

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

struct node *del(struct node *head) {
	while(head->key>='0'&&head->key<='9')
		head=head->next;
	struct node *p=head;
	while(p->next!=NULL)
		if(p->next->key>='0'&&p->next->key<='9')
			p->next=p->next->next;
		else p=p->next;

	return head;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值