C++语言程序设计(第四版)郑莉 第2章课后习题代码

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;


const float PI=3.1416;
void hw2_4(){
	float a=PI;
	cout<<a;
} 

enum Color{WHITE,BLACK=100,RED,BLUE,GREEN=300,YELLOW};
void hw2_5(){
	int a=BLUE;
	cout<<a<<endl;
	int b=YELLOW;
	cout<<b;
	
}

//a++和++a的区别
void hw2_9(){
	int a,b,c;
	a=30;
	cout<<"a="<<a<<endl;
	b=a++;//a++赋值后自加
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;
	c=++a;//++a自加后赋值
	cout<<"a="<<a<<endl;
	cout<<"c="<<c<<endl;
}

void hw2_10(){//给多个变量赋初值
	int k=0;
	for(int i=0,j=10;i<=j;i++,j--){
		k=i*j;
		cout<<i<<"*"<<j<<"="<<k<<" ";
	}
	for(int i=0,j=10;((i<j)||(i=j));i++,j--){
		k=i*j;
		cout<<i<<"*"<<j<<"="<<k<<" ";
	}
}

void hw2_12(){
	int n=100;
	for(n=100;n<=200;n+=2);
	cout<<n<<endl;
	
	n=100;
	do{
		n+=2;
	}while(n<=200);
	cout<<n<<endl;
	
	n=100;
	while(n<=200){
		n+=2;
	}
	cout<<n<<endl;
}

void hw2_16(){
	int n;
	cout<<"请输入一个数字:"<<endl;
	cin>>n;
	cout<<"您输入的数字是:"<<endl;
	cout<<n;
}

void hw2_17(){
    cout<<"int在内存中所占字节数"<<sizeof(int)<<endl;
    cout<<"signed int在内存中所占字节数"<<sizeof(signed int)<<endl;
    cout<<"unsigned int在内存中所占字节数"<<sizeof(unsigned int)<<endl;
    cout<<"short int在内存中所占字节数"<<sizeof(short int)<<endl;
    cout<<"long int在内存中所占字节数"<<sizeof(long int)<<endl;
    cout<<"long在内存中所占字节数"<<sizeof(long)<<endl;
    cout<<"float在内存中所占字节数"<<sizeof(float)<<endl;
    cout<<"char在内存中所占字节数"<<sizeof(char)<<endl;
    cout<<"double在内存中所占字节数"<<sizeof(double)<<endl;
    cout<<"long double在内存中所占字节数"<<sizeof(long double)<<endl;
}

//输出ASCⅡ码为32~127的字符
void hw2_18(){
	for(int i=32;i<=127;i++){
		cout<<(char)i<<" ";
	}
}

void hw2_19(){
	unsigned int x;
	unsigned int y=100;
	unsigned int z=50;
	x=y-z;
	cout<<"y-z="<<x<<endl;
	x=z-y;
	cout<<"z-y="<<x;
}

void hw2_20(){
	int myAge=39;
	int yourAge=39;
	cout<<"I am "<<myAge<<" years old."<<endl;
	cout<<"You are "<<yourAge<<" years old."<<endl;
	myAge++;
	++yourAge;
	cout<<"One year passes…"<<endl;
	cout<<"I am "<<myAge<<" years old."<<endl;
	cout<<"You are "<<yourAge<<" years old."<<endl;
	cout<<"Another year passes…"<<endl;
	cout<<"I am "<<myAge++<<" years old."<<endl;
	cout<<"You are "<<++yourAge<<" years old."<<endl;
	cout<<"Let's print it again."<<endl;
	cout<<"I am "<<myAge<<" years old."<<endl;
	cout<<"You are "<<yourAge<<" years old."<<endl;
}

void hw2_26(){
	char flag;
	while(1){
		cout<<"现在正在下雨吗?(Y/N)"<<endl;
		cin>>flag;
		if(toupper(flag)=='Y'){//toupper()库函数把小写字母转换为大写字母
			cout<<"现在正在下雨。"<<endl;
			break;
		}else if(toupper(flag)=='N'){
			cout<<"现在没有下雨。"<<endl;
			break;
		}else{
			cout<<"请输入Y/N。"<<endl;
		}
	}

}

void hw2_27(){
	int score,i;
	cout<<"你考试考了多少分?(0~100)"<<endl;
	cin>>score;
	if(score>100||score<0){
		cout<<"分数值必须在0~100之间!";
	}else{
		i=score/10;
		switch(i){
			case 10:
			case 9:
				cout<<"你的成绩为优!";
				break;
			case 8:
				cout<<"你的成绩为良!";
				break;
			case 7:
			case 6:
				cout<<"你的成绩为中!";
				break;
			default:
				cout<<"你的成绩为差!";
		}
	}
}

