最近一段时间在看c++primer第五版,准备把课后的习题答案贴出来,做的不对的地方还希望各位看客指正。
ps:所有代码编译环境均为vs2013。
chapter1_1.3#include "stdafx.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello,World!"<<endl;
return 0;
}
chapter1_1.4
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
cout << "enter two numbers:" << endl;
cin >> x >> y;
cout << "The multiply of" << x << "and" << y << "is" << x*y << endl;
return 0;
}
chapter1_1.9
#include "stdafx.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int sum = 0;
for (int i = 50; i < 100; i++)
sum = sum + i;
cout<< sum <<endl;
return 0;
}
chapter1_1.10
#include "stdafx.h"
#include "iostream"
#include "iomanip"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 9; i > 0; i--)
cout << setw(3) << i;
cout << endl;
return 0;
}
chapter1_1.11
#include "stdafx.h"
#include "iomanip"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
cout << "enter two numbers" << endl;
cin >> x >> y;
if (x >= y)
{
for (int i = y; i <=x; i++)
cout << setw(2)<<i;
}
else
for (int i = x; i <= y; i++)
cout << setw(2) << i;
cout << endl;
return 0;
}
chapter1_1.16
#include "stdafx.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "请输入一组数:" << endl;
int i=0,sum = 0;
while (cin >> i)
{
sum = sum + i;
}
cout << "输入数的总和=" << sum<<endl;
return 0;
}