(2011.08.02)自学《C++ 程序设计》(谭浩强 编著)时做过的习题汇总。

来源:互联网 发布:商场抽奖软件破解版 编辑:程序博客网 时间:2024/05/16 19:39

在学习这本书的时候,开始的时候是老师教的,但是自从第二章以后,全是自学的了,一年结束了,这本书也自学完了,下面是自学了两个学期我所做过的习题,温故而知新!

//第三章,第3题,输入一个华氏温度,要求输出摄氏温度。公式为C=(5/9)*(F-32),输出要有文字说明,取两位小数。#include <iostream>#include <iomanip>using namespace std;int main(){//输入float F;cout << "请输入一个华氏温度: ";cin >> F;cout << endl;//计算    float C;C = float(5/9.0)*(F-32);//输出cout << "由该华氏温度转换所得的摄氏温度是:";cout << setiosflags(ios::fixed) << setprecision(2) << C <<endl <<endl;return 0;}


/*第三章,第四题用getchar函数读入两个字符给c1,c2,然后分别用putchar函数和cout语句输出这两个字符。并思考以下问题:1.变量c1,c2应定义为字型还是整型?还是二者皆可?2.若要求输出c1和c2值的ASC2码,应如何处理?*/#include <iostream>using namespace std;int main(){char c1,c2;//输入cout << "请输入c1和c2:";c1 = getchar();  c2 = getchar();      //注意,回车也是一个字符cout << endl;    //输出    cout << "使用putchar输出:";    putchar(c1);putchar('\t');putchar(c2);putchar('\n');cout << "使用cout输出:";cout << c1 << '\t' << c2 << endl;//输出ASC2码    cout << "转换输出:";    cout << int(c1) << '\t' << int(c2) << endl << endl;return 0;}// putchar只能输出一个字符/*  getchar 由宏实现:#define getchar() fgetc(stdin)。getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).当用户键入回车之后, getchar才开始从stdio流中每次读入一个字符.getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续 getchar 调用读取.也就是说, 后续的 getchar 调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键.   */

//10.有一个函数:y=x,x<1   y=2x-1  1-10  ,y=3x-11  x>10  写一个程序,输入x,输出y值。#include <iostream>using namespace std;int main(){   double x,y;   cout << "请输入x的值: ";   cin >> x;   cout << endl << "该函数的计算结果:y=";   if (x<1)   y=x,   cout << y << endl;   else if (x>=1&&x<10)   y = 2*x-1,   cout << y << endl;   else    y = 3*x-11,   cout << y << endl;   cout << endl;return 0;}


//第三章,第11题...用switch/*给出一个百分制的成绩,要求输出成绩等级'A','B','C','D','E'。  90分以上为'A',  80~89分为'B',  70~79分为'C',  60~69分为'D',  60分以下为'E'.  */#include <iostream>using namespace std;int main(){//输入成绩int s;cout << "请输入该学生的成绩:";cin >> s;cout << endl << endl;cout << "该学生的成绩等级为:";//成绩转化为判断数字if (s==100)s=9;else if (s>100);else if (s<0);else s = s/10;  //求出十位数的数字 //判断及输出switch (s){     case 9 : cout<<"A"; break;   case 8 : cout<<"B"; break;   case 7 : cout<<"C"; break;   case 6 : cout<<"D"; break;   default: cout<<"E"; break; }cout << endl << endl; return 0;}



//第三章,12题//给出一个不多于5位的正整数,要求:1.求出它是几位数2.分别打印出每一位数字;3.按逆序打印出各位数字,例如原数为321,应输出123.#include <iostream>using namespace std;int w;//用作第三问,确定位数输出数字int main(){//函数声明int weishu( int a);int dayin( int a);//输入整数dingwei:int n;cout << "请输入一个不多于5位的正整数:";cin >> n;    cout << endl;if (n>=100000) {cout<<"输入错误,请重新输入"<<endl;goto dingwei;}else if (n<=0) {cout<<"输入错误,请重新输入"<<endl;goto dingwei;}    else w=weishu(n),dayin(n);return 0;}//1.求出它是几位数 int weishu( int a){if (a <=99999 && a>=10000) a=5;else if (a <= 9999 && a>=1000) a=4;else if (a <= 999 && a>=100) a=3;else if (a<=99 && a>=10) a=2;else a=1;cout<<"这个正整数的位数是:"<< a <<endl;return a;}//2.分别打印出每一位数字 int dayin( int a){int a5;int a4;int a3;int a2;int a1;//数字提取a5 = a/10000;a4 = a/1000%10;a3 = a/100%100%10;a2 = a/10%1000%100%10;a1 = a%10000%1000%100%10;//数字输出cout<<"分别打印出各个数字:"<<'\t';if (a5==0);elsecout << a5 << '\t';if (a4==0);elsecout << a4 << '\t';if (a3==0);elsecout << a3 << '\t';if (a2==0);elsecout << a2 << '\t';if (a1==0);elsecout << a1 << endl;//3.按逆序打印出各位数字switch(w){case 1 : cout<<a1; break;case 2 : a = a1*10+a2,cout<<a; break;case 3 : a = a1*100+a2*10+a3,cout<<a; break;case 4 : a=a1*1000+a2*100+a3*10+a4,cout<<a; break;default: a=a1*10000+a2*1000+a3*100+a4*10+a5,cout<<a; break;}cout << endl;return 0;}



