天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

#include<stdio.h>
#include
<string.h>
char* strins(char* str1,char* str2,int pos)
{
    
int s_len;
    
int d_len;
    
int i,j;
    pos
--;
    s_len
=strlen(str1);
    d_len
=strlen(str2);
    
for(i=s_len+1;i>=pos;i--/*空出str2的空间*/
        str1[i
+d_len]=str1[i];
    
for(j=pos;str2[j-pos]!='\0';j++/*将字符串str2插入str1中的特定位置*/
        str1[j]
=str2[j-pos];

    
return str1;
}


void main()
{
    
char string1[200];
    
char string2[100];
    
int pos;
    printf(
"请输入初始字符串:");
    gets(string1);
    printf(
"请输入插入字符串:");
    gets(string2);
    printf(
"请输入插入位置:");
    scanf(
"%d",&pos);
    strins(string1,string2,pos);
    printf(
"插入后的字符串是%s\n",string1);
}

posted @ 2012-02-01 01:13 hoshelly 阅读(146) | 评论 (0)编辑 收藏

在C++中,sizeof运算符的作用是返回一个变量或数据类型在内存中所占用的字节数,其语法形式如下:sizeof 变量名; sizeof(变量类型);

sizeof运算符的操作对象可以是某个特定的变量,也可以是变量的数据类型,例如int、double和float等。当对变量对象进行运算时,变量名两边的括号可加可不加,而当操作对象是数据类型时,则必须使用括号把操作对象括起来。
#include<iostream>
#include
<stddef.h>
using namespace std;

int main()
{
    
int i;
    
char c;
    
float f;
    cout
<<"\n int"<<sizeof(int)
        
<<"\n char"<<sizeof(char)
        
<<"\n float"<<sizeof(float)<<endl;//用于数据类型
    cout<<"\n int"<<sizeof i
        
<<"\n char"<<sizeof c
        
<<"\n float"<<sizeof f<<endl;

    
return 0;
}

//sizeof运算符的返回类型为size_t类型,size_t定义于头文件“stddef.h",它是计算机特定的无符号整数类型,用来表示内存中任何对象的大小。


posted @ 2011-12-18 12:48 hoshelly 阅读(220) | 评论 (0)编辑 收藏

运算符重载函数一般采用两种形式,一种是定义为类的成员函数,另一种是定义为类的友元函数。
大多数情况下,使用成员函数和友元函数重载运算符在功能实现上是相同的,重载时如果没有本质的区别,则应该首先考虑使用成员函数以保证数据封装。然而在某些情况下,如C++不能直接进行复数加、减、乘、除的四则运算,但是使用友元函数就可以实现重载这些运算符。
如 定义 
   class complex
{
    public:
            complex(){real=imag=0;}
            complex(double r,double i)
           {
             real=r,imag=r;
            }
   friend complex operator+(const complex &c1,const complex &c2)
  {
        return complex(c1.real+c2.real,c1.imag+c2.imag);
  }...

!注意友元运算符函数的参数类型是引用类型!

一般而言,以下两种调用方法是等价的:
aa@ bb //隐式调用
operator @ (aa,bb) // 显式调用
@为运算符

在实际开发过程中,单目运算符建议重载为成员函数,而双目运算符建议重载为友元函数。通常下双目运算符重载为友元函数比重载为成员函数更方便,但是有时双目运算符必须重载为成员函数,例如赋值运算符。

            

posted @ 2011-12-18 12:34 hoshelly 阅读(1275) | 评论 (0)编辑 收藏

用友元函数重载“++”“--”时需要注意几点:
1)运算符++、--都对单值操作数产生影响,因此使用成员运算符函数重载++和--的成员函数通常返回指针this。
2)由于友元运算符函数没有this指针,因此不能引用this指针所指的对象,使用友元函数重载++、--时,应采用引用参数传递数据。
3)采用前缀和后缀方式的函数内部的语句可以相同,也可以不同,这取决于用户的考虑。
例子:
class book
{
public:
 book(int i=0,int j=0);
 void print();
 friend book operator++(book &op);
private:
 int x,y;
};

book::book(int i,int j)
{
 x=i;
 y=j;
}

void book::print()
{
 cout<<" x: "<<x<<", y "<<y<<endl;
}

book operator++(book &op)  //此处不能写成book operator++(book op), 参数必须是引用传递类型,而不是值传递。若这样做,以下主函数输出的值是不变的
{
 ++op.x;
 ++op.y;
 return op;
}

void main()
{
 book ob(20,30);
 ob.print();
 operator++(ob);
 ob.print();
 ++ob;
 ob.print();
}

 

posted @ 2011-12-18 12:03 hoshelly 阅读(355) | 评论 (0)编辑 收藏

类的实现:

class CNumber
{
private:
 int n;
public:
 CNumber(int number)
 {
  n=number;
 }
 ~CNumber(){ }
 int isEven()
 {
  if(n%2==0)
   return 1;
  else
   return 0;
 }
 
