c++ primer plus 6th 第四章答案

//1

#include<iostream>
#include<string>


int main()
{
using namespace std;
char grade;
char last_name[20];
char first_name[20];
int age;
cout<<"What is your first name? ";
cin.getline(first_name,20);
cout<<"What is your last name? ";
cin.getline(last_name,20);
cout<<"What letter grade do you deserve? ";
cin>>grade;
cout<<"What is your age? ";
cin>>age;
cout<<"Name:"<<last_name<<","<<first_name<<endl;;
cout<<"Grade:"<<char(grade+1)<<endl;;
cout<<"Age:"<<age<<endl;
return 0;
}

//2

#include <iostream>
#include <string>


int main()
{
using namespace std;
string s;
cin>>s;
cout<<s<<endl;
return 0;
}

//3

#include <iostream>
#include <cstring>


int main()
{
using namespace std;
char last_name[20];
char first_name[20];
cout<<"Enter your first name:";
cin.getline(first_name,20);
cout<<"Enter your last name:";
cin.getline(last_name,20);
strcat(last_name,",");
strcat(last_name,first_name);
cout<<"Here's the information in a single string:"<<last_name<<endl;
return 0;
}

//4

#include <iostream>
#include <string>


int main()
{
using namespace std;
string last_name,first_name;
cout<<"Enter your first name:";
cin>>first_name;
cout<<"Enter your last name:";
cin>>last_name;
cout<<"Here's the information in a single string:"<<last_name+","+first_name<<endl;
return 0;
}

//5

#include    <iostream>


int main()
{
using namespace std;
struct CandyBar
{
string name;
double weight;
int k;
};
CandyBar snack=
{
"Mocha Munch",
2.3,
350
};
cout<<snack.name<<endl;
cout<<snack.weight<<endl;
cout<<snack.k<<endl;
return 0;
}

//6

#include    <iostream>


int main()
{
using namespace std;
struct CandyBar
{
string name;
double weight;
int k;
};
CandyBar snack[3];
for(int i=0;i<3;i++)
{
snack[i].name="kkk";
snack[i].weight=i;
snack[i].k=i;
}
for(int i=0;i<3;i++)
{
cout<<snack[i].name<<endl;
cout<<snack[i].weight<<endl;
cout<<snack[i].k<<endl;
}
return 0;
}


//7

#include    <iostream>


int main()
{
using namespace std;
struct Pizza
{
string company_name;
double diameter;
double weight;
};
Pizza one;
getline(cin,one.company_name);
cin>>one.diameter;
cin>>one.weight;
cout<<"company name is:"<<one.company_name<<endl;
cout<<"company diameter is:"<<one.diameter<<endl;
cout<<"company weight is:"<<one.weight<<endl;
return 0;
}

//8

#include    <iostream>


int main()
{
using namespace std;
struct Pizza
{
string company_name;
double diameter;
double weight;
};
Pizza *ptr_one;
ptr_one=new Pizza;
getline(cin,ptr_one->company_name);
cin>>ptr_one->diameter;
cin>>ptr_one->weight;
cout<<"company name is:"<<ptr_one->company_name<<endl;
cout<<"company diameter is:"<<ptr_one->diameter<<endl;
cout<<"company weight is:"<<ptr_one->weight<<endl;
return 0;
}

//9

#include    <iostream>


int main()
{
using namespace std;
struct CandyBar
{
string name;
double weight;
int k;
};
CandyBar *snack=new CandyBar[3];
for(int i=0;i<3;i++)
{
snack[i].name="kkk";
snack[i].weight=i;
snack[i].k=i;
}
for(int i=0;i<3;i++)
{
cout<<snack[i].name<<endl;
cout<<snack[i].weight<<endl;
cout<<snack[i].k<<endl;
}
return 0;
}

//10

#include    <iostream>


int main()
{
using namespace std;
int a[3];
for(int i=0;i<3;i++)
cin>>i>>a[i];
cout<<"the averge is:"<<(a[0]+a[1]+a[2])/3<<endl;
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值