C++程序设计 选择结构实验

#include <iostream>

using namespace std;
int main()
{
	double x,y;
	cout << "Input x value" <<endl;
	cin >> x;
	if(x<1){
		y=x;
	}else if(1<=x && x<10){
		y=2*x-1;
	}else if(x>=10){
		y=3*x-11;
	}
	cout << "y=" << y <<endl;
	 return 0;
}



2.
#include <iostream>

using namespace std;

int main()
{
	int a,b,c,d,t;
	cout << "输入四个整数" <<endl;
	cin >> a >> b >> c >> d;
		if(a>b){
			t=a;
			a=b;
			b=t;
		}if(a>c){
			t=a;
			a=c;
			c=t;
		}
		if(a>d){
			t=a;
			a=d;
			d=t;
		}
		if(b>c){
			t=b;
			b=c;
			c=t;
		}if(b>d){
			t=b;
			d=b;
			d=t;
		}
		if(c>d){
			t=c;
			c=d;
			d=t;
		}
		
			cout << a << "\t" << b << "\t" << c << "\t" << d;
	return 0;
}
修改后:将cout中的a b c d换成 d c b a即为从大到小顺序。
#include <iostream>

using namespace std;

int main()
{
	int a,b,c,d,t;
	cout << "输入四个整数" <<endl;
	cin >> a >> b >> c >> d;
		if(a>b){
			t=a;
			a=b;
			b=t;
		}if(a>c){
			t=a;
			a=c;
			c=t;
		}
		if(a>d){
			t=a;
			a=d;
			d=t;
		}
		if(b>c){
			t=b;
			b=c;
			c=t;
		}if(b>d){
			t=b;
			d=b;
			d=t;
		}
		if(c>d){
			t=c;
			c=d;
			d=t;
		}
		
			cout << d << "\t" << c << "\t" << b << "\t" << a;
	return 0;
}
3.
#include <iostream>

using namespace std;
int main()
{
	int grade,class1;
	cout << "input your grade" <<endl;
	cin >> grade;
	class1=grade/10;
	switch(class1){
		case 10:
		case 9:
			cout << "A" <<endl;
			break;
			
		case 8:
			cout << "B" <<endl;
			break;
			
		case 7:
			cout << "C" <<endl;
			break;		
		case 6:
			cout << "D" <<endl;
			break;
		case 5:
		case 4:
		case 3:
		case 2:
		case 1:
		case 0:
			cout << "E" <<endl;
			break;
			
		return 0;
	}
}








猜你喜欢

转载自blog.csdn.net/qq_62480054/article/details/131585893