實驗11+構造類型——結構體的應用——顧

實驗11+構造類型——結構體的應用——顧

(1)教材330頁第9 題。

#include<iostream>
#define ERROR 0
#define OK 1
using namespace std;

typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList;

void CreateList(LinkList &L,int n){
	L=new LNode;
	L->next=NULL;
	LinkList r=L,p;
	for(int i=0;i<n;++i){
		p=new LNode;
		cin>>p->data;
		p->next=NULL;
		r->next=p;
		r=p;
	}
}

int ListInsert(LinkList &L,int i,int e){
	LinkList p=L;
	int j=0;
	while(p&&(j<i-1)){
		p=p->next;
		++j;
	}
	if(!p||j>i-1) return ERROR;
	LinkList s=new LNode;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return OK;
}

int ListDelete(LinkList &L,int i){
	LinkList p=L,q;
	int j=0;
	while((p->next)&&(j<i-1)){
		p=p->next;
		++j;
	}
	if(!(p->next)||(j>i-1)) return ERROR;
	q=p->next;
	p->next=q->next;
	delete q;
	return OK;
}

void ShowList(LinkList L){
	LinkList p=L;
	while(p){
		p=p->next;
		cout<<p->data<<" ";
	}
	cout<<endl;
}

int main(){
	LNode *L;
	int n,i,e;
	
	cout<<"Enter the number of elements:"<<endl;
	cin>>n;
	cout<<endl<<"Enter the elements:"<<endl;
	CreateList(L,n);
	
	cout<<endl<<"Enter the position of the element which you want to insert and the element:"<<endl;
	cin>>i>>e;
	ListInsert(L,i,e);
	
	cout<<endl<<"Enter the position of the element which you want to delete:"<<endl;
	cin>>i;
	ListDelete(L,i);
	
	cout<<endl<<"Now show the elements:"<<endl;
	ShowList(L);
	return 0;
}

在這裏插入圖片描述
在這裏插入圖片描述

(2)定義一個學生結構體:

/*
struct student{
     char name[20];/*姓名*/ 
     char sex;/*性別*/ 
     int age;/*年齡*/ };
再定義一個學生數組s[5],編寫函數:
void input(struct student *);/*輸入所有學生的數據*/,
void show(struct student *);/*顯示所有學生的數據*/,void sort(struct student *); /*按年齡排序*/
輸入5個學生信息,並把學生按年齡大小順序排列後依次輸出。
*/
#include<iostream>
using namespace std;
struct student{
	char name[20];
	char sex;
	int age;
}s[5],temp;

void input(struct student *s){
	for(int i=0;i<5;++i)
		cin>>s[i].name>>s[i].sex>>s[i].age;
}

void show(struct student *s){
	for(int i=0;i<5;++i)
		cout<<s[i].name<<"\t"<<s[i].sex<<"\t"<<s[i].age<<endl;
}

void sort(struct student *s){
	for(int i=0;i<4;++i){
		for(int j=i+1;j<5;++j){
			if(s[i].age>s[j].age){
				temp=s[i];
				s[i]=s[j];
				s[j]=temp;
			}
		}
	}
}

int main(){
	cout<<"Enter the data of the five students:"<<endl;
	input(s);
	sort(s);
	cout<<"The data of sorted array:"<<endl;
	show(s);
	return 0;
}

在這裏插入圖片描述
在這裏插入圖片描述

(3)定義一個點的結構數據類型,實現下列功能:1)爲點輸入座標值;2)求兩個點的中點座標;3)求兩點間距離。程序以文件名sy11.c 保存。

#include<iostream>
#include<cmath>
using namespace std;

struct Coordinate{
	double x;
	double y;
}data[2];

void cinn(struct Coordinate *data){
	cout<<"Enter the coordinates of two points:"<<endl;
	for(int i=0;i<2;++i)
		cin>>data[i].x>>data[i].y;
	cout<<endl; 
}

void middle(struct Coordinate *data){
	cout<<"The coordinate of the middle point of the two points:"<<endl;
	cout<<(data[0].x+data[1].x)/2<<" "<<(data[0].y+data[1].y)/2<<endl<<endl;
}

void distance(struct Coordinate *data){
	double dis=sqrt((data[0].x-data[1].x)*(data[0].x-data[1].x)+(data[0].y-data[1].y)*(data[0].y-data[1].y));
	cout<<"The distance of the two points:"<<endl;
	cout<<dis<<endl;
}
int main(){
	cinn(data);
	middle(data);
	distance(data);
	return 0;
}

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章