//第三章,14题,输入4个整数,要求按由小到大的顺序输出。#include<iostream>using namespace std;int main(){cout << "请输入四位整数,用空格间开:";int a[4];int i;int t;int j;for (j=0; j<4; j++)//输入四位整数cin >> a[j];cout << endl;for (j=0;j<4;j++){for (i=0;i<3;i++){if(a[i]>a[i+1])t = a[i+1],a[i+1] = a[i],a[i] = t;}}cout << "将数字从小到大的顺序输出:";for (j=0; j<4; j++)cout << a[j] << '\t';cout << endl;return 0;}



//17.求Sn=a+aa+aaa+....+aa...a(n个a)之值,其中a是一个数字,n由键盘输入#include <iostream>#include <cmath>using namespace std;int main(){long int a=2;long int n;long int y;long int i;long int sum;long int y3;cout << "现a=2,Sn=a+aa+aaa+...+aa...a(n个a)\n请输入正整数n:";cin >> n;cout << endl  << "计算结果是:Sn=";for(i=1,sum=0,y3=0;i<=n;i++)y = a*pow(10,i-1),y3 = y3+y,sum = y3 + sum,cout << y3 <<"+";cout << '\b' << "=" << sum << endl;return 0;}


//求n的阶乘#include <iostream>using namespace std;int main(){int n;int i;int sum;int x;cout << "请输入正整数 n=";cin >> n;    for(i=1, x=1, sum=0; i<=n; i++)x = x*i,sum = x+sum;cout << endl  << "n的阶乘等于 " << sum  << endl;return 0;}



//第四章第01题,分别求出两个整数的最大公约数和最小公倍数,用主函数调用两个函数,并输出结果,两个整数由键盘输入。/*       最小公倍数_--解法一  借助最大公约数求最小公倍数步骤:一、利用辗除法或其它方法求得最大公约数;二、 最小公倍数等于两数之积除以最大公约数。           最小公倍数--解法二质因数分解举例:12和27的最小公倍数   12=2×2×3   27=3×3×3必须用里面数字中的最大次方者,像本题有3和3的立方,所以必须使用3的立方(也就是3*3*3),不能使用3 所以:   2×2×3×3×3=4×27=108两数的最小公倍数是108     最大公约数原理  如果有一个自然数a能被自然数b整除,则称a为b的倍数,b为a的约数。几个自然数公有的约数,叫做这几个自然数的公约数。公约数中最大的一个公约数,称为这几个自然数的最大公约数。例: 在2、4、6中,2就是2,4,6的最大公约数。早在公元前300年左右,欧几里得就在他的著作《几何原本》中给出了高效的解法——辗转相除法。辗转相除法使用到的原理很聪明也很简单,假设用f(x, y)表示x,y的最大公约数,取k = x/y,b = x%y,则x = ky + b,如果一个数能够同时整除x和y,则必能同时整除b和y;而能够同时整除b和y的数也必能同时整除x和y,即x和y的公约数与b和y的公约数是相同的,其最大公约数也是相同的,则有f(x, y)= f(y, y % x)(y > 0),如此便可把原问题转化为求两个更小数的最大公约数,直到其中一个数为0,剩下的另外一个数就是两者最大的公约数。例如,12和30的公约数有:1、2、3、6,其中6就是12和30的最大公约数。*///第四章第01题,分别求出两个整数的最大公约数和最小公倍数,用主函数调用两个函数,并输出结果,两个整数由键盘输入。#include <iostream>using namespace std;//辗转相除法inline int gongyue(int x,int y){int t;if(x>y)t=y,y=x,x=t;//将较大数赋给y,xy顺序调换    if(x!=0)gongyue(x,t=y%x);//辗转相除法及递归调用,[公约数(较小数,余数)]else return y;}int main(){//输入整数cout<<"请输入两个整数并用空格间开:";int a;int b;cin >> a >> b;cout << endl;//运算int i = gongyue(a,b);cout << "它们的最大公约数是:" << i << endl;i = a*b/i;cout << "它们的最小公倍数是:" << i << endl;return 0;}



