核心内容:
1、学生信息管理系统
C语言版:
# include <stdio.h>
# include <malloc.h>
# include <string.h>
struct Student
{
int age;
float score;
char name[100];
};
void inputdata(Student * arr,int len)//不管是赋值还是输出数值
{
arr[0].age = 20;
arr[0].score = 88.8f;
strcpy(arr[0].name,"zhangsan");
arr[1].age = 21;
arr[1].score = 98.8f;
strcpy(arr[1].name,"lisi");
arr[2].age = 18;
arr[2].score = 66.6f;
strcpy(arr[2].name,"lidong");
}
void sort(Student * arr,int len)
{
for (int j=1; j<len;j++)
{
for (int k=0; k<len-j;k++)
{
Student tmp;
if (arr[k].score > arr[k+1].score)
{
tmp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = tmp;
}
}
}
}
void outputdata(Student * arr,int len)
{
//接下来我们将结果进行输出
for (int i=0; i<3; i++)
{
printf("%d\t",arr[i].age);
printf("%f\t",arr[i].score);
printf("%s\t",arr[i].name);
printf("\n");
}
}
int main(void)
{
Student * arr = (Student *)malloc(sizeof(Student)*3);
inputdata(arr,3);
sort(arr,3);
outputdata(arr,3);
return 0;
}
运行结果:
Java语言版:
import java.util.ArrayList;
import java.util.Collections;
class Student implements Comparable<Student>
{
public int age;
public float score;
public String name;
public Student(int age,float score,String name)
{
this.age = age;
this.score = score;
this.name = name;
}
@Override
public int compareTo(Student obj)
{
if (this.score > obj.score)
return 1;
else
return -1;
}
public String toString()
{
return this.age+"\t"+this.score+"\t"+this.name;
}
}
public class App1
{
public static void main(String[] args)
{
ArrayList<Student> arr = new ArrayList<Student>();//动态分配1块内存空间,用指针变量指向这块内存空间
arr.add(new Student(20,88.8f,"zhangsan"));
arr.add(new Student(21,98.8f,"lisi"));
arr.add(new Student(18,66.6f,"lidong"));
for (int i=0; i<arr.size();i++)
{
System.out.println(arr.get(i));
}
Collections.sort(arr);
for (int i=0; i<arr.size();i++)
{
System.out.println(arr.get(i));
}
}
}
运行结果:
20 88.8 zhangsan
21 98.8 lisi
18 66.6 lidong
18 66.6 lidong
20 88.8 zhangsan
21 98.8 lisi