第6题:

当number<length,显示数组时数组未被赋值的地方会出现乱码,可以通过初始化数组a[]来改善。

#include <iostream>
using namespace std;

int Fill_array(double a[],int length);
void Show_array(double a[], int length);
void Reverse_array(double a[], int length);

int main()
{
	double a[10];
	int length = 10;
	int number;
	number=Fill_array(a, length);
	cout << number << endl;
	Show_array(a, length);
	Reverse_array( a,  length);
	Show_array(a, length);
	Reverse_array(a+1, length-2);
	Show_array(a, length);
	return 0;
}

int Fill_array(double a[], int length)
{
	double in;
	int i;
	i = 0;
	while (cin>>in && i<length )
	{
		a[i] = in;
		i++;
	}
	return i;
}
void Show_array(double a[], int length)
{
	for (int i=0;i<length;i++)
	{
		cout<< a[i]<<"\t";
	}
	cout << endl;
}
void Reverse_array(double a[], int length)
{
	for (int i = 0; i < length/2; i++)
	{
		int temp;
		temp = a[i];
		a[i] = a[length-1- i];
		a[length - 1 - i] = temp;
	}
}

第7题:

#include <iostream>
const int Max = 5;
double* fill_array(double ar[], int limit);
void show_array(const double ar[], double* tail);
void revalue(double r, double ar[], double* tail);

int main()
{
	using namespace std;
	double properties[Max];
	double* tail = fill_array(properties, Max);
	show_array(properties, tail);
	if (tail)
	{
		cout << "Enter revaluation factor:";
		double factor;
		while (!(cin >> factor))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input;Please enter a number:";
		}
		revalue(factor, properties, tail);
		show_array(properties, tail);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

double* fill_array(double ar[], int limit)
{
	using namespace std;
	double temp;
	int i;
	for (i = 0; i < limit; i++)
	{
		cout << "Enter value #" << (i + 1) << ":";
		cin >> temp;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input;input process terminated.\n";
			break;
		}
		else if (temp < 0)
			break;
		ar[i] = temp;
	}
	return &ar[i];
}
void show_array(const double ar[], double* tail)
{
	using namespace std;
	for (int i = 0; &ar[i] < tail; i++)
	{
		cout << "Property #" << (i + 1) << ": $";
		cout << ar[i] << endl;
	}
}
void revalue(double r, double ar[], double* tail)
{
	for (int i = 0; &ar[i] < tail; i++)
		ar[i] *= r;
}

第8题:

a:

#include <iostream>
#include <string>
const int Seasons = 4;
using namespace std;
const char*Snames[4] = { "Spring","Summer","Fall","Winter" };

void fill(double pa[]);
void show(double da[]);

int main()
{
	double expenses[4];
	fill(expenses);
	show(expenses);
	return 0;
}

void fill(double pa[])
{

	for (int i=0;i<Seasons;i++)
	{
		cout << "Enter " << Snames[i] << " expenses: ";
		cin >> pa[i];
	}
}
void show(double da[])
{
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i=0;i<Seasons;i++)
	{
		cout << Snames[i] << ": $" << da[i] << endl;
		total += da[i];
	}
	cout << "Total Expenses:$" << total << endl;
}

b:

#include <iostream>
#include <string>
const int Seasons = 4;
using namespace std;
const char*Snames[4] = { "Spring","Summer","Fall","Winter" };
struct number
{
	double data[4];
};
void fill(number *pa);
void show(number da);

int main()
{
	number expenses;
	fill(&expenses);
	show(expenses);
	return 0;
}

void fill(number* pa)
{

	for (int i=0;i<Seasons;i++)
	{
		cout << "Enter " << Snames[0] << " expenses: ";
		cin >> (*pa).data[i];
	}
}
void show(number da)
{
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i=0;i<Seasons;i++)
	{
		cout << Snames[i] << ": $" << da.data[i] << endl;
		total += da.data[i];
	}
	cout << "Total Expenses:$" << total << endl;
}

第9题:

#include <iostream>
#include <string>
#pragma warning(disable:4996)
using namespace std;
const int SLEN = 30;
struct student
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
int getinfo(student pa[], int n);
void display1(student st);
void display2(const student* ps);
void display3(const student pa[], int n);

int main()
{
	cout << "Enter class size:";
	int class_size;
	cin >> class_size;
	while (cin.get() != '\n')
	{
		continue;
	}
	student* ptr_stu = new student[class_size];
	int entered = getinfo(ptr_stu, class_size);
	for (int i = 0; i < entered; i++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete[] ptr_stu;
	cout << "Done\n";
	return 0;
}
int getinfo(student pa[], int n)
{
	int number;
	number = 0;
	string name;
	string fa;
	for (number; number < n; number++)
	{
		getline(cin, name);
		getline(cin, fa);
		if (strcmp(&name[0], " ") == 0)
		{
			break;
		}
		if (name.length()>SLEN)
		{
			name[SLEN - 1] = '\0';
		}
		if (fa.length()>SLEN)
		{
			fa[SLEN - 1] = '\0';
		}
		strcpy(pa[number].fullname, &name[0]);
		strcpy(pa[number].hobby, &fa[0]);
	}
	return number;
}
void display1(student st)
{
	if (st.fullname)
	{
		cout << st.fullname << ":"<< st.hobby << endl;
	}
}
void display2(const student* ps)
{
	if (ps->fullname)
	{
		cout << ps->fullname << ":"<< ps->hobby << endl;
	}
}
void display3(const student pa[], int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		cout << pa[i].fullname << ":" << pa[i].hobby << endl;
	}
}

第10题:

函数指针:

#include <iostream>
#include <string>
#pragma warning(disable:4996)
using namespace std;

double add(double x, double y);
double min(double x, double y);
double calculate(double x, double y, double (*pf)(double x, double y));

int main()
{
	while (1)
	{
		double x, y;
		cout << "请输入x和y:";
		cin >> x >> y;
		cout << "两个数字的和为:"<<calculate(x, y,add)<<endl;
		cout << "两个数字的差为:"<<calculate(x, y, min)<<endl;
	}
	return 0;
}

double add(double x, double y)
{
	return x + y;
}

double min(double x, double y)
{
	return x - y;
}

double calculate(double x, double y, double (*pf)(double x, double y))
{
	return (*pf)(x, y);
}

指针数组:

#include <iostream>
#include <string>
#pragma warning(disable:4996)
using namespace std;

double add(double x, double y);
double min(double x, double y);
void calculate(double x, double y, double (*pf[ ])(double x, double y));

int main()
{
	double (*pf[2])(double x, double y) = {add,min};
	while (1)
	{
		double x, y;
		cout << "请输入x和y:";
		cin >> x >> y;
		calculate(x, y, pf);
	}
	return 0;
}

double add(double x, double y)
{
	return x + y;
}

double min(double x, double y)
{
	return x - y;
}

void calculate(double x, double y, double (*pf[])(double x, double y))
{
	for(int i=0;i<2;i++)
	{ 
		if (i==0)
		{
			cout << "两个数的和为:";
		}
		else
		{
			cout << "两个数的差为:";
		}
		cout<<(*pf[i])(x, y);
		cout << endl;
	}
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