C++Primer 第五版 习题答案 第一章 开始
发布时间
阅读量:
阅读量
1.1:
#include <iostream>
int main()
{
return 0;
}
AI写代码
1.2:
#include <iostream>
int main()
{
return 1;
}
AI写代码
1.3:
为了避免使用 using namespace std; 参照 虽然使用 using namespace std; 可能会将std的名字域引入到当前命名空间中并可能造成名字冲突
#include <iostream>
//using namespace std;
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
AI写代码
1.4:
#include <iostream>
int main()
{
std::cout << "input two nums v1, v2:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The producet of " << v1 << " and " << v2 << " is " << v1 * v2 << std::endl;
return 0;
}
AI写代码
1:5
#include <iostream>
int main()
{
std::cout << "input two nums v1, v2:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The producet of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 * v2;
std::cout<< std::endl;
return 0;
}
AI写代码
1.6:
不合法,前两行结束有分号,代表语句结束,第2,3行“<<”之前缺少新的输出流。
std::cout << "The producet of " << v1;
std::cout << " and " << v2;
std::cout << " is "<< v1 * v2<< std::endl;
AI写代码
1.7:
#include <iostream>
/* *asdsa
*ads
* sa/*das*/会被认为是源码
* asd
* asd
* ad
*/
int main()
{
return 0;
}
AI写代码
1.8
第三个非法,改为
std::cout << /* "*/"*/";
AI写代码
1.9:
#include <iostream>
int main()
{
int sum =0, val = 50;
while (val <= 100)
{
sum += val;
++val;
}
std::cout <<"sum = " << sum << std::endl;
return 0;
}
AI写代码
1.10
#include <iostream>
int main()
{
int val = 10;
while (val>=0 )
{
std::cout << val << std::endl;
--val;
}
return 0;
}
AI写代码
1.11:
#include <iostream>
int main()
{
std::cout << "please input two integers:";
int v1 =0, v2 =0 ;
std::cin >> v1 >> v2;
if (v1 > v2)
{
int tmp = v1;
v1 = v2;
v2 = tmp;
}
while (v1 <= v2)
{
std::cout << v1 << std::endl;
++v1;
}
return 0;
}
AI写代码
1.12
求-100到100的累加和,sum=0
1.13:
Ex1.9
#include <iostream>
int main()
{
int sum = 0;
for (int val = 50; val <= 100;++val)
{
sum += val;
}
std::cout << "sum = " << sum << std::endl;
return 0;
}
AI写代码
Ex 1.10
#include <iostream>
int main()
{
for (int val = 10; val >= 0;--val)
{
std::cout << val << std::endl;
}
return 0;
}
AI写代码
Ex 1.11
int main()
{
std::cout << "please input two integers:";
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
if (v1 > v2)
{
int tmp = v1;
v1 = v2;
v2 = tmp;
}
for(;v1 <= v2;++v1)
{
std::cout << v1 << std::endl;
}
return 0;
}p
AI写代码
1.14:
已知迭代次数时,使用for循环比较简洁;不知道迭代次数时使用while。
1.15:
自行练习
1.16
数字输入完成,输入文件结束符,windows下为 crtl Z +Enter
#include <iostream>
int main()
{
int sum = 0, val = 0;
while (std::cin>>val)
{
sum += val;
}
std::cout << "sum =" << sum <<std::endl;
return 0;
}
AI写代码
1.17
#include <iostream>
int main()
{
int currVal = 0, val = 0;
if (std::cin >> currVal)
{
int cnt = 1;
while (std::cin >> val)
{
if (val == currVal)
{
++cnt;
}
else
{
std::cout << currVal << " occurs " << cnt << " times" << std::endl;
currVal = val;
cnt = 1;
}
}
std::cout << currVal << " occurs " << cnt << " times" << std::endl;
}
return 0;
}
AI写代码
1.18
当所有输入数值完全相同时,
按住Ctrl键并按下Z键后才会开始统计数量;
若没有重复数据出现,
当前出现一个与之前不同的数值时,
按住Ctrl键并按下Z键后才会显示最终统计结果。
1.19
见1.11
1.20
#include <iostream>
#include"VisualStudio2012/Sales_item.h"
int main()
{
Sales_item item;
std::cin >> item;
std::cout << item<<std::endl;
}
AI写代码
1.21
#include <iostream>
#include"VisualStudio2012/Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
std::cout << item1 + item2 << std::endl;
}
AI写代码
1.22
#include <iostream>
#include"VisualStudio2012/Sales_item.h"
int main()
{
Sales_item item, sum_item;
std::cin >> sum_item;
while (std::cin >> item)
{
sum_item += item;
}
std::cout << sum_item << std::endl;
}
AI写代码
1.23
#include <iostream>
#include"VisualStudio2012/Sales_item.h"
int main()
{
Sales_item currItem, valItem;
if (std::cin >> currItem)
{
int cnt = 1;
while (std::cin >>valItem)
{
if (currItem.isbn() == valItem.isbn())
{
++cnt;
}
else
{
std::cout << currItem << " occurs " << cnt << " times" << std::endl;
currItem = valItem;
cnt = 1;
}
}
std::cout << currItem << " occurs " << cnt << " times" << std::endl;
}
return 0;
}
AI写代码
1.24
同上
1.25
#include <iostream>
#include "VisualStudio2012/Sales_item.h"
int main()
{
Sales_item total;
if (std::cin >> total) {
Sales_item trans;
while (std::cin >> trans) {
if (total.isbn() == trans.isbn())
total += trans;
else {
std::cout << total << std::endl;
total = trans;
}
}
std::cout << total << std::endl;
}
else
{
std::cerr << "no data?!" << std::endl;
return -1;
}
return 0;
}
AI写代码
0-201-78345-X
全部评论 (0)
还没有任何评论哟~
