第3章 程序设计初步

【例3.1】各行小数点对齐

#include <iostream> 
#include <iomanip> 
using namespace std; 
int main() 
{   
	double a=123.456,b=3.14159,c=-3214.67;   
	cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2); 
	cout<<setw(10)<<a<<endl;  
	cout<<setw(10)<<b<<endl; 
	cout<<setw(10)<<c<<endl;  
	return 0; 
} 

【运行结果】


===============================================================================================

【例3.2】输出单个字符

#include <iostream>  
using namespace std; 
int main() 
{
	char a,b,c;   
	a='B';b='O';c='Y';   
	putchar(a);putchar(b);putchar(c);putchar('\n');  
	putchar(66);putchar(79);putchar(89);putchar(10);  
	return 0; 
}

【运行结果】

===============================================================================================

【例3.3】 输入单个字符

#include <iostream> 
using namespace std; 
int main() 
{
	char c;   
	c=getchar(); putchar(c+32); putchar('\n');  
	return 0; 
}

【运行结果】

=================================================================================================

【例3.4 】用scanf和printf函数进行输入和输出

#include <iostream> 
using namespace std; 
int main()  
{
	int a; float b; char c;   
	scanf("%d %c %f",&a,&c,&b);  //注意在变量名前要加地址运算符&  
	printf("a=%d,b=%f,c=%c\n",a,b,c);  
	return 0; 
}

【运行结果】

===================================================================================================

【例3.5】求一元二次方程式ax2+bx+c=0的根。a,b,c的值在运行时由键盘输入,它们的值满足b2-4ac≥0。

#include<iostream> 
#include<cmath>//cmath为新标准,提倡使用新标准
using namespace std; 
int main()  
{
	float a,b,c,x1,x2;
	cin>>a>>b>>c;
	x1=(-b+sqrt(b*b-4*a*c))/(2*a);
	x2=(-b-sqrt(b*b-4*a*c))/(2*a);//【教材有误】教程上的输出结果有误
	cout<<"x1="<<x1<<endl;
	cout<<"x2="<<x2<<endl;
	return 0; 
}


【运行结果】

===================================================================================================

【例3.6】求三角形的面积。

#include <iostream>  
#include <cmath> 
#include <iomanip>  //使用I/O流控制符要包含头文件iomanip 
using namespace std; 
int main() 
{   
	double a,b,c;   
	cout<<"please enter a,b,c:";  
	cin>>a>>b>>c;   
	if (a+b>c && b+c>a && c+a>b)   
	{ 
		double s,area;
		s=(a+b+c)/2;  
		area=sqrt(s*(s-a)*(s-b)*(s-c));  
		cout<<setiosflags(ios::fixed)<<setprecision(4); //指定输出的数包含4位小 数  
		cout<<"area="<<area<<endl;   
	}  
	else cout<<"it is not a trilateral!"<<endl; 
	return 0; 
}

【运行结果】

==========================================================================================================

 【例3.7 】输入一个字符,判别它是否为大写字母,如果是,将它转换成小写字母;如果不是,不转换。然后输出最后得到的字符

#include<iostream>
using namespace std; 
int main() 
{    
	char ch;   
	cin>>ch;    
	ch=(ch>='A' && ch<='Z')?(ch+32):ch;  
	cout<<ch<<endl;   
	return 0;
 } 

【运行结果】

=====================================================================================================

 【例3.8】编写程序,判断某一年是否为闰年。

#include <iostream> 
using namespace std; 
int main() 
{ 
	int year;   
	bool leap;    
	cout<<"please enter year:";   
	cin>>year; 
	if (year%4==0) 
	{
		if(year%100==0)
		{
			if (year%400==0)
				leap=true;  
			else leap=false;
		}  
		else 
			leap=true;
	}
	else 
		leap=false; 
	if (leap)   
		cout<<year<<" is "; 
	else    cout<<year<<" is not ";
	cout<<" a leap year."<<endl; 
	return 0; 
} 


【运行结果】

=================================================================================================

    【例3.9】 运输公司对用户计算运费。路程(s)越远,每公里运费越低。标准如下:    s<250km 没有折扣  250≤s<500 2%折扣  500≤s<10005%折扣 1000≤s<20008%折扣 2000≤s<3000  10%折扣 3000≤s  15%折扣 

   设每公里每吨货物的基本运费为p(price的缩写),货物重为w(wright的缩写),距离为s,折扣为d(discount的缩写),则总运费f(freight的缩写)的计算公式为   f = p * w * s * (1 - d) 。

#include <iostream> 
using namespace std; 
int main() 
{
	int c,s;   
	float p,w,d,f;   
	cout<<"please enter p,w,s:";  
	cin>>p>>w>>s;  
	if(s>=3000) c=12;  
	else c=s/250;  
	switch (c)   
	{ 
	     case 0:d=0;break; 
		 case 1:d=2;break; 
		 case 2:  
		 case 3:d=5;break; 
		 case 4:
	     case 5: 
		 case 6:  
		 case 7:d=8;break; 
		 case 8: 
		 case 9: 
		 case 10:  
		 case 11:d=10;break; 
		 case 12:d=15;break;  
	}    
	f=p*w*s*(1-d/100.0);    
	cout<<"freight="<<f<<endl;   
	return 0; 
}


【运行结果】

=======================================================================================================

 【例3.10】 求1+2+3+…+100。

#include <iostream> 
using namespace std; 
int main()  
{
	int i=1,sum=0; 
	while (i<=100)  
	{ 
		sum=sum+i; 
		i++;  
	}  
	cout<<"sum="<<sum<<endl;
	return 0;//【教材有误】教材中少了这一行
} 

【运行结果】

==================================================================================================

 【例3.11】 用do-while语句求1+2+3+…+100。

#include <iostream> 
using namespace std; 
int main()  
{
	int i=1,sum=0;  
	do     
	{ 
		sum=sum+i;  
		i++;  
	}while (i<=100);   
	cout<<"sum="<<sum<<endl;  
	return 0; 
} 


【运行结果】

===================================================================================================

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值