高级语言期末2009级B卷(计算机学院)

1.编写一个名为mystrcpy的函数,实现将字符串str1的偶数位子的字符的拷贝到另一个字符串str2中。并编写主函数,在主函数中从键盘读入一个长度<100的字符串str1,然后调用函数mystrcpy;最后输出str2,例如,读入“abcdefgh”,则输出"bdfh".

#include <stdio.h>

void mystrcpy(char *str1,char *str2){
	int i=0,j=0;
	while(str1[i]!='\0'){
		if((i+1)%2==0){
			str2[j]=str1[i];
			j++;
		}
		i++;
	}
}

int main(){
	char str1[]="abcdefgh";
	char str2[100];
	mystrcpy(str1,str2);
	printf("%s",str2);
}

2.编写一个函数,将一维数组转换成一个N阶方阵(二维数组),矩阵的行数由函数的参数指定。然后再main()中调用该函数,并打印输出该仿真的对角线上的值

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

void changesqure(int *a,int n,int **arr,int size){
	int i,j=0,k=0;
	for(i=0;i<n;i++){
		arr[j][k]=a[i];
		k++;
		if(k!=0&&k%size==0){
			j++;
			k=0;
		}
	}
}

int main(){
	int a[]={1,2,3,4,5,6,7,8,9};
	int n = sizeof(a)/sizeof(a[0]);
	int size=sqrt(n);
	int **arr = (int **)malloc(size*sizeof(int *));
	int i,j;
	for(i=0;i<size;i++)
		arr[i]=(int *)malloc(size*sizeof(int));
	changesqure(a,n,arr,size);
	for(i=0;i<size;i++){
		for(j=0;j<size;j++)
			if(i==j)
				printf("%d ",arr[i][j]);
		printf("\n");
	}
}

3.编写一个函数,求2个数的最小公倍数并输出。要求在主函数中从键盘读入2个正的int型整数,求他们的最小公倍数并输出

#include <stdio.h>
 
int gcd(int x,int y) {
	if(y==0)
		return x;
	return 1.0*x*y/gcd(y,x%y);
}

4.某班学生信息包括学号(字符串,长度不超过8位)、姓名(没有空格的字符串,长度不超过20位)和两门课程的成绩。请写出学生结构体,并编写函数CreatList,创建学生链表

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

typedef struct student{
	char num[8];
	char name[20];
	int score1;
	int score2;
	struct student *next;
}student;

struct student *CreatList(int n){
	struct student *head=(struct student*)malloc(sizeof(struct student));
	head->next=NULL;
	struct student *pre=head;
	int i;
	for(i=0;i<n;i++){
		struct student *p=(struct student*)malloc(sizeof(struct student));
		scanf("%s %s %d %d",&p->num,&p->name,&p->score1,&p->score2);
		p->next=pre->next;
		pre->next=p;
	}
	return head->next;
}

5.将上题的学生链表信息读出并写入文件student.txt

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

typedef struct student{
	char num[8];
	char name[20];
	int score1;
	int score2;
	struct student *next;
}student;

void writelist(struct student *head){
	FILE *file;
	if((file=fopen("student.txt","w"))==NULL){
		printf("open error");
		exit(0);
	}
	struct student *p=head;
	while(p!=NULL){
		fprintf(file,"%s %s %d %d",&p->num,&p->name,&p->score1,&p->score2);
		p=p->next;
	}
	fclose(file);
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值