做网站收益搜狗官网
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);
}