//第四章,第03题,写一个判别素数的函数,在主函数输入一个整数,输出是否为素数的信息。#include <iostream>using namespace std;int main(){//输入long int a;cout << "请输入一个整数:";cin >> a;cout << endl << "刚刚输入的数";//判断,输出bool judge(long int a);long int j;j = judge(a);if (j);elsecout<<"不";cout<<"是素数"<<endl;return 0;}bool judge(long int a){int i;if(a==0||a==1)return false;else{for(i=2;i<(a-1);i++){if (a%i==0)return false;else return true;}}}



//第四章,第四题,求a!+b!+c!的值,用一个函数fac(n)求n!.a,b,c的值由主函数输入。#include <iostream>using namespace std;//内嵌函数inline int fac(int x){int i;int s;for (i=1,s=1;i<=x;i++)s=i*s;return s;}//主函数int main(){//输入static int a,b,c;auto int n;cout<<"请输入三个整数,每输入一个整数用空格间开:";cin>>a>>b>>c;cout<<endl;//计算输出n=fac(a)+fac(b)+fac(c);cout<<"这三个数的阶乘相加结果是:"<<n<<endl;return 0;}



//第四章,第8题,用递归方法求n阶勒让德多项式的值,//递归公式为p(0)=1,p(1)=x,p(n>1)=((2n-1)*x-p(n-1)(x)-(n-1)*p(n-2)(x))/n#include <iostream>using namespace std;int main(){//输入cout << "请输入n和x的值,用空格间开 : ";float n,x,s;cin >> n >> x;cout << endl;//计算float p(float ,float);s = p(n,x);cout << "以下是n阶勒让德多项式的值:" << s << endl;return 0;}float p(float n,float x){float sum;if (n==0)return 1;else if (n==1)return x;else {sum=((2*n-1)*x-p(n-1,x)-(n-1)*p(n-2,x))/n;return sum;}}



//第五章,001 用筛选法求100之内的素数。#include <iostream>#include <iomanip>//函数声明bool judge(int x);using namespace std;int main(){    int a[100];//函数声明int i;int j;for (i=0;i<100;i++)//赋值a[i]=i+1;    cout<<"100以内的素数有:";//调用计算输出for (i=0;i<100;i++){j=judge(a[i]);if (j)cout<<setw(8)<<a[i];}return 0;}bool judge(int x){int i;if (x==0||x==1)return false;else{for(i=2;i<x;i++){if(x%i==0)return false;elsereturn true;}}}



//第五章 002 用选择法对10个整数排序.#include <iostream>using namespace std;void array(int a[]);int main(){//inputint a[10];int i;    cout << "please enter 10 numbers:" << endl;    for(i = 0; i<10; i++)cin >> a[i];cout << endl << "以下是重新排列后的结果:";//从小到大排列,用函数调用,选择法。  array(a);//输出    for (i=0;i<10;i++)cout<<a[i]<<"  ";cout<<endl;return 0;}//选择法,从小到大输出void array(int a[]){int i,j,k,t;for (i=0;i<9;i++){ k=i; for(j=k+1;j<10;j++) { if(a[j]<a[k])k=j; t=a[k],a[k]=a[i],a[i]=t; }}}



//第五章 003题求一个3*3矩阵对角线元素之和#include <iostream>using namespace std;int main(){int a[3][3];int i;int j;int s=0;//输入值:for (i=0;i<3;i++){cout << "Please enter 3 numgers on "<< i+1<< " line:";for (j=0; j<3; j++)            cin >> a[i][j];cout << endl;}//计算,输出for (i=0;i<3;i++)s = s+a[i][i];cout << "result=" << s << endl;return 0;}



