#include<iostream>
int main(void)
{
using namespace std;
int min, max;
cout << "please input your frist number: ";
cin >> min;
cout << "please input your next number: ";
cin >> max;
int sum = 0;
for (int i = min; i <= max; i++)
{
sum += i;
}
cout << "the sum of " << min << " + ... +" << max << " is ";
cout << sum << endl;
return 0;
}
2.
#include<iostream>
#include<array>
using namespace std;
const int A = 101;
int main(void)
{
array<long double, A>ferry;
ferry[0] = ferry[1] = 1;
for (int i =2; i <A; i++)
{
ferry[i] = i * ferry[i-1];
}
for (int i = 0; i < A; i++)
{
cout <<i<< "!= " << ferry[i]<< endl;
}
return 0;
}
3.
#include<iostream>
using namespace std;
int main(void)
{
double temp, sum = 0;
do {
cout << "Input a numeral to add:";
cin >> temp;
sum += temp;
} while (temp != 0);
cout << "Iput end.\n" << "The sum" << sum << endl;
return 0;
}
4.
#include<iostream>
using namespace std;
int main(void)
{
const int money = 100;
double Daphne_m = money;
double Cleo_m = 100;
int year = 0;
while (Daphne_m >= Cleo_m)
{
Daphne_m += money * 0.1;
Cleo_m += 0.05 * Cleo_m;
year++;
}
cout << "throught " << year << "Cleo's money is more than Daphne's money" << endl;
cout << "Cleo's money is " << Cleo_m << "Daphne's money is " << Daphne_m << endl;
return 0;
}
5.
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
const string Month[] = { "Jan","feb","mar","Apr","May","jun","jul","Aug","Sept","Oct","Nov",
"Dec" };
int sale_num[12];
int sum = 0;
for (int i = 0; i < 12; i++)
{
cout << Month[i] << ": ";
cin >> sale_num[i];
sum += sale_num[i];
}
cout << sum;
return 0;
}
6.
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
const string month[] = { "Jan","Feb","Mar","Apr","Mar","Jun","jul",
"Aug","Sep","Oct","Dec","Nov" };
int sale_num[3][12];
int year = 0;
double sum = 0;
for (int i = 0; i < 3; i++)
{
cout << "Strating " << i + 1 << " year date." << endl;
for (int j = 0; j < 12; j++)
{
cout << month[j] << " : ";
cin>> sale_num[i][j];
}
cout << "Input Done!"<<endl;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 12; j++)
sum += sale_num[i][j];
cout << "the sum is " << sum;
}
cout << "the sum is " << sum;
return 0;
}
7.
#include<iostream>
#include<string>
using namespace std;
struct car
{
string manufacturer;
int dates;
};
int main(void)
{
int car_number;
car* pcar;//car类型的指针
cout << "How many cars do you wish to catalog? ";
cin >> car_number;
cin.get();//读取缓冲区中的空格
pcar = new car[car_number];//使用NEW来创建一个由相应数量的car结构著称的动态数组
for (int i = 0; i < car_number; i++)
{
cout << "Car #" << i + 1 << ":" << endl;
cout << "Please enter the make :";
getline(cin, pcar[i].manufacturer);//string的读取用getline
cout << "Please enter the made: ";
cin >> pcar[i].dates;//此时缓冲区中有数字且空格,空格不读入在缓冲区中
cin.get();//将缓冲区中的空格读入
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < car_number; i++)
{
cout << pcar[i].dates << " " << endl;
cout << pcar[i].manufacturer << endl;
}
delete [] pcar;
return 0;
}
8.
#include<iostream>
#include<cstring>
using namespace std;
const char Done[] = done;
int main(void)
{
int counter = 0;
char words[20];
cout << "enter words (to stop,type the words down)";
do
{
cin >> words;
cin.get();
counter++;
} while (strcmp(words, Done) != 0);
return 0;
}
9.
#include<iostream>
#include<string>
const char Done[] = "done";
using namespace std;
int main(void)
{
string words;
int counter = 0;
cout << "Enter words (to stop,type the word done):" << endl;
do
{
cin >> words;
cin.get();
counter++;
} while (words != Done);
return 0;
}
10.
#include<iostream>
using namespace std;
int main(void)
{
int max;
cout << "Please input a number as a starting ";
cin >> max;
for (int i = 0; i < max; i++)
{
for (int j=0;j<max-i-1;j++)
cout << ".";
for (int x = 0; x <=i; x++)
cout << "*";
cout<<endl;
}
return 0;
}
|