分类目录归档:C++

C++编程练习

题目出自《C++ Primer Plus》第5章

假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况(我自定义了结构体数组一并完成第六题)。

#include <iostream>
#include <cstdio>
const int this_year = 2017;
const int years = 3;
const int per_year = 12;
struct record
{
        int  years;
        int months;
        long sales;
};
int main()
{
        using namespace std;
        record (*date)[per_year] = new record [years][per_year];
        for (int i = 0;i < years;++i)
                for (int j = 0;j < per_year; ++j)
                {
                        date[i][j].years=this_year+i;
                        date[i][j].months=j+1;
                        cout << "Please input sales for "
                                << date[i][j].years 
                                << " year "
                                << date[i][j].months
                                << " month:\n";
                        cin >> date[i][j].sales;
                }
        long long * sales_per_year = new long long [years];
        for (int i = 0;i < years; ++i)
        {
                for (int j = 0;j < per_year; ++j)
                        sales_per_year[i] += date[i][j].sales;
                cout << this_year + i  
                        << " year had been sale "
                        << sales_per_year[i] 
                        << " books." << endl;
        }       
        return 0;
}

编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:

Enter words (to sto p , type the word done) :</span>
anteater birthday category dumpster 
envy finagle geometry done for sure
You entered a total of 7 words.

您应在程序中包含头文件cstring,并使用函数StrCmp()来进行比较测试

#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
int main()
{
	char * ch = new char;
	char * word = new char [100];
	char * string = new char [100];
	int *pcw = new int ;
	int *pcc = new int ;
	*pcc=*pcw=0;
//确认前面的一个字符不为空
	bool empty=true;
	while ( strcmp(word,"done") )
	{
		*ch=getchar();
		if ( *ch == EOF || *ch == ' ' || *ch == '\n' )
		{	
			if ( empty )
			{
				continue;
			}
			strcpy(word,string);
			string[0]='\0';
			*pcc=0;
			if ( word[0] != ' ' || word[0] != '\n' || word[0] != EOF )
				++*pcw;
			empty=true;
			continue;
		}
		string[*pcc]=*ch;
		empty=false;
		string[(*pcc)+1]='\0';
		(*pcc)+=1;
	}
	cout << "You entered a total of " 
		<< (*pcw)-1 
		<< " words."
		<< endl;
	return 0;
}

题目出自《C++ Primer Plus》第6章
编写一个程序,它毎次读取一个单词,直到用户只输入q。然后, 该程序指出有多少个单词以元音打头,有多少个单词以辅咅打头,还有多少个单词不属于这两类。为此,力法之一睡是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:

Enter words (q to quit ) :
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words be ginning with vowels 
4 words be ginning with consonants
2 other

*需要判断每个字符的前面一个字符和后面一个字符

#include <iostream>
#include <cctype>
#include <cstdio>
using namespace std;
int main()
{
	cout << "Enter words (q to quit):" << endl;
	char ch;
	bool empty = true;
	int vowels,consonants,others;
	others=vowels=consonants=0;
	while ( ch = cin.get() )
	{
		cin.clear();
		if (empty)
		{
			if ( isalpha(ch) )
			{
				if ( ch == 'q' )
				{
					char temp;
					temp=cin.get();
					if (temp == '\n' || temp == ' ' || temp == EOF)
					{	
						empty = true;
						break;
					}
					else
					{	
						++consonants;
						empty = false;
					}
				}
				else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch =='u')
				{
						++vowels;
						empty = false;
				}
				else
				{
					++consonants;
					empty = false;
				}
			}
			else
			{
				++others;
				char temp;
				if ((temp = cin.get()) != '\n' || temp != ' ' || temp != EOF)
				{
					empty = false;
				}
				else
				{
					empty = true;
				}
			}
		}
		else if (ch == '\n' || ch == ' ' || ch == EOF)
			empty = true;
		else
		{
			empty = false;
			continue;
		}
	}
	cout << vowels << " words beginning with vowels" << endl;
	cout << consonants << " words begining with consonants" << endl;
	cout << others << " others" << endl;
	return 0;
}

题目出自《C++ Primer Plus》第8章
函数重载(多态),函数模板

#include <iostream>
template <typename T> void ShowArray(T arr[],int n);
template <typename T> void ShowArray(T * arr[],int n);
int SumArray(int * ,int n);
double SumArray(double * *,int n);
struct debts
{
	char name[50];
	double amount;
};
int main()
{
	using namespace std;
	int things[6] = { 13,31,103,301,310,130 };
	struct debts mr_E[3] = 
	{
		{ "Ima Wolfa", 2400.0 },
		{ "Ura Foxe", 1300.0 },
		{ "Iby Stout", 1800.0 }
	};
	double * pd[3];
	for (int i = 0; i < 3; i++)
		pd[i] = &mr_E[i].amount;
	cout << "Listing Mr. E's counts of things:\n";
	ShowArray(things,6);
	cout << "Listing Mr. E's debts:\n";
	ShowArray(pd,3);
	cout << SumArray(things,6) << endl;
	cout << SumArray(pd,3) << endl;
	return 0;
}
template <typename T> void ShowArray(T arr [], int n)
{
	using namespace std;
	cout << "template A\n";
	for(int i = 0;i < n;i++)
		cout << arr[i] << ' ';
	cout << endl;
	return ;
}
template <typename T> void ShowArray(T * arr [] ,int n)
{
	using namespace std;
	cout << "template B\n";
	for(int i = 0;i < n;i++)
		cout << *arr[i] << ' ';
	cout << endl;
	return ;
}
int SumArray(int * arr, int n)
{
	int result = 0;
	for (int i = 0;i < n;i++)
		result +=  arr[i];
	return result;
}
double SumArray(double * * arr,int n)
{
	double result = 0;
	for(int i = 0;i < n;i++)
		result += *arr[i];
	return result;
}

陆续更新