C++ Primer Plus 自学第六章结尾编程9题

这篇文章包含了一系列C++编程练习,涉及字符串处理(如输入到特定字符为止,转换大小写),数值数组处理(计算平均值,比较数量),菜单驱动程序设计,结构体成员操作,以及文件读写与数据分类。程序涵盖了基本输入输出、条件判断、循环和文件I/O等核心概念。
摘要由CSDN通过智能技术生成

第一题

要求:输入字符串到@字符为止,回显除数字以外输入,将大小写字母相互转换。

//6.1
#include<iostream>
#include<cctype>
int main()
{
	using namespace std;
	cout << "Enter and type @ to terminate input.\n";
	char ch;
	while ((ch = cin.get()) != '@')
	{
		if (islower(ch))
			cout << (char)toupper(ch);
		else if (isupper(ch))
			cout << (char)tolower(ch);
		else if (!isdigit(ch))
			cout << ch;
		else;
	};
	return 0;
}

第二题

要求:将数值输入double数组,遇到非数字值停止输入,报告平均值,多少数字大于平均值。

//6.2
#include<iostream>
const int Max = 10;
int main()
{
	using namespace std;
	double donation[Max];
	int i, sum = 0, count = 0;
	for (i = 0; i<Max && cin >> donation[i]; i++)
		sum += donation[i];
	if (i == 0)
		cout << "No number.\n";
	else
	{
		cout << "Have " << i << " number and "
			<< "Sum is " << sum << endl;
		int average = sum / i;
		cout << "average is " << average<<endl;
		for (int m = 0; m <= i; m++)
			donation[m] > average ? count++ :count ;
		cout << count << " numbers > average.\n";
	}
}

第三题

要求:设计菜单选项,要求对应字母选择选项。

//6.3
#include<iostream>
using namespace std;
void menu(void);
int main()
{
	char ch;
	menu();
	while ((ch = cin.get()) != 'f')
	{
		
		switch (ch)
		{
		case 'c':
		case 'C':
			cout << "car"; 
			break;
		case 'p':
		case 'P':cout << "pianist"; 
			break;
		case 't':
		case 'T':cout << "tree"; 
			break;
		case 'g':
		case 'G':cout << "game";
			break;
		default:cout << "please enter again";
		}
		cout << endl;
		menu();
		cin.get();//吃掉\n;
	};
};

void menu()
{
	cout << "Please one of the following choices: \n"
		"c) carnivore\tp) pianist "
		"\nt)tree\tg) game"
		"\nf)quit " << endl;
}

第四题

要求:用菜单设计选项,选择多个结构成员值并输出。

//6.4
#include<iostream>
using namespace std;
const int strsize = 20;
struct bop {
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};

void choices(void)
{
	cout << "Benvolent Order of Programmers name structure\n"
		"a. display by name \t b. display by title \n"
		"c. display by bopname \t d. display by preference \n"
		"q. quit\n";
}

int main()
{
	bop* Programmers = new bop[5];
	Programmers[0] = { "Wimp Macho","king","WM",1 };
	Programmers[1] = { "Raki Rhodes","boy","Junior Programmer",3 };
	Programmers[2] = { "Celia Laiter","MIPS","CL",2 };
	Programmers[3] = { "Hoppy Hipman","IKUN","Analyer Traines",3 };
	Programmers[4] = { "Pat Hand", "LOOPY","DBOY",2 };

	choices();
	cout << "Enter your choice:\n";
	char ch;
	cin>>ch;
	while (ch != 'q')
	{
		switch (ch)
		{
		case 'a':
			for (int i = 0; i < 5; i++)
				cout << Programmers[i].fullname << endl;
			break;
		case 'b':
			for (int i = 0; i < 5; i++)
				cout << Programmers[i].title << endl;
			break;
		case 'c':
			for (int i = 0; i < 5; i++)
				cout << Programmers[i].bopname << endl;
			break;
		case 'd':
			for (int i = 0; i < 5; i++)
				cout << Programmers[i].preference << endl;
			break;
		default:
			cout<<"Waring! Please enter your choice again.\n";
			goto paris;
		}
		cout << "Enter your next choice:\n";
		paris:cin>>ch;
	}
	cout << "Bye!\n";

	delete[]Programmers;
	return 0;
}

第五题

要求:输入数字,回报税收输出,并且,输入负数或非数字时结束程序。

//6.5
#include<iostream>
const double  lev_1 = 0.0;
const double lev_2 = 0.1;
const double lev_3 = 0.15;
const double lev_4 = 0.20;

const double m_1 = 5000;
const double m_2 = 15000;
const double m_3 = 35000;
int main()
{
	using namespace std;
	double m, n;
	cout << "Please enter your money:\n";
	if (cin>>m&&(m>0))
	{
		if (m <= m_1)
			n = m * lev_1;
		else if (m <= m_2)
			n = (m_2 - m_1) * lev_2;
		else if (m <= m_3)
			n = (m_2 - m_1) * lev_2 + (m_3 - m_2) * lev_3;
		else
			n = (m_2 - m_1) * lev_2 + (m_3 - m_2) * lev_3 + (m - m_3) * lev_4;
	}
	else
	{
		cout << "waring!\n";
		goto paris;
	}
	cout << "the taxation is " << n << " tvarps";
	paris:return 0;
}

第六题

要求:根据输入数量,创造相应数量动态结构,并且将结构成员分类为其他结构成员,然后更据条件输出相应成员或结果。

