C++入门基础第二篇:printf语句与判断结构

第二章 printf语句与判断结构

printf的输出格式

#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	printf("hello world!");
	return 0;
}
  1. 使用printf时最好引用cstdio

Int、float、double、char等类型的输出格式:

#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	int a = 3;
	float b = 3.123456789;
	double c = 3.123456789;
	char d = 'y';
	printf("%d %f %lf %c", a, b, c, d);
	return 0;
}
  1. 所有输出的变量均可包含在一个字符串中

实践

输入一个整数,表示时间,单位是秒。输出一个字符串,用”时:分:秒”的形式表示这个时间。

#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	int hours = 0, mins = 0;
	double seconds=-1;
	
	while(1)
	{
		cout << "请输入一共多少秒";
		cin >> seconds;
		if (seconds == -1 or seconds<0)
		{
			printf("未成功输入");
			exit(-1);
		}
		else
		{
			//hours = int(seconds / 3600);
			//seconds = seconds - 3600 * hours;
			//mins = int(seconds / 60);
			//seconds = seconds - 60 * mins;
			printf("%d时%d分%lf秒\n", hours, mins, seconds);
		}
	}	
	return 0;
}

  1. a%b,a和b需要是整数
  2. 可以用while循环实现一直读取的操作,但是cin要在while里面哦
  3. 这里不够简洁,记住先取余再除以:
  • int hours=t/3600;
  • int minutes=t%3600/60;
  • int seconds=t%60;

扩展功能

Float, double等输出保留若干位小数时用:%.4f, %3lf。
最小数字宽度:%8.3f中的8,代表宽度为8,不足8空格补齐前面空缺。
%-8.3f代表补缺的空格从后面开始,输出内容在前面。
%08.3f,将原来的补空格换成补0。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	float a = 3.123456789;
	double b = 3.123456789;
	printf("%8.4f",a);
	printf("%8.5lf", b);
	return 0;
}

if语句

基础知识

基本if-else语句

#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	int a;
	cin >> a;
	if (a > 5)
	{
		printf("%d is big!\n", a);
		printf("%d + 1 = %d\n", a, a + 1);
	}
	else
	{
		printf("%d is small!\n", a);
		printf("%d - 1= %d\n", a, a - 1);
	}
	return 0;
}
  1. 注意如果if或者else后的语句超过一句要用{};
  2. 条件比较简单可以不要else语句
  3. if-else可以嵌套
IF语句例题
输入一个整数,输出这个数的绝对值
#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	float num=0;
	cout << "请输入数据:";
	cin >> num;
	cout << "该数的绝对值是:";
	if (num > 0)
		cout << num;
	else
		cout << -num;
	return 0;
}
输入两个整数,输出两个数中较大的那个
#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	float a=0,b=0;
	cout << "请输入要比较数据:";
	cin >> a>>b;
	if (a > b)
		cout << "较大的数据是:"<<a;
	else
	{
		if (a < b)
			cout << "较大的数据是:" << b;
		else
			cout << "两者一样大";
	}
	return 0;
}

输入三个整数,输出三个数中最大的那个
#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	float a=0,b=0,c=0;
	cout << "请输入要比较数据:";
	cin >> a>>b>>c;
	if (a > b && a>c)
	{
		cout << "最大的数据是:" << a;
		return 0;
	}
	if (a < b && c<b)
	{
		cout << "最大的数据是:" << b;
		return 0;
	}
	if (a < c && b < c)
	{
		cout << "最大的数据是:" << c;
		return 0;
	}
	cout << "三者一样大";
	return 0;
}

常用的比较运算符
#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	int a, b;
	cin >> a >> b;
	if (a > b) printf("%d>%d\n", a, b);
	if (a < b) printf("%d<%d\n", a, b);
	if (a >= b) printf("%d>=%d\n", a, b);
	if (a <= b) printf("%d<=%d\n", a, b);
	if (a == b) printf("%d==%d\n", a, b);
	if (a != b) printf("%d!=%d\n", a, b);
	return 0;
}

if-else连写

输入一个0到100之间的分数,如果大于等于85,输出A;如果大于等于70并且小于85,输出B;如果大于等于60并且小于70,输出C;如果小于60,输出 D;

