1.
#include <iostream>
int min()
{
using namespace std;
int x, y;
cout << "Enter two numbers: \n";
cin >>x;
cin >>y;
int sum = 0;
for(int i = x; i <= y; ++i)
sum += i;
cout << "The sum is " << sum << endl;
return 0;
}
2.
#include <iostream>
int main()
{
using namespace std;
double money_d = 100, money_c = 100;
int year = 0;
double total_d = 100, total_c = 100;
do
{
total_d = total_d + money_d *0.1;
total_c = money_c + money_c * 0.05;
money_c = total_c;
year++;
cout << year << " year: " << "d have " << total_d << ";c have " << total_c << endl;
}while (total_d >= total_c);
cout << year << " years" << endl;
return 0;
}
3.
#include <iostream>
int main()
{
using namespace std;
double money_d = 100, money_c = 100;
int year = 0;
double total_d = 100, total_c = 100;
do
{
total_d = total_d + money_d *0.1;
total_c = money_c + money_c * 0.05;
money_c = total_c;
year++;
cout << year << " year: " << "d have " << total_d << ";c have " << total_c << endl;
}while (total_d >= total_c);
cout << year << " years" << endl;
return 0;
}
4.
#include <iostream>
int main()
{
using namespace std;
char *month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
int number[12];
int total = 0;
for (int i = 0;i < 12; ++i)
{
cout << "Enter the number of " << month[i] << ": " << endl;
cin >> number[i];
total += number[i];
}
cout << "The total number is " << total << endl;
}
5.
#include <iostream>
int main()
{
using namespace std;
char *month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
char *year[3]= {"the first year" , "the next year", "the third year"};
int number[3][12];
int total = 0;
for (int i = 0; i < 3; ++i)
{
cout << "Enter the number of " << year[i] << ": " << endl;
for (int j = 0; j < 12; ++j) {
cout << month[j] << ": " << endl;
cin >> number[i][j];
total += number[i][j];
}
}
cout << "The total number is " << total << endl;
}
6.
#include <iostream>
#include <string>
using namespace std;
struct car{
string name;
int year;
};
int main()
{
cout<<"How many cars do you wish to catalog? ";
int num;
(cin>>num).get();
car* ps=new car[num];
for(int i=0;i<num;++i)
{
cout<<"Car #"<<i+1<<":\n";
cout<<"Please enter the make: ";
getline(cin,ps[i].name);
cout<<"Please enter the year made: ";
(cin>>ps[i].year).get();
}
cout<<"Here is your collection:\n";
for(int i=0;i<num;++i)
cout<<ps[i].year<<" "<<ps[i].name<<endl;
delete [] ps;
return 0;
}
这题是参考网上的代码,字符串的输入还是没怎么掌握。
7.
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout << "Enter words (to stop, type the word done):";
char *word;
cin >>word;
int sum = 0;
while (strcmp(word, "done"))
{
cin >> word;
sum++;
}
cout << "You enter a total of " << sum << " words" << endl;
return 0;
}
8.
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout << "Enter words (to stop, type the word done):";
string word;
cin >> word;
int sum = 0;
while (word != "done")
{
cin >> word;
sum++;
}
cout << "You enter a total of " << sum << " words" << endl;
return 0;
}
9.
#include <iostream>
int main()
{
using namespace std;
cout << "Enter number of rows: ";
int row;
cin >> row;
for (int i = 1; i <= row; ++i)
{
for (int j = 1; j <= 5-i; ++j)
cout << ".";
for (int j= 1; j <= i; ++j)
cout << "*";
cout << endl;
}
return 0;
}