第一题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char fname[20],lname[20];
char grade;
int age;
cout<<"What is your first name? ";
cin.getline(fname,20);
cout<<"What is your last name? ";
cin.getline(lname,20);
cout<<"what letteer grade do yo deserve? ";
cin>>grade;
cout<<"What is your age? ";
cin>>age;
cout<<"Name: "<<lname<<", "<<fname<<endl;
cout<<"Grade: "<<char(grade+1)<<endl;
cout<<"Age: "<<age<<endl;
return 0;
}
第二题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int Arsize=20;
string name;
string dessert;
cout<<"Enter you name:\n";
getline(cin,name);
cout<<"Enter your favorite dessert:\n";
getline(cin,dessert);
cout<<"I have some delicious "<<dessert;
cout<<" For you, "<<name<<"\n";
return 0;
}
第三题:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int Arsize=20;
char fname[Arsize];
char lname[Arsize];
cout<<"Enter your first name: ";
cin>>fname;
cin.get();
cout<<"Enter your last name: ";
cin>>lname;
strcat(lname,", ");
cout<<"Here's information in a single string: "<<lname<<fname<<endl;
return 0;
}
第四题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string fname,lname;
cout<<"Enter your first name: ";
getline(cin,fname);
cout<<"Enter your last name: ";
getline(cin,lname);
lname=lname+", ";
cout<<"Here's information in a single string: "<<lname<<fname<<endl;
return 0;
}
第五题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct CandyBar
{
string name;
double weight;
int cal;
};
CandyBar snack={"Mocha Munch",2.3,350};
cout<<"name: "<<snack.name<<" weight: "<<snack.weight<<" calories: "<<snack.cal<<endl;
return 0;
}
第六题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct CandyBar
{
string name;
double weight;
int cal;
};
CandyBar member[3]=
{
{"Baidu",1.1,100},
{"Alibaba",1.2,200},
{"Tenxun",1.3,300}
};
cout<<"name: "<<member[0].name<<" weight: "<<member[0].weight<<" calories: "<<member[0].cal<<endl;
cout<<"name: "<<member[1].name<<" weight: "<<member[1].weight<<" calories: "<<member[1].cal<<endl;
cout<<"name: "<<member[2].name<<" weight: "<<member[2].weight<<" calories: "<<member[2].cal<<endl;
return 0;
}
第七题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct Pizza
{
string name;
double rayon;
double weight;
};
Pizza one;
cout<<"Enter the name: ";
getline(cin,one.name);
cout<<"Enter the rayon: ";
cin>>one.rayon;
cout<<"Enter the weight: ";
cin>>one.weight;
cout<<"The name: "<<one.name<<" rayon: "<<one.rayon<<" weight: "<<one.weight<<endl;
return 0;
}
第八题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct Pizza
{
string name;
double rayon;
double weight;
};
Pizza *p=new Pizza;
cin>>p->rayon;
cin.get();//处理换行符
cout<<"Enter the name: ";
getline(cin,p->name);
cout<<"Enter the weight: ";
cin>>p->weight;
cout<<"The name: "<<p->name<<" rayon: "<<p->rayon<<" weight: "<<p->weight<<endl;
delete p;
return 0;
}
第九题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct CandyBar
{
string name;
double weight;
int cal;
};
/*CandyBar *p=new CandyBar[3]
{
{"Baidu",1.1,100},
{"Alibaba",1.2,200},
{"Tenxun",1.3,300}
};*/
//c++11 可以直接初始化
CandyBar *p=new CandyBar[3];
cin>>p->name;
cin>>p->weight;
cin>>p->cal;
cin.get();
cin>>(p+1)->name;
cin>>(p+1)->weight;
cin>>(p+1)->cal;
cin.get();
cin>>(p+2)->name;
cin>>(p+2)->weight;
cin>>(p+2)->cal;
cout<<"name: "<<p->name<<" weight: "<<p->weight<<" calories: "<<p->cal<<endl;
cout<<"name: "<<(p+1)->name<<" weight: "<<(p+1)->weight<<" calories: "<<(p+1)->cal<<endl;
cout<<"name: "<<(p+2)->name<<" weight: "<<(p+2)->weight<<" calories: "<<(p+2)->cal<<endl;
return 0;
}
第十题:
#include <iostream>
//#include <array>
using namespace std;
int main()
{
/*array<float, 3> grade;
float sum=0;
cout<<"Enter first grade: ";
cin>>grade[0];
cout<<"Enter second grade: ";
cin>>grade[1];
cout<<"Enter last grade: ";
cin>>grade[2];*/
float grade[3],sum=0;
cin>>grade[0];
cin>>grade[1];
cin>>grade[2];
sum=sum+grade[0]+grade[1]+grade[2];
cout<<"Times: 3 average grade is: "<<sum/3<<endl;
return 0;
}