天天看點

大一上 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++

繼續閱讀