//第五章 004题 有一个已排好序的数组,今输入一个数,要求按原来排序的规律将它插入数组中。//我稍稍改一下题目,现输入一个数组,要求按从大到小的排序规律将它插入到数级中。#include <iostream>using namespace std;void array(int a[], int n, int x=0);//函数声明int main(){//define and inputint a[5];int x;int i;cout << "Please enter 5 numbers:";for (i=0;i<5;i++)cin >> a[i];cout << endl;//sort for the array and output the resultarray(a,5);cout << "Now here is the sort result:";for (i=0;i<5;i++)   cout<<a[i]<<"    ";cout<<endl;//input an extra number(x!=0) and sort again     cout<<"Please enter a figure again:";cin>>x;cout<<endl;array(a,6,x);   //经过测试,void调用函数中能够使用cout;return 0;}void array(int a[],int n,int x){//先判断X是否存在,若存在,则先将数组变为b[n]int i,j,k,t;  if (x!=0){int b[6];for (i=0; i<5; i++)b[i] = a[i];b[5]=x;     //选择法for(i=0;i<n;i++)  { k=i;     for (j=k+1; j<n; j++) if(b[j]<b[k]) k=j;         t=b[k];b[k]=b[i];b[i]=t;  }   cout << "Now here is the sort result again:";       for (i=0;i<n;i++)          cout << b[i] << "    ";       cout<<endl; }//直接使用选择法排序  else  for(i=0;i<n;i++)  { k=i;     for (j=k+1;j<n;j++) if(a[j]<a[k]) k=j;         t=a[k];a[k]=a[i];a[i]=t;  }}



/*第五章 005题将一个数组中的值按逆序重新存放。*/#include<iostream>using namespace std;int main(){//define and input the arrayint a[100000];int n;int i;cout << "Please input the array numbers which you hope(n):";cin >> n;cout << endl;cout << "Now,please enter the array numbers:";for(i=0;i<n;i++)cin>>a[i];cout<<endl;//outputcout<<"The sort result are as follows:";for(i=n-1;i>=0&&i<n;i--)cout<<a[i]<<"  ";cout<<endl;return 0;}



/* 第五章 第11题 打印以下图案(用字符数组的方法)* * * * * * * * * *  * * * * *   * * * * *    * * * * **/#include<iostream>using namespace std;int main(){/*define*/int i,j;char a[5][5];char space=' ';/*input*/for (i=0; i<5; i++)for (j=0; j<5; j++)a[i][j] = '*';/*output*/for (i=0; i<5; i++){for (j=0; j<i; j++)     cout << space;for (j=0;j<5;j++)cout << a[i][j] << ' ';    cout << '\b';    cout << endl;}return 0;}



/* 第五章 第11题 打印以下图案(用字符数组的方法)* * * * * * * * * *  * * * * *   * * * * *    * * * * **/#include<iostream>#include<string>using namespace std;int main(){/*define*/    void space(int i);int i;string s = "* * * * *";/*figure out*/for (i=0;i<5;i++)space(i), cout << s << endl;return 0;}void space (int i){int k;for (k=0;k<i;k++)cout<<' ';}



//第五章 第17题 输入10个学生的姓名,学号和成绩,将其中不及格者的姓名,学号和成绩输出。#include <iostream>#include <string>using namespace std;/*define*/string name[10];int num[10];int score[10];int i;int main(){/*可分三部走,输入,筛选,输出。现在下面先定义*/    void input(void);    void screen(void);/*输入*/    cout<<"请按顺序输入每位学生的姓名,学号和成绩。"<<endl;    input();    cout<<endl<<endl;/*筛选*/  /*输出*/ cout<<"以下是筛选结果,其中,不及者有:"<<endl;    screen();  return 0;}/*输入*/void input(void){for(i=0;i<10;i++){cout<<"第"<<i+1<<"同学的资料:";cin>>name[i]>>num[i]>>score[i];cout<<endl;}}/*筛选*/  /*输出*/void screen(void){for (i=0;i<10;i++)if (score[i]<60)cout<<name[i]<<' '<<num[i]<<' '<<score[i]<<endl;}



/*  001 输入3个整数,按由小到大的顺序输出。(指针)*/#include <iostream>using namespace std;int main(){  /*定义三位整数和指针变量*/int a;int b;int c;int* pa = &a;int* pb = &b;int* pc = &c;  /*输入a,b,c三位整数*/cout<<"Please enter integers a,b,c : " ;cin>>a>>b>>c;cout<<endl;  /*声明且调用函数,排序a,b,c*/void sort(int* , int* , int* );    sort (pa, pb, pc);  /*排序完成,输出*/cout<<*pa<<" < "<<*pb<<" < "<<*pc<<endl;return 0;}/*定义实现排序功能的函数*/void sort(int* p1, int* p2, int* p3){  /*声明一个实现值互换的函数*/void swap(int* qa, int* qb);  /*判断适用条件,实现值互换*/if (*p1>*p2) swap(p1, p2);if (*p1>*p3) swap(p1, p3);if (*p2>*p3) swap(p2, p3);}/*定义一个实现值互换的函数*/void swap(int* qa, int* qb){int temp;temp = *qa;*qa = *qb;*qb = temp;}  


