用C/C++解决一些数学问题(二)
2015-05-27 17:00
435 查看
1:在日常生活中,一大堆数据,我们要求出这些数据的最大值,这里以整数为例,主要也是重新缕清一下思路
需求:求出20个数中的最大的数,并且输出:
上面的代码显的过于臃肿,且执行效率不高,另外这个框架限制的条件比较死,能不能实现确定多少个数,然后再求出这些数的最大值呢
需求更改:事先确定多少个数,然后输入指定数量的数,求出最大值,代码如下:
2:有时候,一个很长的字符串,我们想将这个字符串全部倒过来,或者全部变成大小写,甚至给这个字符串加密,或者解密等等,我们该怎么处理这些字符串呢
需求:用户输入一段字符,按指定的要求对这个字符串进行加密,然后再排列,最后将密文输出;如果用户选择解密的字符,那么用户输入密文,将密码原文输出
要求1:这段程序用户可以选择加密或者解密,加密执行对应的加密程序,解密执行对应的解密程序
2:加密过程是将字符串全部颠倒过来,然后根据颠倒过来的每个字符的ascii值,根据字符串的长度,和字符所在的位置生成密文
3:加密解密可以选择
根据大致分析,代码如下:
#include <iostream>
using namespace std;
int main()
{
while (true)
{
int a;
cout<<endl<<"===================================="<<endl;
cout<<"选择功能,1:加密\t 2:解密"<<endl<<"请选择:";
cin>>a;
char str[20];
int lenth;
char mstr[20];
switch (a)
{
case 1:
cout<<"输入要加密的字符串:";
cin>>str;
lenth=strlen(str);
for(int i=0;i<=lenth;i++)
{
if(i<lenth)
{
mstr[i]=str[lenth-i-1];
}
else
{
mstr[i]='\0';
}
}
for(int j=0;j<lenth;j++)
{
mstr[j]=mstr[j]+lenth-j;
}
cout<<"加密好的密文:"<<mstr<<endl;
break;
case 2:
cout<<"输入要解密的密文:";
cin>>mstr;
lenth=strlen(mstr);
for (int i = 0; i < lenth; i++)
{
mstr[i]=mstr[i]-lenth+i;
}
for(int i=0;i<=lenth;i++)
{
if(i<lenth)
{
str[i]=mstr[lenth-i-1];
}
else
{
str[i]='\0';
}
}
cout<<"密码原文是:"<<str<<endl;
break;
default:
break;
}
}
}运行结果:
3:经常会查询一个字符串里一共含有指定的字符多少个
代码如下:
#include <iostream>
using namespace std;
int fun1(char str[],char n);
int main()
{
while(1)
{
char str1[300];
char n;
cout<<"输入字符串:";
cin>>str1;
cout<<"输入包含的字符:";
cin>>n;
int mm=sizeof(str1);
cout<<"一共包含的个数:"<<fun1(str1,n)<<endl;
}
}
int fun1(char str[],char n)
{
char str1[300];
strcpy_s(str1,sizeof(str1),str);
int lenth=sizeof(str1);
int num=0;
for(int i=0;i<lenth;i++)
{
if(str[i]==n)
{
num=num+1;
}
}
return num;
}
运行结果:
需求:求出20个数中的最大的数,并且输出:
#include <iostream> using namespace std; int main() { while (true) { int a[20]; for(int i=0;i<20;i++) { cout<<"请输入第"<<i+1<<"个数:"; cin>>a[i]; } for(int j=0;j<20;j++) { if(j==0) { a[j]=a[j]; } else { if(a[j]>a[j-1]) { a[j]=a[j]; } else { a[j]=a[j-1]; if(j==19) { cout<<"最大的数是:"<<a[j]<<endl; } } } } } }运行结果:
上面的代码显的过于臃肿,且执行效率不高,另外这个框架限制的条件比较死,能不能实现确定多少个数,然后再求出这些数的最大值呢
需求更改:事先确定多少个数,然后输入指定数量的数,求出最大值,代码如下:
#include <iostream> using namespace std; int main() { while (true) { int n; cout<<"请输入你统计的数的数量:"; cin>>n; int max; int mm; for(int i=0;i<n;i++) { cout<<"请输入第"<<i+1<<"个数:"; cin>>mm; if(i==0) { max=mm; } else { if(max<mm) { max=mm; } } } cout<<"这"<<n<<"个数最大的数是:"<<max<<endl; } }运行结果:
2:有时候,一个很长的字符串,我们想将这个字符串全部倒过来,或者全部变成大小写,甚至给这个字符串加密,或者解密等等,我们该怎么处理这些字符串呢
需求:用户输入一段字符,按指定的要求对这个字符串进行加密,然后再排列,最后将密文输出;如果用户选择解密的字符,那么用户输入密文,将密码原文输出
要求1:这段程序用户可以选择加密或者解密,加密执行对应的加密程序,解密执行对应的解密程序
2:加密过程是将字符串全部颠倒过来,然后根据颠倒过来的每个字符的ascii值,根据字符串的长度,和字符所在的位置生成密文
3:加密解密可以选择
根据大致分析,代码如下:
#include <iostream>
using namespace std;
int main()
{
while (true)
{
int a;
cout<<endl<<"===================================="<<endl;
cout<<"选择功能,1:加密\t 2:解密"<<endl<<"请选择:";
cin>>a;
char str[20];
int lenth;
char mstr[20];
switch (a)
{
case 1:
cout<<"输入要加密的字符串:";
cin>>str;
lenth=strlen(str);
for(int i=0;i<=lenth;i++)
{
if(i<lenth)
{
mstr[i]=str[lenth-i-1];
}
else
{
mstr[i]='\0';
}
}
for(int j=0;j<lenth;j++)
{
mstr[j]=mstr[j]+lenth-j;
}
cout<<"加密好的密文:"<<mstr<<endl;
break;
case 2:
cout<<"输入要解密的密文:";
cin>>mstr;
lenth=strlen(mstr);
for (int i = 0; i < lenth; i++)
{
mstr[i]=mstr[i]-lenth+i;
}
for(int i=0;i<=lenth;i++)
{
if(i<lenth)
{
str[i]=mstr[lenth-i-1];
}
else
{
str[i]='\0';
}
}
cout<<"密码原文是:"<<str<<endl;
break;
default:
break;
}
}
}运行结果:
3:经常会查询一个字符串里一共含有指定的字符多少个
代码如下:
#include <iostream>
using namespace std;
int fun1(char str[],char n);
int main()
{
while(1)
{
char str1[300];
char n;
cout<<"输入字符串:";
cin>>str1;
cout<<"输入包含的字符:";
cin>>n;
int mm=sizeof(str1);
cout<<"一共包含的个数:"<<fun1(str1,n)<<endl;
}
}
int fun1(char str[],char n)
{
char str1[300];
strcpy_s(str1,sizeof(str1),str);
int lenth=sizeof(str1);
int num=0;
for(int i=0;i<lenth;i++)
{
if(str[i]==n)
{
num=num+1;
}
}
return num;
}
运行结果:
相关文章推荐
- 用C/C++解决一些数学问题(三)
- 用C/C++解决一些数学问题(一)
- 用C++解决数学类问题的练习
- C/C++运行中的一些问题及其解决
- C++ 工作中碰到的一些问题,以及解决的办法
- C/C++ 日常使用遇到的一些问题与解决
- 关于java jni调用c++动态库的一些问题及解决方法
- 怎样才能把一个代码变成软件成品?一个初学者的困惑,我们写的代码都只能是解决一些数学问题而已。怎么把它变成一个软件。
- c++ 导致内存泄露的一些小问题与解决方法
- 一些可以用动态规划(DP)算法解决的问题(C++)
- C++重载运算符+,发现一些问题,暂未解决
- C#与C/C++ dll之间数据传递的一些问题与解决
- Centos6.5 python升级成2.7版本出现的一些问题解决方法
- struts1+tomcat6.0+jbpm整合中遇到的问题遇到的一些问题及解决办法
- C++/python解决无穷皇后问题
- 关于解决tomcat的一些问题-内存溢出 PermGen space以及超时
- 到目前为止想要解决而又没有解决的一些问题
- 如何尽可能高效地使用论坛+解决编程问题的一些方法
- c/c++中利用gsoap调用webservice服务的一个诡异问题解决方法
- 《Visual C++ 2010入门教程》系列三:关于C++的一些问题