-----------------------------------------------------------------
/* * helloworld_1.cpp * * Created on: 2014-05-13 * Author: xiejiaohui */ #includeusing namespace std; int main() { cout<<"同学们,你们好!"; system("PAUSE"); return 0; }
----------------------------------------------------------------
/* * helloworld.cpp * * Created on: 2014-05-13 * Author: xiejiaohui */ #includeusing namespace std; int main() { cout<<"同学们,你们好!"; system("PAUSE"); return 0; }
----------------------------------------------------------------
/* * helloworld2.cpp * * Created on: 2014-05-13 * Author: xiejiaohui */ #includeusing 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 */ #includeusing 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 */ #includeusing 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 */ #includeusing 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 */ #includeusing 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 */ #includeusing 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个数,然后按照从大到小的顺序输出 #includeusing 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 */ #includeusing 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多分支结构 */ #includeusing 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 */ #includeusing namespace std; int main() { for (int i = 0; i <= 50; i++) cout << i << " "; system("PAUSE"); }
----------------------------------------------------------------
/* * forTest2.cpp * * Created on: 2014-05-13 * Author: xiejiaohui */ #includeusing 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 */ #includeusing 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#include 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 */ #includeusing 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#include 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++语言程序设计》 首都经贸大学 李宁 副教授。(现在应该是正教授了)。