码迷,mamicode.com
首页 > 编程语言 > 详细

C++程序设计原理与实践 第十章部分答案

时间:2014-11-10 23:15:47      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   for   

bubuko.com,布布扣
 1 #include "../../st.h"
 2 
 3 class Point{
 4 public:
 5     Point():x(0),y(0){}
 6     Point(int x1,int y1):x(x1),y(y1){}
 7     double re_x(){return x;}
 8     double re_y(){return y;}
 9     friend istream& operator>>(istream&is,Point&p);
10 private:
11     double x; 
12     double y;
13 };
14 
15 ostream& operator<<(ostream& os,Point&p)
16 {
17     return os<<"("<<p.re_x()<<","<<p.re_y()<<")";
18 }
19 
20 
21 istream& operator>>(istream&is,Point&p)
22 {
23     char c1,c2,c3;
24     is>>c1>>p.x>>c2>>p.y>>c3;
25     return is;
26 }
27 
28 
29 void fill_from_file(vector<Point>&p,string&s)
30 {
31     ifstream ist(s.c_str());
32     if(!ist)
33         error("can‘t open input file",s);
34     Point pp;
35     while(ist>>pp)
36         p.push_back(pp);
37 }
38 
39 void file_from_fill(vector<Point>&p,string&s)
40 {
41     ofstream ost(s.c_str());
42     int i;
43     if(!ost)
44         error("can‘t open output file",s);
45     for(i=0;i<p.size();i++)
46         ost<<(<<p[i].re_x()<<,<<p[i].re_y()<<)<<endl;
47 }
48 
49 int main()
50 {
51     int i;
52     int x,y;
53     vector<Point> original_points;
54     vector<Point> processed_points;
55     string oname="output.txt";
56     
57     for(i=0;i<7;i++)
58     {
59         cout<<i+1<<"  输入(x,y):";
60         cin>>x>>y;
61         Point p(x,y);
62         original_points.push_back(p);
63     }
64     
65     file_from_fill(original_points,oname);
66     fill_from_file(processed_points,oname);
67     
68     
69     for(i=0;i<7;i++)
70         cout<<original_points[i]<<endl;
71     for(i=0;i<7;i++)
72         cout<<processed_points[i]<<endl;
73     keep_window_open();
74         
75 }
简单练习
bubuko.com,布布扣
 1 #include "../../st.h"
 2 
 3 int main()
 4 {
 5     string s="mydata.txt";
 6     int i;
 7     int sum=0;
 8     ifstream ist(s.c_str());
 9     if(!ist)
10         error("can‘t open input file",s);
11     while(ist>>i)
12     {
13         sum+=i;
14     }
15     cout<<sum<<endl;
16     keep_window_open();
17         
18 }         
习题1
bubuko.com,布布扣
 1 #include "../../st.h"
 2 
 3 struct Reading{
 4     int hour;
 5     double temperature;
 6     char c;
 7     Reading(int h,double t,char ch):hour(h),temperature(t),c(ch){}
 8 };
 9 
