第七题
题意:
翻译:
第二题
题意:
翻译:
//可以加上头文件用来存放枚举类型来实现打印画的材质
#include<iostream>
#include<string>
#include<cctype>
#include<fstream>
using namespace std;
struct Size
{
int len; //10cm
int weid; //10cm
};
struct gallery
{
string ArtistName;
string Title;
Size s;
};
ofstream fout;
void Medium(string);
int main()
{
string artist;
string med;
gallery artist1;
artist1.ArtistName="CuiXinChun";
artist1.s.len=10;
artist1.s.weid=10;
artist1.Title="RGZN NB!!!";
cout<<"please enter the name of the paintings' artist"<<endl;
cin>>artist;
if(artist=="CuiXinChun")
{
cout<<"The name is "<<artist1.ArtistName<<endl;
cout<<"His painting is "<<artist1.Title<<endl;
cout<<"The painting's lengh is "<<artist1.s.len<<endl;
cout<<"The painting's weigth is "<<artist1.s.weid<<endl;
}
cout<<"please enter the name of the paintings' medium"<<endl;
cin>>med;
Medium(med);//输入到文件了,而且只能输入一次
while(1);
return 0;
}
void Medium(string med)
{
fout.open("Art.txt");
if(!fout.is_open())
{
cout<<"NO FILE"<<endl;
}
switch(med[0])
{
case 'o':
fout<<"oil"<<endl;
break;
case 'w':
fout<<"watercolor"<<endl;
break;
case 'p':
fout<<"pastel,print,"<<endl;
break;
case 'a':
fout<<"acrylic"<<endl;
break;
case 'c':
fout<<"color photo"<<endl;
break;
case 'b':
fout<<"black and white photo"<<endl;
break;
}
fout.close();
}
第四题
- Programming Problems 第4 题回文(Palindrome)的判断。在本题扩展了回文的定义的情况下,从性能和算法等方面考虑该题目的进一步改进,并与第六章的实验题目对比。
题意:
译文:
代码如下:
//This programm reads in a string from the KBD, AND ENDS AT reading a special CHAR
//AND JUDGE WHETHER IT IS A PALINDROME ACCORDING TO THE EXTENDENDED DEFINITION
//May 30, 2010
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
int i,j,k=0,count,length;
i=0;
count=0;
string::size_type len;
string inputstr;
char StrReadIn[30];
char StrPured[30],c[30];
//Enter a string AND ENDS AT reading a special CHAR,here '#'
cout<<"Enter string, '#' to end:";
//getline(cin,a);
cin.get(StrReadIn[i]);
cout<<StrReadIn[i];
while ((i<30) && (StrReadIn[i]!='#'))
{
cin.get(StrReadIn[++i]);
cout<<StrReadIn[i];
}
cout<<"The length is :" <<i<< endl;
j=0; k=0;
while(j<i)
{
if ((StrReadIn[j]<='Z' && StrReadIn[j]>='A')||
(StrReadIn[j]<='z' && StrReadIn[j]>='a')||
(StrReadIn[j]<='9' && StrReadIn[j]>='0'))
StrPured[k++]=StrReadIn[j++];
else j++;
}
cout<<"Pure String is :"<<endl;
for(j=0;j<k;j++)
cout<<StrPured[j];
cout<<endl<<"Reverse string:"<<endl;
for(j=k-1;j>=0;j--)
cout<<StrPured[j];
for(i=0,j=k-1;i<j;i++,j--)
if(StrPured[i]!=StrPured[j])
{
cout<<" It is not a palindrome."<<endl;
return 0;
}
cout<<endl<<" It is a palindrome."<<endl;
return 0;
}
运行结果:
P508 4
题意: