C++必备18个经典程序(2)

来源:互联网 发布:淘宝上买的弹弓枪拒收 编辑:程序博客网 时间:2024/06/07 22:20
10、/*编写一个void sort(int *x,int n)实现将x数组中的n个数据从大到小 
排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件f1.txt中*/
#include <iostream>
#include <fstream>
using namespace std;
void sort (int *x,int n)
{
int i,j,k,t;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
 if(x[j]>x[k]) k=j;
 if(k!=i)
 {
 t=x[i];
 x[i]=x[k];
 x[k]=t;
 }
}
}
int main()
{
ofstream outfile("f1.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int a[10],i;
cout<<"please input 10 numbers:"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
sort(a,10);
              for(i=0;i<10;i++)
{
cout<<a[i]<<" ";
outfile<<a[i]<<" ";
}
outfile.close();
return 0;
}


11、已知数组a中的元素已按由小到大顺序排列,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a[10]={0,12,14,17,34,45,67};
int x,i,j=6;
cout<<"Input a number:"<<endl;
cin>>x;
i=j;
while(a[i]>x)
{
a[i+1]=a[i];
i--;
}
a[++i]=x;
j++;
for(i=0;i<=j;i++)
cout<<a[i]<<",";
cout<<endl;
}


12、/*编写函数replace(char *s,char c1,char c2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件f2.txt中*/


#include <iostream>
#include <fstream>
using namespace std;


void replace(char *s,char c1,char c2)
{
while(*s!='\0')
{
if(*s==c1)
*s=c2;
s++;
}
}
int main()
{
ofstream outfile("f2.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
    cout<<"Enter a string:"<<endl;
char str[100],a,b;
cin>>str;
cout<<"Enter a&b:"<<endl;
cin>>a>>b;
cout<<str<<endl;
outfile<<str<<endl;
replace(str,a,b);
cout<<"New string is:"<<endl;
cout<<str<<endl;
outfile<<str<<endl;
outfile.close();
return 0;
}
13、/*在一个字串s1中查找一子串s2,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/
#include <iostream>
#include <fstream>
using namespace std;


int search(char s1[],char s2[])
{
int i=0,j,len=strlen(s2);
while(s1[i])
{
for(j=0;j<len;j++)
if(s1[i+j]!=s2[j])
break;
if(j>=len)
return i;
else
i++;
}
return -1;
}
int main()
{
char s1[7]="thisis";
char s2[3]="is";
cout<<search(s1,s2);
return 0;
}


14、/*用指针变量输出结构体数组元素。*/
#include <iostream>
#include <fstream>
using namespace std;


struct student
{
int num;
char *name;
char sex;
int age;
}stu[5]={{1001,"lihua1",'F',18},{1002,"lihua2",'M',18},
{1003,"lihua3",'F',18},{1004,"lihua4",'F',18},
{1005,"lihua5",'F',18}};
int main()
{
struct student *p=stu;
for(p=stu;p<stu+5;p++)
cout<<p->num<<" "<<p->name<<" "<<p->sex<<" "<<p->age<<endl;
return 0;
}
15、/*建立一个有三个结点的简单链表:*/
#include <iostream>
#include <fstream>
using namespace std;


struct student
{
int num;
char *name;
int age;
struct student *next;
};


int main()
{
struct student a,b,c,*head,*p;
a.num=1001;
a.name="lihua";
a.age=13;
b.num=1002;
b.name="xiaopeng";
b.age=15;
c.num=1003;
c.name="xiaoming";
c.age=22;
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do{
cout<<p->num<<" "<<p->name<<" "<<p->age<<endl;
p=p->next;
}while(p!=NULL);
return 0;


}


16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[100];
int i,j,n;
cout<<"Please input a string:"<<endl;
cin>>s;
n=strlen(s);
for(i=0,j=n-1;i<j;i++,j--)
if(s[i]!=s[j])  //判断是否为回文
break;
if(i>=j)
cout<<"It is huiwenchuan"<<endl;
else 
cout<<"It is not huiwenchuan"<<endl;
return 0;


}
17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件f3.txt*/
#include <iostream>
#include <fstream>
using namespace std;
void fun(int a[],int n)
{
int i,j,k;
for(i=0;i<=n-1;i++)
for(j=0;j<n-1-i;j++)  //特别注意冒泡排序这个地方 不要写错了
if(a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}


}
int main()
{
int a[10]={12,45,7,8,96,4,10,48,2,46};
ofstream outfile("f3.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
fun(a,10);
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
outfile<<a[i]<<" ";
}
outfile.close();
return 0;

}

18、编写函数countpi,利用公式
    
计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件f4.txt中。

#include <iostream>
#include <fstream>
using namespace std;
double countpi(double eps)
{
int m=1;
double temp=1.0,s=0;
while(temp>=eps)
{
s+=temp;
temp=temp*m/(2*m+1);
m++;
}
return (2*s);
}
int main()
{
ofstream out("f4.txt",ios::out);
if(!out)
{
cerr<<"open error"<<endl;
exit(1);
}
double eps=1e-5;
double pi;
pi=countpi(eps);
cout<<pi<<endl;
out<<pi<<endl;
out.close();
return 0;
}

以上程序均通过VC++6.0测试通过。

下面是18个程序C语言的版本下载地址为:http://download.csdn.net/detail/shuoguo77/6368463


原创粉丝点击