@chawuciren
2019-03-28T12:15:44.000000Z
字数 2866
阅读 989
未分类
1、采用单向链表建立一个学生信息表,每个节点包括学号sno(int型),姓名sname(字符型20位),成绩sscore(int型),并输出。
#include<iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
struct ListNode{
int sno;
char sname[20];
int sscore;
struct ListNode* next;
};
struct ListNode* newnode(int n);
//void deletenode(struct LinkNode* node);
//struct LinkNode* searchnode(struct LinkNode *node);
//struct LinkNode* insertnode(struct LinkNode* node);
void printnode(struct ListNode* head);
int main(){
ListNode* head;
head=newnode(3);
printnode(head);
return 0;
}
struct ListNode* newnode(int n){
ListNode* temp;
ListNode* head;
for(int i=1;i<n;i++){//从一开始是因为第一次循环为头结点和下一个节点赋值了
if(i==1){
head=new ListNode;
temp=head;
cin>>head->sno>>head->sname>>head->sscore;
}
temp->next=new ListNode;
temp=temp->next;
cin>>temp->sno>>temp->sname>>temp->sscore;
}
temp->next=NULL;//如果为节点分配了空间再让他等于NULL好像不可以
return head;
}
//void deletenode(struct ListNode* node){}
//struct ListNode* searchnode(struct ListNode *node){}
//struct ListNode* insertnode(struct ListNode* node){}
void printnode(struct ListNode* head){
while(head!=NULL){
cout<<head->sno<<" "<<head->sname<<" "<<head->sscore;
head=head->next;
}
}
2、P211 第11题
#include<iostream>
using namespace std;
int main()
{
enum weekday{sun,mon,tus,wed,thu,fri,sat};
enum weekday day;
int a,b,c,d,e,f,g,loop;
char ch='A';
f=thu;
for(a=sun;a<=sat;a++)
if(a!=f)
for(b=sun;b<=sat;b++)
if((a!=b)&&(f>b))
for(c=sun;c<=sat;c++)
if((c!=a)&&(c!=b)&&(c!=f)&&(a==c+1)&&(f<c))
for(d=sun;d<=sat;d++)
if((d!=a)&&(d!=b)&&(d!=c)&&(d!=f)&&(c==d+3))
for(e=sun;e<=sat;e++)
if((e!=a)&&(e!=b)&&(e!=c)&&(e!=d)&&(e!=f)&&(d==e+2))
for(g=sun;g<=sat;g++)
if((g!=a)&&(g!=b)&&(g!=c)&&(g!=d)&&(g!=e)&&(g!=f)&&(g==b+2))
for(loop=0;loop<7;loop++)
{ cout<<"Doctor"<<char(ch+loop)<<":";
switch(loop+1)
{
case 1:day=weekday(a);break;
case 2:day=weekday(b);break;
case 3:day=weekday(c);break;
case 4:day=weekday(d);break;
case 5:day=weekday(e);break;
case 6:day=weekday(f);break;
case 7:day=weekday(g);break;
}
switch(day)
{
case sun:cout<<"Sunday"<<endl;break;
case mon:cout<<"Monday"<<endl;break;
case tus:cout<<"Thusday"<<endl;break;
case wed:cout<<"Wednesday"<<endl;break;
case thu:cout<<"Thurday"<<endl;break;
case fri:cout<<"Friday"<<endl;break;
case sat:cout<<"Saturday"<<endl;break;
}
}
return 0;
}
3、(选做)模拟一个用于显示时间的电子时钟。该时钟以时、分、秒的形式记录时间。试编写三个函数:setTime函数用于设置时钟的时间。increase函数模拟时间过去了1秒。showTime 显示当前时间,显示格式为HH:MM:SS。
#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
struct clock{
int hour;
int minute;
int second;
};
class clock_time{
public:
void setTime();
void increase();
void showTime();
private:
struct clock current_time;
};
void clock_time::setTime(){
time_t rawtime;
struct tm *info;
time( &rawtime );
info = localtime( &rawtime );
current_time.hour=info->tm_hour;
current_time.minute=info->tm_min;
current_time.second=info->tm_sec;
}
void clock_time::increase(){
setTime();
}
void clock_time:: showTime(){
while(1){
increase();
system("clear");
cout<<current_time.hour<<":"<<current_time.minute<<":"<<current_time.second<<endl;
}
}
int main(){
clock_time MyClock;
MyClock.showTime();
return 0;
}