Day4 循环和关系表达式

Q1.for循环在每轮循环之前,都将计算表达式的值。

通常,cout在现实bool值之前将他们转化为int,但是cout.setf(ios::boolalpha)函数调用设置了一个标记,该标记命令cout显示布尔值。

Q2.善用size显示数组大小。
Q3.比较前缀格式和后缀格式的效率。

在一般情况下,前缀行数将值加一,然后返回结果;但后缀版本首先复制一个副本,将其加一,然后将副本返回,因此前缀的效率高。

Q4.比较字符串。

C++中可以直接进行比较,对于C风格的字符串,可以使用strcmp()来进行比较,该函数接受两个字符串地址作为参数。

Q5.区别while(先判定表达式,决定是或否执行循环)与do while(首先执行循环体,然后再判定表达式,决定是否执行循环)
Q6.基于范围的for循环(C++11)



课后练习

// exam5.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <string>

using namespace std;

int main()
{
	//e1 要求用户输入两个整数的程序.改程序将计算并输出这个两个整数之间(包括这两个整数)所有整数的和。
	int smallNum = 0; int bigNum = 0;
	cout << endl << "enter two munber : ";
	cin >> smallNum; cin >> bigNum;
	if (bigNum < smallNum)
	{
		int temp = smallNum;
		smallNum = bigNum;
		bigNum = temp;
	}
	int countNum = 0;
	for (int i = smallNum; i <= bigNum; i++)
	{
		countNum = countNum + smallNum;
		smallNum = smallNum++;
	}
	cout << endl << "count = :" << countNum;

	//e2 编写一个要求用户输入数字的程序。每次输入之后,程序都将报告目前位置,所输入的累计和。当用户输入0时,程序结束。
	int num = 0; int countNumber = 0;
	while (true)
	{
		cout  << endl << "enter a munber:";
			cin >> num;
			if (num == 0)
			{
				cout  << endl << "exit";
				break;
			}
			countNumber = countNumber + num;
			cout << endl << "now,the count is : " << countNumber;
	}

	//e3 假设要销售书籍,请编写一个程序,输入全年中每个月的销售数量。程序通过循环,使用初始化为月份字符串的数组逐月进行提示,并将输入的数据存储在一个int数组中
	//然后程序计算数组中个元素的总数,并报告这一年的销售情况。
	string month[12] = { "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	int sale[12]; int countSaleNum = 0;
	for (int i = 0; i < 12; i++)
	{
		cout << endl << "enter the "<< month[i] << " salenum:";
		cin >> sale[i];
		countSaleNum = countSaleNum + sale[i];
	}
	cout << endl << "the count is : " << countSaleNum;

	//e4 设计一个名为car的结构,用他存储下述有关汽车的信息:生产商string,生产年份int。编写一个程序,向用户询问有多少辆汽车,随后程序使用new开创建一个有相应数量的car
	//结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商和年份信息。
	
	//e5 编写一个程序,他使用一个char数组和循环来每次读取一个单词,直到用户输入done为止,随后,改程序指出用户
	//输入了多少个单词(不包括done在内)

	cout << "enter word (to stop,type the word done)";
	char *pt_word = new char[20];
	cin >> pt_word;
	char done[20] = "done";
	while (pt_word != done)
	{
		cout << endl << pt_word;
		cin >> pt_word;
	}
	cout << endl << "done!!!";
	

	//e6 使用string完成上一题
	cout << "enter word (to stop,type the word done)";
	string word; int countword = 0;
		cin >> word;
		while (word != "done")
		{
			countword++;
			cin >> word;
			//countword++;
		}
		cout << "you entered a total of "<< countword <<"words";

	//e7 编写一个使用循环嵌套的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应的行数的星号,其中第一行一个第二行两个
	//以此类推,在星号不够的情况下,在星号前面加上句点。
	int t = 0;
	cout << endl << " enter number of rows: ";
	cin >> t;
	for (int i = 1;i <= t;i++)
	{
		for (int j = 0;j < t - i;j++)
		{
			cout << ".";
		}
		for (int k = 0; k < i; k++)
		{
			cout << "*";
		}
		cout << endl;
	}
	

 



	cin.get();
	cin.get();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值