1.用结构体输出学生姓名、学号和成绩
#include <memory.h>
#include <string.h>
#include <stdio.h>
typedef struct Student{
char Name[20]; //Name
char ID[15]; //学号
int Grade; //成绩
Student(char *name, char *id, int grade)
{
memcpy(Name,name,(1+strlen(name))*sizeof(char));
memcpy(ID,id,(1+strlen(id))*sizeof(char));
Grade=grade;
}
void Show()
{
printf("姓名为%s,学号为%s,分数为%d。\n",Name,ID,Grade);
}
}Student;
void main()
{
Student s_1("a","2012302590014",54);
Student s_2("b","2012302590081",95);
Student s_3("c","2012302590144",96);
Student s_4("d","2012302590169",97);
s_1.Show();
s_2.Show();
s_3.Show();
s_4.Show();
}
参考资料:C++ primer 4th