 int isOdd()
 {
  if(n%2!=0)
   return 1;
  else
   return 0;
 }
 
 int isPrime()
 {
  int i;
  if(n<1)
   return 0;
  for(i=2;i<=sqrt(n);i++)
   if(n%i==0)
    return 0;
   else
       return 1;
  return 0;
 }
 bool isAPrime(int n)
 {
  for(int i=2;i<sqrt(n);i++)
   if(n%i==0)
    return false;
   return true;
 }
 
 int isGoldBach()
 {
  int i;
  int halfnum=n/2;
   for(i=2;i<=halfnum;i++)
          if(isAPrime(i)&&isAPrime(n-i))
     return 1;
 
  
  return 0;
 }

 void print()
 {
  if(isEven())  cout<<"This number is even."<<endl;
     if(isOdd())   cout<<"This number is odd."<<endl;
     if(isPrime())  cout<<"This number is prime."<<endl;
     if(isGoldBach())  cout<<"This number is goldbach."<<endl;
 }
};


主函数:

void main()
{
 int num;
 cout<<"Please enter one number:"<<endl;
 cin>>num;
 CNumber numb(num);
  numb.print();
}



posted @ 2011-11-26 13:38 hoshelly 阅读(431) | 评论 (0)编辑 收藏

二分法思想:假定f(x)在区间(x,y)上连续   
先找到a、b属于区间(x,y),使f(a),f(b)异号,
说明在区间(a,b)内一定有零点,然后求f[(a+b)/2],   
现在假设f(a)<0,f(b)>0,a<b    
①如果f[(a+b)/2]=0,该点就是零点,   如果f[(a+b)/2]<0,则在区间((a+b)/2,b)内有零点,(a+b)/2=>a,从①开始继续使用   中点函数值判断。   如果f[(a+b)/2]>0,则在区间(a,(a+b)/2)内有零点,(a+b)/2<=b,从①开始继续使用   中点函数值判断。   这样就可以不断接近零点。   通过每次把f(x)的零点所在小区间收缩一半的方法,使区间的两个端点逐步迫近函数的零点,以求得零点的近似值,这种方法叫做二分法。

头文件定义
class CEquation
{
private:
 double solution;            //方程的近似解
 double a, b;                //近似解的区间
 double (*p_fx)(double x);   //p_fx是一个指向函数的指针,指向方程式求值函数
 double (*p_solution)(double x, double y); //指向由近似解区间求近似解的函数的指针
 double delta;               //求解精度
public:
 CEquation(double av, double bv, double (*p1)(double), double (*p2)(double,double), double dv);
 double biSection();        //二分法求方程近似解
 void printSolution() const;
};

类的实现及主函数实现:
CEquation::CEquation(double a_val, double b_val, double (*p1)(double), double (*p2)(doubledouble), double delta_val)
{
    a 
= a_val;
    b 
= b_val;
    p_fx 
= p1;
    p_solution 
= p2;
    solution 
= p_solution(a, b);
    delta 
= delta_val;
}


double fx(double x)  //方程为: e^x+4^x3-6^x2+3x-2=0
{
    
return exp(x)+4.0*x*x*x-6.0*x*x+3.0*x-2.0;
}


double middle(double x, double y)  //中值
{
    
return 0.5*(x+y);
}


double CEquation::biSection()
{
double h;

    
while (fabs(a-b) > delta)
    
{
        h 
= p_solution(a, b);
        
if (p_fx(a)*p_fx(h) > 0)
            a 
= h;
        
else
            b 
= h;
    }

    solution 
= p_solution(a, b);
    
return solution;
}


void CEquation::printSolution() const
{
    cout 
<< "Solution is: " << solution << endl;
}


void main ()
{
CEquation a(
0.01.0, fx, middle, 1e-6);

    a.biSection();
    a.printSolution();
}




posted @ 2011-11-25 20:54 hoshelly 阅读(2114) | 评论 (0)编辑 收藏

     摘要:  小累,国庆假期过去一大半,C++这时候才把递归和函数这一块知识点慢慢地啃完了,结束之前,今晚自己写了一个小程序,实现四则运算,适合小学生使用。程序说明:1)允许用户选择一种类型的算术问题来学习,输入1表示加法,2表示减法,3表示乘法,4表示除法,5表示四种混合运算;2)由于程序代码中反复无穷递归,所以该程序的运算会不断进行下去,退出请自动关闭程序;源代码如下: Code highl...  阅读全文

posted @ 2011-10-05 22:03 hoshelly 阅读(1220) | 评论 (1)编辑 收藏



下午照样看C++how to program,加油,要到数组和指针了,接下来就是类的深入剖析了,这几天在加强火力猛攻这块地方,哈哈,还是编程菜鸟!不过我自己感觉比大一那时候真的有点进步了,有那种coding 的感觉了,努力学习还是有收获的。闲话休说,把今天下午自己写的代码发上来!
#include<iostream>
using namespace std;
int main()
{
    
int row,line;//定义行、列数的变量
    for(row=1;row<=10;row++)
    {
        
for(line=1;line<=row;line++)
            cout
<<"*";
        cout
<<endl;
    }
    cout
<<"\n";//第一个图形至此打印出来
    for(row=10;row>=1;row--)
    {
        
for(line=1;line<=row;line++)
            cout
<<"*";
        cout
<<endl;
    }
    cout
<<"\n";//第二个图形打印出来
    for(row=10;row>=1;row--)
    {
        
for(line=1;line<=row;line++)
            cout
<<"*";
            cout
<<endl;
        
for(line=1;line<=(11-row);line++)
            cout
<<" ";
    
    }
    cout
<<"\n";// 第三个图形打印出来
    for(row=1;row<=10;row++)
    {
        
for(line=1;line<=10-row;line++)
            cout
<<" ";

        
for(line=1;line<=row;line++)
            cout
<<"*";
            cout
<<endl;
    
    } 
//最后一个图形!
    cout<<endl;
    
return 0;

}


程序运行结果





posted @ 2011-09-25 16:13 hoshelly 阅读(427) | 评论 (0)编辑 收藏


我们知道,std::cout<<endl是使输入的数强制输出,以前我没发现,今天发现,如果是输入一行数的话,使用这个std::cout<<endl,程序是默认每输出一个数就回车的,而不是排成一行!
请看一下一例:
该程序要求输入长度,然后输出一个四条边都带相同数量星号的矩形。
#include<iostream>
using namespace std;
int main()
{
    
int side,rowPosition,size;
    cout
<<"input the square side: ";//输入矩形的宽度
    cin>>side;
    size
=side;//使长宽的边所带星号数量相同
    while(side>0)//双重循环输出矩形
    {
        rowPosition
=size;
        
while(rowPosition>0)
        
{
            
if(size==side||side==1||rowPosition==1||rowPosition==size)
                cout
<<'*'<<;
            
else
                cout
<<' ';
            
--rowPosition;
        }

        cout
<<'\n';//在这里等一行自然输出后,在利用cout<<‘\n'回车,输出下一行
        --side;
    }

    cout
<<endl;//这里总的强制输出所有输入的字符
      return 0;
    
}

        
        




    

    

程序运行效果如下图,输入8;





如果在程序的每条cout语句中加上<<endl; 那么程序运行的效果(图所限,"end line": inserts a newline into the stream and calls flush.有省略一些)如下:





后注:刚刚在维基百科里查到std::endl的定义,它说,"end line": inserts a newline into the stream and calls flush. 这就是说endl的功能就是强制输出和换行,现在懂了,感谢博友的认真更正,学习了。:)

posted @ 2011-09-23 05:18 hoshelly 阅读(421) | 评论 (2)编辑 收藏


Tonight I was studying the credit card codes, all of these was copied from my book,"C++ ——how to program".

This are the program demand:
1)Account number(an integer);
2)Balance at the begining of the month;
3)Total all items charged by this customer this month;
4)Total of all items applied to this customer's account this month;
5)Allowed credit limit.


The program should input each of these facts, calculate the new balance(=begining balance+charges-credits) and determine if the new balance exceeds the customer credit limit.
For those customers whose credit limit is exceeded, the program should display the customer's account number,credit limit,new balance and the message "Credit limit exceeded".

#include<iostream>
#include
<iomanip>// formatting integers and the precision of floating point values
using namespace std;

int main()
{
    
int accountNumber;
    
double balance,charges,credits,limit;

    cout
<<"Enter account number(-1 to end):"
        
<<setiosflags(ios::fixed|ios::showpoint);// to convert data from fixed point number representation to floating point representation
    cin>>accountNumber;
    
while(accountNumber!=-1)
    
{
        cout
<<"Enter beginning balance:";
        cin
>>balance;
        cout
<<"Enter total charges: ";
        cin
>>charges;
        cout
<<"Enter total credits:";
        cin
>>credits;
        cout
<<"Enter credit limit:";
        cin
>>limit;
        balance
+=charges-credits;

        
if(balance>limit)
            cout
<<"Account:  "<<accountNumber
                
<<"\nCredit limit: "<<setprecision(2)<<limit //accurate to two decimal point
                
<<"\nBalance:    "<<setprecision(2)<<balance
                
<<"\nCredit Limit Exceeded.\n";
        cout
<<"\nEnter account number(-1 to end): ";
        cin
>>accountNumber;
    }


    cout
<<endl;
    
return 0;
}


 

input -1 and the program will be ended.

posted @ 2011-09-23 01:13 hoshelly 阅读(272) | 评论 (0)编辑 收藏

仅列出标题
共12页: First 4 5 6 7 8 9 10 11 12