Primer_nine

first:

main.cpp

#include <iostream>
#include "golf.h"
using namespace std;
const int Mens=5;
int main()
{
    golf team[Mens];
    cout<<"Enter up to "<<Mens<<" golf team members:\n";
    int i;
    for(i=0;i<Mens;i++)
    {
        if(setgolf(team[i])==0)
            break;
    }
    for(int j=0;j<i;j++)
        showgolf(team[j]);
    setgolf(team[0], "Fred Norman", 5);
    showgolf(team[0]);
    handicap(team[0], 3);
    showgolf(team[0]);

    return 0;
}


golf.cpp

#include <iostream>
#include "golf.h"
#include <cstring>
int setgolf(golf &g)
{
    std::cout<<"Please enter golfer's full name:";
    std::cin.getline(g.fullname,Len);
    if(g.fullname[0]=='\0')
        return 0;
    std::cout<<"Please enter handicap for "<<g.fullname<<" :";
    while(!(std::cin>>g.handicap))
    {
        std::cin.clear();
        std::cout<<"Please enter an integer: ";
    }
    while(std::cin.get()!='\n')
        continue;
    return 1;
}
void setgolf(golf &g,const char *name,int hc)
{
    std::strcpy(g.fullname,name);
    g.handicap=hc;
}
void handicap(golf &g,int hc)
{
    g.handicap=hc;
}
void showgolf(const golf &g)
{
    std::cout << "Golfer:   " << g.fullname << "\n";
    std::cout << "Handicap: " << g.handicap << "\n\n";
}


golf.h

#include <iostream>
#include "golf.h"
#include <cstring>
int setgolf(golf &g)
{
    std::cout<<"Please enter golfer's full name:";
    std::cin.getline(g.fullname,Len);
    if(g.fullname[0]=='\0')
        return 0;
    std::cout<<"Please enter handicap for "<<g.fullname<<" :";
    while(!(std::cin>>g.handicap))
    {
        std::cin.clear();
        std::cout<<"Please enter an integer: ";
    }
    while(std::cin.get()!='\n')
        continue;
    return 1;
}
void setgolf(golf &g,const char *name,int hc)
{
    std::strcpy(g.fullname,name);
    g.handicap=hc;
}
void handicap(golf &g,int hc)
{
    g.handicap=hc;
}
void showgolf(const golf &g)
{
    std::cout << "Golfer:   " << g.fullname << "\n";
    std::cout << "Handicap: " << g.handicap << "\n\n";
}


second:

#include <iostream>
#include <string>
using namespace std;
void strcount(const string str);
int main()
{
    string input;
    cout<<"Enter a line: \n";
    getline(cin,input);
    while(input!="")
    {
        strcount(input);
        cout<<"Enter next line (empty line to quit): \n";
        getline(cin,input);
    }
    cout<<"Bye\n";
    return 0;
}
void strcount(const string str)
{
    static int total=0;
    int count=0;
    cout<<"\""<<str<<"\"contains";
    count=str.size();
    total+=count;
    cout<<count<<"characters\n";
    cout<<total<<"characters total\n";
}

three:

#include <iostream>
#include <cstring>
using namespace std;
struct charff
{
    char dross[20];
    int slag;
};
int main()
{
    charff *ps=new charff[2];
    strcpy(ps[0].dross,"what");
    strcpy(ps[1].dross,"why");
    ps[0].slag=1;
    ps[1].slag=2;
    for(int i=0;i<2;i++)
    {
        cout<<ps[i].dross<<"      "<<ps[i].slag<<endl;
    }
    return 0;
}

four:


 maiin.cpp

#include <iostream>
#include "SALES.h"
using namespace std;
using namespace SALES;
int main()
{
    double val[3]={2000,3000,5000};
    Sales one;
    setSales(one,val,3);
    showSales(one);


    Sales two;

    
    setSales(two);
    showSales(two);

    return 0;
}

SALES.cpp


#include <iostream>
#include "SALES.h"
namespace SALES
{
	void setSales(Sales &s,const double ar[],int n)
{

	for(int i=0;i<n;i++)
		s.sales[i]=ar[i];
    double sum=0;
    s.max=s.sales[0];
    s.min=s.sales[0];
	for(int i=0;i<n;i++)
	{
		sum+=ar[i];
		if(s.max<ar[i])
			s.max=ar[i];
		if(s.min>ar[i])
			s.min=ar[i];
	}
	s.average=sum/3;
	for(int i=n;i<QUARTERS;i++)
		s.sales[i]=0;
}
void setSales(Sales &s)
{
	for(int i=0;i<QUARTERS;i++)
		s.sales[i]=(i+1)*10;
	double sum=0;
	s.max=s.sales[0];
	s.min=s.sales[0];
	for(int i=0;i<QUARTERS;i++)
	{
		sum+=s.sales[i];
		if(s.max<s.sales[i])
			s.max=s.sales[i];
		if(s.min>s.sales[i])
			s.min=s.sales[i];
	}
	s.average=sum/QUARTERS;
}
void showSales(const Sales &s)
{
	std::cout<<"Sales:"<<std::endl;
	for(int i=0;i<QUARTERS;i++)
		std::cout<<s.sales[i]<<"     ";
	std::cout<<std::endl;
	std::cout<<"max= "<<s.max<<std::endl;
	std::cout<<"min= "<<s.min<<std::endl;
	std::cout<<"average= "<<s.average<<std::endl;
}
}

SALES.h


#ifndef SALES_H_INCLUDED
#define SALES_H_INCLUDED
namespace SALES
{
	const int QUARTERS = 4;
	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};
	//copis the lesser of 4 or n items from the array ar
	//tothe sales member of s and computes and stores the
	//average,maximum,and minimum values of the entered itmes;
	//remaining elements of sales, if any, set to 0
	void setSales(Sales &s,const double ar[],int n);

	//gathers sales for 4 quarters interactively, stores them
	//in hte sales menber of s and computes and stores the
	//average,maximum,and minumum values
	void setSales(Sales &s);

	//display all information
	void showSales(const Sales &s);
}


#endif // SALES_H_INCLUDED


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值