#include<iostream>
#include<cstdio>
using namespace std;
int main() 
{
	float grade;
	cout << "输入成绩是多少:";
	cin >> grade;
	if (grade < 0 or grade>100)
	{
		printf("成绩错误!");
		return 0;
	}
	if (grade >= 85)
		printf("A");
	else
	{
		if (grade >= 70)
			printf("B");
		else
		{
			if (grade >= 60)
				printf("C");
			else printf("D");
		}
	}

	return 0;
}

练习
1. 简单计算器输入两个数,以及一个运算符+, -,*, /,输出这两个数运算后的结果。当运算符是/,且除数是0时,输出”Divided by zero!”; 当输入的字符不是+, -, *, /时,输出”Invalid operator!”。
#include<iostream>
#include<string>
using namespace std;
//看ASCII码
int main()
{
	char str[10]; int length=0;
	cin >> str;
	length = strlen(str);
	for (int i = 0; i <= length - 1; i++)
	{
		cout << int(str[i]) << " ";
	}
	return 0;
}
  1. 字符串读入可以直接cin
#include<iostream>
#include<string>
using namespace std;
void a(int a1[][2])
{
	a1[1][1] = 10;
}
int main()
{
	int i = 0,j=0;
	int a1[2][2] ;
	for (i = 0; i < 2; i++)
		for (j = 0; j < 2; j++)
			a1[i][j] = 1;
	a(a1);
	cout << a1[1][1];
	return 0;
}
  1. 二维数组可以说直接传的地址
#include<iostream>
#include<cstdio>
#define Maxsize 20
#define MaxCoutlength 20
using namespace std;
//做一个简单的加减乘除运算器

//栈定义
typedef  struct
{
	float num[Maxsize];
	int top;
}SqStack_num;


//初始化栈
void InitStack(SqStack_num& S)
{
	S.top = -1;
}


//新元素入栈
bool Push(SqStack_num& S, float x)
{
	if (S.top == Maxsize - 1)
		return false;
	else
		S.num[++S.top] = x;
	return true;
}


//新元素出栈
bool Pop(SqStack_num& S, float & x)
{
	if (S.top == -1)
		return false;
	else
		x = S.num[S.top--];
	return true;
}

bool emptySqStack(SqStack_num S)
{
	if (S.top == -1)
		return true;
	else return false;
}
//访问栈顶元素
bool gettop(SqStack_num S,float &x)
{
	if (S.top == -1)
		return false;
	else
		x=S.num[S.top];
	return true;
}

//括号匹配
bool brackeCheck(char str[], int length, SqStack_num S)
{
	int i = 0;float x = 0;
	for (i = 0; i <= length - 1; i++)
	{
		if (str[i] == '(')
			Push(S, int(str[i]));
		else
			if (str[i] == ')')
			{
				if (Pop(S, x))
				{
					if (str[i] == ')' && x != 40)
						return false;
				}
				else
					return false;

			}
	}
	return emptySqStack(S);
}

//将字符129转为数字129,特殊字符也转为ASCII

//转为后缀表达式
int PostfixExpression(SqStack_num S, char* str, int length, float str_new[][MaxCoutlength])
{
	int j = 0;
	int i = 0;
	InitStack(S);
	float ok=0;
	for (i = 0; i <= length - 1; )
	{
		int num = 0;//str表达式的某个字符放在这
		float sym = 0;//栈里弹出的运算符放在这
		num = int(str[i]);
		if (num <=57 && num >= 48)
		{
			while ((num <=57 && num >= 48)|| num==46)
			{   
				if(num!=46)
				{
					str_new[0][j] = str_new[0][j] * 10 + num - 48;
					num = int(str[++i]);
				}
				else
				{
					i++;
					num = int(str[i]);
					int xiao = 1;
					while (num <= 57 && num >= 48)
					{
						float xiaoshu = 1;
						int q = 1;
						for (q = 1; q <= xiao; q++)
						{
							xiaoshu = 0.1 * xiaoshu;
						}
						xiao++;
						str_new[0][j] = str_new[0][j]  + (num - 48)*xiaoshu;
						num = int(str[++i]);
					}
				}
			}
			j++;
		}
		else
		{ 
			if (num == 40)
			{
				Push(S, num);
				i++;
				continue;
			}
			if (num == 41)
			{
				while (Pop(S, sym) && sym !=40)
				{
						str_new[0][j] = sym;
						str_new[1][j++] = 1;  //代表这个地方是运算符,0代表是数字
				}
				i++;
				continue;
			}
			if (emptySqStack(S))
			{
				Push(S, int(str[i]));
				i++;
				continue;
			}
			if (num == 42 || num == 47)
			{
				do 
				{
					gettop(S, sym);
					if (sym == 42 || sym == 47)
					{
						Pop(S, sym);
						str_new[0][j] = sym;
						str_new[1][j++] = 1;
					}
				} while (sym == 42 || sym == 47);
				Push(S, num);
				i++;
				continue;
				
			}
			if (num == 43 || num == 45)
			{
				do
				{
					gettop(S, sym);
					if (sym == 42 || sym == 47)
					{
						Pop(S, sym);
						str_new[0][j] = sym;
						str_new[1][j++] = 1;
					}
				} while (sym != 40 );
				Push(S, num);
				i++;
				continue;
			}
		}
	}
	while (Pop(S, ok))
	{
		str_new[0][j] = ok;
		str_new[1][j++] = 1;
	}
	return j;
}


