结构体定义和使用
#include <iostream>
#include <string>
using namespace std;
//结构体的定义和使用
struct Student{
string name;
int age;
int score;
} s3;
int main(){
//用.访问属性并赋值 ,struct关键字可以省略
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//初始化赋值
struct Student s2 ={
"李四",
19,
80
};
cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
//在定义结构体时顺便创建结构体变量
s3.name = "王五";
s3.age = 20;
s3.score = 90;
cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;
return 0;
}
结构体数组
#include <iostream>
#include <string>
using namespace std;
struct Student{
string name;
int age;
int score;
};
int main(){
//创建结构体数组
struct Student stuArray[3] = {
{"张三",18,100},
{"李四",19,22},
{"王五",20,88}
};
//修改结构体数组中某个元素
stuArray[2].name = "赵六";
stuArray[2].age = 29;
stuArray[2].score = 27;
//遍历
for(int i=0;i<3;i++){
cout << "姓名:" << stuArray[i].name << " 年龄:" << stuArray[i].age << " 分数:" << stuArray[i].score << endl;
}
return 0;
}
结构体指针
#include <iostream>
#include <string>
using namespace std;
//结构体指针,指针变量通过->访问属性
struct Student{
string name;
int age;
int score;
};
int main(){
Student s = {"张三",18,100};
Student *p = &s;
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
return 0;
}
结构体嵌套
#include <iostream>
#include <string>
using namespace std;
struct Student{
string name;
int age;
int score;
};
struct Teacher{
string id;
string name;
int age;
Student s;
};
int main(){
Student s = {"张三",18,90};
Teacher t;
t.id="001";
t.name="王五";
t.age = 40;
t.s = s;
cout << "教师编号:" << t.id << " 教师姓名:" << t.name << " 教师年龄:" << t.age <<
" 带教学生姓名:" << t.s.name << " 分数:" << t.s.score << endl;
return 0;
}
结构体作为函数参数
点操作示例程序
#include <iostream>
#include <cmath>
using namespace std;
struct pointT{
double x, y;
};
void setPoint(double x, double y, pointT &p);
double getX(const pointT &p);
double getY(const pointT &p);
void showPoint(const pointT &p);
double distancePoint(const pointT &p1, const pointT &p2);
int main(){
pointT p1,p2;
setPoint(1,1,p1);
setPoint(2,2,p2);
//cout << getX(p1) << " " << getY(p2) << endl;
showPoint(p1);
cout << " -> ";
showPoint(p2);
cout << " = " << distancePoint(p1,p2) << endl;
return 0;
}
void setPoint(double x, double y, pointT &p){
p.x = x;
p.y = y;
}
double getX(const pointT &p){
return (p.x);
}
double getY(const pointT &p){
return (p.y);
}
void showPoint(const pointT &p){
cout << "(" << p.x << "," << p.y << ")";
}
double distancePoint(const pointT &p1, const pointT &p2){
return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
结构体案例
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
/**
设计老师和学生的结构体,在老师结构体中有老师姓名和一个存放5名学生的数组作为成员
学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的
学生赋值,最终打印老师数据及老师所带的学生数据
*/
struct Student{
string sName;
int score;
};
struct Teacher{
string tName;
Student students[5];
};
void allocatespace(Teacher teachers[],int len){
string nameseed = "ABCDE";
for(int i=0;i<len;i++){
teachers[i].tName = "Teacher_";
teachers[i].tName += nameseed[i];
for(int j=0;j<5;j++){
teachers[i].students[j].sName = "Student_";
teachers[i].students[j].sName += nameseed[j];
int random = rand()%61+40; //0-100
teachers[i].students[j].score = random;
}
}
}
void printInfo(Teacher teachers[],int len){
for(int i=0;i<len;i++){
cout << "老师姓名:" << teachers[i].tName << endl;
for(int j=0;j<5;j++){
cout << "\t学生姓名:" << teachers[i].students[j].sName << " 分数:" << teachers[i]
.students[j].score << endl;
}
}
}
int main(){
//创建老师数组
Teacher teachers[3];
int len = sizeof(teachers)/sizeof(teachers[0]);
//函数赋值
srand((unsigned)time(NULL)); //随机数种子
allocatespace(teachers,len);
//打印
printInfo(teachers,len);
return 0;
}
通讯录管理系统(无UI版)
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
/*
通讯录管理系统(无UI版)
实现功能:
添加联系人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
显示联系人:显示通讯录中所有联系人信息
删除联系人:按照姓名进行删除指定联系人
查找联系人:按照姓名查看指定联系人信息
修改联系人:按照姓名重新修改指定联系人
清空联系人:清空通讯录中所有信息
退出通讯录:退出档当前使用的通讯录
*/
void showMenu(){
cout << "*************************" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "*************************" << endl;
}
struct Person{
string name;
int sex;//0代表女,1代表男
int age;
string phone;
string address;
};
struct AddressBook{
Person persons[MAX];
int size;
};
void addPerson(AddressBook *ads){
if(ads->size==MAX){
cout << "通讯录已满,无法添加" << endl;
return;
}else{
string name;
cout << "请输入姓名:";
cin >> name;
ads->persons[ads->size].name=name;
int sex;
cout << "请输入性别(0是女,1是男):";
while(true){
cin >> sex;
if(sex==0 || sex==1){
ads->persons[ads->size].sex=sex;
break;
}
cout << "输入有误,请重新输入:" << endl;
}
int age;
cout << "请输入年龄:";
cin >> age;
ads->persons[ads->size].age=age;
string phone;
cout << "请输入电话:";
cin >> phone;
ads->persons[ads->size].phone=phone;
string address;
cout << "请输入地址:";
cin >> address;
ads->persons[ads->size].address=address;
cout << "添加成功" << endl;
ads->size++;
system("pause");
system("cls");//清屏操作
}
}
void showPerson(AddressBook *ads){
if(ads->size==0){
cout << "目前记录为空" << endl;
}else{
for(int i=0;i<ads->size;i++){
cout << "\t姓名:" << ads->persons[i].name << "\t";
cout << "性别:" << (ads->persons[i].sex==1?"男":"女") << "\t";
cout << "年龄:" << ads->persons[i].age << "\t";
cout << "电话:" << ads->persons[i].phone << "\t";
cout << "地址:" << ads->persons[i].address << endl;
}
}
system("pause");
system("cls");
}
int isExist(AddressBook *ads,string name){
for(int i=0;i<ads->size;i++){
if(ads->persons[i].name==name){
return i;
}
}
return -1;
}
void delPerson(AddressBook *ads){
string name;
cout << "请输入要删除的联系人姓名:";
cin >> name;
int res = isExist(ads,name);
if(res==-1){
cout << "查无此人" << endl;
}else{
for(int i=res;i<ads->size;i++){
ads->persons[i]=ads->persons[i+1]; //数据前移
}
ads->size--;//更新人数
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findPerson(AddressBook *ads){
string name;
cout << "请输入要查找的联系人姓名:";
cin >> name;
int res = isExist(ads,name);
if(res==-1){
cout << "查无此人" << endl;
}else{
cout << "\t姓名:" << ads->persons[res].name << "\t";
cout << "性别:" << (ads->persons[res].sex==1?"男":"女") << "\t";
cout << "年龄:" << ads->persons[res].age << "\t";
cout << "电话:" << ads->persons[res].phone << "\t";
cout << "地址:" << ads->persons[res].address << endl;
}
system("pause");
system("cls");
}
void modifyPerson(AddressBook *ads){
string name;
cout << "请输入要修改的联系人姓名:";
cin >> name;
int res = isExist(ads,name);
if(res==-1){
cout << "查无此人" << endl;
}else{
string newname;
cout << "请输入姓名:";
cin >> newname;
ads->persons[res].name=newname;
int sex;
cout << "请输入性别(0是女,1是男):";
while(true){
cin >> sex;
if(sex==0 || sex==1){
ads->persons[res].sex=sex;
break;
}
cout << "输入有误,请重新输入:" << endl;
}
int age;
cout << "请输入年龄:";
cin >> age;
ads->persons[res].age=age;
string phone;
cout << "请输入电话:";
cin >> phone;
ads->persons[res].phone=phone;
string address;
cout << "请输入地址:";
cin >> address;
ads->persons[res].address=address;
cout << "修改成功" << endl;
}
system("pause");
system("cls");//清屏操作
}
void clearPerson(AddressBook *ads){
char c;
cout << "确定要清空吗?(输入y确定,输入n取消):";
cin >> c;
if(c=='y'){
ads->size=0;
cout << "通讯录已清空" << endl;
}
else{
return;
}
}
int main(){
AddressBook ads;
ads.size=0;//初始化
while(true){
//封装一个函数,用于显示菜单界面
showMenu();
int select;
cout << "输入一个选择:";
cin >> select;
switch(select){
case 0:
cout << "您已退出系统" << endl;
return 0;
break;
case 1:
addPerson(&ads);
break;
case 2:
showPerson(&ads);
break;
case 3:
delPerson(&ads);
break;
case 4:
findPerson(&ads);
break;
case 5:
modifyPerson(&ads);
break;
case 6:
clearPerson(&ads);
system("pause");
system("cls");//清屏操作
break;
}
}
return 0;
}