IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 大一上 | 【C++】代码整理 -> 正文阅读

[C++知识库]大一上 | 【C++】代码整理

大一上 | 【C++】代码整理

仅记录一下大一上写下的“垃圾”代码

1. 二进制转十进制

#include<iostream>
using namespace std;
int main()
{
	int dec = 0;
	char ch;
	cout << "binary = ";
	do
	{
		// 从输入流中读取一个字符赋给 ch
		cin.get(ch);
	} while (ch != '0' && ch != '1');
	do
	{
		int x = ch - '0';
		dec += x;
		cin.get(ch);
		if (ch == '0' || ch == '1')
		{
			dec *= 2;
		}
	} while (ch == '0' || ch == '1');
	cout << "decimal = " << dec;
	return 0;
}

?

2. 十进制转二进制

#include<iostream>
using namespace std;
int main()
{
	int x, s[16] = { 0, };
	cout << "decimal = "; 
	cin >> x;
	for (int i = 15; i >= 0; i--)
	{
		s[i] = x % 2;
		x /= 2;
	}
	cout << "binary = ";
	for (int i = 0; i <= 15; i++)
	{
		cout << s[i];
		if ((i + 1) % 8 == 0)
		{
			cout << " ";
		}
	}
	return 0;
}

?

3. XYZ+YZZ=532的解

#include<iostream>
using namespace std;
int main()
{
	int X, Y, Z;
	cout << "XYZ+YZZ=532的解为" << endl;
	for (X = 0; X != 9; X++)
		for (Y = 0; Y != 9; Y++)
			for (Z = 0; Z != 9; Z++)
				if (532 == (Y + X) * 100 + (Y + Z) * 10 + Z * 2)
				{
					cout << "X=" << X << endl;
					cout << "Y=" << Y << endl;
					cout << "Z=" << Z << endl;
				}
	return 0;
}

?

4. 输出 2-20内的素数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	bool b;
	for (int x = 2; x <= 20; x++)
	{
		b = true;
		for (int j = 2; j <= sqrt(1.0 * x); j++)
		{
			if (x % j == 0)
			{
				b = false;
			}
		}
		if (b)
			cout << x << " ";
	}
	return 0;
}

?

5. 求圆周率Π (小数点后六位)

这里用到的公式是: π 4 ≈ 1 1 ? 1 3 + 1 5 ? 1 7 ? \frac{\pi}{4}\approx \frac{1}{1}-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}\cdots 4π?11??31?+51??71??

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	double old_sum = 0.0;
	double sum = 1.0, s = 1.0;
	int n = 1;
	while (abs(sum-old_sum)>1e-6)
	{
		old_sum = sum;
		s = s * (-1);
		n = n + 2;
		sum += s / n;
	}
	double pi = 4 * sum;
	printf("%f", pi);
	return 0;
}

?

6. 公鸡母鸡小鸡问题

公鸡5元一只,母鸡3元一只,小鸡1/3元一只,给100元,则公鸡,母鸡,小鸡各买多少只?

#include<iostream>
using namespace std;
int main()
{
	short int x, y, z;
	cout << "cock\t" << "hen\t" << "chick" << endl;
	for (x = 0; x < 20; x++)
	{
		for (y = 0; y < 33; y++)
		{
			z = 100 - x - y;
			if (5.0 * x + 3.0 * y + z / 3.0 == 100)
				cout << x << '\t' << y << '\t' << z << endl;
		}
	}
	return 0;
}

?

7. 10进制转8进制

#include<iostream>
using namespace std;
int main()
{
	long S;
	int n, m = 0, T[100];
	cout << "pleast importer your num" << endl;
	cout << "Decimal:";
	cin >> S;
	while (S != 0)
	{
		n = S % 8;
		S = S / 8;
		T[++m] = n;
	}
	int k = m;
	cout << "Octonary:";
	while (k >= 1)
	{
		cout << T[k];
		k--;
	}
	return 0;
}

?

8. 十进制转二进制

#include<iostream>
using namespace std;
int main()
{
	long S;
	int n, m=0, T[100];
	cout << "Please input your number" << endl;
	cout << "Decimal:";
	cin >> S; 
	while (S != 0)
	{
		n = S % 2;
		S = S / 2;
		T[++m] = n;
	}
	cout << "Binary:";
	for (int k = m; k>=1; k--)
		cout << T[k];
	return 0;
}

?

9. 等差数列

#include<iostream>
using namespace std;
int main()
{
	long an, S;
	int a1, d, n, m;
	cout << "首项:" << endl;
	cin >> a1;
	cout << "公差: " << endl;
	cin >> d;
	cout << "项数: " << endl;
	cin >> n;
	an = a1 + (n - 1) * d;
	cout << "所以an=" << an << endl;
	S = 0; m = 1;
	while (m <= n)
	{
		an = a1 + (m - 1) * d;
		S = S + an;
		m++;
	}
	cout << "前n项等于:" << S << endl;
	return 0;
}

?

10. 最大公约数

#include<iostream>
using namespace std;
int main()
{
	int m, n;
	cout << "input 2 numbers(the first number is the bigger one)" << endl;
	cout << "?	"; cin >> m;
	cout << "?	"; cin >> n;
	int temp = n;
	cout << m << " and " << n << " maximal common divisor is ";
	while (temp)
	{
		temp = m % n;
		m = n;
		n = temp;
	}
	cout << m;
	return 0;
}