//利用后缀表达式计算
bool count( SqStack_num S,float str_new[][MaxCoutlength], int j,float &result)
{
	InitStack(S);
	int i = 0; int num = 0;
	float a = 0, b = 0;
	for (i = 0; i <j; i++)
	{
		if (str_new[1][i] == 0)
			Push(S, str_new[0][i]);
		else
		{
			if (Pop(S, b) && Pop(S, a))
			{
				if (str_new[0][i] == 42)
				{
					Push(S, a * b);
					continue;
				}
				if (str_new[0][i] == 47)
				{
					if(b != 0)
					{ 
						Push(S, float(a / b));
						continue;
					}
					else
					{
						cout << "除数不能为0"<<endl;
						return false;
					}
				}
				
				if (str_new[0][i] == 43)
				{
					Push(S, a + b);
					continue;
				}
				if (str_new[0][i] == 45)
				{
					Push(S, a - b);
					continue;
				}

			}
			else
				cout << "出错了"<<endl;
			
		}
	}
	Pop(S, result);
	return true;
}

int main()
{
	//初始化一个顺序栈
	SqStack_num S;
	InitStack(S);
	char str[MaxCoutlength]; 
	int length = 0;
	
	while (1)
	{
		float str_new[2][MaxCoutlength] = { 0 };
		cout << "请输入计算表达式:";
		cin >> str;
		if (str[0] == '#')
			return 0;
		length = 0;
		length = strlen(str);
		if (length == 0)  //成功输入检测
		{
			cout << "输入错误"<<endl;
			continue;
		}
		if (brackeCheck(str, length, S)) //括号检测
		{
			int j=PostfixExpression(S, str, length, str_new);
			float result=0;
			if(count(S, str_new, j,result))
				cout << "最终结果是:" << result<<endl;
			//检测没有错误后计算
		}
		else
		{
			cout << "输入错误"<<endl;
			continue;
		}
	}
	return 0;
}

  1. 栈要要始终注意进栈出栈的顺序哦,后进先出
  2. 这里是字符串中缀表达式,先变数组存,后面继续用栈的功能实现中缀换后缀,再用栈计算;
  3. 注意循环的时候,循环条件和循环执行的次数要写对
2. 判断闰年。闰年有两种情况:能被100整除时,必须能被400整除;不能被100整除时,被4整除即可。输入一个年份,如果是闰年输出yes,否则输出no。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int years;
	cout << "请输入年份:";
	cin >> years;
	if (years % 100 == 0)
		if (years % 400 == 0)
			cout << "是闰年";
		else
			cout << "不是闰年";
	else
		if (years % 4 == 0)
			cout << "是闰年";
		else
			cout << "不是闰年";
	return 0;
}
  1. 如果写成上面,脑子有点问题了
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int years;
	cout << "请输入年份:";
	cin >> years;
	if ((years % 400 == 0 && years % 100 == 0) || years % 4 == 0)
		cout << "是闰年";
	return 0;
}
ASCII对照表

C++关于字符的输入操作

c++字符(串)输入

条件表达式

与:&&
或:||
非!
(a>b)?a:b

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	if (a >= b && a >= c) cout << a << endl;
	else if (b >= a && b >= c)cout << b << endl;
	else cout << c << endl;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值