第五章
1.
#include<iostream>
usingnamespacestd;
intmain()
{
inta,b,c;
intsum=0;
cout<<"Pleaseinputtwonumbers:"<<"a=_\b";
cin>>a;
cout<<"b=_\b";
cin>>b;
if(a>b)swap(a,b);
c=a;
while(c>=a&&c<=b)
{
sum+=c;
c++;
}
cout<<endl<<"Theresultis:"<<sum;
return0;
}
2.
#include<iostream>
#include<array>
#include<limits>
usingnamespacestd;
constintArsize=100;
intmain()
{
cout<<"thebiggestnumberofLONGLONG:"<<LONG_LONG_MAX<<endl;
array<longdouble,101>factorials={0};
factorials[0]=factorials[1]=1;
for(inti=2;i<=Arsize;i++)
factorials[i]=factorials[i-1]*i;
cout<<"100!="<<factorials[100]<<endl;
return0;
}
3.
#include<iostream>
usingnamespacestd;
intmain()
{
cout<<"Pleaseinputthenumber:"<<endl;
inta,sum;
sum=0;
while((cin>>a)&&(0!=a))
{
sum+=a;
cout<<"therealtimeresultis:"<<sum<<endl;
}
return0;
}
4.
#include<iostream>
usingnamespacestd;
intmain()
{
inta,b;
cout<<"PleaseinputthemoneyDaphneinput: ";
cin>>a;
cout<<endl<<"PleaseinputthemoneyCleoinput:";
cin>>b;
intsuma=a,sumb=b;
inti=0;
while(suma>=sumb)
{
suma+=0.1*a;
sumb+=0.05*sumb;
i+=1;
}
cout<<endl<<i<<"yearslaterCleowillpassDaphe"<<endl;
cout<<"Daphe'smoneyis"<<suma<<endl;
cout<<"Cleo'smoneyis"<<sumb<<endl;
return0;
}
5.
#include<iostream>
usingnamespacestd;
//char*months[12]=.....不好,用指针先定义地址,再定义内容,虽然这里这样也可以运行
stringmonths[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
intmain()
{
intsale[12];
inti=0,sum=0;
while(i<12 )
{
cout<<"inputthesalesof"<<months[i]<<":";
cin>>sale[i];
sum+=sale[i];
i++;
}
cout<<"thetotalsaleofthisyearis"<<sum<<endl;
return0;
}
8.
#include<iostream>
#include<cstring>
usingnamespacestd;
intmain()
{
chara[20];
inti=0;
cout<<"Enterwords(tostop,typetheworddone):"<<endl;
// constchar*a="done";
//cout<< strcmp(a,"done");//测试如果相等,返回什么值
cin>>a;
while(0!=strcmp(a,"done"))
{
i++;
cin>>a;
}
cout<<"Youenteredatotalof"<<i<<"words."<<endl;
return0;
}
9.
#include<iostream>
#include<string>
usingnamespacestd;
intmain()
{
stringa;
inti=0;
cout<<"Enterwords(tostop,typetheworddone):"<<endl;
cin>>a;
while("done"!=a)
{
i++;
cin>>a;
}
cout<<"Youenteredatotalof"<<i<<"words."<<endl;
return0;
}
10.
#include<iostream>
usingnamespacestd;
intmain()
{
inta,m,n;
cout<<"Enterthenumberofrows:";
cin>>a;
cout<<endl;
intsum=a;
while(a>0)
{
n=a-1;
m=sum-n;
while(n>0)
{
cout<<".";
n--;
}
while(m>0)
{
cout<<"*";
m--;
}
cout<<endl;
a--;
}
return0;
}
第六章
1.
#include<iostream>
#include<cstring>
#include<cctype>
usingnamespacestd;
intmain()
{
charinfo[100];
inti=0;
cin.get(info[i]); //应get,cin的话空格不处理
while(info[i]!='@')
{
if(islower(info[i]))
info[i]=toupper(info[i]);
elseif(isupper(info[i]))
info[i]=tolower(info[i]);
if(info[i]<'9'&&info[i]>'0') //注意找出数字。加单引号,不加的话就去找asc码去了
{cin.get(info[i]);continue;} //得用get把数字读过,否则一直卡在数字这里循环
i++;
cin.get(info[i]);
}
info[i+1]='\0'; //加结束符,要不数组读后面,可能上次程序的结果写入了相应的内存地址
cout<<info<<endl;
return0;
}
2.
#include<iostream>
#include<cstring>
#include<cctype>
usingnamespacestd;
intmain()
{
doubledona[10];
inti=0,j=0;
doublesum=0;
doubleave;
while(cin>>dona[i])
{
sum+=dona[i];
i++;
}
ave=sum/i;
for(intn=0;n<i;n++)
{
if(dona[n]>ave)
j++;
}
cout<<"membersare";
for(intn=0;n<i;n++)
cout<<dona[n]<<"";
cout<<endl<<"averageis"<<ave<<endl;
cout<<"Thereare"<<j<<"numberslargerthanaverage";
return0;
}
3.
#include<iostream>
usingnamespacestd;
intmain()
{
cout<<"Pleaseenteroneofthefollowingchoices:"<<endl;
cout<<"c)carnivore p)pianist"<<endl;
cout<<"t)tree g)game"<<endl;
cout<<"f"<<endl;
cout<<"Pleaseenter c,p,t,org:";
// intinc;
charinc;
intflag=0;
//enum {c,p,t,g,f}; //这样下面casec中的c等字母就不用加单引号,但是输入0123时才行枚举是整形,同时inc需要改成int
while(0==flag)
{
cin>>inc;
switch(inc)
{
case'c':cout<<"youselectcarnivore"<<endl;flag=1;break;
case'p':cout<<"youselectpianist"<<endl;flag=1;break;
case't':cout<<"youselecttree"<<endl;flag=1;break;
case'g':cout<<"youselectgame"<<endl;flag=1;break;
case'f':cout<<"youselectf"<<endl;flag=1;break;
default:cout<<endl<<"Pleaseenter c,p,t,org:";
}
}
return0;
}
4.
#include<iostream>
usingnamespacestd;
#definestrsize30
//constintstrsize=30;
structbop{
charfullname[strsize];
chartile[strsize];
charbopname[strsize];
intpreference;
};
intmain()
{
cout<<"Pleaseenteroneofthefollowingchoices:"<<endl;
cout<<"a.displaybyname b.displaybytitle"<<endl;
cout<<"c.displaybybopname d.displaybypreference"<<endl;
cout<<"q.quit"<<endl;
bopa[6]={
{"WimpMacho","title1","bopname1",0},{"RakiRhodes","title2","bopname2",1},
{"RakiRhodes","title3","bopname3",2},{"Celialaiter","title4","bopname4",2},
{"HoppyHipman","title5","bopname5",0},{"PatHand","title6","bopname6",1}
};
charinc;
cout<<"Enteryourchoice:";
cin>>inc;
while(inc!='q')
{
switch(inc)
{
case'a':
for(inti=0;i<6;i++)
cout<<a[i].fullname<<endl;
break;
case'b':
for(inti=0;i<6;i++)
cout<<a[i].tile<<endl;
break;
case'c':
for(inti=0;i<6;i++)
cout<<a[i].bopname<<endl;
break;
case'd':
for(inti=0;i<6;i++)
{
if(0==a[i].preference)
cout<<a[i].fullname<<endl;
if(1==a[i].preference)
cout<<a[i].tile<<endl;
if(2==a[i].preference)
cout<<a[i].bopname<<endl;
}
break;
default:cout<<endl<<"Pleaseenter a,b,c,dorq:";
}
cout<<"Nextchoice:";
cin>>inc;
}
cout<<"Bye!";
return0;
}
5.
#include<iostream>
usingnamespacestd;
//注意这里默认是整型,若输入带小数的,例如5.6,cin读入Int5,后又读入.所以会出现读入一次后推出,若要适用于double的,可把earn改为double
intmain()
{
intearn;
intsumtax;
cout<<"Pleaseinputyourearn:";
while(cin>>earn&&earn>=0)
{
sumtax=0;
if(earn<5000)
{
sumtax=0;
cout<<"thetaxyoushouldgiveis:"<<sumtax<<endl;
}
elseif(earn>5000&&earn<=15000)
{
sumtax= (earn-5000)*0.1;
cout<<"thetaxyoushouldgiveis:"<<sumtax<<endl;
}
elseif(earn>15000&&earn<=35000)
{
sumtax=10000*0.1+(earn-15000)*0.15;
cout<<"thetaxyoushouldgiveis:"<<sumtax<<endl;
}
else{
sumtax=10000*0.1+20000*0.15+(earn-35000)*0.2;
cout<<"thetaxyoushouldgiveis:"<<sumtax<<endl;
}
cout<<"inputyourearnagain:";
}
cout<<"Bye!";
return0;
}
6.
#include<iostream>
usingnamespacestd;
structdonation
{
stringname;
doubledonate;
};
intmain()
{
intn;
cout<<"Pleaseinputthenumberofdonations:";
cin>>n;
donation*p=newdonation[n];
cout<<endl<<"Pleaseinputthenamesandthemoney:"<<endl;
inti=0;
while(i<n)
{
cin>>p[i].name;
cin>>p[i].donate;
i++;
}
cout<<"***************GrandPatrons******************"<<endl;
while(i>=0)
{
if(p[i].donate>=10000)
cout<<p[i].name<<" "<<p[i].donate<<endl;
i--;
}
cout<<"Bye!";
return0;
}
7.
#include<iostream>
//#include<cstring>
usingnamespacestd;
intmain()
{
intm=0,n=0,k=0;
// chars[20];//用c风格字符串,while比较不好比,s[0]='q'会对以q开头的单词判断为结束,所以用cstring中的函数,较string麻烦些
strings;
cout<<"Enterwords(qtoquit):"<<endl;
cin>>s;
// while(strcmp(s,"q")!=0)
while(s!="q")
{
if(isalpha(s[0]))
{
if('a'==s[0]||'e'==s[0]||'i'==s[0]||'o'==s[0]||'u'==s[0]||'A'==s[0]||'E'==s[0]||'I'==s[0]||'O'==s[0]||'U'==s[0])
m++;
elsen++;
}
else
k++;
cin>>s;
}
cout<<m<<"wordsbeginningwithvowels"<<endl;
cout<<n<<"wordsbeginningwithconsonants"<<endl;
cout<<k<<"others"<<endl;
cout<<"Bye!";
return0;
}
8.
#include<iostream>
#include<fstream>
#include<cstdlib>//forexit
usingnamespacestd;
//注意文本放在debug生成的exe(可执行文件)同文件夹下,Qt放在Makefile所在的文件夹下
intmain()
{
intn=0;
ifstreamintext;
// intext.open("D:\\fan.txt");//放在D盘根目录下
intext.open("fan.txt");
if(!intext.is_open())
{
cout<<"couldn'topenthefile!"<<endl;
exit(EXIT_FAILURE);
}
charc;
// while(intext.good()) //good/eof是标志位,有相关行为会置位,但么有读入行为,这里也可以用good,确保输入正确,然后下面用if判断是达到末尾,也可以while((ch =cin.get())!=EOF)
while(!intext.eof())
{
n++;
intext.get(c);
cout<<c;
}
// if(intext.eof())
// cout<<"Endoffilereached."<<endl;
// else
// cout<<"Somethingbadhappened."<<endl;
intext.close();
cout<<"Thenumberofthetextis"<<n<<endl;
return0;
}
9.
#include<iostream>
#include<fstream>
#include<stdlib.h> //atof用到
usingnamespacestd;
structdonation
{
stringname;
doubledonate;
};
intmain()
{
intn;
ifstreamf;
f.open("fan.txt");
cout<<"readinformationfromtext..."<<endl;
f>>n;
cout<<"nis"<<n<<endl;
donation*p=newdonation[n];
f.get(); //这个太重要了,注意这种cin后面跟getline的,getline会先接受到回车符
inti=0;
while(i<n)
{
getline(f,p[i].name);
cout<<p[i].name;
chars[20];
f.getline(s,20,'\n');
p[i].donate=atof(s); //stringconverttodouble
//也可以不用上面的atof函数,用下面这样的,在读入时控制格式,只是注意换行符的处理
// f>>p[i].donate;
// f.get();
cout<<p[i].donate<<endl;
i++;
}
cout<<"***************GrandPatrons******************"<<endl;
while(i>=0)
{
if(p[i].donate>=10000)
cout<<p[i].name<<" "<<p[i].donate<<endl;
i--;
}
f.close();
cout<<"Bye!";
return0;
}
//fan.txt
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000