C++基础语法复习

#include<iostream>
#include <iomanip.h>
using namespace std;
int main()
{
	char a, b;
	a = 'A';
	b = 65;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	
	char c1, c2, c3, c4;
	char n1, n2;
	c1 = 'a';
	c2 = 97;
	c3 = '\x61';
	c4 = 0141;
	cout << "c1=" << c1 << '\t' << "c2=" << c2 << endl;
	cout << "c3=" << c3 << '\t' << "c4=" << c4 << endl;
	n1 = '\n';
	n2 = '\t';
	cout << "c1=" << c1 << n2 << "c2=" << c2 << n1;
	cout << "c3=" << c3 << n2 << "c4=" << c4 << n1;
	//
	/*!算术关系逻辑赋值逗号(优先级)
	判断是否为闰年*/
	int year;
	cin >> year;
	if ((year % 400 == 0)||(year % 4 == 0 && year % 100 != 0))
	{
		cout << "yes" << endl;
	}
	cout << "no" << endl;

	///
    //逗号表达式   ++i  i++
	int a;
	a = (3 + 4, 5 * 6, 2 + 1);
	cout << a << endl;
	(a = 3 * 5, a * 4), a + 5;
	cout << a << endl;

	int i = 3;
	int j = 0;
	cout << (j++) + i << endl;
	///
	int i, j, k, l;
	cin >> hex >> i;  //十六进制
	cin >> oct >> j;  //八进制
	cin >> k;
	cin >> dec >> l;  //十进制
	
	//占位输出  头文件#include <iomanip.h>
	int i = j = 0;
	cout << setw(6) << i << setw(10) << j << endl;
	/
	float x=3.14,y=100;
     cout.setf(ios::scientific,ios::floatfield);
     //表明浮点数用科学表示法输出 
     cout << x<<’\t’;
     cout <<y<<endl; 
     ///
     //冒泡排序
    int a[11] = {9,8,7,6,5,4,3,2,1,0};
	int i, j, t;
	for (int i = 1; i < 9; i++)
	{
		for (int j = 1; j < 10 - i; j++)
		{
			if (a[j]>a[j+1])
			{
				t = a[j + 1];
				a[j + 1] = a[j];
				a[j] = t;
			}
		}
	}
	for (int i = 1; i < 11; i++)
	{
		cout << a[i];
	}
	//
	//综合条件运算符  ++问题  逻辑运算  运算优先级
	int   x=10,  y=9;
	int   a,b,c;
	a=(--x= =y++)?--x:++y;
	b=x++;
	c=y;
	//
	//有3个整数a,b,c,由键盘输入,输出其中最大的数
	//三个数的简单逻辑排序
	int a[3];
	for (int i = 0; i < 3; i++)
	{
		cin >> a[i];
	}
	for (int i = 0; i < 3; i++)
	{
		cout << a[i] << endl;
	}
	if (a[0]>a[1])
	{
		if (a[0] > a[2])
			cout << "max=" << a[0];
	}
	else 
	{
		if (a[1]>a[2])
		{
			cout << "max=" << a[1];
		}
		else
		{
			cout << "max=" << a[2];
		}
	}
	
	//用do while求一个和
	int  i=1,sum=0;   //定义变量,初始化
     do                 //构造循环
     {     sum=sum+i;   // 循环体,多次执行
             i=i+1;
     }while (i<=100);
    cout<<“sum=<<sum<<endl;
	//for实现
	int  i, sum;
    for (i=1, sum=0; i<=100; i++)
          sum=sum+i;
     cout<<“sum=<<sum<<endl;
     
     //for循环的嵌套上面有个冒泡排序
     
     //最大公约数
     int m = 8,n=5;
	if (m>n)
	{
		int a;
		a = m % n;
		if (a=0)
		{
			cout << "最大公约数=" << n<<endl;
		}
		else
		{
			while (a=m%n)
			{
				m = n;
				n = a;
			}
			cout<< "最大公约数=" << n << endl;
		}
	}
	/
	//最小公倍数 (待实现)
	
	//判断素数
	int t;
	cin >> t;
	int i;
	for (i = 2; i < t/2; i++)
	{
		if (t%i==0)
		{
			break;
		}
	}
	if (i >= t / 2)
	{
		cout << "yes" << endl;
	}
	else cout << "no" << endl;
	//范围求素数
	int t,k,i;
	for (t = 50, k = 0; t <= 100; t++)
	{
		for (i = 2; i < t; i++)
			if (t%i == 0)
				break;
		if (i == t)
		{
			cout << t << "	";
				k++;
			if (k % 5 == 0)  cout << endl;
		}
	}

//计算:2+22+222+.....+2222222=?
int s = 0;
	int t = 2;
	for (int i = 1; i <=6; i++)
	{
		t = t * 10 + 2;
		s = s + t;
	}
	cout << s;

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值