1、
代碼
#include<iostream>
int main()
{using namespace std;int num1;int num2;int total=0;cout << "請輸入開始數字\n";cin >> num1;cout << "請輸入結束數字\n";cin >> num2;for (num1; num1<=num2; num1++)total = num1 + total;cout << num1 << " 和 " << num2 << "之間的整數和為 " << total <<endl;return 0;
}
執行結果
2、
代碼
#include<iostream>
int main()
{using namespace std;double total = 0.0;double in;cout << "請輸入數字:";cin >> in;while (in != 0){total += in;cout << "全部輸入數的和為:" << total << "\n";cout << "請輸入下一個數字:";cin >> in;}cout << "執行結束";return 0;
}
執行結果
3、
代碼
#include<iostream>
int main()
{using namespace std;double daphne=100;double cleo=100;int year=1;while (daphne >= cleo){daphne += 100*0.1;cleo += cleo*0.05;cout << "第" << year << "年,daphne投資價值為 " << daphne << "cleo投資價值為 " << cleo <<endl;year++;}cout << "第 " << year << "年,時cleo超過daphne的投資價值"<< endl;return 0;
}
執行結果
4、
代碼
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{using namespace std;int sales[12];int total;for (int i=1; i<=12; i++){cout << "請輸入" << months[i-1] << "銷售數量:";cin >> sales[i-1];}for (int j=0; j<12; j++){total = total+sales[j];}cout << "總銷售為:" << total <<endl;return 0;
}
執行結果
5、
代碼
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{using namespace std;int sales[3][12];int total[3] = {0}; //一定要初始化。不然初始值不為0int sum;for (int a=1;a<=3;a++){for (int i=1; i<=12; i++){cout << "請輸入第"<< a << "年" << months[i-1] << "銷售數量:";cin >> sales[a-1][i-1];}}for (int b=0; b<3; b++){for (int j=0; j<12; j++){total[b] = total[b]+sales[b][j];}sum = sum + total[b];cout << "第" << b+1 << "年總銷量為" << total[b] <<endl;}cout << "總銷售為:" << sum <<endl;return 0;
}
執行結果
6、
代碼
<pre name="code" class="cpp">//一定要加while (cin.get() != '\n');
#include<iostream>
using namespace std;
const int LEN = 60;
struct Car
{char brand[LEN];int year;
};
int main()
{int num;cout << "How many cars do you wish to catalog?";cin >> num;while (cin.get() != '\n');Car *ps = new Car[num];for (int i=0;i<num;i++){cout << "Car #" << (i+1) << ":\n";cout << "Please enter the make:";cin.getline(ps[i].brand, LEN);cout << "Please enter the year made:";cin >> ps[i].year;while(cin.get() != '\n');}cout << "Here is your collection:\n";for (int i=0; i<num; i++){cout << ps[i].year << " " << ps[i].brand <<endl;}delete [] ps;return 0;
}
執行結果
7、
代碼
#include<iostream>
#include<cstring>
const int STR_LEN=60;
int main()
{using namespace std;char words[STR_LEN];int count=0;cout << "Enter words(to stop,type the word done):\n";while (cin >> words && strcmp("done", words))++count;cout << "You entered a total of " << count << " words .\n" <<endl;return 0;
}
執行結果
8、
代碼
#include<iostream>
#include<string>
int main()
{using namespace std;string words;int count=0;cout << "Enter words {to stop,type the word done}:\n";while (cin >> words && words != "done")++count;cout << "You entered a total of " << count << " words .\n";return 0;
}
執行結果
9、
代碼
#include<iostream>
int main()
{using namespace std;int row;cout << "Enter number of row:";cin >> row;for (int i=0; i<row; i++){for (int j=row-i; j>1; j--){cout << ".";}for (int k=0; k<=i; k++){cout << "*";}cout << "\n";}return 0;
}
執行結果
版權聲明:本文博主原創文章。博客,未經同意不得轉載。