10 ostream& operator<<(ostream&os,Reading&r)
11 {
12     return os<<r.hour<<,<<r.temperature<<r.c<<endl;
13 }
14 
15 istream& operator>>(istream&is,Reading&r)
16 {
17     char c;
18     is>>r.hour>>c>>r.temperature>>r.c;
19     return is;
20 }
21 
22 void change(Reading&r)
23 {
24     if(r.c==c)
25     {
26         r.c=f;
27         r.temperature=32+r.temperature*1.8;
28     }
29 
30 }
31 
32 void fill_from_file(vector<Reading>&r,string&s)
33 {
34     ifstream ist(s.c_str());
35     if(!ist)
36         error("can‘t open input file",s);
37     Reading rr(0,0,c);
38     while(ist>>rr)
39     {
40         change(rr);
41         r.push_back(rr);
42     }
43 }
44 
45 
46 int main()
47 {
48     string s="mydata.txt";
49     int hour;
50     double tem;
51     char ch;
52     vector<Reading> readings;
53     ofstream ost(s.c_str());
54     if(!ost)
55         error("can‘t open output file",s);
56     while(cin>>hour>>tem>>ch)
57     {
58         if(hour<0||hour>23)
59             error("hour out if range");
60         ost<<Reading(hour,tem,ch);
61     }
62     ost.close();
63 
64     fill_from_file(readings,s);
65     int i,j;
66     int h,t;
67     for(i=0;i<readings.size();i++)
68         for(j=0;j<readings.size()-i-1;j++)
69             if(readings[j].temperature>readings[j+1].temperature)
70             {
71                 h=readings[j].hour;
72                 t=readings[j].temperature;
73                 readings[j].hour=readings[j+1].hour;
74                 readings[j].temperature=readings[j+1].temperature;
75                 readings[j+1].hour=h;
76                 readings[j+1].temperature=t;
77             }
78     double sum=0;
79     for(i=0;i<readings.size();i++)
80         sum+=readings[i].temperature;
81     cout<<"middle:"<<readings[readings.size()/2].temperature<<endl<<"平均:"<<sum/readings.size()<<endl;
82 
83     keep_window_open();
84         
85 }        
习题2 3 4
bubuko.com,布布扣
 1 void print_year(ofstream&ofs,Year&y)
 2 {
 3     ofs<<y.year<<"   year:\n";
 4     for(int i=0;i<12;i++)
 5     {
 6         if(y.month[i].month!=not_a_month)
 7         {
 8             ofs<<"\tmonth  "<<y.month[i].month<<endl;
 9             for(int j=0;j<32;j++)
10             {
11                 if(y.month[i].day[i].is_day!=not_a_day)
12                 {
13                     ofs<<"\t\tday  "<<y.month[i].day[i].is_day<<endl;
14                     for(int k=0;k<24;k++)
15                         if(y.month[i].day[i].hour[k]!=not_a_reading)
16                             ofs<<"\t\t\t"<<i<<" hour: "<<y.month[i].day[i].hour[k]<<endl;
17                 }
18 
19             }
20         }
21     }
22 }    
习题5
bubuko.com,布布扣
  1 #include "../../st.h"
  2 
  3 class Roman_int{
  4 public:
  5     int as_int(string s);
  6     int re_int(){return i;}
  7     char re_kind(){return ch;}
  8     Roman_int()
  9         :i(0){}
 10     Roman_int(char c)
 11         :ch(c),i(0){}
 12     Roman_int(char c,int ii)
 13         :ch(c),i(ii){}
 14 private:
 15     int i;
 16     char ch;
 17 };
 18 
 19 string as_roman(int i)
 20 {
 21     if(i<1||i>>3999)
 22         error("over range 1~3999");
 23     string digits[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
 24     string tens[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
 25     string hundreds[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
 26     string thousands[4] = {"", "M", "MM", "MMM"};
 27 
 28     string ss="";
 29     ss+=thousands[i/1000];
 30     ss+=hundreds[i%1000/100];
 31     ss+=tens[i%100/10];
 32     ss+=digits[i%10];
 33 
 34     return ss;
 35 }
 36 
 37 int Roman_int::as_int(string s)
 38 {
 39     int b[100];
 40     int sum=0;
 41     int i1=0,sum1=0;
 42     for(i1=0;i1<s.size();i1++)
 43     {
 44         switch(s[i1])
 45         { 
 46             case I:b[i1]=1,sum1++;continue;
 47             case V:b[i1]=5,sum1++;continue;
 48             case X:b[i1]=10,sum1++;continue;
 49             case L:b[i1]=50,sum1++;continue;
 50             case C:b[i1]=100,sum1++;continue;
 51             case D:b[i1]=500,sum1++;continue;
 52             case M:b[i1]=1000,sum1++;continue;
 53             default: break;
 54         }
 55     }
 56     if(sum1==1)
 57         sum=b[0];
 58     else
 59     {
 60       sum=0;
 61       for(i1=0;i1<sum1;i1++)
 62       {  
 63           if(b[i1]>=b[i1+1])
 64               sum=sum+b[i1];
 65           else
 66           {
 67               sum=sum+(b[i1+1]-b[i1]);
 68               i1++;
 69           }
 70       } 
 71     }
 72     i=sum;
 73     return sum;
 74 }
 75 
 76 istream& operator>>(istream&is,Roman_int&ri)
 77 {
 78     string s;
 79     is>>s;
 80     ri.as_int(s);
 81     return is;
 82 }
 83 
 84 ostream& operator<<(ostream&os,Roman_int&ri)
 85 {
 86     return os<<ri.re_int();
 87 }
 88 
 89 class Token_stream{
 90 public:
 91     Token_stream();
 92     Roman_int get();
 93     Roman_int get(istream&);
 94     void putback(Roman_int ri);
 95 private:
 96     bool full;
 97     Roman_int buffer;
 98 };
 99 
100 Token_stream::Token_stream()
101     :full(false),buffer(0)
102 {
103 }
104 
105 void Token_stream::putback(Roman_int t)
106 {
107     if(full) error("putback() into a full buffer");
108     buffer=t;
109     full=true;
110 }
111 
112 Roman_int Token_stream::get(istream&ifs)
113 {
114     if(full){
115         full=false;
116         return buffer;
117     }
118     char ch;
119     ifs>>ch;
120     switch(ch){
121     case ;: case q: case (: case ): case {:
122     case }:  case +: case -: case *: case /:
123         return Roman_int(ch);
124     case I: case V: case X: 
125     case C: case D: case M:
126         { 
127             string s="";
128             s+=ch;
129             while(ifs>>ch)
130             {
131                 if(ch==I||ch==V||ch==X||ch==C||ch==D||ch==M)
132                     s+=ch;
133                 else
134                 {
135                     ifs.putback(ch);
136                     break;
137                 }
138             }
139             Roman_int r(8);
140             r.as_int(s);
141             return r;
142         }
143     case f: 
144         while(cin.get(ch)&&isalpha(ch))   
145                 ;
146             cin.putback(ch);
147             return Roman_int(f);
148     case t:
149             while(cin.get(ch)&&isalpha(ch))   
150                 ;
151             cin.putback(ch);
152             return Roman_int(t);
153     default:
154         error("bad token");
155         }
156 }
157 
158 Token_stream ts;
159 double expression(istream&ifs);
160 
161 double primary(istream&ifs)
162 {
163 
164     Roman_int t=ts.get(ifs);
165     switch(t.re_kind()){
166     case (:
167     {
168         int d=expression(ifs);
169         t=ts.get(ifs);
170         if(t.re_kind()!=))  
171             error("‘)‘expected");
172         return d;
173     }
174     case 8:
175         return t.re_int();
176     default:
177         error("primary expected",t.re_kind());
178     }
179 }
180 
181 double primary1(istream&ifs)
182 {
183     Roman_int t=ts.get(ifs);
184     while(true)
185     {
186         switch(t.re_kind()){
187         case {:
188         {
189             double d=expression(ifs);
190             t=ts.get(ifs);
191             if(t.re_kind()!=})  
192                 error("‘}‘expected");
193             return d;
194         }
195         default:
196             ts.putback(t);
197             return primary(ifs);
198         }
199     }
200 }
201 
202 double term(istream&ifs)
203 {
204     double left=primary1(ifs);
205     Roman_int t=ts.get(ifs);
206 
207     while(true){
208         switch(t.re_kind()){
209         case *:
210             left*=primary1(ifs);
211             t=ts.get(ifs);
212             break;
213         case /:
214             {
215                 double d=primary1(ifs);
216                 if(d==0)
217                     error ("divide by 0");
218                 left /=d;
219                 t=ts.get(ifs);
220                 break;
221             }
222         default:
223             ts.putback(t);
224             return left;
225         }
226     }
227 }
228 
229 double expression(istream&ifs)
230 {
231     double left=term(ifs);
232     Roman_int t=ts.get(ifs);
233     while(true){
234         switch(t.re_kind()){
235         case +:
236             left+=term(ifs);
237             t=ts.get(ifs);
238             break;
239         case -:
240             left-=term(ifs);
241             t=ts.get(ifs);
242             break;
243         default:
244             ts.putback(t);
245             return left;
246         }
247     }
248 }
249 
250 int main()
251 {
252     try{
253     double val=0;
254     Roman_int t;
255     while(cin)
256     {
257         t=ts.get(cin);
258 
259 
260         if(t.re_kind()==f)
261         {
262             double val1=0;
263             string s;
264             cin>>s;
265             ifstream ifs(s.c_str());
266             if(!ifs)
267                 error("can not open input file1",s);
268             t=ts.get(cin);
269             if(t.re_kind()==t)
270             {
271                 cin>>s;
272                 ofstream ofs(s.c_str());
273                 if(!ofs)
274                     error("can not open output file1",s);
275                 while(ifs)
276                 {
277                     t=ts.get(ifs);
278                     while(t.re_kind()==;)
279                         t=ts.get(ifs);
280                     if(t.re_kind()==q) 
281                         break;
282                     ts.putback(t);
283                     ofs<<as_roman(expression(ifs))<<endl;
284                 }
285             }
286         }
287         else
288         {
289 
290             while(t.re_kind()==;)
291                 t=ts.get(cin);
292             if(t.re_kind()==q) 
293             {
294                 keep_window_open("~~");
295                 return 0;        
296             }        
297             ts.putback(t);
298             cout<<as_roman(expression(cin))<<endl;
299         }
300     }
301     return 0;
302 
303     }
304 
305 catch(exception&e){
306     cerr<<e.what()<<endl;
307     while(1);
308     return 1;
309 }
310 
311 catch(...){
312     cerr<<"exception\n";
313     while(1);
314     return 2;
315 }
316     
317 }
习题6 7 10
bubuko.com,布布扣
 1 void fn(string&a,string&b,string&c)
 2 {
 3     ifstream ifs1(a.c_str());
 4     if(!ifs1)
 5         error("can not open input file1",a);
 6     ifstream ifs2(b.c_str());
 7     if(!ifs2)
 8         error("can not open input file1",b);
 9     vector<string> s;
10     string ss;
11     while(ifs1>>ss)
12         s.push_back(ss);
13     while(ifs2>>ss)
14         s.push_back(ss);
15     sort(s.begin(),s.end());//习题9
16     ofstream ofs(c.c_str());
17     if(!ofs)
18         error("can not open output file1",c);
19     for(int i=0;i<s.size();i++)
20         ofs<<s[i]<< ;
21 }
习题8 9
bubuko.com,布布扣
 1 #include "../../st.h"
 2 
 3 int main()
 4 {
 5     string s1="a.txt";
 6     double sum=0;
 7     double d;
 8     char ch;
 9     ifstream ifs(s1.c_str());
10     if(!ifs)
11         error("can not open input file1",s1);
12     while(ifs>>ch)
13     {
14         if(isdigit(ch))
15         {
16             ifs.putback(ch);
17             ifs>>d;
18             sum+=d;
19         }
20     }
21 
22     cout<<sum<<endl;
23     keep_window_open();
24 }
习题11
bubuko.com,布布扣
 1 //用到第十一章内容
 2 
 3 #include "../../st.h"
 4 
 5 int main()
 6 {
 7     string s1="a.txt";
 8     string str1;
 9     string s;
10     int i=0;
11     cout<<"input the word:";
12     cin>>str1;
13     ifstream ifs(s1.c_str());
14     if(!ifs)
15         error("can not open input file1",s1);
16     while(getline(ifs,s))
17     {
18         i++;
19         stringstream ss(s);
20         string str;
21         while(ss>>str)
22         {
23             if(str==str1){
24                 cout<<i<<"    "<<s<<endl;
25                 break;
26             }
27         }
28 
29     }
30 
31     keep_window_open();
32 } 
习题12

 

C++程序设计原理与实践 第十章部分答案

标签:style   blog   http   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/yueba/p/4088413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!