C++primer记录
(仅记录,不保证正确与规范度,有问题请指正)
第三章编程题
1.已知1英尺=12英寸 用const表示转换因子
2. 各种转换 疑问点在于浮点数采用float还是double 不同类型对程序运行有什么具体影响
全使用float或double对结果无影响?
使用cout.setf尝试,同样的输入跟结果展示
4 long
第四章编程题
1、
注意使用cin.getline(),不然遇到空格会像书中例子一样出错
#include<iostream>
using namespace std;
struct student
{
char first_name[80];
char last_name[80];
char grade;
int age;
};
int main(){
student stu;
cout<<"What is your first name? ";
cin.getline(stu.first_name,80);
cout<<"What is your last name? ";
cin.getline(stu.last_name,80);
cout<<"What letter grade do you deserve? ";
cin>>stu.grade;
stu.grade+=1;
cout<<"What is your age? ";
cin>>stu.age;
cout<<"Name: "<<stu.last_name<<", "<<stu.first_name<<endl
<<"Grade: "<<stu.grade<<endl<<"Age: "<<stu.age<<endl;
return 0;
}
2、
使用string类,string类的I/O 读取一行字符串用getline(cin,str)
getline(cin,stu.first_name);
3、
考察char数组和cstring
#include<iostream>
#include<cstring>
using namespace std;
int main(){
const int max=50;
char F[max];
char L[max];
cout<<"Enter your first name:";
cin.getline(F,max);
cout<<"Enter your last name:";
cin.getline(L,max);
strcat(L,", ");
strcat(L,F);
cout<<"Here's the information in a single string: "<<L<<endl;
return 0;
}
4、
string,拼接字符串
#include<iostream>
#include<string>
using namespace std;
int main(){
string F;
string L;
cout<<"Enter your first name:";
getline(cin,F);
cout<<"Enter your last name:";
getline(cin,L);
L=L+", "+F;
cout<<"Here's the information in a single string: "<<L<<endl;
return 0;
}
5、
#include<iostream>
using namespace std;
struct CandyBar
{
string brand;
float weight;
int cal;
};
int main(){
CandyBar snack={
"Mocha Munch",
2.3,
350
};
cout<<"brand:"<<snack.brand<<endl<<"weight:"<<snack.weight
<<endl<<"calorie:"<<snack.cal<<endl;
return 0;
}
6、
偷懒用了个for循环
#include<iostream>
using namespace std;
struct CandyBar
{
string brand;
float weight;
int cal;
};
int main(){
CandyBar snack[3]={{"Mocha Munch",2.3,350},{"ddd",2.4,234},{"lll",2.5,345}};
for (int i = 0; i < 3; i++)
{
cout<<"brand:"<<snack[i].brand<<endl<<"weight:"<<snack[i].weight
<<endl<<"calorie:"<<snack[i].cal<<endl;
}
return 0;
}
7、
#include<iostream>
using namespace std;
struct Pizza
{
string brand;
float dia;
float weight;
};
int main(){
Pizza AAA;
cout<<"Enter brand:";
getline(cin,AAA.brand);
cout<<"Enter dia:";
cin>>AAA.dia;
cout<<"Enter wei:";
cin>>AAA.weight;
cout<<"brand:"<<AAA.brand<<endl<<"dia:"<<AAA.dia
<<endl<<"weight:"<<AAA.weight<<endl;
return 0;
}
8、
注意,先输入直径后加一个cin.get()
#include<iostream>
using namespace std;
struct Pizza
{
string brand;
float dia;
float weight;
};
int main(){
Pizza * AAA=new Pizza;
cout<<"Enter dia:";
cin>>AAA->dia;
cout<<"Enter brand:";
cin.get();
getline(cin,AAA->brand);
cout<<"Enter wei:";
cin>>AAA->weight;
cout<<"brand:"<<AAA->brand<<endl<<"dia:"<<AAA->dia
<<endl<<"weight:"<<AAA->weight<<endl;
delete AAA;
return 0;
}
9、
const int size =3;
CandyBar * snack=new CandyBar[size];
10、
#include<iostream>
#include<array>
using namespace std;
int main(){
array<float,3> grade;
float time=0;
int i=0;
for ( i = 0; i < 3; i++)
{
cout<<"enter your grade:";
cin>>grade[i];
time+=grade[i];
}
cout<<"cishu:"<<i+1<<endl;
cout<<"average:"<<time/3<<endl;
return 0;
}
第五章
1、
#include<iostream>
using namespace std;
int main(){
int a,b;
cout<<"enter two postive int:";
cin>>a;
cin>>b;
int sum=0;
for (int i = a; i <= b; i++)
{
sum+=i;
}
cout<<"sum:"<<sum<<endl;
return 0;
}
2、
#include<iostream>
#include<array>
using namespace std;
const int ArSize = 16;
int main(){
array<long double,ArSize> factorials;
factorials[1]=factorials[0]=1;
for (int i = 2; i < ArSize; i++)
factorials[i]=i*factorials[i-1];
for (int i = 0; i < ArSize; i++)
cout<<i<<"! = "<<factorials[i]<<endl;
return 0;
}
3、
#include<iostream>
using namespace std;
int main(){
int a=1;
int sum=0;
while (a!=0)
{
cout<<"enter number:";
cin>>a;
sum+=a;
cout<<"sum="<<sum<<endl;
}
return 0;
}
4、
#include<iostream>
using namespace std;
int main(){
int year=0;
double Daphne = 100;
double Cleo = 100;
while (Cleo<=Daphne)
{
year++;
Daphne += 10;
Cleo *= 1.05;
}
cout<<"After "<<year<<" years, Cleo more than Daphne."<<endl
<<"Cleo:"<<Cleo<<" dollar"<<endl<<"Daphne:"<<Daphne<<" dollar"<<endl;
return 0;
}
5、
#include<iostream>
using namespace std;
int main(){
string month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
int sum=0,a=0;
for (int i = 0; i < 12; i++)
{
/* code */
cout<<month[i]<<" sales: ";
cin>>a;
sum+=a;
}
cout<<"annual sales:"<<sum<<endl;
return 0;
}
6、
#include<iostream>
using namespace std;
int main(){
string month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
int sales[3][13]={0};
int sumall=0;
int sum = 0;
for (int i = 0; i < 3; i++)
{
sum=0;
for (int j = 0; j < 12; j++)
{
cout<<month[j]<<" sales: ";
cin>>sales[i][j];
sum+=sales[i][j];
}
sales[i][12]=sum;
sumall+=sum;
cout<<i+1<<" annual sales:"<<sales[i][12]<<endl;
}
cout<<"Three years sales: "<<sumall<<endl;
return 0;
}
7、
注意这个cin.get()不能少。
#include<iostream>
using namespace std;
struct car
{
string manu;
int year;
};
int main(){
int num=0;
cout<<"how many cars do you wish to catalog? ";
cin>>num;
car * ps=new car[num];
for (int i = 0; i < num; i++)
{
cout<<"Car #"<<i+1<<":"<<endl;
cout<<"Please enter the make: ";
cin.get();
getline(cin,ps[i].manu);
cout<<"Please enter the year made: ";
cin>>ps[i].year;
}
cout<<"Here is your collection:"<<endl;
for (int i = 0; i < num; i++)
{
cout<<ps[i].year<<" "<<ps[i].manu<<endl;
}
delete [] ps;
return 0;
}
8、
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char word[50]={};
int count = 0;
cout<<"Enter words:"<<endl;
while (strcmp(word,"done"))
{
cin>>word;
count++;
}
cout<<"You entered a total of "<<count-1<<" words."<<endl;
return 0;
}
9、
判断条件改一下就行
while (word!="done")
10、
嵌套循环
#include<iostream>
using namespace std;
int main(){
int rows = 0;
cout<<"Enter number of rows:";
cin>>rows;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j>i; j--)
{
cout<<".";
}
for (int k = 0; k < i; k++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
第六章
第一题没保存…
2、
cin表达式对于非数字输入为false
#include<iostream>
using namespace std;
const int Max = 10;
int main(){
int i=0;
double total=0;
double avg=0;
int count=0;
double num[Max]={0};
cout << "enter numbers: ";
for (i = 0; i < Max; i++)
{
if(!(cin>>num[i]))
break;
total+=num[i];
}
avg=total/i;
for (int j = 0; j < i; j++)
{
if(num[j]>avg)
count++;
}
cout<<"avg: "<<avg<<endl<<"how many: "<<count<<endl;
return 0;
}
3、
#include<iostream>
using namespace std;
int main(){
cout<<"Please enter one of the following choices: "<<endl
<<"c) carnivore\t\t"<<"p) pianist"<<endl
<<"t) tree \t\t"<<"g) game"<<endl;
char ch;
while (cin>>ch)
{
switch (ch)
{
case 'c':
cout<<"A maple is a carnivore."<<endl;
break;
case 'p':
cout<<"A maple is a pianist."<<endl;
break;
case 't':
cout<<"A maple is a tree."<<endl;
break;
case 'g':
cout<<"A maple is a game."<<endl;
break;
default:
cout<<"Please enter a c,p,t,or g: ";
}
}
return 0;
}
4、
函数写在后面,声明在前面。
#include<iostream>
using namespace std;
const int strsize=30;
const int size = 5;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main(){
char ch;
void display_bop(bop* a);
void display_name(bop* a);
void display_title(bop* a);
void display_pre(bop* a);
bop test[size]={
{ "Wimp Mache", "BOSS", "AS", 0 },
{ "Raki Rhodes", "Junior Programmer", "MA", 1 },
{ "Celia Laiter", "Manager", "MIPS", 2 },
{ "Hoppy Hipman", "Analyst Trainee", "CL", 1 },
{ "Pat Hand", "Student", "LOOPY", 2 }
};
cout<<"BOP: "<<endl
<<"a by name \t\t"<<"b by title"<<endl
<<"c by bopname\t\t"<<"d by pre"<<endl
<<"q.quit"<<endl;
cout<<"Enter your choice: ";
while (cin>>ch&&ch!='q')
{
switch (ch)
{
case 'a':
display_name(test);
break;
case 'b':
display_title(test);
break;
case 'c':
display_bop(test);
break;
case 'd':
display_pre(test);
break;
default:
break;
}
cout<<"Next choice: ";
}
cout<<"Bye!"<<endl;
return 0;
}
void display_bop(bop* a)
{
for (int i = 0; i < size; i++)
cout<<a[i].bopname<<endl;
}
void display_name(bop* a)
{
for (int i = 0; i < size; i++)
cout<<a[i].fullname<<endl;
}
void display_title(bop* a)
{
for (int i = 0; i < size; i++)
cout<<a[i].title<<endl;
}
void display_pre(bop* a)
{
for (int i = 0; i < size; i++)
{
switch (a[i].preference)
{
case 0:
cout<<a[i].fullname<<endl;
break;
case 1:
cout<<a[i].title<<endl;
break;
case 2:
cout<<a[i].bopname<<endl;
break;
}
}
}
5、
#include<iostream>
using namespace std;
int main(){
double tvarps=1;
double tax=0;
cout<<"enter your income: ";
while (cin>>tvarps&&tvarps>0)
{
if (tvarps-5000<0)
{
cout<<"your tax is "<<tax<<" tvarps"<<endl;
}
else if (tvarps-15000<0)
{
tax=(tvarps-5000)*0.1;
cout<<"your tax is "<<tax<<" tvarps"<<endl;
}
else if (tvarps-35000<0)
{
tax=1000+(tvarps-15000)*0.15;
cout<<"your tax is "<<tax<<" tvarps"<<endl;
}
else
{
tax=4000+(tvarps-35000)*0.2;
cout<<"your tax is "<<tax<<" tvarps"<<endl;
}
cout<<"Enter your income again: ";
}
return 0;
}
6、
#include<iostream>
#include<string>
using namespace std;
struct donation
{
string name;
double money;
};
int main(){
void display_Patrons(donation * a,int SIZE);
void display_GrandPatrons(donation * a,int SIZE);
int size = 0;
cout << "Enter numbers of patrons: ";
cin>>size;
donation * ps = new donation[size];
for (int i = 0; i < size; i++)
{
cout<<"Enter name:";
cin>>ps[i].name;
cout<<"Enter money:";
cin>>ps[i].money;
}
cout<<"Grand Patrons"<<endl;
display_GrandPatrons(ps,size);
cout<<"Patrons"<<endl;
display_Patrons(ps,size);
delete [] ps;
return 0;
}
void display_GrandPatrons(donation * a,int SIZE)
{
int count=0;
for (int i = 0; i < SIZE; i++)
{
if(a[i].money>10000)
cout<<a[i].name<<"\t\t"<<a[i].money<<endl;
count++;
}
if(count==0)
cout<<"none"<<endl;
}
void display_Patrons(donation * a,int SIZE)
{
int count=0;
for (int i = 0; i < SIZE; i++)
{
if(a[i].money<10000)
{
cout<<a[i].name<<"\t\t"<<a[i].money<<endl;
count++;
}
}
if(count==0)
cout<<"none"<<endl;
}
8、
没有异常处理!!!很坏的习惯!
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main(){
ifstream inFile;
char filename[20];
cout<<"Please enter name of data file: ";
cin.getline(filename,20);
inFile.open(filename);
if (!inFile.is_open())
{
cout<<"Could not open the file "<<filename<<endl;
exit(EXIT_FAILURE);
}
int count=0;
char ch;
inFile >> ch; //同下理
while (inFile.good())
{
++count; //++count是因为文件为空的时候count不出错
inFile >> ch;
}
cout<<count<<endl;
inFile.close();
return 0;
}
9、
别的逻辑没改,文件数据读取部分如下。
ifstream inFile;
char filename[20];
cout<<"Please enter name of data file: ";
cin.getline(filename,20);
inFile.open(filename);
if (!inFile.is_open())
{
cout<<"Could not open the file "<<filename<<endl;
exit(EXIT_FAILURE);
}
inFile>>size;
inFile.get(); //吃掉换行符
donation * ps = new donation[size];
for (int i = 0; i < size; i++)
{
getline(inFile,ps[i].name);//inFile已经到下一行
inFile>>ps[i].money;
inFile.get(); //读取完数字后吃掉换行符
}
第七章
1、调和平均数计算
#include<iostream>
using namespace std;
double avg(double x,double y);
int main(){
cout<<"Please enter two number: ";
double x,y,result;
while (cin>>x>>y&&x>0&&y>0)
{
result=avg(x,y);
cout<<result<<endl;
cout<<"Please enter two number: ";
}
return 0;
}
double avg(double x,double y)
{
double avg=0;
avg=2.0*x*y/(x+y);
return avg;
}
2、
#include<iostream>
using namespace std;
const int size=10;
int enter(double a[]);
void display(double a[],int b);
double cal(double a[],int b);
int main(){
int i=0;
double a=0;
double golf_score[size];
i=enter(golf_score);
display(golf_score,i);
a=cal(golf_score,i);
cout<<"your avg is "<<a<<endl;
return 0;
}
int enter(double a[])
{
cout<<"please enter your golf score(输入非数字结束):";
int g=0;
for (int i = 0; i < size; i++)
{
if(cin>>a[i])
g++;
else
break;
}
return g;
}
void display(double a[],int b)
{
cout<<"your golf score is ";
for (int i = 0; i < b; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
double cal(double a[],int b)
{
double sum=0;
double avg=0;
for (int i = 0; i < b; i++)
{
sum+=a[i];
}
avg=sum/b;
return avg;
}
3、
a、按值传输
void display(box a)
b、传递box结构地址
void display(box * a)
c、
#include<iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void display(box a);
void cal(box * a);
int main(){
box b1={"aaaa",4,5,6,90};
display(b1);
cal(&b1);
display(b1);
return 0;
}
void display(box a)
{
cout<<a.maker<<endl<<a.height<<endl<<a.length<<endl<<a.width<<endl<<a.volume<<endl;
}
void cal(box * a)
{
a->volume=a->height*a->length*a->width;
}
4、
#include<iostream>
using namespace std;
long double probability(unsigned numbers,unsigned picks,unsigned numbers2,unsigned specical);
int main(){
double total1,choices1,total2,choices2;
cout<<"enter the total1,choices,,total2,choices2:";
while ((cin>>total1>>choices1>>total2>>choices2)&&choices1<=total1&&choices2<=total2)
{
cout<<"You have one chance in "<<probability(total1,choices1,total2,choices2)
<<" of winning.\n";
}
cout <<"bye!\n";
return 0;
}
long double probability(unsigned numbers,unsigned picks,unsigned numbers2,unsigned specical)
{
long double result = 1.0;
long double n;
unsigned p;
for (n=numbers,p=picks; p>0; n--,p--)
{
result*=n/p;
}
for (n=numbers2,p=specical; p>0; n--,p--)
{
result*=n/p;
}
return result;
}
5、
算到20!
#include<iostream>
using namespace std;
long long cal(int a);
int main(){
int b;
long long total;
cout<<"Please enter a num:";
while (cin>>b)
{
total=cal(b);
cout<<b<<"!= "<<total<<endl;
}
return 0;
}
long long cal(int a)
{
long long sum=a;
if (a>1)
sum=sum*cal(a-1);
else
sum=1;
return sum;
}
6、
#include<iostream>
using namespace std;
int Fill_array(double a[],int size);
void Show_array(double a[],int true_size);
void Reverse_array(double a[],int true_size);
int main(){
int SIZE=0;
int True_s=0;
cout<<"enter size of array: ";
cin>>SIZE;
double * arr=new double [SIZE];
True_s=Fill_array(arr,SIZE);
Show_array(arr,True_s);
Reverse_array(arr,True_s);
Show_array(arr,True_s);
Reverse_array(arr+1,True_s-2); //除去第一个元素和最后一个
Show_array(arr,True_s);
delete [] arr;
return 0;
}
int Fill_array(double a[],int size)
{
int i=0;
cout<<"Please double:"<<endl;
for ( i; i < size; i++)
{
if (!(cin>>a[i]))
{
break;
}
}
return i;
}
void Show_array(double a[],int true_size)
{
for (int i = 0; i < true_size; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void Reverse_array(double a[],int true_size)
{
double tmp = 0;
for (int i = 0, j =true_size-1; i < j; i++,j--)
{
tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}
7、
double * Fill_array(double * begin,double * end)
{
double * ps;
cout<<"Please double:"<<endl;
for ( ps=begin; ps < end; ps++)
{
if (!(cin>>*ps))
{
break;
}
}
return ps;
}
void Show_array(double * begin,double * true_end)
{
for (double * ps=begin; ps < true_end; ps++)
{
cout<<*ps<<" ";
}
cout<<endl;
}
void Reverse_array(double * begin,double * true_end)
{
double *ps=begin;
double *pa=true_end-1;
double tmp = 0;
for (ps, pa; ps<pa; ps++,pa--)
{
tmp=*ps;
*ps=*pa;
*pa=tmp;
}
}
8、
double数组
#include<iostream>
using namespace std;
const int Seasons = 4;
const char * Snames[Seasons] = {"Spring","Summer","Fall","Winter"};
void fill(double pa[]);
void show(double* pa);
int main(){
double expenses[Seasons]={0};
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* pa)
{
double total=0;
cout<<"\nEXLENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout<<Snames[i]<<": $"<<pa[i]<<endl;
total+=pa[i];
}
cout <<"Total Expenses: $"<<total<<endl;
}
结构
struct exp
{
double exp_sea;
};
void fill(exp pa[]);
void show(exp pa[]);
9、
#include<iostream>
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 sizer: ";
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 i=0;
for (i; i < n; i++)
{
cout<<"enter students fullname: ";
cin.getline(pa[i].fullname,SLEN);
if(pa[i].fullname[0]=='\0')
break;
cout<<"enter students hobby: ";
cin.getline(pa[i].hobby,SLEN);
cout<<"enter students ooplevel: ";
cin>>pa[i].ooplevel;
cin.get();
}
return i;
}
void display1(student st)
{
cout<<st.fullname<<endl;
cout<<st.hobby<<endl;
cout<<st.ooplevel<<endl;
}
void display2(const student * ps)
{
cout<<ps->fullname<<endl;
cout<<ps->hobby<<endl;
cout<<ps->ooplevel<<endl;
}
void display3(const student pa[],int n)
{
for (int i = 0; i < n ;i++)
{
cout<<pa[i].fullname<<endl;
cout<<pa[i].hobby<<endl;
cout<<pa[i].ooplevel<<endl;
}
}
10、
#include<iostream>
using namespace std;
double add(double x,double y){return x+y;}
double minuss(double x,double y){return x-y;}
double multi(double x,double y){return x*y;};
double calculate(double a,double b,double (*ps)(double,double)){return ps(a,b);}
int main(){
double x=0,y=0;
double (*pf[3])(double,double)={add,minuss,multi};
string name[3]={"add","minuss","multi"};
cout<<"Please enter two num: ";
while (cin>>x>>y)
{
for (int i = 0; i < 3; i++)
{
cout<<name[i]<<" two number is: "<<calculate(x,y,pf[i])<<endl;
}
cout<<"Please enter two num: ";
}
return 0;
}