/*  001 输入3个整数,按由小到大的顺序输出。(引用)*/#include <iostream>using namespace std;int main(){int a;                                            //定义变量int b;int c;cout<<"Please input three integers : " ;           //输入数值cin>>a>>b>>c;cout<<endl;void exchange(int& ,int& ,int&);                   //声明调用函数exchange(a, b, c);cout<<a<<" < "<<b<<" < "<<c<<endl;                 //输出结果return 0;}void exchange(int& e, int& f, int& g)                  //定义exchange函数,对a,b,c排序{void swap(int& , int& );   //声明swap,实现值互换的功能if (e>f) swap(e, f);if (e>g) swap(e, g);if (f>g) swap(f, g);}void swap(int& h, int& i){int temp;temp = h;h = i;i = temp;}



/* 输入3个字符串,按由小到大的顺序输出(指针) */#include <iostream>//预处理命令#include <string>using namespace std;void exchange(string* ,string* ,string* );//声明函数void swap(string* ,string* );int main()//主函数{string a;//定义变量string b;string c;string* string1 = &a;string* string2 = &b;string* string3 = &c;cout<<"Please input three strings: "<<endl;//输入cin >> a >> b >> c;cout << endl;exchange(string1, string2, string3);//sort,排序,调用函数cout << endl;//输出结果cout << *string1 << endl << *string2 << endl << *string3 << endl ;return 0;}void exchange(string* aa, string* bb, string* cc){if (*aa>*bb) swap (aa, bb);if (*aa>*cc) swap (aa, cc);if (*bb>*cc) swap (bb, cc);}void swap (string* one, string* two){string temp;temp = * one;* one = * two;* two = temp;}



/* 输入3个字符串,按由小到大的顺序输出(引用) */#include <iostream>//预处理命令#include <string>using namespace std;void exchange(string& ,string& ,string& );//声明函数void swap(string& ,string& );int main()//主函数{string a;//定义变量string b;string c;cout<<"Please input three strings: "<<endl;//输入cin >> a >> b >> c;cout << endl;exchange(a, b, c);//sort,排序,调用函数cout << endl;//输出结果cout << a << endl << b << endl << c << endl ;return 0;}void exchange(string& aa, string& bb, string& cc){if (aa>bb) swap (aa, bb);if (aa>cc) swap (aa, cc);if (bb>cc) swap (bb, cc);}void swap (string& one, string& two){string temp;temp = one;one = two;two = temp;}



/**************(指针)输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。写3个函数:001输入10个数    002进行处理    003输出10个数                                                       **************/#include <iostream>//预处理文件using namespace std;void input(int* ,int );//函数声明void deal(int* ,int );void output(int* ,int );int main(){int n = 10;//变量定义int a[10];int* p = a;input (p, n);//函数调用deal (p, n);output (p, n);return 0;}void input (int* p, int n){int i;cout << "请输入" << n << "个整数,按回车结束: " <<endl;for (i = 0 ; i < n ; i++)cin >> *(p+i);cout << endl;}void deal(int* p, int n){int m = 0;int temp;int i;for (i = 0; i<n; i++)//最小数与第一个数对换if (*(p+i)<=*(p+m))m = i;//符合条件,则记下地址temp = *(p+m) ;*(p+m) = *p;*p = temp;m = 0 ;for (i = 0; i<n; i++)   //最大数与最后的数对换if (*(p+i)>=*(p+m))m = i;temp = *(p+m);*(p+m) = *(p+n-1);*(p+n-1) = temp;}void output(int* p, int n){int i;cout << "\n以下是处理结果:";for ( i = 0; i<n; i++)cout << *(p+i) << " ";cout << endl;}



/*             第六章,  第四题  有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数。  写一段函数实现以上功能,在主函数中输入n个整数,并输出调整后的n个数。 */#include <iostream>using namespace std;void handle(int* , int n ,int m);//函数声明int main(){int m;//defineint n;int i;int a[10000];int* p = a;cout << "请输入该数组的整数个数n: " ;//确定数组数目cin >> n;cout << endl << endl;cout << "请输入需要往后移的元素的位置m: ";//确定m的位置cin >> m;cout << endl << endl;cout << "请输入该数组元素: ";for (i = 0; i<n; i++)//输入数组元素cin >> *(p+i);cout << endl <<endl;handle(p, n, m);//函数声明cout << "以下是转换结果:"//结果输出 << endl;for (i = 0; i<n; i++)cout << *(p+i) <<"  ";cout << endl;return 0;}void handle(int* p, int n ,int m){int hou[100];//m后的数的数组int qian[100];//m前的数的数组int i;int j;for (i = 0; i<m; i++)qian[i] = *(p+i);//m前的数的数组赋值for (i=m, j=0; i<n; i++,j++)hou[j] = * (p+i);//m后的数的数组赋值for (i=0; i<n-m; i++)//对*p重新赋值,注意判定条件n-m*(p+i) = hou[i];for (i=n-m, j=0; i<n; i++,j++)//注意判定条件* (p+i) = qian[j];}



