文件尾条件需要整理
第五章循环和关系表达式
1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里先假设先输入较小的整数。
#include<iostream>
using namespace std;
int main() {
int begin;
int end;
int sum = 0;
cin >> begin;
cin >> end;
for (int i = begin; i <= end; i++) {
sum += i;
}
cout << sum;
return 0;
}
2.
#include<iostream>
#include<array>
using namespace std;
const int ArSize = 16;
int main() {
array<long long, ArSize> factorials;
factorials[1] = factorials[0] = 1LL;
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<iostream>
#include<array>
using namespace std;
int main() {
int x;
int sum = 0;
while (cin >> x) {
if (x == 0)
break;
sum += x;
cout << sum << endl;;
}
return 0;
}
4.
#include<iostream>
#include<array>
using namespace std;
int main() {
float x,y;
float sum1 = 100;
float sum2 = 100;
int year = 1;
while (sum2<=sum1) {
sum1 += 10;
sum2 += sum2 * 0.05;
year++;
}
cout << year << endl;
cout << sum1 << endl;
cout << sum2 << endl;
return 0;
}
5.
#include<iostream>
using namespace std;
int main() {
const string Month[] = { "1","2","3","4","5","6","7","8","9","10","11","12" };
int sell[12];
int sum = 0;
for (int i = 0; i < 12; i++) {
cin >> sell[i];
}
for (int i = 0; i < 12; i++)
{
cout << Month[i] << ":" << sell[i] << endl;
sum += sell[i];
}
cout << sum;
return 0;
}
6.
#include<iostream>
using namespace std;
int main() {
const string Month[] = { "1","2","3","4","5","6","7","8","9","10","11","12" };
int sell[3][12];
int sum = 0;
for (int i = 0; i < 3; i++) {
for(int j = 0;j<12;j++)
cin >> sell[i][j];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 12; j++) {
cout << "第"<<i+1<<"年"<<Month[j] << "月:" << sell[i][j] << endl;
sum += sell[i][j];
}
}
cout <<"总量:" << sum;
return 0;
}
7.
#include<iostream>
#include<string>
using namespace std;
int main() {
int num;
struct car {
string corporation;
int year;
};
cout << "How many cars do you wish to catalog?" << endl;
cin >> num;
cin.get();
car* Car;
Car = new car[num];
for (int i = 0; i < num; i++) {
cout << "Please enter the make" << endl;
getline(cin, Car[i].corporation);
cout << "Please enter the year made" << endl;
cin >> Car[i].year;
cin.get();
}
cout << "Here is your collection" << endl;
for (int i = 0; i < num; i++) {
cout << Car[i].year << " " << Car[i].corporation;
}
return 0;
}
Cin.get()
能在程序结束时输入一个字符,让程序停留在运行界面
8.
#include<iostream>
#include<string>
using namespace std;
int main() {
int c = 0;
char words[20];
cout << "Enter words(to stop,type the word done):" << endl;
while (strcmp("done", words) != 0)
{
c++;
cin >> words;
cin.get();
}
cout << "You entered a total of " << c-1 << " words." << endl;
return 0;
}
9.
#include<iostream>
#include<string>
using namespace std;
int main() {
int c = 0;
string words;
cout << "Enter words(to stop,type the word done):" << endl;
while ("done"!= words!= 0)
{
c++;
cin >> words;
cin.get();
}
cout << "You entered a total of " << c-1 << " words." << endl;
return 0;
}
10.
#include<iostream>
#include<string>
using namespace std;
int main() {
int row;
cout << "Enter number of rows: " << endl;
cin >> row;
for (int i = 0; i < row; i++) {
for (int j = 0; j < row - i - 1; j++) {
cout << ".";
}
for (int j = 0; j <= i; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}