1、简易学生信息库(结构体)
# include<iostream>
# include<iomanip>
using namespace std;
struct Student
{
int num;
float score;
struct Student *next;
};
int main()
{
struct Student a, b, c, *head, *p;
a.num = 1;
a.score = 90;
b.num = 2;
b.score = 80;
c.num = 3;
c.score = 70;
head = &c;
c.next = NULL;
p = head;
while(p!=NULL)
{
cout<<p->num<<" "<<fixed<<setprecision(1)<<p->score<<endl;
p=p->next;
}
return 0;
}
2、简易学生信息库(类)
# include<iostream>
using namespace std;
# define MAX 10000
class student{
private:
int num;
char name[10];
char sex;
float Math;
float Chinese;
float English;
public:
void input();
void sum();
void aval();
};
void student::input(){
cout<<"请输入学生学号:";
cin>>num;
cout<<"请输入学生姓名:";
cin>>name;
cout<<"请输入学生性别:";
cin>>sex;
cout<<"请输入数学成绩:";
cin>>Math;
cout<<"请输入语文成绩:";
cin>>Chinese;
cout<<"请输英语学成绩:";
cin>>English;
}
void student::sum(){
float S = Math+ Chinese+ English;
cout<<S<<endl;
}
void student::aval(){
float S = Math+ Chinese+ English;
cout<<S/3<<endl;
}
int main()
{
student liuyihan[MAX];
for(int i = 1; i < MAX; i++)
{
cout<<"请输入第"<<i<<"位学生的信息: "<<endl;
liuyihan[i].input();
cout<<endl;
cout<<"该学生的总分是: ";
liuyihan[i].sum();
cout<<endl;
cout<<"该学生的平均分是: ";
liuyihan[i].aval();
cout<<endl;
}
return 0;
}
3、简易职工信息库(类)
# include<iostream>
using namespace std;
class Employee{
private:
char name[10];
char sex;
int num;
float gongzi;
float jingtie;
float xiaoyi;
public:
void input();
void sum1();
void sum2();
void sum3();
};
void Employee::input(){
cout<<"请输入你的名字:" <<endl;
cin>>name;
cout<<"请输入你的性别:" <<endl;
cin>>sex;
cout<<"请输入你的工号:" <<endl;
cin>>num;
cout<<"请输入你的基础工资:" <<endl;
cin>>gongzi;
cout<<"请输入你的岗位津贴:" <<endl;
cin>>jingtie;
cout<<"请输入你的效应工资:" <<endl;
cin>>xiaoyi;
}
void Employee::sum1(){
cout<<endl;
cout<<"应付工资:"<<endl;
float a = gongzi +jingtie + xiaoyi;
cout<<a<<endl;
}
void Employee::sum2(){
cout<<endl;
cout<<"个人所得税:"<<endl;
float a = gongzi +jingtie + xiaoyi;
if(a <= 3500)
cout<<0<<endl;
else
cout<<(a - 3500)*0.03<<endl;
}
void Employee::sum3(){
float b;
cout<<endl;
cout<<"实发工资:"<<endl;
float a = gongzi +jingtie + xiaoyi;
if(a <= 3500)
b = 0;
else
b = (a - 3500)*0.03;
cout<<b<<endl;
}
int main()
{
Employee liuyihan;
liuyihan.input();
liuyihan.sum1();
liuyihan.sum2();
liuyihan.sum3();
return 0;
}
4、链表的创建和输出
# include<stdio.h>
# include<iostream>
# include<stdlib.h>
using namespace std;
#define OK 1
#define ERRPR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
void CreateList_R(LinkList &L, int n)
{
LinkList p, r;
int i;
L = new LNode;
L->next = NULL;
r = L;
for(i = 0; i < n; ++i)
{
p = new LNode;
cin>>p->data;
p->next = NULL;
r->next = p;
r = p;
}
}
void ShowList(LinkList L)
{
LinkList p;
p = L->next;
while(p)
{
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
LinkList kk;
int n;
cin>>n;
CreateList_R(kk, n);
ShowList(kk);
return 0;
}
|