那时候做的题目,可能做出的质量不高,不过可以费尽了心思了,每次做题目都很有收获,而且,每当做出了一道题,都会相当地有成就感,做题目即能巩固问题,又要增加经验,发现问题,真是提升的好方法,所以也很喜欢做题,只是有时候会一道题花费很多时间,不过做出来了就后就会觉得是值得的。
五个月左右,零零散散的时间,自学完了这个C++ Primer Plus ,下面是做过的题目,自己常看看可以温故而知新呢!
// 编程练习_02_06.cpp
/*******************第02章(开始学C++),第06题,
编写一个程序,要求用户输入小时数和分钟数,在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值。
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
******************************************************************************************************************************/
#include <iostream> // a preprocesor directive
using namespace std; // make definitions visible
int main() // function header
{ // start of function body
int a; // declare an integar variable a
int b; // declare an integar variable b
cout << "Enter the number of hours: "; // message
cin >> a; // input the value of the variable a
cout << "Enter the number of minutes: "; // message
cin >> b; // input the value of the variable a
void display(int&, int&); // function prototype for display()
display(a, b); // call the display()
return 0; // terminate main()
} // end of function body
void display(int& x, int& y) // define the display() function
{
cout << "Time: "
<< x
<< ":"
<< y
<<endl;
}
// 编程练习 04.01_display message.cpp
/***************************************************************
1.编写一个C++程序,如下述输出范例所示的那样请求并显示信息:
What is your first name? Betty Sue
What is your last name? Yew
What letter grade do you deserve? B
What is your age? 22
Name: Yew, Betty Sue
Grade: C
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。
假设用户请求A、B或C,所以不必担心D和F之间的空档。
********************************************************************************************/
#include <iostream> // a preprocessor directive
#include <string> // for enter the name
using namespace std;
int main()
{
// define
string fname; // define the first name
string lname; // define the last name
char grade; // define the grade
int age; // define the age
// calculate and cout
cout << "What is your first name? ";
getline(cin, fname);
cout << "What is your last name? ";
getline(cin, lname);
cout << "What letter grade do you deserve? ";
cin >> grade;
getline(cin, *(new string)); //下面getline一次,忽略1行
// cout << endl;
cout << "What is your age? ";
cin >> age;
// cout << endl;
// cout the last message
cout << "Name: " << lname << "," << fname <<endl;
cout << "Grade: " << (char(grade+1)) << endl;
return 0;
}
// 编程练习 04.01_display message_2.cpp
/***************************************************************
1.编写一个C++程序,如下述输出范例所示的那样请求并显示信息:
What is your first name? Betty Sue
What is your last name? Yew
What letter grade do you deserve? B
What is your age? 22
Name: Yew, Betty Sue
Grade: C
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。
假设用户请求A、B或C,所以不必担心D和F之间的空档。
********************************************************************************************/
#include <iostream> // a preprocessor directive
#include <string> // for enter the name
using namespace std;
struct message
{
char *FirstName;
char *LastName;
char Grade;
int age;
};
/*有错,连对象都还没有定义
int main()
{
string temp;
cout << "What is your first name? ";
cin >> temp;
message.FirstName = new char [strlen(temp) + 1];
strcpy(message.FirstName, temp);
cout << "What is your last name? ";
cin >> temp;
message.LastName = new char [strlen(temp) + 1];
strcpy(message.LastName, temp);
cout << "What letter grade do you deserve? ";
cin.get();
cin.get(message.Grade);
cout << "What is your age? ";
cin >> message.age;
cout << "Name: " << message.*LastName << ", " << message.*FirstName;
cout << endl << "Grade: " << ((char)(message.Grade + 1)) << endl;
cin.get();
return 0;
}
***/
/*************************************************************************
打算参考书上的
char *ps;
ps = new char[strlen(animal) + 1];
strcpy(ps, animal);
谁知道呢?挺多错误的。
G:\Temp\编程练习_04.01_display message_2.cpp:43: error: `LastName' undeclared (first use this function)
G:\Temp\编程练习_04.01_display message_2.cpp:43: error: (Each undeclared identifier is reported only once for each function it appears in.)
G:\Temp\编程练习_04.01_display message_2.cpp:43: error: expected primary-expression before '.*' token
************************************************************************/
int main()
{
string temp;
cout << "What is your first name? ";
cin >> temp;
message msg;
msg.FirstName = new char [strlen(temp.c_str()) + 1]; // string 类型获取长度方法 strlen(string.c_str())
strcpy(msg.FirstName, temp.c_str());
cout << "What is your last name? ";
cin >> temp;
msg.LastName = new char [strlen(temp.c_str()) + 1];
strcpy(msg.LastName, temp.c_str());
cout << "What letter grade do you deserve? ";
cin.get();
cin.get(msg.Grade);
cout << "What is your age? ";
cin >> msg.age;
cout << "Name: " << msg.LastName << ", " << msg.FirstName;
cout << endl << "Grade: " << ((char)(msg.Grade + 1)) << endl;
cin.get();
return 0;
}
// 编程练习_04.09_nestedloopdisplaychar.cpp -- display linechar use nested loop
/**************************** ** 题目要求 ** *******************************
9.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。
然后,程序将显示相应的行数的星号,
其中第一行包括一个星号,第二行包括两个星号,依此类推。
第一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。
该程序的运行情况如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
******************************************************************************/
#include <iostream>
using namespace std;
int main()
{
cout << "Enter number of rows: ";
int n;
cin >> n;
while (n <= 0)
{
cout << "Please Enter again and the number must be equal or greater than zero: ";
cin >> n;
}
char point = '.';
char asterisk = '*';
int i; // define two variable for circulate
int j;
int m = n; // mark the biggest number
for (; n; --n)
{
for (i = n-1; i; --i)
cout << point;
for (j = 0; j < (m - n + 1); ++j)
cout << asterisk;
cout << endl;
}
return 0;
}
// 很奇怪,在GCC里面运行正常,然后放在VC++6.0就不正常了。
// VC++ 2010 运行正常。
/************************************************************************************
网友修改后
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
F:\Temp\test.cpp(26) : error C2248: 'brand' : cannot access private member declared in class 'CandyBar'
F:\Temp\test.cpp(17) : see declaration of 'brand'
F:\Temp\test.cpp(30) : error C2248: 'weight' : cannot access private member declared in class 'CandyBar'
F:\Temp\test.cpp(18) : see declaration of 'weight'
F:\Temp\test.cpp(34) : error C2248: 'calorie' : cannot access private member declared in class 'CandyBar'
F:\Temp\test.cpp(19) : see declaration of 'calorie'
F:\Temp\test.cpp(41) : error C2248: 'brand' : cannot access private member declared in class 'CandyBar'
F:\Temp\test.cpp(17) : see declaration of 'brand'
F:\Temp\test.cpp(42) : error C2248: 'weight' : cannot access private member declared in class 'CandyBar'
F:\Temp\test.cpp(18) : see declaration of 'weight'
F:\Temp\test.cpp(43) : error C2248: 'calorie' : cannot access private member declared in class 'CandyBar'
F:\Temp\test.cpp(19) : see declaration of 'calorie'
F:\Temp\test.cpp(118) : error C2593: 'operator >>' is ambiguous
F:\Temp\test.cpp(132) : error C2593: 'operator >>' is ambiguous
F:\Temp\test.cpp(145) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
test.obj - 9 error(s), 0 warning(s)
************************************************************************************/
// 编程练习_04.9_CandyBarmain.cpp
/***************************************
9.结构 CandyBar 包含3个成员,
第一个成员存储了糖块的品牌;
第二个成员存储糖块的重量(可有小数);
第三个成员存储了糖块的卡路里含量(整数)。
请编写一个程序,声明这个类。
使用new动态分配数组。
*****************************************/
#include <iostream>
#include <string>
using namespace std;
const int NuLL = 0;
// Candy Bar.h -- class include three data members
class CandyBar
{
public:
friend istream&operator >> (istream&, CandyBar&); // operator << redefine
friend ostream&operator << (ostream&, CandyBar&); // operator >> redefine
friend CandyBar* update();//---------------------------------------------------------------------------------
friend CandyBar* display();//因为要在类外访问私有,没有添加友元
private:
string brand;
double weight;
int calorie;
CandyBar* next; // to create a linked list
};
istream& operator >> (istream& input, CandyBar& cinCandyBar)
{
cout << "Please input the brand of the sugar:";
input >> cinCandyBar.brand ;
cout << endl;
cout << "Please input the weight of the sugar:";
input >> cinCandyBar.weight;
cout << endl;
cout << "Please input how many calorie in this sugar:";
input >> cinCandyBar.calorie;
cout << endl;
return input;//-------------------------------需要返回流,为了可以后续同类操作 cout>>a>>1>>"d";
}
ostream& operator << (ostream& output, CandyBar& coutCandyBar)
{
output << coutCandyBar.brand << "\t\t"
<< coutCandyBar.weight << "\t"
<< coutCandyBar.calorie << endl;
return output;//----------------------------------------------------------同上
}
// CandyBarmain.cpp -- the main function of CandyBar
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------后面的函数要用,放main里访问不到
CandyBar* head = NuLL;
CandyBar* pa;
CandyBar* pb;
int main()
{
// CandyBar* pc;
// CandyBar* temp;
int choice();
choice();
return 0;
}
int choice()
{
// the Candy Bar function
CandyBar* update();
CandyBar* display();
// circulate choice
while(1)
{
// display the message
cout << "Welcome to our Candy Bar. " << endl
<< " 1.display the message of Candy Bar."<<endl
<< " 2.update the message of Candy Bar."<<endl
<< " 3.exit the system."<<endl
<< "Now, please choice a function you want to come true and enter the function number: ";
//start realize the function
char ch;
switch (ch=cin.get()){ // -----------------------------switch() 缺个大括号 {
case '1':
cout << "\n";//-------------------------------这之后的好几个\n写成/n了
display();
break;
case '2':
cout << "\n";
update();
break;
case '3':
cout << "\n Success to exit the system! ";
return 0;
default :
cin.clear();//---------------------------------写错 clean
while(ch!='\n')
cin.get();
cout << "\nplease enter a true number";
break;
}//-------------------------------------------------------同上,缺}
}
return 0;
}
CandyBar* update()
{
// make sure update number
cout << "How many message you want to update? ";
int n;
cin >> n;
cout << endl;
if (head == NuLL) // if it is first input
{
pb = new CandyBar;
head = pb;
cin >> *head;
pa = head;
--n;
}
else // search the address of last linked list
for (pa = head; pa->next != NuLL; pa = pa -> next);
// input the data
for (int i = 0; i<n; i++)
{
pb = new CandyBar;
pa->next = pb;
pa = pb;
cin >> *pa;
}
pa->next = NuLL;
return (head);
}
CandyBar* display()
{
cout << " \n Brand\t\tWeight\tcalorie\n" ;
for (pb = head; pb ; pb = pb->next) //pb->next = NuLL =号是赋值 ==是判断
//这里循环后才执行 pb = pb->next,
//所以判断pb就行,
cout << *pb; //需要传的是引用,不是指针,先解指针
return 0;
}
// 编程练习_05.03_计算投资价值
/********************* 第3题 *****************************************
3.Daphne 以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,
即每年10元美元:
利息=0.10*原始存款
而Cleo以5%的复利投资了100美元。
也就是说,利息是当前存款(包括获得的利息)的5%,:
利息=0.05*当前存款
Cleo在第一年投资100美元的盈利是5%---得到了105美元。
下一年的盈利是105美元的5%即5.25美元,依此类推。
请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,
并显示此时两个人的投资价值。
**************************************************************************/
#include <iostream>
using namespace std;
int main()
{
const double depositd = 100; // d的本金
double depositc = 100; // c的本金
const double interestrd = 0.10;
const double interestrc = 0.05;
double interestd; // d的利息
double interestc; // c的利息
double sumd = 0;
double &sumc = depositc;
int year;
//先计算出第一年的
sumd += depositd;
interestd = interestrd * depositd;
interestc = interestrc * depositc;
sumc += interestc;
for (year = 2; sumc > sumd; ++year)
{
sumd += interestd;
interestc = interestrc * depositc;
sumc += interestc;
}
cout << "Invest of Cleo: " << sumc << endl;
cout << "Invest of Daphne: " << sumd << endl;
cout << "After year: " << year << endl;
while(2);
return 0;
}
// 编程练习_05.04_用循环显示数据.cpp
/****************************** 第4题 **************************************
4.假设要销售 C++ For Fools 一书。请编写一个程序,输入全年中每个月的销售量
(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char* 数组
(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。
然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
*****************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int monnum = 12;
int sale [monnum];
// string smonth[]是用C++中的string 定义的字符串数组
// 如果你想用C语言中的指针(呵呵,同样也是C++中的,只是在C++中少用了,大多都用string代替了),
// 你就把 string smonth[]= 用 char *scmonth[]= 代替
string pointout[monnum] =
// char *scmonth[] =
{
"Please enter the distribution records of January: ",
"Please enter the distribution records of February: ",
"Please enter the distribution records of March: ",
"Please enter the distribution records of April: ",
"Please enter the distribution records of May: ",
"Please enter the distribution records of June: ",
"Please enter the distribution records of July: ",
"Please enter the distribution records of August: ",
"Please enter the distribution records of September: ",
"Please enter the distribution records of October: ",
"Please enter the distribution records of November: ",
"Please enter the distribution records of December: "
};
int i = 0;
int sum = 0;
for (; i < 12; ++i)
{
cout << pointout[i];
// 你用char *scmonth[] 就把 cout << smonth[i];用cout << scmonth[i];代替
// cout << scmonth[i];
cin >> sale[i];
cout << endl;
sum += sale[i];
}
cout << "\nTotal: " << sum << endl;
return 0;
}
// 编程练习_05.06_设计car结构.cpp 简单的动态链表
/********************************* 第6题 *************************************
6.设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组成
string 对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。
随后,程序使用new来创建一个由相应数量的car 结构数组。接下来,程序提示用户输入每
辆辐的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交
替读取数值和字符串(参见第四章)。最后,程序将显示每个结构的内容。该程序的运行情
况如下:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Peease enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
*******************************************************************************/
#include <iostream>
using namespace std;
int n;
class car
{
public:
string name;
int year;
car* next;
};
int main()
{
car* creat();
car* display(car*);
display(creat());
while(1);
return 0;
}
car* creat()
{
car *head;
car *pa;
car *pb;
cout << "How many cars do you wish to catalog?";
cin >> n;
while (!cin )
{
cout << "Enter Error, please enter again:";
cin.clear();
while(cin.get() != '\n')
continue;
cin >> n;
}
int i;
head = new car;
pa = head;
for (i = 1; i <= n; ++i)
{
pb = pa;
cout << "Car #" << i << ":" << endl;
cout << "Please enter the make: ";
cin >> pa -> name;
cout << "Please enter the year made: ";
cin >> pa -> year;
pa = new car;
pb -> next = pa;
}
pa = 0;
pb -> next = 0;
return head;
}
car* display(car *head)
{
cout << "Here is your collection: " << endl;
car *pa;
for (pa = head; pa != 0; pa = pa -> next)
cout << pa -> year << " " << pa -> name << endl;
return 0;
}
// 编程练习_05.7_计算读取单词.cpp
/******************* 第07题 ************************************************
7.编写一个程序,它使用一个 char 数组和循环来每次读取一个单词,
直到用户输入 done 为止。随后,该程序指出用户输入了多少个单词(不包括done在内).
下面是该程序的运行情况:
Enter words (to stop, type the word done):
anteaster birthday category dumpster
anvyfiagle geometry done for sure
You entered a total of 7 words.
您应在程序中包含头文件 cstring,并使用函数 strcmp() 来进行测试.
******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter words (to stop, type the word done): " << endl;
const int ch_ar_si = 100;
char word[ch_ar_si];
int n = 0;
while(cin >> word && strcmp("done", word))
++n;
cout << "You enterd a total of " << n << "words.";
while(2);
return 0;
}
// 编程练习_06.06_动态链表应用.cpp
/*************************** 第六题 ***********************************
6.编写一个程序,记录捐助给”维护合法权利团体“的钱。
该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。
这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:
用来存储姓名的字符数组(或string)和用来存储款项的double成员。
读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。
该列表前应包含一个标题,指出下面的捐款者是重要的捐款人(Grand Patrons).
然后,程序将列出其他的捐款者,该列表要以Patrons开头。
如果某种类别没有捐款者,则程序将打印单词"none"。
该程序只显示这两种类别,而不进行排序。
*****************************************************************************/
#include <iostream>
#include <string>
using namespace std;
struct jq
{
string name;
double money;
jq *next;
};
int main()
{
// enter donors numbers
int n;
int t;
cout << "Please enter the number of donors:";
do
{
t = 0;
cin >> n;
if (!cin)
{
cout << "Enter error, Please enter again: ";
cin.clear();
while(cin.get() != '\n');
t++;
}
}while(t);
// prototype
jq* creat(int n);
jq* print(jq *head, int n);
// function call
jq* temp = creat(n);
print(temp, n);
// The end
while(2);
return 0;
}
jq* creat(int n)
{
// define three pointer to make the list
jq* head = new jq; // 链表结点
jq* pa = head;
jq* pb = pa;
// enter data
int i;
for (i = 1; i <= n; ++i)
{
pb -> next = pa;
pb = pa;
cout << "Please enter number " << i << " donors money: ";
int t;
do
{
t = 0;
cin >> pa -> money;
if (!cin)
{
cout << "Enter error, Please enter again: ";
cin.clear();
while(cin.get() != '\n');
}
}while(t);
cout << "Please enter number " << i << " name: ";
while(cin.get() != '\n'); // 消回车
getline(cin, pa -> name); // 输字符串
if (pa -> name == "") // string 不读取 \n
pa -> name = "none"; // 若不输入名则自动为none
cout << endl;
pa = new jq;
}
pa = 0;
pb -> next = 0;
return head;
}
jq* print(jq* head, int n)
{
jq* pa;
// screen 1
cout << "Grand Patrons List: " << endl;
for (pa = head; pa != 0; pa = pa -> next)
if(pa -> money >= 10000)
cout << pa -> name << "\t\t" << pa -> money << endl;
// screen 2
cout << endl << "Other Patrons List: " << endl;
for (pa = head; pa != 0; pa = pa -> next)
if(pa -> money < 10000)
cout << pa -> name << "\t\t" << pa -> money << endl;
cout << "Finish!";
return 0;
}
// 编程练习_07.02_处理数组的函数.cpp
/**********************************************************************
2.编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。
程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。
请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。
***********************************************************************/
#include <iostream>
int const GolfArSize = 10;
int TrueSize = 0;
int main()
{
using namespace std;
cout << "Now, begin to remember the score." << endl;
void SaveGolf(int* Golf);
void ShowGolf (const int * Golf);
void AverageGolf (const int* Golf);
int Golf[GolfArSize];
SaveGolf(Golf);
ShowGolf(Golf);
AverageGolf(Golf);
return 0;
}
void SaveGolf(int* Golf)
{
using namespace std;
int i = 0;
cout << "If you want to end the programme,Please enter a letter." << endl;
for (i; i < GolfArSize; i++)
{
cout << "Please enter " << i+1 << " golf score: ";
if (cin >> Golf[i])
{
}
else
{
TrueSize = i;
break;
}
TrueSize = i;
}
cout << "Enter End" << endl;
}
void ShowGolf (const int * Golf)
{
using namespace std;
int i= 0;
cout << endl;
cout << "Now show the score in next line: " << endl;
for (i; i < TrueSize; i++)
{
cout << Golf[i] << '\t';
}
cout << endl;
}
void AverageGolf (const int* Golf)
{
using namespace std;
int i = 0;
int sum = 0;
for (i; i < TrueSize; ++i)
{
sum += Golf[i];
}
cout << "Average Score is : " << sum / TrueSize << endl;
}
// 本次编程时遇到了可提早结束输入的问题,后来想到多定义一个全局变量解决问题。
// 另外,这里还学习注意用const访问数组。用三种函数对数组实现功能。
// 编程练习_07.08_处理数组和结构的函数.cpp
/******************************************************************
8.这个练习让您编写处理数组和结构的函数。下面是程序的框架,
请提供其中描述的函数,以完成该程序。
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array or upon encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n);
// display1()takes a student structure as an argument
// and displays its contents
void display1(student st);
// display2()takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student *ps);
// display3()takes the address of the first element of an array
// of student structures and the number of array elements as
// arrguments and displays the contents of the structures
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;
}
*****************************************************************/
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array or upon encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n);
// display1()takes a student structure as an argument
// and displays its contents
void display1(student st);
// display2()takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student *ps);
// display3()takes the address of the first element of an array
// of student structures and the number of array elements as
// arrguments and displays the contents of the structures
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";
cin.get();
cin.get();
cin.get();
return 0;
}
// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array or upon encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n ; ++i)
{
cout << "Please enter this student's fullname: ";
int j;
cin.get(pa[i].fullname[0]);
if (pa[i].fullname[0] == '\n')
{
break;
}
for (j = 1; j < SLEN && (pa[i].fullname[j-1] != '\n'); ++j)
{
cin.get(pa[i].fullname[j]);
pa[i].fullname[j+1] = '\0';
}
cout << "Please enter this student's hobby: ";
cin >> pa[i].hobby;
cout << "Please enter this student's ooplevel: ";
cin >> pa[i].ooplevel;
cin.get();
}
return i;
}
// display1()takes a student structure as an argument
// and displays its contents
void display1(student st)
{
cout << st.fullname << '\t' << st.hobby << '\t'
<< st.ooplevel << endl;
}
// display2()takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student *ps)
{
cout << ps -> fullname << '\t'
<< ps -> hobby << '\t'
<< ps -> ooplevel << endl;
}
// display3()takes the address of the first element of an array
// of student structures and the number of array elements as
// arrguments and displays the contents of the structures
void display3(const student pa[], int n)
{
for (int i = 0; i < n; ++i)
{
if (pa[i].fullname == "")
{
break;
}
cout << pa[i].fullname << '\t' ;
cout << pa[i].hobby << '\t' ;
cout << pa[i].ooplevel << endl;
}
}
附:2011.10.08
刚刚,非常感谢一位网友,指出了这里面一道题目的错误,然后,还帮发给我了正确的答案,后来,我仔细看了一下,加上了注释,知道了错在哪里了,嘿嘿,非常感谢。
下面是源代码:
// 编程练习_05.03_计算投资价值
/********************* 第3题 *****************************************
3.Daphne 以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,
即每年10元美元:
利息=0.10*原始存款
而Cleo以5%的复利投资了100美元。
也就是说,利息是当前存款(包括获得的利息)的5%,:
利息=0.05*当前存款
Cleo在第一年投资100美元的盈利是5%---得到了105美元。
下一年的盈利是105美元的5%即5.25美元,依此类推。
请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,
并显示此时两个人的投资价值。
**************************************************************************/
// g++ -O3 -o benifit.exe -lstdc++ benifit.cpp
#include <iostream>
using namespace std;
int main()
{
const double depositd = 100; // d的本金
double depositc = 100; // c的本金
const double interestrd = 0.10;
const double interestrc = 0.05;
double interestd; // d的利息
double interestc; // c的利息
double sumd = 0;
double &sumc = depositc;
int year;
/*
//先计算出第一年的
sumd += depositd; // d的第一年本金加利息
interestd = interestrd * depositd; // d的固定利息
interestc = interestrc * depositc; // c的固定利息
sumc += interestc; // c的第一年的本金加利息
for (year = 2; sumc > sumd; ++year) // 因为计算了第一年,所以从第一年开始算,当c的总额大于d的时候,结束循环
{
sumd += interestd;
// **********************************************************************************************
interestc = interestrc * depositc; // 求出c的利息,利率* c的本金 // <- 就是错在这里了
// **********************************************************************************************
sumc += interestc;
}
*/
//
sumd = depositd; // d的第一年本金加利息
interestd = interestrd * depositd; // d的固定利息
year = 0;
while(true) { // 从第一年开始计算,不断循环,符合条件则用break跳出循环
year ++;
sumd += interestd; // d可以直接进行利息累加
sumc += sumc * interestrc; // c的计算方法是上一年的乘以利息再加上本金
cout << year << " year later, Daphne got " << sumd <<
" and Cleo got " << sumc << endl;
if(sumc > sumd) {
break;
}
}
cout << "Invest of Cleo: " << sumc << endl;
cout << "Invest of Daphne: " << sumd << endl;
cout << "After year: " << year << endl;
system("pause");
return 0;
}