一
//计算输入的两个数字之间的和
#include <iostream>
int main(){
using namespace std;
int firstNum, secondNum,finalNum=0;
cout << "Inpute two numbers(separate with Spaces): ";
cin >> firstNum >> secondNum;
for (; firstNum <= secondNum; firstNum++)
finalNum += firstNum;
cout << "Result: "<<finalNum;
return 0;
}
二
//formore.cpp -- more looping with for
#include <iostream>
#include <array>
const int ArSize = 100;
int main(){
std::array<long double, ArSize>factorials;
factorials[1] = factorials[0] = 1;
for (int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i - 1];
for (int i = 0; i < ArSize; i++)
std::cout << i << "! = " << factorials[i] << std::endl;
return 0;
}
三
//输入累计和
#include <iostream>
int main(){
using namespace std;
float inputNum,cumNum=0;
do{
cout << "Input the number: ";
cin >> inputNum;
cumNum += inputNum;
cout << "Cumulative equals " << cumNum<<endl<<endl;
} while (inputNum != 0);
return 0;
}
2023/1/20更新:改正在第一次输入0时不退出程序的错误
//输入累计和
#include <iostream>
int main() {
using namespace std;
float inputNum, cumNum = 0;
cout << "Input the number: ";
cin >> inputNum;
while (inputNum != 0) {
cumNum += inputNum;
cout << "Cumulative equals " << cumNum << endl << endl;
cout << "Input the number: ";
cin >> inputNum;
}
return 0;
}
四
//投资比较
#include <iostream>
int main(){
float daphne, cleo;
daphne = cleo = 100;
for (int yearn = 1; cleo <= daphne; yearn++){
daphne += 10;
cleo +=cleo*0.05;
std::cout << "After " << yearn << " years, Cleo will have " << cleo << " dollas, " << "Daphne will have " << daphne << " dollas"<<std::endl;
}
return 0;
}
五
//图书一年销售情况
#include <iostream>
int main(){
using namespace std;
unsigned int books[16], yearbooks=0;
for (int i = 1; i < 13; i++){
cout <<i << "月: ";
cin >> books[i];
yearbooks += books[i];
}
cout << "今年图书的销售量为 " << yearbooks<<endl;
return 0;
}
2023/1/20更新:使用string对象数组存储月份
//图书一年销售情况
#include <iostream>
int main(){
using namespace std;
unsigned int books[16], yearbooks = 0;
string month[12] = {"January","February","March","Aprial","May","June","July","August","September","October","November","December"};
for (int i = 0; i < 12; i++){
cout << month[i] << ": ";
cin >> books[i];
yearbooks += books[i];
}
cout << "\nAnnual book sales " << yearbooks << endl;
return 0;
}
六
//图书三年销售量情况--二维数组
#include <iostream>
int main(){
using namespace std;
unsigned int books[12][3], yearbooksfirst ,yearbookssecond , yearbooksthird;
yearbooksfirst = yearbookssecond = yearbooksthird = 0;
for (int i = 1; i < 37; ){
int year,month;
for ( ;i < 13;i++){
year = 1;
month = i;
cout << "第" << year << "年" << month << "月: ";
cin >> books[month][year];
yearbooksfirst += books[month][year];
}
for ( ;i > 12 && i < 25; i++){
year = 2;
month = i - 12;
cout << "第" << year << "年" << month << "月: ";
cin >> books[month][year];
yearbookssecond += books[month][year];
}
for ( ;i > 24 && i < 37 ; i++){
year = 3;
month = i - 24;
cout << "第" << year << "年" << month << "月: ";
cin >> books[month][year];
yearbooksthird += books[month][year];
}
}
cout << "第1年图书的销售量为 " << yearbooksfirst << endl
<< "第2年图书的销售量为 " << yearbookssecond << endl
<< "第3年图书的销售量为 " << yearbooksthird << endl
<< "图书3年的总销售量为 " << yearbooksfirst + yearbookssecond + yearbooksthird << endl;
return 0;
}
2023/1/20更新:改正books数组存储错误
//5图书三年销售量情况--二维数组
#include <iostream>
int main(){
using namespace std;
unsigned int books[12][3], yearbooks[3] = { 0 },total=0;
int year = 0, month = 0;
for (int i = 0; i < 36;i++ ){//输入数据
if ( i < 12){
month = i;
cout << "第" << year+1<< "年" << month +1<< "月: ";
cin >> books[month][year];
yearbooks[year] += books[month][year];
}
else{
i / 12 == 0 ? month = 0:month = i % 12;
year = i / 12;
cout << "第" << year+1 << "年" << month+1 << "月: ";
cin >> books[month][year-1];
yearbooks[year] += books[month][year-1];
}
}
for (int i = 0; i < year+1; i++) {//输出数据
cout << "第" << i + 1 << "年图书销售量: " << yearbooks[i]<<endl;
total += yearbooks[i];
}
cout << "图书3年销售量: " << total;
return 0;
}
七
//汽车的生产商和生产年份
#include <iostream>
#include <string>
using namespace std;
struct car{
string makename;
unsigned int madeyear;
};
int main(){
unsigned int inputCars ;
cout << "How many cars do you wish to catalog? ";
cin >> inputCars;
cin.ignore();//忽略换行符
car* carCus = new car[inputCars];
for (int i = 1; i < inputCars+1; i++){
cout << "Car #" << i << ":" << endl
<< "Please enter the make: ";
getline(cin, carCus[i - 1].makename);
//cin.get(carCus[i - 1].makename, sizeof(carCus[i - 1].makename) + 1).get();
//上面这行代码不能运行,待解
cout << "Please enter the year made: ";
cin >> carCus[i - 1].madeyear;
cin.ignore();
}
for (;inputCars!=0; inputCars++){
cout << "Here is your collection: "<<endl;
break;
}
for (int i = 1; i < inputCars+1; i++)
cout << carCus[i - 1].madeyear << "\t" << carCus[i - 1].makename << endl;
delete[] carCus;
for (; inputCars == 0; inputCars++)
;
return 0;
}
2023/1/20更新:逻辑梳理
//汽车的生产商和生产年份
#include <iostream>
#include <string>
using namespace std;
struct car {
string makename;
unsigned int madeyear;
};
int main() {
unsigned int inputCars;
cout << "How many cars do you wish to catalog? ";
cin >> inputCars;
if (inputCars > 0) {
cin.ignore();//忽略换行符
car* carCus = new car[inputCars];
for (int i = 0; i < inputCars; i++) {//cin.get(carCus[i].makename, sizeof(carCus[i].makename) + 1).get();这行代码不能运行,待解
cout << "Car #" << i + 1 << ":\nPlease enter the make : ";
getline(cin, carCus[i].makename);
cout << "Please enter the year made: ";
cin >> carCus[i].madeyear;
cin.ignore();
}
cout << "Here is your collection: \n";
for (int i = 0; i < inputCars; i++)
cout << carCus[i].madeyear << "\t" << carCus[i].makename << endl;
delete[] carCus;
}
cout << "\nBye!";
return 0;
}
八
//记录输入的单词数--char数组
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
char inputChar[45];
int count=0;
cout << "Enter words(to stop, type the word done): "<<endl;
cin >> inputChar;
for (; strcmp(inputChar, "done");count++)
cin >> inputChar;
cout <<"You entered a total of " << count << " words";
return 0;
}
九
//记录输入的单词数--string对象
#include <iostream>
#include <string>
int main()
{
using namespace std;
string inputChar;
int count=0;
cout << "Enter words(to stop, type the word done): "<<endl;
cin >> inputChar;
for (; inputChar!="done"; count++)
cin >> inputChar;
cout <<"You entered a total of " << count << " words";
return 0;
}
十
//输出*--二维数组
#include <iostream>
int main()
{
using namespace std;
unsigned short int rows;
cout << "Enter the number of rows: ";
cin >> rows;
char** stars = new char*[rows];//声明二维数组
for (int i = 0; i < rows; i++)
stars[i] = new char[rows];
for (int i = 0; i < rows; i++)//初始化二维数组
{
int m = 0;
for (; m < rows - i-1; m++)//m:.的个数,行数
stars[i][m] = '.';
for (; m < rows; m++)//m:*的个数,行数
stars[i][m] = '*';
m = 0;
}
for (int i = 0; i < rows; i++)//输出二维数组
{
int m = 0;
for (; m < rows; m++)
cout << stars[i][m];
cout << endl;
m = 0;
}
delete[]stars;
return 0;
}
2023/1/20更新:实质是循环输出两个符号
//输出.和*
#include <iostream>
int main(){
using namespace std;
unsigned short int rows;
cout << "Enter the number of rows: ";
cin >> rows;
char period = '.', asterisk = '*';
for (int i = 1; i < rows+1; i++) {//输出几行
for (int a = 0; a < rows - i; a++)//输出句点
cout << period;
for (int b = 0; b < i; b++)//输出星号
cout << asterisk;
cout << endl;
}
return 0;
}