`

c++初学

    博客分类:
  • c++
阅读更多
/*
 * helloworld.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include<iostream>

int main() {
	std::cout<<"同学们,你们好!";
	return 0;
}


/*
 * helloworld2.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include<iostream.h>

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

int main()
{
	hello("同学们");
	hello("老师们");
	return 0;
}


/*
 * area.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include<iostream.h>

#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;
}


/*
 * maxValue.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include <iostream.h>

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 = 3, x3 = 8;
	cout << MaxValue(x1, x2, x3);
	return 0;
}


/*
 * twoValue.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include <iostream.h>

int main()
{
	int i, j;
	cout << "请输入两个整数:";
	cin >> i >> j;
	cout << "2个数中比较大的数是:";
	if (i >= j)
		cout << i << endl;
	else
		cout << j << endl;
}


/*
 * threeValue.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include <iostream.h>

int main()
{
	int i, j, k;
	cout << "请输入3 个整数: ";
	cin >> i >> j >> k;
	cout << "个数中最大的是:";
	if (i >= j) {
		if (i >= k)
			cout << i << endl;
		else
			cout << k << endl;
	} else {
		if (j >= k)
			cout << j << endl;
		else
			cout << k << endl;
	}
}


/*
 * threeValue2.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include <iostream.h>

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;
}


/*
 * threeValueSort.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include <iostream.h>

// 输入3个数,然后按照从大到小的顺序输出
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 << i << ' ' << j << ' ' << k;
	cin.get();
	cin.get();
}


/*
 * scoreLeve.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include <iostream.h>
/**
 * 输入一个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;

	return 0;
}


/*
 * scoreLeve2.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

/**
 * 输入一个0~100分范围内的一个成绩,显示相应的登记,
 * 但要求用switch多分支结构代替原来的if多分支结构
 */

#include <iostream.h>

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分之间";
	}

}


/*
 * forTest.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include<iostream.h>

int main()
{
	for (int i = 0; i < 5; i++)
		cout << i << " ";
}


/*
 * forTest2.cpp
 *
 *  Created on: 2013-7-28
 *      Author: xiejiaohui
 */

#include<iostream.h>

int main() {
	double x, s = 0;
	cout << "请输入10个数:";
	for (int i = 0; i < 10; i++) {
		cin >> x;
		s += x;
	}
	cout << "合计:" << s;
}


/*
 * forTest3.cpp
 *
 *  Created on: 2013-7-28
 *      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.h>

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;
	}
}


/*
 * whileTest.cpp
 *
 *  Created on: 2013-7-28
 *      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.h>
#include<math.h>

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;
}


/*
 * whileTest2.cpp
 *
 *  Created on: 2013-7-28
 *      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.h>

int main()
{
	int n = 1, d = 2;
	double s = 0.0, s0;
	while ((s0 = double(n) / d) >= 0.000001) {
		s += s0;
		n += 2; // n(i+1) = n(i)+2
		d += d; // d(i+1) = 2*d(i)
	}
	cout << s;
}


/*
 * whileTest3.cpp
 *
 *  Created on: 2013-7-28
 *      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.h>
#include<math.h>

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 << sin(x);

}


说明:以上例子来源于中央广播电视大学视频
《c++语言程序设计》 首都经贸大学 李宁 副教授。

以上程序在 eclipse for c/c++ 中编译通过。

2013-07-28
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics