15、输入两个字符串,连接成一个字符串,并输出。用指针实现。
#include<iostream>
using namespace std;
void main()
{
char s1[100],s2[100];
cin>>s1>>s2;
char *p,*q;
p=s1;q=s2;
while(*p!='/0')
{
p++;
}
while(*q!='/0')
{
*p++=*q++;
}
*p='/0';
cout<<s1<<endl;
}
16、编写一个简单的加密/解密程序,函数encrypt以一个字符指针为参数,改变字符串中每个元素的值,
将其加1。函数decrypt以一个字符指针为参数,改变字符串中每个元素的值,将其减1。main函数中定义
一个字符数组存储一个字符串,调用encrypt函数和decrypt函数,并打印加密、解密后的字符串。
#include<iostream>
using namespace std;
void jiami(char *s)
{
while(*s!='/0')
{
*s=*s+1;
s++;
}
}
void jiemi(char *s)
{
while(*s!='/0')
{
*s=*s-1;
s++;
}
}
void main()
{
char word[50];
cout<<"请输入要加密的字符:"<<endl;
cin>>word;
char *ss;
ss=word;
jiami(ss);
cout<<"加密后/t"<<ss;
cout<<"请输入要解密的字符:"<<endl;
cin>>ss;
jiemi(ss);
cout<<"解密的结果是:/t"<<ss<<endl;
}
********************************************************************************************
*********************************
17、请用户输入10首歌名,歌名存入一字符指针数组,然后分别按原序、字母序和字母逆序(从Z到A)显
示这些歌名。
#include<iostream>
using namespace std;
void zheng(char *music[],int n)
{
char *p;
int i,j;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(strcmp(music[i],music[j])>0)
{
p=music[i];
music[i]=music[j];
music[j]=p;
}
}
}
void fan(char *music2[],int n2)
{
char *p;
int i,j;
for(i=0;i<n2-1;i++)
for(j=i+1;j<n2;j++)
{
if(strcmp(music2[i],music2[j])<0)
{
p=music2[i];
music2[i]=music2[j];
music2[j]=p;
}
}
}
void main()
{
char *music[]=
{"love","zordream","lovingyou","flying","bird","fast","good","hoho","yellow"};
int n=9;
int n2;
for(int c=0;c<n;c++)
cout<<music[c]<<" ";
cout<<endl;
cout<<"输入1则按字母顺序排列:输入2则按字母逆顺序排列"<<endl;
cin>>n2;
if(n2==1)
{
zheng(music,n);
cout<<"正序:"<<endl;
for(int a=0;a<n;a++)
cout<<music[a]<<endl;
cout<<endl;
}
else{
cout<<"逆序:"<<endl;
fan(music,n);
for(int b=0;b<n;b++)
cout<<music[b]<<endl;
}
}
********************************************************************************************
*********************************
18、编写一个电话号码薄应用系统,初始化一个电话号码薄,号码薄中有姓名、电话号码。编写一个菜单
,具有按电话号码查找、按姓名查找和退出功能。按电话号码查找时,输入电话号码,输出姓名及电话号
码或未找到,若输入的是部分号码,如“130”则输出所有以“130”开头的号码;按姓名查找时,输入姓
名,输出姓名及电话号码或未找到,若输入“杨”时,列出所有姓名以“杨”开头的姓名及号码;选择退
出时结束程序,否则继续选择功能。
#include<iostream>
using namespace std;
struct phone
{
char name[50];
char num[50];
char address[50];
} p1={"张无忌", "13065301124", "光明顶"},
p2={"洪七公", "15124102854", "丐帮"},
p3={"东方不败", "13954871124", "日月神教"},
p4={"张三丰", "13065301124", "武当山"},
p5={"令狐冲", "15124108724", "恒山"},
p6={"胡斐", "13054878534", "雪山"},
p7={"韦小宝", "13065301704", "皇宫"},
p8={"萧峰", "15124102157", "丐帮"},
p9={"段玉", "13954871180", "燕子屋"},
p10={"慕容复", "13065301124", "燕子屋"},
p11={"虚竹", "15124102124", "山洞"},
p12={"殷天正", "13054871134", "天鹰教"};
void output(phone s[],int n) //这个输出找到的结果的函数:
{
cout<<s[n].name<<"/t"<<s[n].num<<"/t"<<s[n].address<<endl;
}
void findname(phone pp[],char ss[])
{
int t=strlen(ss);
for(int i=0;i<12;i++)
{
for(int j=0;j<t;j++)
{
if(ss[j]!=pp[i].name[j])
break;
else if(j==(t-1))
{
output(pp,i);
break;
}
}
}
}
void findtel(phone pp[],char ss[])
{
int t=strlen(ss);
for(int i=0;i<12;i++)
{
for(int j=0;j<t;j++)
{
if(ss[j]!=pp[i].num[j])
break;
else if(j==(t-1))
{
output(pp,i);
break;
}
}
}
}
void main()
{
phone p[12]; //定义一个结构数组
p[0]=p1; p[1]=p2; p[2]=p3; p[3]=p4; p[4]=p5; p[5]=p6; //把结构数组中的元素初始化
p[6]=p7; p[7]=p8; p[8]=p9; p[9]=p10; p[10]=p11;
cout<<"选择要查找的方式,按相应数字:"<<endl;
cout<<"****************1:按姓名查找****************"<<endl;
cout<<"****************2:按号码查找****************"<<endl;
cout<<"****************3:退出电话本****************"<<endl;
for(int z=0;z<1000;z++) //循环1000次
{
int choice;
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"请输入姓名:"<<endl;
char s[10];
cin>>s;
findname(p,s);
break;
}
case 2:
{
cout<<"请输入电话号码:"<<endl;
char s[10];
cin>>s;
findtel(p,s);
break;
}
case 3:
break;
}
if(choice==3)
{
cout<<"查找结束"<<endl;
break;
}
}
}
********************************************************************************************
*********************************
(19)://温度转换
#include <iostream>
using namespace std;
inline double zhuanhuan (double x)
{
return (5/9.0)*(x-32);
}
inline double zhuanhuan2 (double x)
{
return (9*x-160)/5.0;
}
void main()
{
cout<<"请输入摄氏温度";
int x;
cin>>x;
cout<<zhuanhuan(x)<<endl;
cout<<"请输入华氏温度";
int y;
cin>>y;
cout<<zhuanhuan2(y)<<endl;
}
********************************************************************************************
*********************************
20编程求解约瑟夫数问题
#include<iostream>
using namespace std;
int qiujie(int *Child,int n,int m)
{
int i=-1,j=0,k=1;
while(1)
{
for(j=0;j<m;)
{
i=(i+1)%n;
if(Child[i]!=-1)
j++;
}
if(k==n)
break;
cout<<Child[i]<<",";
Child[i]=-1;
k=k+1;
}
cout<<endl;
return(Child[i]);
}
void main()
{
int *allChild,j,k,l;
cin>>j>>k;
if((allChild= new int[j])!=NULL)
{
for(l=0;l<j;l++)
{
cout<<l+1<<",";
allChild[l]=l+1;
}
cout<<endl;
cout<<"最后胜出的小孩:"<<endl<<qiujie(allChild,j,k)<<endl;
}
}
********************************************************************************************
*********************************
21:求平均成绩和及格率(80分及格)
#include<iostream>
using namespace std;
int main()
{
int i,n,x=0,z=0;
cin>>n;
for (i=0;n<=100;++i)
{
x+=n;
if (n>=80)
{
z++;
}
cin>>n;
if (n<0)
break;
}
cout<<"平均分是"<<(double)x/i<<endl;
if ((double)(z/i)>=0.8)
cout<<"A"<<endl;
else
cout<<"B"<<endl;
return 0;
}
********************************************************************************************
*********************************
22://从键盘输入一个字符串,倒序输出;
#include<iostream>
using namespace std;
void main()
{
int i;
char s[30];
cin>>s;
int t;
t=strlen(s);
for(i=t;t>=0;t--)
cout<<s[t];
}
********************************************************************************************
*********************************
23://1+3+5+..99
#include<iostream>
using namespace std;
int x(int z=99)
{
if (z==1)
return 1;
else
return z+x(z-2);
}
void main()
{
cout<<x();
}
********************************************************************************************
*********************************
24://编写一个函数接收一个整数,该函数返回该整数的所有因子和
#include<iostream>
using namespace std;
int h(int n)
{
int jieguo=0;
while(n!=0)
{
int t=n%10;
jieguo+=t;
n=n/10;
}
return jieguo;
}
void main()
{
int x;
cin>>x;
cout<<h(x)<<endl;
}
********************************************************************************************
*********************************
25:求圆周率
#include<iostream>
using namespace std;
void main()
{
int i,n=1; double s=0;
for (i=1; (1.0/(2*i-1))>1e-8; i++)
{
s=s+(1.0/(2*i-1))*n;
n*=-1;
}
cout<<4*s<<endl;
}
********************************************************************************************
*********************************
26:求CMN
#include<iostream>
using namespace std;
double cmn (double m,double n)
{
int i,sum2=1,t=n;
int sum=1;
while (n>=1)
{
sum=sum*n;
n--;
}
for (i=1;i<=t;i++)
{
sum2=sum2*m;
m--;
}
return (double) (sum2/sum);
}
void main()
{
int x,y;
cin>>x>>y;
cout<<cmn(x,y)<<endl;
}
********************************************************************************************
*********************************
27://求2的几次方
#include<iostream>
using namespace std;
int main()
{
cout<<"请输入想求的2的几次方"<<endl;
int a=2,b=1,c;
cin>>c;
for (int d=0;d<c;++d)
b*=a;
cout<<"2的"<<c<<"次方="<<b<<endl;
}
********************************************************************************************
*********************************
28://判断是否为素数:
#include<iostream>
using namespace std;
bool sushu(int x)
{
for(int i=2;i<x;i++)
if(x%i==0)
return false;
return true;
}
void main()
{
int xx;
cin>>xx;
cout<<sushu(xx)<<endl;
}
********************************************************************************************
*********************************
29:猴子吃桃递归求
#include <iostream>
using namespace std;
int eat(int n)
{
if (10==n)
return 1;
else
return 2*(eat(n+1)+1);
}
void main()
{
for (int i=1; i<=10; i++)
cout<<"第"<<i<<"天还剩桃子:"<<eat(i)<<endl;
}
********************************************************************************************
*********************************
(30) 编写一个实现对学生成绩管理的程序,每名学生信息包括学生的姓名、学号、数学成绩、英语成绩
。要求:程序每次运行时从文件中读取学生成绩信息到结构数组中(最多100名学生),程序结束前将学
生信息写到文件中。设计一个菜单,具有1、显示所有学生信息,2、增加一名学生信息,3、修改一名学
生信息,4、排序,5、退出功能。文件可以采用文本文件也可以采用二进制文件
#include<iostream>
#include<fstream>
using namespace std;
struct student
{
char name[10];
int num;
int math;
int eng;
};
void sortnum(student s[],int n)
{
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(s[i].num>s[j].num)
{
student x;
x=s[i];
s[i]=s[j];
s[j]=x;
}
}
void sortmath(student s[],int n)
{
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(s[i].math<s[j].math)
{
student x;
x=s[i];
s[i]=s[j];
s[j]=x;
}
}
void printall(student s[],int n)
{
cout<<"姓名"<<"/t"<<"学号"<<"/t"<<"忍术"<<"/t"<<"体术"<<endl;
for(int z=0;z<n;z++)
{
cout<<s[z].name<<"/t"<<s[z].num<<"/t"<<s[z].math<<"/t"<<s[z].eng<<endl;
}
}
void insert(student s[],int n)
{
cout<<"请输入要添加的学生的信息:"<<endl;
cin>>s[n].name>>s[n].num>>s[n].math>>s[n].eng;
}
void xiugai(student s[],int n)
{
cout<<"请输入要修改的学生的序号:"<<endl;
int num;
cin>>num;
int x;
for(x=0;s[x].num!=num;x++);
cout<<"请输入新的忍术成绩";
int mathnew;
cin>>mathnew;
cout<<"请输入新的体术成绩";
int engnew;
cin>>engnew;
s[x].math=mathnew;
s[x].eng=engnew;
cout<<"修改结束,请继续选择要执行的操作^_^"<<endl;
}
void main()
{
int MAX;
cout<<"请输入目前学生人数:"<<endl;
cin>>MAX;
student s[100];
ifstream f;
f.open("d://student.txt",ios::in);
int i;
for(i=0;i<MAX;i++)
f>>s[i].name>>s[i].num>>s[i].math>>s[i].eng;
cout<<"*******************************************************"<<endl;
cout<<"木叶村忍者学校第一次模拟考试学生成绩管理系统:"<<endl;
cout<<"选择要执行的操作:"<<endl;
cout<<"1:显示所有学生信息:"<<endl;
cout<<"2:增加一个学生信息:"<<endl;
cout<<"3:修改一个学生信息:"<<endl;
cout<<"4:排序"<<endl;
cout<<"5:退出"<<endl;
cout<<"*******************************************************"<<endl;
int choice;
cin>>choice;
while(choice!=5)
{
if(choice==1)
{
printall(s,MAX);
}
else if(choice==2)
{
insert(s,MAX);
MAX++;
cout<<"添加结束,请继续选择操作^_^:"<<endl;
}
else if(choice==3)
{
xiugai(s,MAX);
}
else if(choice==4)
{
cout<<"1:按学号排序"<<endl;
cout<<"2:按忍术成绩排序"<<endl;
int choice2;
cin>>choice2;
if(choice2==1)
sortnum(s,MAX);
else if(choice2==2)
sortmath(s,MAX);
cout<<"排序结束,请继续选择要执行的操作^_^"<<endl;
}
ofstream f;
f.open("d://student.txt",ios::out);
for(int i=0;i<MAX;i++)
f<<s[i].name<<" "<<s[i].num<<" "<<s[i].math<<" "<<s[i].eng<<endl;
cin>>choice;
}
cout<<"谢谢使用"<<endl;
}