天天看点

大一上 c + +上机实验总结(五)

大一上 c + +上机实验总结目录:返回目录

1、输入任一个整数,判别它是否能被3整除;若能被3整除,输出YES;不能被3整除,输出NO

【解答】

#include<iostream>
using namespace std;
int main()
{
int integer;
cout<<"请输入任意一个整数:"<<endl;
cin>>integer;
if(integer%3==0)
  cout<<"YES"<<endl;
else 
  cout<<"NO"<<endl;
return 0;
}
           

2、对输入的字符进行大小写转换,如果输入为大写字母则显示其对应的小写字母,如果输入为小写字母则显示其对应的大写字母。

【解答】

#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"请输入任意一个字符:"<<endl;
cin>>ch;
if(ch>='A'&&ch<='Z')
  ch=ch+32;
else
  if(ch>='a'&&ch<='z')
	  ch=ch-32;
cout<<ch<<endl;
return 0;
}
           

3、P65 同步练习2.1 程序练习的第3题

输入一个正整数,使用if语句,判断它的奇偶性。

【解答】

#include<iostream>
using namespace std;
int main()
{ 
	int a ;
	cout << "请输入一个正整数:" ;
	cin >> a ;
	if ( a%2 )
	     cout << "这是一个奇数!" << endl;
	else 
		cout << "这是一个偶数!" << endl ;
}
           

4、P35 二、程序设计的第1题

输入平面上某点横坐标x和纵坐标y,若该点在如图1.11所示的方块区域内,则输出true;否则,输出false。

【解答】

#include <iostream>
using namespace std;
int main()
{
	double x,y;
	bool b;
	cout << "please input x, y:\n";
	cin >> x >> y;
	b = ( -2<=x ) && ( x<=2 ) && ( -2<=y ) && ( y<=2 );
	if(b)
		cout<<"true"<<endl;
	else
		cout <<"false"<< endl;	
        return 0;
}
           

5、输入三个字符,求出其中值最小的字符。

#include <stdio.h>
int main()
{  char a,b,c,min;
   cout<<"输入三个字符:\n";
   cin>>a>>b>>c;
   if(a<b)
	   min=a;
   else
	   min=b;
   if(c<min)
	   min=c;
   cout<<"三个字符中最小的字符为:"<<min<<endl;
   return 0;}
           

6、求分段函数的值

【解答】

#include<iostream>
using namespace std;
int main()
{
float x,y;
cout<<"请输入x的值:"<<endl;
cin>>x;
if(x<1)
  y=x;
else
  if(x<10)
	  y=2*x-1;
  else
	  y=3*x-11;
cout<<"y="<<y<<endl;
return 0;
}
           
c++

继续阅读