一.定义一个结构体变量(包括年、月、日),计算该日在本年中是第几天
1. 结构体:结构体里已经开辟了空间了。
1)不需要Creat,即不用malloc申请动态内存空间。
区:链表要Creatlist。
a)因为所有存储方式链表的头指针都要开辟空间。(链表操作过程中对于结构体指针,都要用malloc来开辟内存空间)
b)顺序表存储:
1>若里面的DATA数组没有下标:则Creatlist中要用malloc开辟数组的空间
2>若里面的DATA数组有下标:则不用开辟空间
c)链式存储:每个结点都需要malloc
2)结构体变量不需要当作函数参数传递,直接在函数里面修改就行了。
3)访问结构体成员的方法
a)成员运算符 .
b)指向结构体的指针 (*p).month=12;
struct LNode{
int month;
.....
};
struct LNode today;
struct LNode *p=&today;
(*p).month=12;
2.头文件学习
1)#include
2)#include
3.c++中输入空格的方法
1)单纯输入一个空格:getchar(),没有参数,不需加其它头文件
2)输入的字符串接收多个空格,遇到回车结束输入
a)string类型:getline(cin,字符串名,结束符)
istream& getline ( istream &is , string &str , char delim );
1> istream &is :表示一个输入流,cin;
2>string&str:表示把从输入流读入的字符串存放在这个字符串中(可以自己随便命名,str什么的都可以);
3>char delim:表示遇到这个字符停止读入,在不设置的情况下系统默认该字符为'n',(即遇到回车停止读入)。
b)字符数组:
1>cin.getline(字符数组名,接收长度,结束符)
2>cin.get(字符数组名,接收长度,结束符)
注意:由于最后一位用来添加空字符。所以,如果第二个参数为20,那么这个函数最多存储19个字符
两者的区别:cin.get()每次读取一整行并把由Enter键生成的换行符留在输入队列中,然而cin.getline()每次读取一整行并把由Enter键生成的换行符抛弃。
#includeusing namespace std; struct LNode{ int year; int month; int day; }; int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; void jisuan(struct LNode a,int *b) { int i,sum=0,answer=0; for(i=1;i>a.year; getchar(); cin>>a.month; getchar(); cin>>a.day ; if(a.year%400==0||(a.year%4==0&&a.year%100!=0)) jisuan(a,b); else{ b[2]=28; jisuan(a,b); } return 0; }
二.输入年、月、日,计算该日在本年中是第几天(不用结构体)
#includeusing namespace std; int a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int days(int year,int month,int day) { int i,sum=0,answer=0; for(i=1;i >year; getchar(); cin>>month; getchar(); cin>>day ; int answer; if(year%400==0||(year%4==0&&year%100!=0)) answer=days(year,month,day); else{ a[2]=28; answer=days(year,month,day); } cout<
三.编写print函数打印学生成绩数组
1.简便方法:结构数组 struct LNode a[5];
2.注意getchar()的用法
#includeusing namespace std; struct LNode{ int num[5]; string name[5]; int score[5][3]; }a; void print() { int i,j; cout< a.num[i]; getchar(); } cout<<"name:"; for(i=0;i<5;i++){ cin>>a.name[i]; getchar(); } cout<<"score:"< >a.score[i][j]; getchar(); } } print(); return 0; }
四.在三.的基础上编写input输入五个学生的数据
#includeusing namespace std; struct LNode{ int num[5]; string name[5]; int score[5][3]; }a; void input() { int i,j; cout<<"num:"; for(i=0;i<5;i++){ cin>>a.num[i]; getchar(); } cout<<"name:"; for(i=0;i<5;i++){ cin>>a.name[i]; getchar(); } cout<<"score:"< >a.score[i][j]; getchar(); } } } void print() { int i,j; cout< 五.创建动态链表
#includeusing namespace std; typedef struct LNode *PtroLNode; struct LNode{ int Data; PtroLNode Next; }; typedef PtroLNode List; List Creatlist() { List head,p,end; int b; head=(List)malloc(sizeof(struct LNode)); head->Next =NULL; end = head; while(cin>>b&&b!=0){ getchar(); //输入一个空格 p=(List)malloc(sizeof(struct LNode)); p->Data =b; p->Next =NULL; end->Next =p; end = p; } return head; } int main() { List a=Creatlist(); return 0; }