//6.6
#include <iostream>
#include <string>
using namespace std;
const double money = 10000;
struct Patrons
{
    string name_p;
    double money_p;
};
struct G_Patrons
{
    string name_g;
};
int main()
{
    int m;
    cout << "How many Patrons ?\n";
    cin >> m;
    Patrons* ps = new Patrons[m];
    G_Patrons* pg = new G_Patrons[m];
    G_Patrons* p = new G_Patrons[m];
    cout << "Enter the Patrons and their money:\n";
    for (int i = 0; i < m; i++)
    {
       cin.get();
       getline(cin, ps[i].name_p);
       cin >> ps[i].money_p;
    }
    int count =0,count_g = 0;
    cout << "All Patrons:\n";
    for (int i = 0; i < m; i++)
    {
        cout << "name: " << ps[i].name_p 
            << "\tmoney: " << ps[i].money_p << endl;
        if (ps[i].money_p > money)
        {  
            pg[count_g].name_g = ps[i].name_p;
            count_g++;
        }
        else
        {
            p[count].name_g = ps[i].name_p;
            count++;
        }
    }
    cout << "Grand Patrons:\n";
    if (count_g == 0)
        cout << "none\n";
    else
        for (int i = 0; i < count_g; i++)
            cout << pg[i].name_g << endl;
    cout << "Other Patrons:\n";
    if (count == 0)
        cout << "none\n";
    else
        for (int i = 0; i < count; i++)
            cout << p[i].name_g << endl;
    delete [] ps;
    delete [] p;
    delete [] pg;
    return 0;

}

第七题

要求:输入英语短句,在输入‘q'字符后程序结束,并且,得到分别以非字母,元音字母,辅音字母为开头的单词的数量。

//6.7
#include<iostream>
#include<cctype>
const int ArSize = 25;
int main()
{
	using namespace std;
	char word[ArSize];
	cout << "Enter words (q to quit):\n";
	int m = 0, n = 0, i = 0;
	cin>>word;
	while (word[0]!= 'q'&&word[1]=='\0')//很细节,要注意到。
	{
		if (!isalpha(word[0]))
			m++;
		else
			switch (word[0])
			{
			case'a':
			case'A':
			case'e':
			case'E':
			case'i':
			case'I':
			case'O':
			case'o':
			case'U':
			case'u':
				n++;
				break;
			default:
				i++;
				break;
			};
		cin >> word;
	}
	cout << n << " words beginning with vowels.\n";
	cout << i << " words beginning with consonants.\n";
	cout << m << " others.\n";
	return 0;
}

第八题

要求:打开一个文件,逐个读取字符,并且计数,到达结尾后,统计输出文件的字符数量。

//6.8
#include<iostream>
#include<fstream>
#include<cstdlib>
int main()
{
	using namespace std;
	ifstream inFile;
	const char filename[60] = { "D:\\visual scripts\\carinfo.txt" };
	inFile.open(filename);
	if (!inFile.is_open())
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	char ch;
	int i;
	for (i = 0; !inFile.eof(); i++)
	{
	   inFile >> ch;

	};
	cout << "the charactors' total is " << i;
	return 0;
}

第九题

要求:用文件读取信息的形式,完成第六题。

//6.9
#include <iostream>
#include <string>
#include<fstream>
#include<cstdlib>

using namespace std;
const double money = 10000;
struct Patrons
{
    string name_p;
    double money_p;
};
struct G_Patrons
{
    string name_g;
};
int main()
{
    //将题目需要目标文件及内容进行创建
    ofstream outFile;
    outFile.open("D:\\visual scripts\\Patrons.txt");
    cout << "enter the content to destination file.\n";
    outFile << "4\n"
        "Sam Stone\n2000\n"
        "Freida Flass\n100500\n"
        "Tammy Tubbs\n5000\n"
        "Rich Raptor\n55000\n";
    outFile.close();
    

    //检测
    ifstream inFile;
	const char filename[60] = { "D:\\visual scripts\\Patrons.txt" };
	inFile.open(filename);
	if (!inFile.is_open())
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}

    
    //创建动态结构数组
    int m;
    cout << "How many Patrons ?\n";
    inFile >> m;
    cout << m<<endl;
    Patrons* ps = new Patrons[m];
    G_Patrons* pg = new G_Patrons[m];
    G_Patrons* p = new G_Patrons[m];

   
   //读取并将结构数组成员内容分类
   // cout << "Enter the Patrons and their money:\n";
    cout << "读取文件中的内容..." << endl;
    for (int i = 0; i < m; i++)
    {
       inFile.get();
       getline(inFile, ps[i].name_p);
       inFile>> ps[i].money_p;
    }
    int count =0,count_g = 0;
    cout << "All Patrons:\n";
    for (int i = 0; i < m; i++)
    {
        cout << "name: " << ps[i].name_p 
            << "\tmoney: " << ps[i].money_p << endl;
        if (ps[i].money_p > money)
        {  
            pg[count_g].name_g = ps[i].name_p;
            count_g++;
        }
        else
        {
            p[count].name_g = ps[i].name_p;
            count++;
        }
    }


    //输出结构数组
    cout << "Grand Patrons:\n";
    if (count_g == 0)
        cout << "none\n";
    else
        for (int i = 0; i < count_g; i++)
            cout << pg[i].name_g << endl;
    cout << "Other Patrons:\n";
    if (count == 0)
        cout << "none\n";
    else
        for (int i = 0; i < count; i++)
            cout << p[i].name_g << endl;
    
 
    delete [] ps;
    delete [] p;
    delete [] pg;
    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值