第四章.编程练习

第一题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     char fristname[20];
 7     char lastname[20];
 8     char grade;
 9     int age;
10 
11     cout<<"what is your first name? ";
12     cin.getline(fristname,20);
13     cout<<"what is your last name ? ";
14     cin.getline(lastname,20);
15     cout<<"what letter grade do you deserve ";
16     cin>>grade;
17     cout<<"what is your age ";
18     cin>>age;
19 
20     cout<<"name: "<<lastname<<" , "<<fristname<<endl;
21     cout<<"grade "<<char(grade+1)<<endl;
22     cout<<"age "<<age<<endl;
23     return 0;
24 }

第二题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     string name;
 7     string dessert;
 8 
 9     cout<<"enter your name ";
10     getline(cin,name);//c++中用getline输入string类的用法
11     cout<<"enter your favorite dessert ";
12     getline(cin,dessert);
13     cout<<"i have some delicous "<<dessert<<" for you "<<name<<endl;
14     return 0;
15 }

第三题

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     char lastname[40],firstname[20];
 8 
 9     cout<<"enter your first name";
10     cin.getline(firstname,20);
11     cout<<"enter your last name";
12     cin.getline(lastname,20);
13 
14     strcat(lastname,", ");
15     strcat(lastname,firstname);
16 
17     cout<<"here is the information in a single string "<<lastname<<endl;
18     return 0;
19 }

第四题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     string firstname,lastname;
 7     string name;
 8 
 9     cout<<"enter your first name ";
10     cin>>firstname;
11     cout<<"enter your last name ";
12     cin>>lastname;
13 
14     name=lastname+", "+firstname;
15 
16     cout<<"here is the information in a single string: "<<name<<endl;
17     return 0;
18 }

第五题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct CandyBar
 5 {
 6     string kind;
 7     float weight;
 8     int cal;
 9 };
10 
11 int main()
12 {
13     CandyBar snack={"mocha munch",2.3,250};
14     cout<<snack.kind<<","<<snack.weight<<" ,"<<snack.cal<<endl;
15     return 0;
16 }

第六题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct CandyBar
 5 {
 6     string kind;
 7     float weight;
 8     int cal;
 9 };
10 
11 int main()
12 {
13     CandyBar snack[3]={{"mocha munch",2.3,250},{"shdjks",6.3,4546},{"sdf",5.6,497}};
14     cout<<snack[0].kind<<","<<snack[0].weight<<" ,"<<snack[0].cal<<endl;
15     cout<<snack[1].kind<<","<<snack[1].weight<<" ,"<<snack[1].cal<<endl;
16     cout<<snack[2].kind<<","<<snack[2].weight<<" ,"<<snack[2].cal<<endl;
17     return 0;
18 }

第七题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct Pizza
 5 {
 6     string company;
 7     int d;
 8     int weight;
 9 };
10 int main()
11 {
12     Pizza pi;
13     cout<<"enter the information";
14     cout<<"enter the company ";
15     getline(cin,pi.company);
16     cout<<"enter the d ";
17     cin>>pi.d;
18     cout<<"enter the weight ";
19     cin>>pi.weight;
20     cout<<pi.company<<endl<<pi.d<<endl<<pi.weight<<endl;
21     return 0;
22 }

第八题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct Pizza
 5 {
 6     string company;
 7     int d;
 8     int weight;
 9 };
10 int main()
11 {
12     Pizza *pi=new Pizza;
13     
14     cout<<"enter the d ";
15     cin>>pi->d;
16     cout<<"enter the company";
17     getline(cin,pi->company);
18     cout<<"enter the weight ";
19     cin>>pi->weight;
20 
21     cout<<pi->company<<endl<<pi->d<<endl<<pi->weight<<endl;
22     return 0;
23 
24 }

第九题

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 struct CandyBar
 5 {
 6     string kind;
 7     float weight;
 8     int cal;
 9 };
10 
11 int main()
12 {
13     CandyBar *ps=new CandyBar[3];
14 
15     ps[0].kind="mocha munch";
16     ps[0].weight=2.3;
17     ps[0].cal=250;
18 
19     ps[1].kind="shdjks";
20     ps[1].weight=6.3;
21     ps[1].cal=525;
22     
23     ps[2].kind="dfsdfs";
24     ps[2].weight=5.6;
25     ps[2].cal=45;
26     
27     cout<<ps->kind<<","<<ps->weight<<" ,"<<ps->cal<<endl;
28     cout<<(ps+1)->kind<<","<<(ps+1)->weight<<" ,"<<(ps+1)->cal<<endl;
29     cout<<(ps+2)->kind<<","<<(ps+2)->weight<<" ,"<<(ps+2)->cal<<endl;
30 
31     return 0;
32 }

第十题

 1 #include<iostream>
 2 #include<string>
 3 #include<array>
 4 using namespace std;
 5 int main()
 6 {
 7     array<float,3>ps;
 8     cout<<"enter the score ";
 9     cin>>ps[0]>>ps[1]>>ps[2];
10     cout<<ps[0]<<ps[1]<<ps[2]<<endl;
11     cout<<(ps[0]+ps[1]+ps[2])/3.0;
12     return 0;
13 }

 

posted @ 2014-12-02 17:02  陶修瑕  阅读(149)  评论(0编辑  收藏  举报