您的位置:首页 > 编程语言 > C语言/C++

C++PRIMER PLUS第六版课后编程答案 5.6-510

2014-03-27 16:11 609 查看
5.6
#include <iostream>
#include <string>
void main56()
{
using std::cout;
using std::cin;
using std::string;

string m[12]={"1","2","3","4","5","6","7","8","9","10","11","12"};
const string *s=m;
int arr[3][15];
int sum=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<12;j++,s++)
{
cout<<"Please enter the "<<i+1<<" years "<<*s<<" moth sales:";
cin>>arr[i][j];
sum+=arr[i][j];
cout<<"Now sum is "<<sum<<"\n";
}
s=m;//重新令s指向m的开头
}
cout<<"All sum is "<<sum<<", THE END\n";
cin.get();
}


5.7

#include <iostream>
#include <string>
using namespace std;
struct car{
string name;
int year;

};
void get(car *);
void show(const car const *);

void main57()
{
cout<<"How many cars do your wish to catalog?";
int num;
cin>>num;
car *c=new car[num];
for(int i=0;i<num;i++,c++)
{
cout<<"Car #"<<i+1<<":"<<endl;
get(c);
show(c);

}
cin.get();
}
void get(car *c)
{
cin.get();
cout<<"Please enter the make:";
string name;
getline(cin,name);
cout<<"\nplease enter the years of make:";
int y;
cin>>y;
c->name=name;
c->year=y;

}

void show(const car const *c)
{
cout<<"/nHere is your collection: ";
cout<<c->year<<"  "<<c->name<<endl;
}


5.8有点BUG,详看5.9,我懒得改了

#include <iostream>
#include <cstring>
using namespace std;
void main58()
{

char test[20];
int count=0;
char ch;
int i=0;
cout<<"Enter words (to stop,type the word done):";
//cin.get();
while(strcmp(test,"done")!=0)
{
//cout<<"is in"<<endl;
//cin.get(ch)>>test[i];
cin.get(ch);
if(ch==' ')
{
test[i]='\0';
count++;
//cout<<"i=0"<<test<<"\ncount="<<count;
i=0;

}
else
{
test[i]=ch;
test[i+1]='\0';
//cout<<"i++"<<test<<endl;
i++;
}
}
cout<<"You entered a total of "<<count<<" words";
cin.get();

}


5.9(有错误,看下一道正确的)

#include <iostream>
#include <string> //cstring 没有定义string类型的符号运算符,例如==,!=

//要注意输入是这种情况  doneff ajgk done,这时候,要注意doneff的判断 增加flag量
using namespace std;
void main59()
{

string test="";
string t="done";
//if(test==t)

char ch;
int count=0;
int flag=1;
cout<<"Enter words (to stop, type the word done):"<<endl;
//cin.get();
while(test!=t)
{
//cout<<"Test2="<<test<<"  Count="<<count<<endl;
//cout<<"here"<<endl;
flag=1;
while(flag==1)
{
cin.get(ch);
if(ch=='\n')	//回车键的表示
break;
else if(ch!=' ')
{

test=test+ch;

}
else
{
test="";
count++;
flag=0;
}
//cout<<"test1="<<test<<" count="<<count<<endl;
//cout<<"in in"<<endl;
}
}
cout<<"You enter a total of "<<count<<" words";
cin.get();

}

5.9(正确版)

#include <iostream>
#include <cstring>
//#include <cctype>
using namespace std;
int main()
{
string test;
cout<<"Enter words (to stop ,type the word done)"<<endl;
int counts=0;
do{
cin>>test;
if(strcmp(test.c_str(),"done")==0)
break;
else
counts++;
}while(true);
cout<<"You entered a total of "<<counts<<" words"<<endl;

}运行截图:



感谢网友的指出! --------2014.10.8

5.10

#include <iostream>

void main510()
{
using namespace std;
int row;
cout<<"Enter number of rows:";
cin>>row;
for(int i=0;i<row;i++)
{
for(int j=0;j<row;j++)
{
if(j<row-i-1)
cout<<".";
else
cout<<"*";
}
cout<<endl;
}
cin.get();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: