//文件读入与保存:
#include<stdio.h>
#define size 4
struct stud
{
char name[10];
int num;
int age;
char addr[15];
}
stud[size];
void save()
{
FILE *fp;
int i;
if((fp=fopen("stu.txt","wb"))==NULL)
{
printf("can not open \n");
return;
}
for(i=0;i<size;i++)
if(fwrite(&stud[i],sizeof(struct stud),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void main()
{
int i;
printf("imput data of students:\n");
for(i=0;i<size;i++)
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
save();
}
//选择排序
#include <stdio.h>
#include <stdlib.h>
void insert(int *arr, int a, int n)
{
int i;
int key = a;
for(i=0; i<n; i++){
if(key < arr[i]){
int j;
for(j=n-1; j>=i; j--){
arr[j+1] = arr[j];
}
arr[i] = key;
return;
}
}
arr[n] = key;
return;
}
void sort(int *arr, int size){
if(size<2)
return;
int i;
for(i=1; i<size; i++){
insert(arr, arr[i], i);
}
}
void main(){
int i, n, *arr;
printf("请输入数组的大小 n = ");
scanf("%d", &n);
arr = (int*)malloc(sizeof(int)*n);
printf("请输入%d个数组元素:\n", n);
for(i=0; i<n; i++){
scanf("%d", &arr[i]);
}
printf("在排序前,数组为:");
for(i=0; i<n; i++){
printf("%d ", arr[i]);
}
printf("\n调用排序函数中...\n");
sort(arr, n);
printf("在排序后,数组变为:");
for(i=0; i<n; i++){
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
}
转载于:https://blog.51cto.com/dengmiao/1113587