CPP从头学起4:程序流程结构

程序结构:

CPP支持的三种程序结构:顺序结构,选择结构,循环结构。

1. 顺序结构:

顺序结构已经学过了,不在赘述。

2. 选择结构:

(1)if 语句:

a. 单行 if 语句:

就一行 if 代码,基础的条件满足就执行,不满足就结束。

如下例子所示:

#include <iostream>
using namespace std;
#include <string>//CPP风格字符串要包含这个头文件
int main() 
{
	//1. 用户输入高考分数
	int  score = 0;
	cin >> score;

	//打印用户输入的分数
	cout <<"输入的分数为:"<<score << endl;

	//判断是否过了600分,过了上985大学
	if(score>600)
	{
		cout << "恭喜考上985" << endl;
	}
	system("pause");
	return 0;
}

结果如下:

b. 多行 if 语句:

#include <iostream>
using namespace std;
#include <string>//CPP风格字符串要包含这个头文件
int main() 
{
	//1. 用户输入高考分数
	int  score = 0;
	cin >> score;

	//打印用户输入的分数
	cout <<"输入的分数为:"<<score << endl;

	//判断是否过了600分,过了上985大学,未过就是考不上985
	if(score>600)
	{
		cout << "恭喜考上985" << endl;
	}
	else 
	{
		cout << "未考上985" << endl;
	}
	system("pause");
	return 0;
}

结果如下:

c. 多条件 if 语句:

#include <string>//CPP风格字符串要包含这个头文件
int main() 
{
	//1. 用户输入高考分数
	int  score = 0;
	cin >> score;

	//打印用户输入的分数
	cout <<"输入的分数为:"<<score << endl;

	//判断是否过了600分,过了上985大学,未过就是考不上985
	if(score>600)
	{
		cout << "恭喜考上985" << endl;
	}
	else if(score>580)
	{
		cout << "考上211" << endl;
	}
	else
	{
		cout << "未考上985211" << endl;
	}
	system("pause");
	return 0;
}

这里有一个需要注意的点

else if (score>580) 这里,原本我写的是else if ( 600>score>580) ,得出的结果相同。

但是如果改成( 580<score<600 ),对应的结果则会出错,但是如果只写成(580<score)时候则正常。

因此,得出结论,else if这里不要多写!!!

d. 嵌套 if 语句:

#include <iostream>
using namespace std;
#include <string>//CPP风格字符串要包含这个头文件
int main() 
{
	//1. 用户输入高考分数
	int  score = 0;
	cin >> score;

	//打印用户输入的分数
	cout <<"输入的分数为:"<<score << endl;

	//判断是否过了600分,过了上985大学,未过就是考不上985
	if(score>600)
	{
		if (score > 690)
		{
			cout << "恭喜考上TNU" << endl;
		}
		else if (score>630)
		{
			cout << "恭喜考上NEU" << endl;
		}
		else
		{
			cout << "考上985" << endl;
		}
	}
	else if(580<score)
	{
		cout << "考上211" << endl;
	}
	else
	{
		cout << "未考上985211" << endl;
	}
	system("pause");
	return 0;
}

(2)三目运算符:

#include <iostream>
using namespace std;
#include <string>//CPP风格字符串要包含这个头文件
int main() 
{
	//三目运算符,将a和b进行比较,大的赋值给c
	int  a = 10;
	int  b = 20;
	int  c = 0;

	 c = (a > b ? a : b);
	
	cout <<c<< endl;

	system("pause");
	return 0;
}

结果如下:

注意:在CPP中,三目运算符返回的是变量,可以继续赋值

(a>b?a:b)=100;

(3)switch语句:

switch缺点:无法判断一个区间,只能判断是整型或字符型。

#include <iostream>
using namespace std;
#include <string>//CPP风格字符串要包含这个头文件
int main() 
{
	//给电影打分 5分经典,4分很好,3分一般,3以下烂片
	int  score = 0;
	cin >> score;
	switch (score)
	{
	case 5:
		cout << "经典电影" << endl;
		break;
	case 4:
		cout << "很好电影" << endl;
		break;
	case 3:
		cout << "一般电影" << endl;
		break;
	default:
		cout << "烂片" << endl;
        break;
	}

	system("pause");
	return 0;
}

结果如下:

注意:每个case 都要加入break,如果不加,就会把其下面的case 都执行一遍。

3.循环结构:

(1)while循环语句:

#include <iostream>
using namespace std;
#include <string>//CPP风格字符串要包含这个头文件
int main() 
{
	//在屏幕中打印0-9这十个数
	int  num = 0;
	while (num < 10) 
	{
		cout << num << endl;//先输出一个0
		num++;
     }

	system("pause");
	return 0;
}

结果如下:

练习:猜数字

CPP程序 #include "stdio.h" #include #include #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 struct pcb { /* 定义进程控制块PCB */ char name[10]; char state; int super; int ntime; int rtime; struct pcb* link; }*ready=NULL,*p; typedef struct pcb PCB; sort() /* 建立对进程进行优先级排列函数*/ { PCB *first, *second; int insert=0; if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/ { p->link=ready; ready=p; } else /* 进程比较优先级,插入适当的位置中*/ { first=ready; second=first->link; while(second!=NULL) { if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/ { /*插入到当前进程前面*/ p->link=second; first->link=p; second=NULL; insert=1; } else /* 插入进程优先数最低,则插入到队尾*/ { first=first->link; second=second->link; } } if(insert==0) first->link=p; } } input() /* 建立进程控制块函数*/ { int i,num; //clrscr(); /*清屏*/ printf("\n 请输入进程数?"); scanf("%d",&num); for(i=0;iname); printf("\n 输入进程优先数:"); scanf("%d",&p->super); printf("\n 输入进程运行时间:"); scanf("%d",&p->ntime); printf("\n"); p->rtime=0;p->state='w'; p->link=NULL; sort(); /* 调用sort函数*/ } } int space() { int l=0; PCB* pr=ready; while(pr!=NULL) { l++; pr=pr->link; } return(l); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { printf("\n qname \t state \t super \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ { PCB* pr; printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { disp(pr); pr=pr->link; } } destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ { printf("\n 进程 [%s] 已完成.\n",p->name); free(p); } running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ { (p->rtime)++; if(p->rtime==p->ntime) destroy(); /* 调用destroy函数*/ else { (p->super)--; p->state='w'; sort(); /*调用sort函数*/ } } main() /*主函数*/ { int len,h=0; char ch; input(); len=space(); while((len!=0)&&(ready!=NULL)) { ch=getchar(); h++; printf("\n The execute number:%d \n",h); p=ready; ready=p->link; p->link=NULL; p->state='R'; check(); running(); printf("\n 按任一键继续......"); ch=getchar(); } printf("\n\n 进程已经完成.\n"); ch=getchar(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值