void hw2_28_1(){
	char choice,c;
	while(1){
		cout<<"Menu:A(dd) D(lete) S(ort) Q(uit),Select one:";
		cin>>c;
		choice=toupper(c);
		if(choice=='A'){
			cout<<"数据已经增加。"<<endl;
			continue;
		}else if(choice=='D'){
			cout<<"数据已经删除。"<<endl;
			continue;
		}else if(choice=='S'){
			cout<<"数据已经排序。"<<endl;
			continue;
		}else if(choice=='Q'){
			break;
		}
	}
}

void hw2_28_2(){
	char choice,c;
	while(1){
		cout<<"Menu:A(dd) D(lete) S(ort) Q(uit),Select one:";
		cin>>c;
		choice=toupper(c);
		switch(choice){
			case 'A':
				cout<<"数据已经增加。"<<endl;
				break;
			case 'D':
				cout<<"数据已经删除。"<<endl;
				break;
			case 'S':
				cout<<"数据已经排序。"<<endl;
				break;
			case 'Q':
				exit(0);
				break;
			default:
				;
		}
	}
}
void hw2_29_while(){
	int i,j,k,flag;
	i=2;
	while(i<=100){
		flag=1;
		k=sqrt(i);//限制除数的范围
		j=2;
		while(j<=k){//穷举法判断质数
			if(i%j==0){
				flag=0;
				break;
			}
			j++;
		}
		if(flag){
			cout<<i<<"是质数。"<<endl;
		}
		i++;//判断下一个数
	}
}

void hw2_29_dowhile(){
	int i,j,k,flag;
	i=2;
	do{
		flag=1;
		k=sqrt(i);//限制除数的范围
		j=2;
		do{//穷举法判断质数
			if(i%j==0){
				flag=0;
				break;
			}
			j++;
		}while(j<=k);
		if(flag){
			cout<<i<<"是质数。"<<endl;
		}
		i++;//判断下一个数
	}while(i<=100);
}

void hw2_29_for(){
	int i,j,k,flag;
	for(i=2;i<=100;i++){
		flag=1;
		k=sqrt(i);//限制除数的范围
		for(j=2;j<=k;j++){
			if(i%j==0){
				flag=0;
				break;
			}		
		}
		if(flag){
			cout<<i<<"是质数。"<<endl;
		}
	}	
}


void hw2_32_while(){
	int n=18;
	int m=0;
	while(m!=n){
		cout<<"请猜这个数的值为多少?(0-100)";
		cin>>m;
		if(n>m){
			cout<<"你猜的值太小啦!"<<endl;
		}else if(n<m){
			cout<<"你猜的值太大啦!"<<endl;
		}else{
			cout<<"你猜对啦!"<<endl;
		}
	}
}

void hw2_32_dowhile(){
	int n=18;
	int m=0;
	do{
		cout<<"请猜这个数的值为多少?(0-100)";
		cin>>m;
		if(n>m){
			cout<<"你猜的值太小啦!"<<endl;
		}else if(n<m){
			cout<<"你猜的值太大啦!"<<endl;
		}else{
			cout<<"你猜对啦!"<<endl;
		}
	}while(m!=n);
}

enum Weekday{
	SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY
};
void hw2_33(){
	int i;//声明整型变量
	Weekday d=THURSDAY;//对其赋值
	cout<<"d="<<d<<endl;
	i=d;//对整型变量赋Weekday类型的值
	cout<<"i="<<i<<endl;
	d=(Weekday)6;
	cout<<"d="<<d<<endl;
	d=(Weekday)4;
	cout<<"d="<<d<<endl;
}


void hw2_34(){
	enum color{red,yellow,blue,white,black};
	enum color pri;
	int n,loop,i,j,k;
	n=0;
	for(i=red;i<=black;i++){//第一个球
		for(j=red;j<=black;j++){//第二个球
			if(i!=j){//前两个球不同
				for(k=red;k<=black;k++){//第三个球
					if((k!=i)&&(k!=j)){//第三个球不同于前两个
						n=n+1;//取法加一
						cout.width(4);
						cout<<n;//次数输出
						for(loop=1;loop<=3;loop++){//输出结果循环三次三个球
							switch(loop){
								case 1:pri=(enum color)i;break;//第一个球的颜色
								case 2:pri=(enum color)j;break;//第二个球的颜色
								case 3:pri=(enum color)k;break;//第三个球的颜色
								default:break;
							}
							switch(pri){//输出球的颜色
								case red:cout<<"      red";break;
								case yellow:cout<<"      yellow";break;
								case blue:cout<<"      blue";break;
								case white:cout<<"      white";break;
								case black:cout<<"      black";break;
								default:break;
							}
						}
						cout<<endl;
					}
				}
			}
		}
	}
	cout<<"total:"<<n<<endl;
}

void hw2_35(){
	int i,j;
	cout<<' ';
	for(i=1;i<10;i++){
		cout<<setw(4)<<i;
	}
	cout<<endl;
	for(i=1;i<10;i++){
		cout<<i;
		for(j=1;j<10;j++){
			cout<<setw(4)<<(i*j);
		}
		cout<<endl;
	}
}
int main(){
	hw2_35();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值