C++初學2

在 C++初學的基礎上做了部分修改,並且在Dev-C++中重新編譯。

-----------------------------------------------------------------

/*
* helloworld_1.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include<iostream>

using namespace std;

int main() {
cout<<"同學們,你們好!";
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* helloworld.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include<iostream>

using namespace std;

int main() {
cout<<"同學們,你們好!";
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* helloworld2.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include<iostream>

using namespace std;

void hello(char *s)
{
cout << s << ", 你們好!" << endl;
}

int main()
{
hello("同學們");
hello("老師們");
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* area.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include<iostream>

using namespace std;

#define PI 3.1416

double Area(double r)
{
return PI * r * r;
}

int main()
{
double radius, area;
cout << endl << "請輸入圓的半徑:";
cin >> radius;
area = Area(radius);
cout << endl << "圓的面積:" << area;
system("PAUSE");
}

----------------------------------------------------------------

/*
* maxValue.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include <iostream>

using namespace std;

int MaxValue(int a, int b, int c) {
if (a < b) a = b;
if (a < c) a = c;
return a;
}

int main() {
int x1 = 5, x2 = 30, x3 = 8;
cout << "三個數中最大的數爲:" << MaxValue(x1, x2, x3);
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* twoValue.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include <iostream>

using namespace std;

int main() {
int i, j;
cout << "請輸入2個整數:";
cin >> i >> j;
cout << "2個數中比較大的數是:";
if(i >= j)
cout << i << endl;
else
cout << j << endl;
system("PAUSE");
return 0;
}


----------------------------------------------------------------

/*
* threeValue.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include <iostream>

using namespace std;

int main() {
int i, j, k;
cout << "請輸入3個整數:" << endl;
cin >> i >> j >> k;
cout << "3個數中最大的是:";
if(i >= j) {
if(i >= k)
cout << i << endl;
else
cout << k << endl;
} else {
if(j >= k)
cout << j << endl;
else
cout << k << endl;
}
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* threeValue2.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include <iostream>

using namespace std;

int main() {
int i, j, k;
cout << "請輸入3個整數: ";
cin >> i >> j >> k;
cout << "3個整數中比較大的整數是:";
if(i < j) i = j;
if(i < k) i = k;
cout << i << endl;
system("PAUSE");
}


----------------------------------------------------------------

/*
* threeValueSort.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

// 輸入3個數,然後按照從大到小的順序輸出
#include <iostream>

using namespace std;

int main() {
int i, j, k, p;
cout << "請輸入3個整數:";
cin >> i >> j >> k;
if (i < j) {
p = i;
i = j;
j = p;
}
if (i < k) {
p = i;
i = k;
k = p;
}
if (j < k) {
p = j;
j = k;
k = p;
}
cout << endl << "三個整數從大到小的排序爲:";
cout << i << ' ' << j << ' ' << k;
system("PAUSE");
}


----------------------------------------------------------------

/*
* scoreLeve.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include <iostream>

using namespace std;

/**
* 輸入一個0~100分範圍內的成績,
* 顯示相應的等級
* 90~100 -優
* 80~89 -良
* 70~79 -中
* 60~69 -及格
* 60分以下 -不及格
*/

int main() {
float score;
cout << "請輸入成績:";
cin >> score;
if (score < 0 || score > 100)
cout << "成績輸入必須在0~100分之間";
else if (score < 60)
cout << "不及格" << endl;
else if (score < 70)
cout << "及格" << endl;
else if (score < 80)
cout << "中" << endl;
else if (score < 90)
cout << "良" << endl;
else
cout << "優" << endl;

system("PAUSE");
return 0;
}


----------------------------------------------------------------

/*
* scoreLeve2.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

/**
* 輸入一個0~100分範圍內的一個成績,顯示相應的登記,
* 但要求用switch多分支結構代替原來的if多分支結構
*/

#include <iostream>

using namespace std;

int main() {
float score;
cout << "請輸入0~100分之間的成績:";
cin >> score;
switch(int(score) / 10) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: cout << "不及格" << endl; break;
case 6: cout << "及格" << endl; break;
case 7: cout << "中" << endl; break;
case 8: cout << "良" << endl; break;
case 9:
case 10: cout << "優" << endl; break;
default: cout << "成績必須輸入在0~100分之間";
}
system("PAUSE");
return 0;
}


----------------------------------------------------------------

