C++ primer 5 -- 第一章习题答案(会持续更新)

本文提供了C++ Primer第五版第一章的部分习题答案,包括计算-100到100整数之和,以及涉及文件重定向、Sales_item类的输入输出等题目。
摘要由CSDN通过智能技术生成

C++ primer 5 -- 第一章习题答案

1.9

#include <stdio.h>
#include <iostream>
#include <math.h>
int main()
{

        int sum = 0, vec = 50while (vec <= 100  )
        {
             sum += vec;
             ++vec;
        }

        std::cout << sum << std::endl;
        return 0;
}

1.10

int main()
{
        
        int sum = 0, vec = 10;

        while (vec >= 0  )
        {

             std::cout << vec << std::endl;
             --vec;
        }
        return 0;
}

1.11

 int main()
{
      
        int vec1 = 0, vec2 = 0, vec= 0;
        std::cout << "please input two integer, eg 1, 4" << std::endl;

        std::cin >> vec1 >> vec2;
        while ( vec1  <= vec2 )
        {

             std::cout << vec1 << std::endl;
             ++vec1;
        }
        return 0;
}

1.12
将-100到100的所有整数相加, sum=0

1.13

int main()
{
        int sum = 0;
        for(int i = 50; i <= 100; i++)
        {
             sum += i;
        }
        std::cout << sum << std::endl;
        return 0;
}
int main()
{
        for(int i = 10; i >= 0; i--)
        {
           std::cout << i << std::endl;
        }
        return 0;
}
int main()
{
        int vec1 = 0, vec2 = 0;

        std::cout << "please input two integer" << std::endl;
        std:: cin >> vec1  >> vec2;
        for(int i = vec1; i <= vec2; i++)
        {
           std::cout << i << std::endl;
        }
        //std::cout << sum << std::endl;
        return 0;
}

1.17

#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
        int vec1 = 0, vec2 = 0, vec= 0;
        std::cout << "please input two integer, eg 1, 4" << std::endl;

        std::cin >> vec1 >> vec2;

        if( vec2 >= vec1)
        {
             while(vec1 <= vec2)
             {
                 cout << vec1 << std::endl;
                 ++vec1;
             }
        }
        else
        {
             while(vec2 <= vec1)
             {
                 cout << vec2 << endl;
                 ++vec2;
             }
        }
        return 0;
}

1.20

#include <stdio.h>
#include <iostream>
#include <math.h>
#include "Sales_item.h"

using namespace std;
int main()
{
    Sales_item  item1, item2;
	
	cin >> item1 >> item2;
    cout << item1 << item2 <<endl;
	return 0;
                                                                                                                                                                                       
}

最简单g++ main.cpp 编译即可,自定义输入文件<add_item>以及输出文件, 使用文件重定向:
$ ./a.out <add_item>myoutput

1.21

int main()
{
    Sales_item  item1, item2;
    
	cout <<"please input" << endl;
	cin >> item1 >> item2;
	if( item1.isbn() == item2.isbn() )
	{
		cout << item1 + item2 <<endl;
	}
    else
	{
        cerr <<"Data must refer to same ISBN" << endl;		
	}
	return 0;
                                                                                                                                                                                       
}

1.22

int main()
{
    Sales_item  item1, item2;
    
	cout <<"please input" << endl;

	 cin >> item1;
      while( cin >> item2)
	  {
			   if( item2.isbn() == item1.isbn() )
	           {
		            item1 += item2; 
	           }
               else
	           {
                     cerr <<"Data must refer to same ISBN or end " << endl;		
	           }
	  }
	  cout << item1 << endl;
	return 0;
                                                                                                                                                                                       
}

1.23
与eg.1.4.4相同, 只不过将输入整数类型转换为Sales_item 类型。

int main()
{
    Sales_item  item1, item2;
    
	cout <<"please input" << endl;

	 if( cin >> item1 )
	 {
		    int cnt = 1;
           while( cin >> item2)
	       {
			   if( item2.isbn() == item1.isbn() )
	           {
		            ++cnt; 
	           }
               else
	           {
                     cout  << item1 << "occurs" << cnt << " times " << endl;
					 item1 = item2;
					 cnt = 1;		
	           }
	       }  
	     cout  << item1 << "occurs" << cnt << " times " << endl;
	 }
	return 0;
                                                                                                                                                                                       
}

1.25

int main() 
{
    Sales_item total; // variable to hold data for the next transaction

    // read the first transaction and ensure that there are data to process
    if (std::cin >> total) {
		Sales_item trans; // variable to hold the running sum
        // read and process the remaining transactions
        while (std::cin >> trans) {
			// if we're still processing the same book
            if (total.isbn() == trans.isbn()) 
                total += trans; // update the running total 
            else {              
		        // print results for the previous book 
                std::cout << total << std::endl;  
                total = trans;  // total now refers to the next book
            }
		}
        std::cout << total << std::endl; // print the last transaction
    } else {
        // no input! warn the user
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值