先上源码,过几天要考试,考完后总结
第一题
#include <iostream>
using namespace std;
double Calculate(double x, double y);
int main()
{
double a, b;
double avr;
cout << "Enter two numbers: ";
cin >> a >> b;
while(a!=0 && b!=0)
{
avr = Calculate(a, b);
cout << "The Average of this two numbers is " << avr << endl;
cout << "Enter two numbers: ";
cin >> a >> b;
}
cout << "Bye!" << endl;
return 0;
}
double Calculate(double x, double y)
{
return (2.0*x*y/(x+y));
}
第二题
#include <iostream>
using namespace std;
const int ArSize = 10;
void Input(double grade[], int & count);
void Print(double grade[], int count);
double Calculate(double grade[], int count);
int main()
{
double grade[ArSize];
double avr;
int count = 0;
Input(grade, count);
Print(grade, count);
avr = Calculate(grade, count);
cout << "Average of grades is " << avr << endl;
return 0;
}
void Input(double grade[], int & count)
{
cout << "Enter Golf Grades of no more than 10 people." << endl
<<"Press negative number or letter to quit: " << endl;
for(int i = 0; i < ArSize; i++)
{
cout << "#" << i+1 << ". : ";
if(!(cin >> grade[i]) || grade[i] < 0 )
{
break;
}
else
{
count++;
}
}
}
void Print(double grade[], int count)
{
cout << "You have input " << count << " grades: " << endl;
for(int i = 0; i < count; i++ )
{
cout << grade[i] << " ";
}
cout << endl;
}
double Calculate(double grade[], int count)
{
double sum = 0;
for(int i = 0; i < count; i++ )
{
sum = sum + grade[i];
}
return (sum/count);
}
第三题
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
typedef struct
{
char maker[40];
float height;
float width;
float length;
float volume;
}box;
void Print(box member);
void Handle(box * pt);
int main()
{
box member1 = {"Made in China", 1, 2, 3, 0};
cout << "original address: " << &member1.maker << endl;
Print(member1);
Handle(&member1);
Print(member1);
return 0;
}
void Print(box member)
{
static int count = 0;
if(count == 0)
{
cout << "\nBefore handled: " << endl;
cout << member.maker << ": " << member.height << "*" << member.width << "*" << member.length << " ?= " ;
cout << member.volume << endl;
count ++;
}
else
{
cout << "\nAfter handled: " << endl;
cout << member.maker << ": " << member.height << "*" << member.width << "*" << member.length << " = " ;
cout << member.volume << endl;
}
cout << "duplication address: " << &member.maker << endl;
//strcpy(member.maker, "change");//按结构名称传递参数,结构中的字符串被整体复制,而不是只复制字符串首地址
}
void Handle(box * pt)
{
pt->volume = pt->height * pt->length * pt->width;
}
第四题
#include<iostream>
using namespace std;
long double probability(int numbers, int picks, int SecSector);
int main()
{
int total, choices, SS;
cout << "Enter the total number of choices on the game card and\n"
"the number of picks allowed and the number of the second sector:\n";
while((cin >> total >> choices >> SS) && choices <= total)
{
cout << "You have one chance in ";
cout << probability(total, choices, SS);
cout << " of winning.\n";
cout << "Next three numbers (q to quit): ";
}
cout << "bye\n";
return 0;
}
long double probability(int numbers, int picks, int SecSector)
{
long double result = 1.0;
long double n;
int p;
for(n = numbers, p = picks; p > 0; n--, p--)
{
result = result * (long double)n / (long double)p;
}
result = result * SecSector;
return result;
}
第五题
#include <iostream>
using namespace std;
long double Recursion(int num);
int main()
{
int number;
cout << "Enter a number "
"and q to quit: ";
while(cin >> number)
{
if(number < 0)
{
cout << "Enter error!" << endl;
}
else
{
cout << number << "! = " << Recursion(number) << endl;
}
cout << "Enter a number "
"and q to quit: ";
}
cout << "bye!\n";
return 0;
}
long double Recursion(int num)
{
if(num == 0)
return 1;
else if(num == 1)
return num;
else
{
return (num * Recursion(num-1));
}
}
第六题
#include <iostream>
using namespace std;
const int ArSize = 20;
int Fill_array(double Arr[],const int ArSize);
void Show_array(double Arr[], int count);
void Reverse_array(double Arr[], int count);
int main()
{
double Array[ArSize];
int count;
count = Fill_array(Array, ArSize);
Show_array(Array, count);
Reverse_array(Array, count);
Show_array(Array, count);
return 0;
}
int Fill_array(double Arr[],const int ArSize)
{
int count = 0;
cout << "Enter the numbers in the array no more than " << ArSize << ":\n";
while(cin >> Arr[count] && count <20)
{
count++;
}
return count;
}
void Show_array(double Arr[], int count)
{
cout << "You have input " << count << " numbers\n"
<< "They are:\n";
for(int i = 0; i < count; i++)
{
cout << Arr[i];
cout << " ";
}
cout << endl;
}
void Reverse_array(double Arr[], int count)
{
double temp;
cout << "Reverse the array: \n";
for(int i = 1; i < count/2; i++)
{
temp = Arr[i];
Arr[i] = Arr[count-1-i];
Arr[count-1-i] = temp;
}
}
第七题
#include <iostream>
const int Max = 5;
double * fill_array(double ar[], int limit);
void show_array(const double ar[], double * pt_end);
void revalue(double r, double ar[], double * pt_end);
int main()
{
using namespace std;
double properties[Max];
double * pt_head = properties;
double * pt_end;
pt_end = fill_array(properties, Max);
show_array(properties, pt_end);
cout << pt_head << " " << pt_end << endl; //test
cout << (unsigned int)pt_end - (unsigned int)pt_head << endl;//test
cout << (pt_end - pt_head) << " " << sizeof(double) //test
<< " " << sizeof(char) << " " << sizeof(int) << endl;//test
if((int)(pt_end - pt_head) > 0)
{
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, pt_end);
show_array(properties, pt_end);
}
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-1]);
}
void show_array(const double ar[], double * pt_end)
{
using namespace std;
int i = 0;
while(i < Max)
{
cout << "Property #" << (i+1) << ": $";
cout << ar[i] << endl;
if(&ar[i] == pt_end)
break;
i++;
}
}
void revalue(double r, double ar[], double * pt_end)
{
int i = 0;
while(i < Max)
{
ar[i] *= r;
if(&ar[i] == pt_end)
break;
i++;
}
}
第八题1
#include <iostream>
using namespace std;
const int Seasons = 4;
const char Snames[][10] = {"Spring","Summer","Fall","Winter"};
void fill(double pa[]);
void show(double * da);
int main()
{
double expenses[Seasons];
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;
}
第八题2
#include <iostream>
using namespace std;
const int Seasons = 4;
const char Snames[][10] = {"Spring","Summer","Fall","Winter"};
typedef struct
{
double expenses[Seasons];
}expen;
void fill(expen * pa);
void show(expen * da);
int main()
{
expen expense;
fill(&expense);
show(&expense);
return 0;
}
void fill(expen * pa)
{
for(int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pa->expenses[i];//混搭,指针和数组的操作类似,子函数接收元素首地址
}
}
void show(expen * da)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for(int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << da->expenses[i] << endl;
total += da->expenses[i];
}
cout << "Total Expenses: $" << total << endl;
}
第九题
#include <iostream>
#include <string>
#include <cstring>
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 count = 0;
char input[SLEN];
cout << "Enter the #1 strudent's name, enter \"enter\" to quit: ";
cin.get(input, SLEN);
while(input[0] != '\0')
{
strcpy((pa+count)->fullname, input);
cout << "Enter the hobby: ";
cin.get();
cin.get((pa+count)->hobby, SLEN);
cout << "Enter the ooplevel: ";
cin >> (pa+count)->ooplevel;
cin.get();
count++;
cout << "Enter the #" << count+1 << " strudent's name, enter \"enter\" to quit: ";
cin.get(input, SLEN);
}
return count;
}
void display1(student st)
{
cout << "Name: " << st.fullname << ", hobby: " << st.hobby << ", ooplevel: " << st.ooplevel << endl;
}
void display2(const student * ps)
{
cout << "Name: " << ps->fullname << ", hobby: " << ps->hobby << ", ooplevel: " << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
for(int i = 0; i < n; i++)
{
cout << "#" << i+1 << " Name: " << pa[i].fullname
<< ", hobby: " << pa[i].hobby << ", ooplevel: " << pa[i].ooplevel << endl;
}
}
第十题
#include <iostream>
using namespace std;
double add(double x, double y);
double multiply(double x, double y);
double subtract(double x, double y);
double calculate(double a, double b, double (*pt[3])(double, double));
int main()
{
int num1 = 10, num2 = 20;
double (*ps[3])(double,double) = {add,multiply,subtract};
calculate(num1, num2, ps);
return 0;
}
double add(double x, double y)
{
return (x + y);
}
double multiply(double x, double y)
{
return (x * y);
}
double subtract(double x, double y)
{
return (x - y);
}
double calculate(double a, double b, double (*pt[3])(double, double))
{
for(int i = 0; i < 3; i++)
{
cout << "#" << i+1 << ": " << (*pt[i])(a,b) << endl;
}
return 0;
}