编译器为Clion 2019.03.02
//第一题
# include
# include
using namespace std;
int main()
{
string firstname = {};
string lastname = {};
int age = 0;
char grade = {};
cout<<"what is your first name?";
getline(cin,firstname);//读取从键盘的输入,用getline对象可以输入含有空格的字符串,
//是读取整行,以换行符为字符串结束的标志。
cout<<"what is your last name?";
getline(cin, lastname);
cout<<"what letter grade do you deserve?";
cin>>grade;
cout<<"what is your age?";
cin>>age;
cout<<"name:"<<firstname<<","<<lastname<<endl;
cout<<"Grade:"<<char(grade+1)<<endl;
cout<<"Age:"<<age<<endl;
return 0;
}
//第二题
# include
# include
int main()
{
using namespace std;
const int maxsize = 20;
string s1;
string s2;
cout<<"please input your name:\n";
getline(cin, s1);
cout<<"please input your favorite dessert:\n";
getline(cin, s2);
cout<<"I have some delicious "<<s2<<" for you "<<s1<<endl;
return 0;
}
//第三题
# include
int main()
{
using namespace std;
const int max = 20;
char first[max];
char last[max];
cout<<"enter your first name:";
cin.getline(first, max);
cout<<"enter your last name:";
cin.getline(last, max);
cout<<"here is the information in a single string: "<< last<<" "<<first<<endl;
return 0;
}
//第四题
# include
# include
# include
using namespace std;
int main()
{
string first ;
string last ;
cout<<"please input your first name:";
getline(cin, first);
cout<<"please input your last name:";
getline(cin, last);
cout<<"here's the information in a single string:"<<last<<", "<<first<<endl;
return 0;
//第五题
# include
# include
using namespace std;
struct candyBar{
string name;
double weight;
int carlorie;
};
int main()
{
candyBar snack={
"Mocha Munch",
2.3,
350
};
cout<<snack.name<<endl;
cout<<snack.weight<<endl;
cout<<snack.carlorie<<endl;
return 0;
}
//或者第五题
# include
using namespace std;
struct candyBar{
char name[20];
double weight;
int carlorie;
};
int main()
{
candyBar snack={
"Mocha Munch",
2.3,
350
};
cout<<snack.name<<endl;
cout<<snack.weight<<endl;
cout<<snack.carlorie<<endl;
return 0;
}
//第六题 结构数组
# include
# include
using namespace std;
struct candyBar{
string name;
double weight;
int carlorie;
};
int main()
{ //创建一个包含有3个结构元素的candyBar数组
candyBar arm[3]={
{"Mocha Munch", 2.3,350},
{"Dove",70, 2386},
{"cache",123.6,5380}
};
cout<<"candyBar_1: "<<arm[0].name<<","<<arm[0].weight<<","<<arm[0].carlorie<<endl;
cout<<"candyBAr_2: "<<arm[1].name<<","<<arm[1].weight<<","<<arm[1].carlorie<<endl;
cout<<"candyBar_3: "<<arm[2].name<<","<<arm[2].weight<<","<<arm[2].carlorie<<endl;
}
//第七题???
//创建一个结构,从键盘输入结构的元素
# include
# include
using namespace std;
struct pizza{
string name;
double size;
int number;
};
int main()
{
pizza orders[3];
cout<<"please input the 1st name:"<<endl;
getline(cin, orders[0].name);
cout<<"the size:"<<endl;
cin>>orders[0].size;
cout<<"the number:"<<endl;
cin>>orders[0].number;
cout<<"please input the 2nd name:"<<endl;
getline(cin, orders[1].name);
cout<<"the size:"<<endl;
cin>>orders[1].size;
cout<<"the number:"<<endl;
cin>>orders[1].number;
cout<<"please input the 3rd name:"<<endl;
getline(cin,orders[2].name);
cout<<"the size:"<<endl;
cin>>orders[2].size;
cout<<"the number:"<<endl;
cin>>orders[2].number;
cout<<orders[0].name<<","<<orders[0].size<<","<<orders[0].number<<endl;
cout<<orders[1].name<<","<<orders[1].size<<","<<orders[1].number<<endl;
cout<<orders[2].name<<","<<orders[2].size<<","<<orders[2].number<<endl;
return 0;
}
//第八题 用new 为结构分配内存,而不是声明结构变量
# include
# include
# include
using namespace std;
struct pizza{
string name;
double size;
int weight;
};
int main()
{
pizza * point = new pizza;
cout<<"input the name:";
getline(cin, point->name);
cout<<"input the size:";
cin>>point->size;
cout<<"input the weight";
cin>>point->weight;
cout<<"name:"<<point->name<<endl;
cout<<"size:"<<point->size<<endl;
cout<<"weight:"<<point->weight<<endl;
delete [] point;
return 0;
}
//第九题
# include
# include
using namespace std;
struct candyBar{
string name;
double weight;
int carlorie;
};
int main()
{ //用new 创建一个含有三个元素的结构数组
candyBar * point=new candyBar[3];
point[0] = {"Mocha Munch", 2.3,350};
point[1] = {"Dove",70, 2386};
point[2] = {"cache",123.6,5380};
cout<<"candyBar_1: "<<point[0].name<<","<<point[0].weight<<","<<point[0].carlorie<<endl;
cout<<"candyBAr_2: "<<point[1].name<<","<<point[1].weight<<","<<point[1].carlorie<<endl;
cout<<"candyBar_3: "<<point[2].name<<","<<point[2].weight<<","<<point[2].carlorie<<endl;
delete [] point;
return 0;
}
//第十题 用array对象存储数据
# include
# include
int main()
{
using namespace std;
array<double, 3> a1;
cout<<"please input the first :";
cin>>a1[0];
cout<<"please input the second :";
cin>>a1[1];
cout<<"please input the third :";
cin>>a1[2];
cout<<"1 "<<a1[0]<<endl;
cout<<"2 "<<a1[1]<<endl;
cout<<"3 "<<a1[2]<<endl;
return 0;
}