C++程序设计原理与实践 第十八章部分答案
1 int strcmp1(const char* s1,const char*s2) 2 { 3 int i=0; 4 cout<<strlen(s1)<<endl; 5 while(*(s1+i)==*(s2+i)) 6 { 7 if(*(s1+i)==‘\0‘) 8 return 0; 9 else 10 i++; 11 } 12 return *(s1+i)-*(s2+i); 13 //return((*(unsignedchar*)(s1+i)<*(unsignedchar*)(s2+i))?-1:+1); 14 15 } 16 17 char* cat_dot(const char* s1,const char* s2,const char* s3) 18 { 19 char* c=new char[strlen(s1)+strlen(s2)+strlen(s3)+1]; 20 if (c == NULL) exit (1); 21 char* ch=c; 22 while(*s1!=‘\0‘) 23 *c++=*s1++; 24 while(*s3!=‘\0‘) 25 *c++=*s3++; 26 while((*c++=*s2++)!=‘\0‘) 27 ; 28 return ch; 29 }
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 istream& read_w(istream&is,char*b,int max) 6 { 7 is.width(max); 8 is>>b; 9 cout<<strlen(b)<<endl; 10 if(strlen(b)==max-1) 11 { 12 char* c1=new char[]; 13 is>>c1; 14 if(*c1!=NULL) 15 cerr<<"过长"<<endl; 16 } 17 18 return is; 19 } 20 21 istream& read_w(istream&is,char*b) 22 { 23 24 is>>b; 25 return is; 26 } 27 28 int main() 29 { 30 char *s=new char; 31 while(read_w(cin,s)) 32 { 33 cout<<s<<endl; 34 cout<<(void *)(&s[1])<<endl; //输出地址 35 } 36 37 while(1); 38 }
1 //这是双向图 2 //可考虑单向图和随机图 3 4 //#include "../../st.h" 5 #include<ctime> 6 #include<cstdlib> 7 #include <vector> 8 #include <iostream> 9 using namespace std; 10 11 double random(double start,double end) 12 { 13 return start+(end-start)*rand()/(RAND_MAX+1.0); 14 } 15 16 struct sd{ 17 int n; 18 vector<int> ns; 19 int danger; //1 陷阱 2 蝙蝠 0 无 20 bool b; //怪兽存在 21 sd():n(0),danger(0),b(false){} 22 23 void find_danger(vector<sd>&vsd); 24 25 }; 26 27 void sd::find_danger(vector<sd>&vsd) 28 { 29 if(vsd[ns[0]].danger==1||vsd[ns[1]].danger==1||vsd[ns[2]].danger==1) 30 cout<<"我感到一阵微风吹来"<<endl; 31 if(vsd[ns[0]].danger==2||vsd[ns[1]].danger==2||vsd[ns[2]].danger==2) 32 cout<<"我听到蝙蝠的声音"<<endl; 33 if(vsd[ns[0]].b==true||vsd[ns[1]].b==true||vsd[ns[2]].b==true) 34 cout<<"我闻到了怪兽的味道"<<endl; 35 } 36 37 void set_danger(vector<sd>&vsd,const int n) 38 { 39 int i; 40 srand((unsigned)time(NULL)); 41 for(i=1;i<n;i++) 42 { 43 vsd[i].danger=int(random(0,3)); 44 } 45 } 46 47 void fn(vector<int>&vint,const int n) 48 { 49 int i=0; 50 int j; 51 int m; 52 srand((unsigned)time(NULL)); 53 while(i<n) 54 { 55 int f=0; 56 m=int(random(1,n+1)); 57 for(j=0;j<vint.size();j++) 58 if(m==vint[j]) 59 { 60 f=1; 61 break; 62 } 63 if(f==0) 64 { 65 vint.push_back(m); 66 i++; 67 } 68 } 69 70 } 71 72 73 void f(vector<sd>&vsd,int n1,int n2,int n3,int n4) 74 { 75 vsd[n1].ns.push_back(n2); 76 vsd[n1].ns.push_back(n3); 77 vsd[n1].ns.push_back(n4); 78 } 79 80 void f1(vector<sd>&vsd) 81 { 82 83 f(vsd,1,2,3,4); 84 f(vsd,2,1,5,6); 85 f(vsd,3,1,7,8); 86 f(vsd,4,1,9,10); 87 f(vsd,5,2,6,10); 88 f(vsd,6,2,5,7); 89 f(vsd,7,3,6,8); 90 f(vsd,8,3,7,9); 91 f(vsd,9,4,8,10); 92 f(vsd,10,4,9,5); 93 /*f(vsd,11,5,22,12); 94 f(vsd,12,5,11,13); 95 f(vsd,13,6,12,14); 96 f(vsd,14,6,13,15); 97 f(vsd,15,7,14,16); 98 f(vsd,16,7,15,17); 99 f(vsd,17,8,16,18); 100 f(vsd,18,8,17,19); 101 f(vsd,19,9,18,20); 102 f(vsd,20,9,19,21); 103 f(vsd,21,10,20,22); 104 f(vsd,22,10,21,11);*/ 105 } 106 107 int start(vector<sd>&vsd,const int n) 108 { 109 srand((unsigned)time(NULL)); 110 int i=int(random(1,n));; 111 vsd[i].b=true; 112 return i; 113 } 114 115 int find_num(int i,vector<sd>&vsd) 116 { 117 int j=1; 118 for(j=1;j<vsd.size();j++) 119 { 120 if(vsd[j].n==i) 121 return j; 122 } 123 return 0; 124 } 125 126 int find_num1(int i,vector<sd>&vsd) 127 { 128 return vsd[i].n; 129 } 130 131 int griffin_run(vector<sd>&vsd,int griffin_sd) 132 { 133 srand((unsigned)time(NULL)); 134 int i=int(random(0,3)); 135 vsd[griffin_sd].b=false; 136 vsd[vsd[griffin_sd].ns[i]].b=true; 137 return vsd[griffin_sd].ns[i]; 138 } 139 140 int main() 141 { 142 srand((unsigned)time(NULL)); 143 vector<sd>vsd; 144 vector<int>vint; 145 fn(vint,10); 146 sd s; 147 vsd.push_back(s); 148 for(int p=1;p<11;p++) 149 { 150 vsd.push_back(s); 151 vsd[p].n=vint[p-1]; 152 } 153 set_danger(vsd,11); 154 155 f1(vsd); 156 157 int arrow=3; //箭 158 int boold=2; 159 int griffin_sd= start(vsd,11); 160 int now_sd=int(random(1,11)); 161 vsd[now_sd].danger=0; 162 163 164 for(int p=1;p<11;p++) 165 { 166 cout<<p<<"\t"<<vsd[p].n<<"\t"<<vsd[p].ns[0]<<"\t"<<vsd[p].ns[1]<<"\t"<<vsd[p].ns[2]<<"\t"<<vsd[p].danger<<"\t"<<vsd[p].b<<endl; 167 } 168 169 170 char c; 171 int i1,i2,i3; 172 173 174 while(1) 175 { 176 if(vsd[now_sd].b==true) 177 { 178 cout<<"game over 1"<<endl; 179 while(1); 180 return 0; 181 } 182 if(vsd[now_sd].danger==1) 183 { 184 cout<<"game over 2"<<endl; 185 while(1); 186 return 0; 187 } 188 else if(vsd[now_sd].danger==2) 189 { 190 srand((int)time(0)); 191 now_sd=int(random(1,11)); 192 now_sd=vsd[now_sd].n; 193 cout<<"蝙蝠把你带到了"<<now_sd<<"房间"<<endl; 194 continue; 195 } 196 vsd[now_sd].find_danger(vsd); 197 if(arrow>0) 198 cout<<"你在"<<vsd[now_sd].n<<"房间中;房间"<<vsd[now_sd].n<<"通过地道与房间"<<vsd[vsd[now_sd].ns[0]].n<<"、"<<vsd[vsd[now_sd].ns[1]].n<<"、"<<vsd[vsd[now_sd].ns[2]].n<<"相连;移动还是射击?"<<endl; 199 else 200 cout<<"你在"<<vsd[now_sd].n<<"房间中;房间"<<vsd[now_sd].n<<"通过地道与房间"<<vsd[vsd[now_sd].ns[0]].n<<"、"<<vsd[vsd[now_sd].ns[1]].n<<"、"<<vsd[vsd[now_sd].ns[2]].n<<"相连;没箭了请移动"<<endl; 201 cin>>c; 202 if(c==‘m‘) 203 { 204 cin>>i1; 205 now_sd=find_num(i1,vsd); 206 continue; 207 } 208 else if(c==‘s‘) 209 { 210 211 cin>>i1>>i2>>i3; 212 i1=find_num(i1,vsd); 213 i2=find_num(i2,vsd); 214 i3=find_num(i3,vsd); 215 arrow--; 216 217 if(vsd[i1].b==true||vsd[i2].b==true||vsd[i3].b==true) 218 { 219 boold--; 220 if(boold==0) 221 { 222 cout<<"you win"<<endl; 223 while(1); 224 return 0; 225 } 226 else 227 { 228 griffin_sd=griffin_run(vsd,griffin_sd); 229 for(int p=1;p<11;p++) 230 { 231 cout<<p<<"\t"<<vsd[p].n<<"\t"<<vsd[p].ns[0]<<"\t"<<vsd[p].ns[1]<<"\t"<<vsd[p].ns[2]<<"\t"<<vsd[p].danger<<"\t"<<vsd[p].b<<endl; 232 } 233 continue; 234 } 235 } 236 } 237 else 238 cout<<"error"<<endl; 239 240 } 241 return 0; 242 }
习题4 利用sizeof strlen
习题8 创建副本后负责转换在比较
习题9
静态存储区: 全局
栈:局部 函数
自由存储区:new
栈扩展向低 自由存储区的数组想高地址存储
习题11 跳表 百度吧 = =
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。