/*
* forTest.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include <iostream>

using namespace std;

int main() {
for (int i = 0; i <= 50; i++)
cout << i << " ";
system("PAUSE");
}

----------------------------------------------------------------

/*
* forTest2.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

#include <iostream>

using namespace std;

int main() {
double x, s = 0;
cout << "請輸入10個整數:";
for (int i = 0; i < 10; i++) {
cin >> x;
s += x;
}
cout << "合計:" << s;
system("PAUSE");
}

----------------------------------------------------------------

/*
* forTest3.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

/**

用for循環顯示一個帶 *號的6行的三角形

*
***
*****
*******
*********
***********

行號 空格數 *數
0 5 1
1 4 3
2 3 5
3 2 7
4 1 9
5 0 11
------------
i 5-i i+i+1

*/

#include <iostream>

using namespace std;

int main() {
for (int i = 0, j = 0; i < 6; i++) {
for (j = 0; j < 5 - i; j++)
cout << ' '; // 打印空格
for ( j = 0; j < i + i + 1; j++)
cout << '*'; // 打印 *
cout << endl;
}
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* whileTest.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

/*
求數列:
1/2, 3/4, 5/8, 7/16, 9/32......
的所有大於等於0.000001的數據項之和,
顯示輸出計算的結果。

方法一:利用通項公式
序號 分子 分母
1 2*1-1=1 2=2
2 2*2-1=3 2*2=4
3 2*3-1=5 2*2*2=8
4 2*4-1=7 2*2*2*2=16
... ... ...
i 2*i-1 2*2*2*...*2

*/

#include <iostream>
#include <math.h>

using namespace std;

int main() {
int i = 1;
double s = 0.0, s0;
while ((s0 = (i + i - 1) / pow(2, i)) >= 0.000001) {
s += s0;
i++;
}
cout << s;
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* whileTest2.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

/*
求數列:
1/2, 3/4, 5/8, 7/16, 9/32......
的所有大於等於0.000001的數據項之和,
顯示輸出計算的結果。

方法二:利用遞推公式
序號 分子 分母
1 1 2
2 1+2=3 2*2=4
3 3+2=5 2*4=8
4 5+2=7 2*8=16
... ... ...
i ni di
i+1 ni+2 2*di

*/

#include <iostream>

using namespace std;

int main() {
int n = 1, d = 2;
double s = 0.0, s0;
while ((s0 = double(n) / d) >= 0.000001) {
s += s0;
n += 2;
d += d;
}
cout << s;
system("PAUSE");
return 0;
}

----------------------------------------------------------------

/*
* whileTest3.cpp
*
* Created on: 2014-05-13

* Author: xiejiaohui
*/

/*

已知

sinx = x/1 - x*x*x/3*2*1 + x*x*x*x*x/5*4*3*2*1 - x*x*x*x*x*x*x/7*6*5*4*3*2*1 + ......

設計一個程序,輸入弧度 x, 通過累加所有絕對值大於等於0.000001

的項來計算 sinx 的近似值,顯示計算結果並對照調用標準函數sin的計算結果.

序號 分子 分母
1 x 1!
2 x*(-x*x)=-x*x*x 1!*2*3=3!
3 (-x*x*x)*(-x*x)=x*x*x*x*x 3!*4*5=5!
4 x*x*x*x*x*(-x*x)=-x*x*x*x*x*x*x 5!*6*7=7!
... ... ...
i n(i) d(i)
i+1 n(i)*(-x*x) d(i)*(2*i - 2)(2*i - 1)

*/

#include <iostream>
#include <math.h>

using namespace std;

int main() {
const double dPI = 2 * 3.1415926;
double x;
cout << "請輸入一個弧度值:";
cin >> x;
if (x > 0.0)
while (x > dPI) x -= dPI;
else
while (x < -dPI) x += dPI;

double i = 1.0, n = x, d = 1.0, s = 0, s0;

while (fabs(s0 = n / d) >= 0.000001) {
s += s0;
i++;
n *= (-x * x);
d *= (i + i - 2) * (i + i -1);
}

cout << "我的程序計算的值爲: " << s << endl;
cout << "sin()函數計算的值爲:" << sin(x);
system("PAUSE");
return 0;
}

----------------------------------------------------------------

說明:以上例子來源於中央廣播電視大學視頻
《c++語言程序設計》 首都經貿大學 李寧 副教授。(現在應該是正教授了)。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章