#include <iostream>using namespace std;   int prime(int year)   {                                     //判断年份是闰年还好是平年   cout<<"再次输入年份 : ";   cin>>year;   if(year%4==0&&year%100!=0)   return 1;  //真,是闰年 多一天366天   else   return 0;  //假,是平年  365天   }   void main()   {      int month,day,year;   cout<<"输入年份,月份和日期 : "<<endl;           //   cin>>year>>month>>day;   if(prime(year))       //为真,是闰年   {            if(month==1)   cout<<"是"<<year<<"年的第 "<<day<<"天"<<endl;          if(month==2)   cout<<"是"<<year<<"年的第 "<<31+day<<"天"<<endl;          if(month==3)   cout<<"是"<<year<<"年的第 "<<31+29+day<<"天"<<endl;     if(month==4)   cout<<"是"<<year<<"年的第 "<<31+29+31+day<<"天"<<endl;    if(month==5)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+day<<"天"<<endl;   if(month==6)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+31+day<<"天"<<endl;    if(month==7)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+31+30+day<<"天"<<endl;    if(month==8)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+31+30+31+day<<"天"<<endl;    if(month==9)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+31+30+31+31+day<<"天"<<endl;    if(month==10)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+31+30+31+31+30+day<<"天"<<endl;    if(month==11)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+31+30+31+31+30+31+day<<"天"<<endl;     if(month==12)   cout<<"是"<<year<<"年的第 "<<31+29+31+30+31+30+31+31+30+31+30+day<<"天"<<endl;   }   else    {            if(month==1)   cout<<"是"<<year<<"年的第 "<<day<<"天"<<endl;          if(month==2)   cout<<"是"<<year<<"年的第 "<<31+day<<"天"<<endl;          if(month==3)   cout<<"是"<<year<<"年的第 "<<31+28+day<<"天"<<endl;     if(month==4)   cout<<"是"<<year<<"年的第 "<<31+28+31+day<<"天"<<endl;    if(month==5)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+day<<"天"<<endl;   if(month==6)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+31+day<<"天"<<endl;    if(month==7)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+31+30+day<<"天"<<endl;    if(month==8)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+31+30+31+day<<"天"<<endl;    if(month==9)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+31+30+31+31+day<<"天"<<endl;    if(month==10)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+31+30+31+31+30+day<<"天"<<endl;    if(month==11)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+31+30+31+31+30+31+day<<"天"<<endl;     if(month==12)   cout<<"是"<<year<<"年的第 "<<31+28+31+30+31+30+31+31+30+31+30+day<<"天"<<endl;        }               }



/*******第七章,第01题定义一个结构体变量(包括年,月,日)编写程序,要求输入年,月,日,程序能计算并输出该日在本年中是第几天。注意润年问题。*****************************************************************/#include <iostream>using namespace std;struct date{private:int year;int month;int day;public:void input();int deal1();};void date::input(){cout<< "请输入年,月,日,并用空格间开:";cin >> year >> month >> day;cout<< endl;}int date::deal1(){//先用if断断是不是1月和2月的//再用if判断是不是润年//后用确定2月份的天数后,后面的天数可以直接加上/*判断每个月份的天数31天的月份有:1.3.5.7.8.10.1230天的月份有:4.6.9.1128或29天的月份有:2*/int n;//设定n为总天数static int n2;int d = day;//31天的static int n31 = 31;int& n1 = n31;int& n3 = n31;int& n5 = n31;int& n7 = n31;int& n8 = n31;int& n10 = n31;int& n12 = n31;//30天的static int n30 = 30;int& n4 = n30;int& n6 = n30;int& n9 = n30;int& n11 = n30;//开始计算if ( (month==1)||(month==2) ){if (month==1) {n = d;return n;}if (month==2) {n = n1+d;return n;}}else{if (year%4==0&&year%100!=0)n2 = 29;elsen2 = 28;switch(month){case 3 : n = n1+n2+d; break;case 4 : n = n1+n2+n3+d; break;case 5 : n = n1+n2+n3+n4+d; break;case 6 : n = n1+n2+n3+n4+n5+d; break;case 7 : n = n1+n2+n3+n4+n5+n6+d; break;case 8 : n = n1+n2+n3+n4+n5+n6+n7+d; break;case 9 : n = n1+n2+n3+n4+n5+n6+n7+n8+d; break;case 10 : n = n1+n2+n3+n4+n5+n6+n7+n8+n9+d; break;case 11 : n = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+d; break;case 12 : n = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+d; break;}}return n;}void main(){typedef date time;//本句没什么必要用,写出这句是因为想试一下用typedeftime e;date* p;p = &e;(*p).input();(*p).deal1();int n = (*p).deal1();cout<< "该日在本年中是第 " << n << "天" <<endl; }



