1.输入两个整数,然后求之间数的和
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
int num1;
int num2;
int i;
int sum=0;
cout<<"Please enter the first number:";
cin>>num1;
cout<<"Please enter the second number:";
cin>>num2;
for(i=num1;i<=num2;i++)
{
sum+=i;
}
cout<<"The sum of number "<<num1<<" to "<<num2<<",sum= "<<sum<<endl;
return 0;
}
2.重新5.4
#include "stdafx.h"
#include<iostream>
#include<string>
#include<array>
using namespace std;
const int ArSize =16;
int main()
{
array<long double, ArSize+1> factorials;
factorials[0]=factorials[1]=1.0;
for(int i=2;i<= ArSize;i++)
{
factorials[i]=i*factorials[i-1];
}
for (int i = 0; i <= ArSize; i++)
{
cout<<i<< "! = "<<factorials[i]<<endl;
}
return 0;
}
3.求累计和
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
int num;
int sum=0;
while(1)
{
cout<<"Enter a number(0 to exit):";
cin>>num;
if(num==0)
{
break;
}
sum+=num;
cout<<"Until now,the sum of the number you input ="<<sum<<endl;
}
cout<<"Ending!"<<endl;
return 0;
}
4.投资比较
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
double D_account=100;
double C_account=100;
int years=0;
while(D_account>=C_account)
{
C_account+=C_account*0.05;
D_account+=10;
years++;
}
cout<<"After "<<years<<" years."<<"Cleo account ("<<C_account
<<") will more than Daphne account ("<<D_account<<");"<<endl;
return 0;
}
5.书一年中每月销售量
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
string month[12] = { "January", "February", "March", "April", "May", "June", "July",
"August", "Septempber", "October", "November", "December" };
int sell[12];
int total_sales = 0;
cout<<"Enter the sales of book <C++ for Fools> each month: " << endl;
for(int i = 0; i < 12; i++)
{
cout<<month[i]<<":";
cin>>sell[i];
total_sales+=sell[i];
}
cout<<"The total sales = "<<total_sales<<endl;
cout<<"The sales of every month as following:"<<endl;
for (int i=0; i<12;i++)
{
cout<<month[i]<<": "<<sell[i]<<endl;
}
return 0;
}
6.书三年中每月销量
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
string month[12]={ "January", "February", "March", "April", "May", "June", "July",
"August", "Septempber", "October", "November", "December" };
string years[3]={"first","second","third"};
unsigned int sales[3][12]={ 0 };
unsigned int total_sales[3]={ 0 };
for (int i = 0; i < 3; i++)
{
cout<<"Enter "<<years[i]<<" year sales of book <C++ for Fools> each month: "
<<endl;
for (int j=0; j<12;j++)
{
cout<<month[j]<< ": ";
cin >>sales[i][j];
total_sales[i]+=sales[i][j];
}
}
for (int i=0;i<3;i++)
{
cout<<years[i]<<" year total sales is "<<total_sales[i]<<endl;
}
cout<<"Three years total sales is "<<total_sales[0]+total_sales[1]+total_sales[2]
<<endl;
return 0;
}
7.汽车信息
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct car
{
string company;
int pyear;
};
int main()
{
int size = 0;
cout << "How many cars do you wish to catalog? ";
cin >>size;
cin.get();
struct car *pcar = new struct car[size];
for (unsigned int i = 0; i < size; i++)
{
cout << "Car #" << i + 1 << ":" << endl;
cout << "Please enter the make: ";
getline(cin, pcar[i].company);
cout << "Please enter the year make: ";
cin >> pcar[i].pyear;
cin.get();
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < size; i++)
{
cout << pcar[i].pyear << " " << pcar[i].company << endl;
}
return 0;
}
8.读取单词个数
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
unsigned int n_word = 0;
char input[128];
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> input)
{
if (strcmp(input, "done"))
{
n_word++;
}
else
{
break;
}
}
cout << "You entered a total of " << n_word << " words." << endl;
return 0;
}
9.改写8
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
unsigned int n_word = 0;
string input;
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> input)
{
if (input != "done")
{
n_word++;
}
else
{
break;
}
}
cout << "You entered a total of " << n_word << " words." << endl;
return 0;
}
10.嵌套程序
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
int row = 0;
cout<<"Enter number of rows: ";
cin>>row;
for(int i=1;i<=row;i++)
{
for(int j=i;j<row;j++)
{
cout<<".";
}
for(int j=0;j<i;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}