zoukankan      html  css  js  c++  java
  • 考试

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 //-----------------
     5 class GNumber
     6 {
     7     char chuan[50];
     8 public:
     9     void set();
    10     friend int search (GNumber& str);
    11 };
    12 //-------------------
    13 int search(GNumber& s)
    14 {    
    15     int num=0;                         //定义num用于整数个数的统计
    16     for(int i=0;i<50;i++)       
    17     {   string str1="";                   //新建一个用于储存整数的字符串
    18 
    19         if(s.chuan[i]>='0'&&s.chuan[i]<='9')                       //用以判断是否为数字
    20            for(int j=i;;j++)                        // 判断其后是否有连续数字,若有,则储存
    21            { 
    22                if(s.chuan[j]<'0'||s.chuan[j]>'9')      //若不是数字,则跳过非数字区域,并跳出判断后一个是否是数字的循环体
    23                { i=j;break;}     
    24                str1=str1+s.chuan[j];
    25            }                 //若是数字,则储存起来
    26     
    27            if(str1!=""&&str1.length()>0)       //判断是否是“整数”
    28            {
    29                cout<<str1<<endl;
    30                num++;                              //统计整数个数
    31            }                         
    32     }
    33     return num;                 //返回统计的个数,调用即可返回
    34 }
    35 //-----------------
    36 void GNumber::set()
    37 {  
    38     for (int i=0;(chuan[i]=cin.get())!='\n';i++)         //调用cin 类的get这样一个成员函数,当输入的不是回车的时候执行空操作;否则终止循环
    39         ;
    40 }
    41 //----------------------
    42 void main()
    43 {
    44     GNumber s1;
    45     cout<<"please input a string:"<<endl;
    46     s1.set();
    47     cout<<endl;
    48     cout<<"有下列数字:\n";
    49     int num=search(s1);               //将查找统计出来的整数个数赋值给main函数中的变量 num;
    50     cout<<"共计"<<num<<"个\n";
    51     system("pause");
    52 }
      1 #include<iostream>
      2 #include<iomanip>
      3 using namespace std;
      4 //======================================定义类
      5 class TDarray
      6 {
      7     int a[3][4];   //定义成员数据
      8 public:
      9     void set();              //成员函数,用于输入
     10     friend void change(TDarray& b,TDarray& c);          // 用于交换
     11     friend void output(const TDarray& b);   //   用于输出
     12 };
     13 //==========================================
     14 void TDarray::set()                //循环输入二维数组
     15 {
     16     for (int i=0;i<3;i++)
     17         for(int j=0;j<4;j++)
     18             cin>>a[i][j];
     19 }
     20 //=========================================
     21 void change(TDarray& b,TDarray& c)
     22 {
     23     for (int i=0;i<3;i++)
     24     {   
     25     for(int j=0;j<4;j++)              
     26         c.a[i][(j+1)%4]=b.a[i][j];   //通过取余可以将 数字轮换
     27     }
     28 
     29 }
     30 //===========================================
     31 void output(const TDarray& b)     //输出
     32 {
     33     for (int i=0;i<3;i++)
     34     {
     35         for(int j=0;j<4;j++)
     36             cout<<setw(4)<<b.a[i][j];
     37         cout<<endl;
     38     }
     39 }
     40 //===========================================
     41 void main()
     42 {
     43     cout<<"Please input 12 numbers:\n";         //提醒用户输入数据
     44     TDarray num;
     45     TDarray c;
     46     num.set();                                  //通过成员函数设置成员数据
     47     cout<<"The former one is:\n";               //先输出原先的数组
     48     output(num);
     49     change(num,c);                                 //做转换
     50     cout<<"The latter one is:\n";                //输出转换过后的数组
     51     output(c); 
     52 }
     53 //----------------------------------
     54 #include<iostream>
     55 #include<string>
     56 #include<sstream>
     57 using std::string;//using std::string
     58 #define ostream std::ostream//#define ostream std::ostream
     59 #define istream std::istream//#define istream std::istream
     60 #define cout std::cout
     61 #define cin std::cin
     62 class GNumber
     63 {
     64 private:
     65     string str;
     66     int *a;                                //Use the pointer is much faster than the vector
     67     int n;                                //for record the number of integer
     68 public:
     69     GNumber(){a=new int[100];n=0;};        //define the constructor to allocate the space for a;
     70     ~GNumber(){delete[]a;};                //define a destructor to delete the space that new create
     71     void stoi();                        
     72     friend ostream&operator<<(ostream&,const GNumber&);//(ostream&,const GNumber&)
     73     friend istream&operator>>(istream&,GNumber&);//(istream&,GNumber&)
     74 };
     75 void GNumber::stoi()
     76 {
     77     int i;
     78     for(int i=0;i<str.size();i++)
     79         if(str[i]>'9'||str[i]<'0')
     80             str[i]=' ';
     81     std::istringstream sin(str);//std::istreamstream sin(str);
     82     for(;sin>>a[n];n++);
     83     n--;                    //the n must be minus 1 because When finished entering,n have been plus one
     84 }
     85 ostream&operator<<(ostream&out,const GNumber&number)
     86 {
     87     out<<"The number of integers is:"<<number.n<<'\n';
     88     for(int i=0;i<number.n;i++)
     89         out<<number.a[i]<<" ";
     90     out<<'\n';
     91     return out;
     92 }
     93 istream&operator>>(istream&input,GNumber&number)
     94 {
     95     cout<<"please input the string without space:"<<std::endl;
     96     input>>number.str;
     97     return input;
     98 }
     99 int main()
    100 {
    101     GNumber number;
    102     cin>>number;
    103     number.stoi();
    104     cout<<number;
    105     system("pause");
    106     return 0;
    107 }
    108 //-------------------
    109 #include<iostream>
    110 using std::cin;
    111 using std::cout;                //using the Standard namespace of cin and cout
    112 #define ostream std::ostream    //define ostream to replace std::ostream
    113 #define istream std::istream
    114 class TDarray
    115 {
    116 public:
    117     int **a;                //Defined 2D pointer//二级指针用的漂亮,int **a;
    118 public:
    119     TDarray();                //Define a constructor to allocate space to the 2D pointer//TDarray();~TDarray();
    120     ~TDarray();                //Defined destructor to delete the 2D pointer space
    121     void tdarray();            //Defined function "tdarray" to convert//void tdarray();
    122     friend ostream&operator<<(ostream&,const TDarray&);        //overloading ostream to output//friend  ostream& operator<<(ostream&,TDarray&)
    123     friend istream&operator>>(istream&,TDarray&);            //overloading istream to input//friend istream& operator>>(istream&,TDarray&)
    124     
    125 };
    126 TDarray::TDarray()
    127 {
    128     a=new int*[3];            //allocated to the 2D pointer three lines of 1D pointer Dynamically    //a=new int*[3];a[i]=new int[4]
    129 for (int i=0;i<3;i++)
    130     a[i]=new int[4];        //using for statement to allocated to each row four space dynamically
    131 }
    132 TDarray::~TDarray()
    133 {
    134     for (int i=0;i<3;i++)    
    135         delete[]a[i];        //using for statement to delete the space of each row、//两次delete
    136     delete a;                //delete the space of 2D pointer
    137     cout<<"have delete the memory now"<<'\n';        //using the message to ensure that the destructor is executed
    138 }
    139 
    140 void TDarray::tdarray()
    141 {
    142     for(int i=0;i<3;i++)        //using for to convert each line
    143     {
    144         int temp=a[i][3];        //Create a temporary variable to store the last element of each row
    145         for(int j=3;j>0;j--)    
    146             a[i][j]=a[i][j-1];    //let each element be equal to its previous element
    147         a[i][0]=temp;            //make the first element be equal to the temporary variable
    148     }    
    149 }
    150 istream&operator>>(istream&input,TDarray&array)
    151 {
    152     for(int i=0;i<3;i++)
    153         for(int j=0;j<4;j++)
    154             input>>array.a[i][j];
    155     return input;                    //return input to make sure cin can continuous input 
    156 }
    157 ostream&operator<<(ostream&out,const TDarray&array)
    158 {
    159     for(int i=0;i<3;i++)
    160     {
    161         for(int j=0;j<4;j++)
    162             out<<array.a[i][j]<<" ";
    163         out<<'\n';
    164     }
    165     return out;                            //return out to make sure cout can continuous output 
    166 }
    167 int main()
    168 {
    169     //freopen("tdarray.txt","r",stdin);        //Text input instead of keyboard input
    170     TDarray array;                            //define the object of TDarray
    171     cin>>array;                                //input the array
    172     cout<<array;                            //output the array
    173     array.tdarray();                        //using the function to cover
    174     cout<<array<<array;                        //output the array covered//不错,输出两个矩阵,还按照矩阵的形式
    175     system("pause");
    176     return 0;                                //the main function must return 0 to pass in c++standard
    177 }
    178 //-------------------
    179 //定义一个结构体变量(包括年、月、日)编写程序。要求输入年、月、日,
    180 //程序能计算输出该日在本年中是第几天。(注意闰年问题),写一个函数days,实现上面的计算。由主函数将年、月、日传递给函数days,
    181 //计算出该日在本学年中是第几天,并将结果传回主函数输出。(上次作业的改进版本,用类实现,请看老师的PPT)
    182 #include<iostream>
    183 using namespace std;
    184 class Days
    185 {
    186 public:
    187     int riqi(int &year,int &month, int&day);
    188 }Days1;
    189 int Days::riqi(int &year,int &month, int&day)
    190 {
    191     int change;//判断是否是闰年
    192     if(year%4==0&&year%100!=0||year%400==0)
    193         change=1;
    194     else
    195         change=2;
    196     int num1[12]={31,28,31,30,31,30,31,31,30,31,30,31};//数组内的元素为各月天数
    197     int num2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    198     int i,sum=0;//初始化,i全局变量
    199     switch(change)
    200     {
    201     case 1:
    202     {
    203         for(i=0;i<month-1;i++)
    204             sum=sum+num1[i];
    205         sum=sum+day;
    206     }
    207     case 2:
    208     {
    209         for(i=0;i<month-1;i++)
    210             sum=sum+num2[i];
    211         sum=sum+day;
    212     }
    213     }
    214     
    215     return sum;
    216 }
    217 int main()
    218 {
    219     int a,b,c;
    220     cout<<"请输入年,月,日"<<endl;
    221     cin>>a>>b>>c;
    222     int z=Days1.riqi(a,b,c);
    223     cout<<"该日在本年中是第"<<z<<""<<endl;
    224         return 1;
    225 }
    226 //----------------
    227 #include<iostream>
    228 #include<cmath>
    229 using namespace std;
    230 class Point 
    231 {
    232 protected:
    233     double x;
    234     double y;
    235 public:
    236     void Set(double ix,double iy)
    237     {
    238         x=ix;
    239         y=iy;
    240     }
    241     void change()
    242     {
    243         
    244         x=x+5;
    245         y=y+6;
    246     }
    247 
    248     double xOffset()
    249     {
    250         return x;
    251     }
    252     double yOffset()
    253     {
    254         return y;
    255     }
    256     double angle()
    257     {
    258         return (180/3.14159)*atan2(y,x);//特别重要
    259     }
    260     double radius()
    261     {
    262         return sqrt(x*x+y*y);
    263     }
    264 };
    265     int main()
    266     {
    267         Point p;//定义对象
    268         double x,y;
    269         cout<<"Enter x and y"<<endl;
    270         cin>>x>>y;
    271         p.Set(x,y);//访问成员函数
    272         p.change();
    273         cout<<"angle="<<p.angle()
    274             <<",radius="<<p.radius()<<",x offset="<< p.xOffset()
    275             <<", y offset="<<p.yOffset()<<endl;
    276         system("pause");
    277         return 1;//要有返回值
    278     }
    279 
    280 //-----------------------------
    281 #include<iostream>
    282 #include<iomanip>
    283 using namespace std;
    284 class Date
    285 {
    286     int year,month,day;
    287 public:
    288     void add(int &y,int &m,int &d);
    289     void print();
    290 };
    291 void Date::add(int &y,int &m,int &d)//定义类的成员函数,要有预操作符
    292 {        
    293         year=y;
    294         month=m;
    295         day=d;
    296     int change;//判断是否是闰年
    297     if(year%4==0&&year%100!=0||year%400==0)
    298         change=1;
    299     else
    300         change=2;
    301     int num[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    302     if(change==1)
    303         num[2]=29;
    304     if(day<=num[month]-1)//判断是否大于该月总天数
    305         day=day+1;
    306     else
    307     {
    308         day=1;
    309         month++;
    310         if(month>12)
    311         {
    312             month=1;
    313             year++;
    314         }//判断原时期是否是12月最后一天
    315 
    316     }
    317 }
    318     //--------------------------------------------
    319     void Date:: print()
    320     {    
    321         cout<<"该日期加一天后变为"<<endl;
    322         cout<<"按日,月,年的顺序输出"<<endl;
    323         cout<<setfill('0');//设置日期格式
    324         cout<<setw(2)<<day<<'-'<<setw(2)<<month<<'-'<<setw(4)<<year<<endl;
    325         
    326     }
    327 //-----------------------------------------
    328     int main()
    329     {
    330         Date s;
    331         cout<<"请输入年,月,日"<<endl;
    332         int y,m,d;
    333         cin>>y>>m>>d;
    334         s.add(y,m,d);
    335         s.print();
    336         return 1;
    337     }
    338 
    339 
    340 
    341 
    342 
    343 
    344     
  • 相关阅读:
    代码生成器所用到的东西
    被float.parse吃掉的0.03...
    Microsoft Visual Studio 2010 Ultimate ISO 官方下载地址
    关于.net实现网站模板机制(非标签替换)
    关于ACCESS的事务与存储过程的调用
    关于如何实现左中右三栏布局, 左右固定宽度,中间随屏幕自适应
    几个台湾优秀个人博客网站
    [转]MathType常见问题
    Qt中int转换成QString
    fatal error C1189: #error : The C++ Standard Library forbids macroizing keywords. Enable warning C4005 to find the forbidden macro.
  • 原文地址:https://www.cnblogs.com/herizai/p/3101521.html
Copyright © 2011-2022 走看看