/*************************************第七章第4题;编写一个函数print,打印一个学生的成绩数组,该数组中有五个学生的数据,每个学生的数据包num(学号)\name(姓名)、score[3](3门课的成绩)。用print函数输出这些数据,再用一个函数input,用来输入五个学生的数据。*******************************************************************/#include <iostream>#include <string>//姓名可用字符串#include <iomanip>//后面用到setw(16)using namespace std;class print{private:int num;string name;int score[3];public:void input_p();//声明输入的函数void output_p();//声明输出的函数};void print::input_p(){cin >> num>> name>> score[0] >> score[1] >> score [2];}void print::output_p(){cout << num << setw(16) << name << setw(16) << score[0] << setw(16) << score[1] << setw(16) << score[2]<< setw(16) << endl;}void main(){print s[5];int i;cout << "学号" << setw(8) << "姓名" << setw(8) << "成绩1"<< setw(8) << "成绩2"<< setw(8) << "成绩3"<< setw(8);cout << endl;for (i=0; i<5; i++){ (s[i]).input_p();}cout << "以下是打印输出:\n" <<endl;cout << "学号" << setw(16) << "姓名" << setw(16) << "成绩1"<< setw(16) << "成绩2"<< setw(16) << "成绩3"<< setw(16);for (i=0; i<5; i++){ (s[i]).output_p();}}/***************本题若输入时逐个提示,效果更好。*************************/



/********第八章——第五题00. 将本章的例84改写成一个多文件的程序;01. 将类定义放在头文件 arraymax.h中;02. 将成员函数定义放在源文件 arraymax.cpp 中;03. 主函数放在源文件file1.cpp 中 ;end. 请写出完整的程序,上机调试并运行。*******************************************//***************   原题  ************************/* 例84  找出一个整型数组中的元素的最大值。 #include <iostream>using namespace std;class Array_max//声明类{public:void set_value();void max_value();void show_value();private:int array[10];//整型数组int max;//max用来存放最大值};void Array_max::set_value()//成员函数定义,向数组元素输入数值{int i;for(i=0; i<10; i++)cin >> array[i];}void Array_max::max_value() //成员函数定义,找数组元素中的最大值{int i;max = array[0];for (i=1; i<10; i++)if(array[i]>max)max = array [i];}void Array_max::show_value()//成员函数定义,输出最大值{cout << "max=" << max;}int main(){Array_max arrmax;//定义对象 arrmaxarrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;}***********************************************************************//********    arraymax.h#include <iostream>using namespace std;class Array_max//声明类{public:void set_value();void max_value();void show_value();private:int array[10];//整型数组int max;//max用来存放最大值};*******************************************************//******  02. 将成员函数定义放在源文件 arraymax.cpp 中;#include <iostream>using namespace std;void Array_max::set_value()//成员函数定义,向数组元素输入数值{int i;for(i=0; i<10; i++)cin >> array[i];}void Array_max::max_value() //成员函数定义,找数组元素中的最大值{int i;max = array[0];for (i=1; i<10; i++)if(array[i]>max)max = array [i];}void Array_max::show_value()//成员函数定义,输出最大值{cout << "max=" << max;}**************//*************03. 主函数放在源文件file1.cpp 中 ;using namespace std;#include "arraymax.h"#include "arraymax.cpp"int main(){Array_max arrmax;//定义对象 arrmaxarrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;}************************************************************/



// arraymax.cpp 中;#include <iostream>using namespace std;void Array_max::set_value()//成员函数定义,向数组元素输入数值{int i;for(i=0; i<10; i++)cin >> array[i];}void Array_max::max_value() //成员函数定义,找数组元素中的最大值{int i;max = array[0];for (i=1; i<10; i++)if(array[i]>max)max = array [i];}void Array_max::show_value()//成员函数定义,输出最大值{cout << "max=" << max;}


//arraymax.h#include <iostream>using namespace std;class Array_max{public:void set_value();void max_value();void show_value();private:int array[10];//整型数组int max;//max用来存放最大值};