?

11. 斐波那契数列

#include<iostream>
using namespace std;
int main()
{
	long long int a0 = 0, a1 = 1, a2;
	short int n;
	cout << "n = "; cin >> n;
	cout << a0 << '\t' << a1 << '\t';
	for (int i = 3; i <= n; i++)
	{
		a2 = a0 + a1;
		cout << a2 << '\t';
		a0 = a1;
		a1 = a2;
		if (i % 10 == 0) cout << endl;
	}
	return 0;
}

?

12. 打印圣诞树

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int n; char ch;
	cout << "How many lines ?\t"; cin >> n;
	cout << "What character ?\t"; cin >> ch;
	for (int i = 0; i < n; i++)
	{
		cout << setw((2 * n + 1) / 2 - i) << ch;
		for (int j = 1; j < (2 * i + 1); j++)
			cout << ch;
		cout << endl;
	}
	for (int k = 0; k < n / 2; k++)
		cout << setw(n - 1) << ch << ch << ch << endl;
	return 0;
}
// Sample Run
// How many lines ?        6
// What character ?        *
//      *
//     ***
//    *****
//   *******
//  *********
// ***********
//     ***
//     ***
//     ***

?

13. 求100内的素数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int m, k, i, n = 0;
	for (m = 2; m <= 100; m++)
	{
		k = int(sqrt(double(m)));
		i = 2;
		while (m % i && i <= k)
			i++;
		if (i > k)
		{
			cout << m << '\t';
			n++;
			if (n % 5 == 0) cout << endl;
		}
	}
	return 0;
}

?

14. 牛顿法迭代求根

/*
	f(x) = x^3 + x^2 + 10x - 20
	f'(x) = 3x^2 + 2x + 10
	牛顿迭代公式:
		x(n+1) = x(n) - f(xn) / f'(xn)
*/
#include<iostream>
#include<cmath>
using namespace std;
double f1(double x);
double f2(double x);
int main()
{
	double x, x0, epson;
	cout << "input test root, x0 = ";
	cin >> x0;
	cout << "input precision, epson = ";
	cin >> epson;
	do
	{
		x = x0;
		x0 = x - f1(x) / f2(x);
	} while (fabs(x0 - x)>epson);
	cout << "the root is:" << x0;
	return 0;
}

double f1(double x)
{
	double y = pow(x, 3) + pow(x, 2) + 10 * x - 20;
	return y;
}
double f2(double x)
{
	double y = 3 * pow(x, 2) + 2 * x + 10;
	return y;
}

?

15. 计算圆的周长和面积 / 球的面积和体积

#include<iostream>
using namespace std;

const double PI = 3.14159;
typedef double funType(double);
funType circlePerimeter, circleArea, ballArea, ballVolume;
double callFun(funType* qr, double r);

int main()
{
	double r;
	cout << "please input your number of radius: "; cin >> r;
	cout << endl;

	cout << "the circlePerimeter is " << callFun(circlePerimeter, r) << endl << endl;

	cout << "the circleArea is " << callFun(circleArea, r) << endl << endl;

	cout << "the ballArea is " << callFun(ballArea, r) << endl << endl;

	cout << "the ballVolume is " << callFun(ballVolume, r) << endl << endl;
	return 0;
}

double circlePerimeter(double r)
{
	return 2 * PI * r;
}

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

double ballArea(double r)
{
	return 4 * PI * r * r;
}

double ballVolume(double r)
{
	return 4.0 / 3.0 * PI * r * r * r;
}

double callFun(funType* qr, double r)
{
	return qr(r);
}

?

16. 链表的基本操作

  1. define.h
    #include<iostream>
    using namespace std;
    struct node
    {
    	int data;
    	node* next;
    };
    
    typedef struct node* List;
    
    void construct(List& L, int a[], int n);
    
    void print(List L);
    
    void destroy(List& L);
    
  2. func.cpp
    #include"define.h"
    
    void print(List L)
    {
    	struct node* p = L;
    	while (p)
    	{
    		cout << p->data << endl;
    		p = p->next;
    	}
    }
    
    void construct(List &L, int a[], int n)
    {
    	if (L || n < 1)
    		return;
    
    	struct node* p;
    	struct node* rear = L;
    
    	p = (struct node*)malloc(sizeof(struct node));
    	p->data = a[0];		
    	p->next = NULL;
    	L = p;
    	rear = L;
    
    	for (int i = 1; i < n; i++)
    	{
    		p = (struct node*)malloc(sizeof(struct node));
    		p->data = a[i];
    		p->next = NULL;
    		rear->next = p;
    		rear = rear->next;
    	}
    	//return true;
    }
    
    void destroy(List &L)
    {
    	struct node* p = L;
    	while (L)
    	{
    		L = L->next;
    		free(p);
    		p = L;
    	}
    }
    
  3. main.cpp
    #include"func.cpp"
    
    int main()
    {
    	int a[10], i;
    	for (i = 0; i < 10; i++)
    	{
    		a[i] = i;
    	}
    	List L = NULL;;
    	construct(L, a, 10);
    	print(L);
    	cout << "---------------------------------------" << endl;
    	destroy(L);
    	print(L);
    	return 0;
    }
    
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-07-04 22:37:15  更:2022-07-04 22:37:33 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 17:36:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码