//file1.cpp 中#include <iostream>#include "arraymax.h"#include "arraymax.cpp"int main(){Array_max arrmax;//定义对象 arrmaxarrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;}

/*第八章,第六题006.需要求3个长方柱的体积,请编一个基于对象的程序。    数据成员包括length(长)、width(宽)、height(高).要求用成员函数实现以下功能:01.由键盘分别输入3个长方柱的长、宽、高;02.计算长方形的体积;03.输出3个长方柱的体积。***************************************************/#include <iostream>using namespace std;class rectangular_column;//声明类typedef rectangular_column rc;//用别名定义类int n;//全局变量,用作记录长方体的个数class rectangular_column{public:void input();void deal();void output();protected:int N;//记录第几个长方体double legth;double width;double height;double volume;};void rc::input(){n++;N = n;cout<< "请输入第" << N << "个长方柱的长度: " ;cin >> legth;cout <<endl;cout<< "请输入第" << N << "个长方柱的宽度:" ;cin >> width;cout <<endl;cout<< "请输入第" << N << "个长方柱的高度:" ;cin >> height;cout <<endl;}void rc::deal(){volume = legth * width * height;}void rc::output(){cout << "第" << N << "个长方体的体积是:" << volume << endl;}void main(){rc a[3];int i;for (i=0; i<3; i++){a[i].rc::input();a[i].rc::deal();}for (i=0; i<3; i++)a[i].rc::output();}

//第9章 004题 //建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。#include <iostream>#include <string>using namespace std;class student{public:void output();student(string na, int nu) :name(na),num(nu){};protected:string name;int num;};void student::output(){cout << "名字:" << name <<endl;cout << "学号:" << num << endl;cout << endl;}void main(){student a[5]={student("一号",001),student("二号",002),student("三号",003),student("四号",004),student("五号",005)};student *p = a;(*p).output();(*(p+2)).output();(*(p+4)).output();}

//第09章第5题 建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数max,用指向对象的指针作函数参数。//这是改过的,你弄混了指针、引用和对象:#include <iostream>using namespace std;class student{ public:  void input();  int num;  int score;};void student::input(){ cout << "请输入学号: " ; cin >> num; cout <<endl; cout << "请输入成绩:"; cin >> score; cout <<endl; cout << endl;}void main(){ student a[5]; for (int i=0; i<5; i++) {  cout << i+1 <<": ";  a[i].input(); } student *max(student&, student&); student *p; p = &a[0]; for (i=0; i<4; i++)  p = max (*p,a[i+1]); cout << "max = " << p->num << "  " << p->score <<endl;}student *max(student& c1, student& c2){ if ((c1.score) > (c2.score))  return &c1; else   return &c2;}/*噢,运行对了。其实还想问问:为什么调用max的时候又不用*号了呢?定义直接用max为什么不可以呢?要用*max()??注意指针运算符*是一个单目运算符,从右往左结合,定义max函数的*是和左面的student结合,代表max函数的返回类型,调用时在使用就是反引用函数返回值(student类型指针)所指向的内容了,这样和前面的p造成赋值类型不兼容**********************************************************************/

/*第09章 第09题商店销售某一商品,商店每天公布统一折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者,还可以享受9.8折优惠。现已知当天3名销货员的销售情况为:销货员号(num) 101 102 103销货件数(quantity) 5 12 100销货单价(price) 23.5 24.56 21.5请编程序,计算出当日此商品的总销售款sum,以及每件商品的平均售价。要求用静态数据成员和静态成员函数。(提示:将折扣discout、总销售款sum和商品销售总件数n声明为静态数据成员,再定义静态成员函数average(求平均售价)和display(输出结果).**/#include <iostream>using namespace std;class sell{public:static void average();static void display();sell (int nu, int qu, float pr):num(nu),quantity(qu),price(pr){};protected:int num;int quantity;float price;public:static int n;static int sum;static double discount;};int sell::n = 0;int sell::sum = 0;double sell::discount = 0.98;void sell::average(){for (int i=0; i<quantity; i++,n++);//累加总件数double a;//平均售价cout << num <<  "商品的平均售价是:";if (quantity>10)a = price * discount;elsea = price;cout << price <<endl;sum += (a * quantity);}void sell::display(){cout << "销售总件数是:" << n << endl;cout << "总销售款是:" << sum << endl;}void main(){sell s[3] ={   sell (101, 5, 23.5),  sell (102, 12, 24.56),  sell (103, 100, 21.5)};for (int i=0; i<3; i++)s[i].average();s[i].display();}


原创粉丝点击