声明:一刷题目时借鉴了两位博主的代码,题目也来自这两位博主,附上他们的CSDN博客: Dcwjh的博客 和GanonYou的博客
1、输入 n 个十进制数转换成二进制写到文件,n 是随机得到的
一刷代码:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stack>
#include <cmath>
using namespace std;
template < typename T > void pushElements( T &stackRef, int);
template < typename T > void popElements( T &stackRef);
void toBinary( int );
void toDemical( );
static stack < int > intDequeStack;
int main()
{
int n;
int integer;
int choice;
cout << " 1. demical change to binary." << endl;
cout << " 2. binary change to demical." << endl;
cout << " 0 to quit" << endl;
cin >> choice;
while ( choice )
{
switch (choice)
{
case 1:
cout << "Enter the number of integer: ";
cin >> n;
while ( n )
{
cout << "Enter the current integer:";
cin >> integer;
cout << "Binary: ";
toBinary( integer );
cout << endl;
--n;
}
break;
case 2:
cout << "\ndemical: " <<endl;
toDemical();
cout << endl;
break;
default:
break;
}
cout << " 1. demical change to binary." << endl;
cout << " 2. binary change to demical." << endl;
cout << " 0 to quit" << endl;
cin >> choice;
}
}
//十进制转换为二进制
void toBinary( int inte )
{
int reminder;
while ( inte > 0 )//全部压栈之后载出栈
{
reminder = inte % 2;
pushElements( intDequeStack, reminder );
inte /= 2;
}
popElements( intDequeStack );//出栈
}
//利用栈进行二进制到十进制的转换
template< typename T> void pushElements( T &stackRef ,int inte )
{
stackRef.push( inte );
}
template< typename T> void popElements( T &stackRef )
{
ofstream outFile( "binary.txt", ios::out | ios::app );
if( !outFile )
{
cerr << "File could not be opened." << endl;
exit( 1 );
}
while( !stackRef.empty() )
{
cout << stackRef.top();
outFile << stackRef.top();
stackRef.pop();
}
outFile << endl;
}
//二进制转换为十进制
void toDemical( )
{
string binToDec;
int sum = 0;
int i = 0;
ifstream inFile("binary.txt");
if( !inFile )
{
cerr << "File cannot opened!" << endl;
exit( 1 );
}
while( inFile >> binToDec )
{
//整型流基数的转换只针对整型数据,
//文件中读入的是二进制数据,所以无法完成转换
while( i < binToDec.size() )
{
if( binToDec[i] == '1')//写成“1”有错
sum += static_cast<int>(pow( 2.0,binToDec.size() - i -1 ));
i++;
}
cout << sum << endl;
//计数值在一组循环结束时,要归零重新计数。
sum = 0;
i = 0;
}
}
二刷代码:
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;
void toBinary( int );
int main()
{
int n;
cout << "Please enter the number of integers:";
cin >> n;
cout << "Please enter " << n << " integers:" << endl;
while(n--)
{
int inte;
cin >> inte;
toBinary( inte );
}
}
void toBinary( int inte)
{
stack<int> s;
int temp;
while(inte > 0)
{
temp = inte % 2;
s.push( temp );
inte /= 2;
}
ofstream outputFile("binary.txt",ios::app);
if(!outputFile)
{
cerr << "File can not be opened." << endl;
exit( 1 );
}
while (!s.empty())
{
cout << s.top();
outputFile << s.top();
s.pop();
}
outputFile << endl;
cout << endl;
}
2、写两个模板函数:插入排序法的迭代实现与递归实现
这题综合了网上资源和自己写的,好几个版本都放上来了,正确版本和错误版本我都标注出来了,大家可以对比下。参考网上的当时没有标记具体来源,现在已经不太好找了,对原作者说声抱歉。
#include <iostream>
using namespace std;
template < typename T > void insertSortIterator( T [], const int );
template < typename T > void insertSortRecursive( T [], const int );
template < typename T > void Insert( T [], const int );
int main()
{
int a []= {5,3,6,2,7,4,8,1,0,9};
double b[] = { 3.4, 1.1, 9.9, 0.3, 0.2, 5.4, 5.3, 5.5, 123, 3.39 };
insertSortIterator( a, sizeof( a ) / sizeof( *a ) -1 );
for( int item:a )
cout << item << " ";
cout <<endl;
insertSortRecursive( b, sizeof( b ) / sizeof( *b ) - 1 );
for( double item: b)
cout << item <<" ";
cout << endl;
}
//插入排序法的迭代实现
template < typename T >
void insertSortIterator( T a[], const int size )
{
//法一 RIGHT
//自己改写*******************************************
//for( int i = 1; i < 10; i++ )
//{
// int j;
// if( a[ i - 1 ] > a[ i ] )
// {
// int temp = a[ i ];
// for( j = i - 1; j >= 0; j -- )
// {
// if( a[ j ] > a[ j + 1 ] )
// a[ j + 1 ] = a[ j ];
// else
// break;//如果前者不再大于后者,此次循环可以跳过。
// a[ j ] = temp;
// }
// }
//}
//法二 RIGHT
//网上最优*******************************************
for (int i = 1; i < 10; i++)
{
//int temp = a[i];
//int j = i;
// //如果前面的元素小于temp,则向后移
//for (; j > 0 && a[j - 1] > temp; j--)
// a[j] = a[j - 1];
// //前一个元素(array[j - 1])和后一个元素(array[j])是相同的
// //在下一轮时,当array[j - 1]小于或等于temp时,将temp插入array[j](即上一轮的array[j - 1])
//a[j] = temp;
Insert( a, i );//与上述语句作用相同
}
//法三 RIGHT
//参考博客********************************************
//int i = 0;
//int temp;
//int j;
//for( i = 1; i < 10; i++ )
//{
// j = i;
// temp = a[ i ];
// while( j > 0 && a[ j - 1 ] > temp )
// {
// a[ j ] = a[ j - 1 ];
// j--;
// }
// a[ j ] = temp;
//}
wrong
//int i = 0;
//for( i = 1, i < size; i++ )
//{
// if( a[ i - 1 ] > a[ i ] )
// {
// int j = i;
// while( --j && a[ j ] > a[ j + 1 ] )
// a[ j + 1 ] = a[ j ];
// a[ j ] = a[ i ];
// }
//}
//for( i = 0; i < 10; i++ )
//{
// cout << a[ i ] << " ";
//}
//cout << endl;
}
//插入排序法的递归实现
template < typename T >
void Insert( T a[], const int size)
{
int j = size;
T temp = a[ size ];
for(; j >= 0 && a[ j -1 ] > temp; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = temp;
}
template < typename T >
void insertSortRecursive( T a[], const int size )
{
if( size > 0 )
{
insertSortRecursive( a, size - 1 );//规模缩小
Insert( a, size ); //缩小后排序
}
else
return;
}
//自己写的初始版本,WRONG!!!
//template < typename T >
//void insertSortRecursive( T a, const int size )
//{
// if( size == 1 )
// return array;
// else if( size == 2 )
// {
// if( a[ 0 ] > a[ 1 ] )
// swap( a[ 0 ], a[ 1 ] );
// return array;
// }
// else
// return insertSortRecursive( array, size - 1 );
//}
3、字符串的解析.
文件中有类似的一行行字符串“(010)(15012345678)|123|(430070)”,按以下格式输出: “区号| 电话号码| 城市编号| 邮编
一刷代码:
这个代码就是参考 GanonYou的这篇博客。
//文件中有类似的一行行字符串“(010)(15012345678)|123|(430070)”,
//按以下格式输出:“区号| 电话号码| 城市编号| 邮编”
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void outputLine( string, string, string, string );
int main()
{
ifstream inputFile("teleNumber.txt" ,ios::in );
if( !inputFile )
{
cerr << "File can notopened!" << endl;
exit( 1 );
}
cout << "area" << setw( 18 ) << "telephone number" << setw( 9 )
<< "NO.city" << setw( 13 ) << "mail number" << endl;
string info;
string area,tele,city,mail;
while( inputFile >> info)
{
//area = info.substr( 1, 3 );
//tele = info.substr( 6, 11);
//city = info.substr( 19, 3);
//mail = info.substr( 24, 6);
//区号
//参数:指定子串位置;子串长度
area = info.substr( info.find("(") + 1, info.find(")") - info.find("(") - 1);
info.erase(info.find("("), info.find(")") - info.find("(") + 1);
tele = info.substr( info.find("(") + 1, info.find(")") - info.find("(") - 1);
info.erase(info.find("("), info.find(")") - info.find("(") + 1);
info.erase(info.find("|"), 1);
city = info.substr( 0, info.find("|") );
info.erase(0, info.find("|") + 1);
mail = info.substr( info.find("(") + 1, info.find(")") - info.find("(") - 1);
info.erase(info.find("("), info.find(")") - info.find("(") + 1);
outputLine( area, tele, city, mail );
}
}
void outputLine( string area, string tele, string city, string mail )
{
cout << area << setw( 3 )<<"|"<< setw( 14 ) << tele << setw( 4 )<<"|"
<< setw( 5 ) << city << setw( 4 )<<"|"<< setw( 8 ) << mail << endl;
}
二刷代码:
二刷时觉得一刷时写代码太麻烦了,上述过程完全可以用strtok()/strtok_s()来简单解决,关于这两个函数的用法,我先放一个网上找到的博主链接,后面有空我再就这个知识点另写一篇博客。
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
#define MAXSIZE 100
int main()
{
ifstream inputFile( "teleNumber.txt" );
if(!inputFile)
{
cerr << "File can not opened!" << endl;
exit( 1 );
}
char str[MAXSIZE];
char *tokenPtr = NULL;
char *buf;
while (inputFile >> str)
{
//cout << "str:" << str;
tokenPtr = strtok_s(str,"()|",&buf);
while (tokenPtr)
{
if( *buf == '\0' )
//buf中的内容为‘\0’时,最后一个字符串已经复制给tokenPtr,只输出字符串就好
{
cout << tokenPtr;
break;
}
cout << tokenPtr << " | ";
tokenPtr = strtok_s( NULL, "()|",&buf);
//cout << buf << " ";
}
cout << endl;
}
}
4、设计一个多项式类 Polynomial :包括构造函数、复制构造函数、析构函数、赋值函数、 实现两个多项式相加
这里为了简化问题,我们将所有的多项式都统一看成
p = a0 + a1 x + a2x^2 + a3 x^3 + ………… + anx^n
然后将所有的系数a0、a1、a2直至an依次存储在一个向量vector中(其实数组也行)。
通过对vector元素的操作,我们就可以实现对多项式的基本操作。
一刷代码:
//Polynomial.h
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iomanip>
#include <vector>
using namespace std;
class Polynomial
{
public:
static const int SIZE = 100;
Polynomial();
//Polynomial( Polynomial & );
Polynomial( double [], const int );
~Polynomial();
void setCoe( double [], int );
void setMaxPow( int );
void print() const;
//赋值运算符,未经重载就可以运用于任何类对象,所以可以不写
//Polynomial operator=( Polynomial &p )
//{
// Polynomial::maxPow = p.maxPow;
// for( int i = 0; i < Polynomial::maxPow; i++ )
// {
// coe[ i ] = p.coe[ i ];
// }
// return *this;
//}
//两种重载运算符+的方法
Polynomial operator+( Polynomial &p)
{
int finalMaxPower = ( p.maxPow < maxPow ) ? p.maxPow : maxPow;
//this->maxPow = finalMaxPower;//要把最大幂次赋值给返回对象,否则返回对象保持原有的最大幂次
cout << fixed <<setprecision( 1 );
//较短的多项式相较于较长多项式,有一部分未被初始化,所以计算结果出现垃圾值
//当多项式短长/长短 出现顺序不同时,运行结果就有误,所以要先判断长短
Polynomial p3( ( p.maxPow > maxPow ) ? p : *this );
if( p.maxPow > maxPow )
{//多项式p更长,p3 = p,p3 = p3 + this;
for( int i = 0; i < finalMaxPower; i++ )
p3.coe[ i ] += coe[ i ];
return p3;
}
else
{//多项式this更长,p3 = this,p3 = p3 + p;
for( int i = 0; i < finalMaxPower; i++ )
p3.coe[ i ] += p.coe[ i ];
return p3;
}
}
//当运算符重载为类的成员函数时,函数的参数个数比原来的操作数要少一个(后置单目运算符除外),
//这是因为成员函数用this指针隐式地访问了类的一个对象,它充当了运算符函数最左边的操作数。
friend Polynomial operator+( Polynomial &p1, const Polynomial &p2)
{
cout << fixed <<setprecision( 2 );
int finalMaxPower = ( p1.maxPow < p2.maxPow ) ? p1.maxPow : p2.maxPow;
p1.maxPow = finalMaxPower;
//较短的多项式相较于较长多项式,有一部分未被初始化,所以计算结果出现垃圾值
Polynomial p3( ( p1.maxPow > p2.maxPow ) ? p1 : p2 );//将较长多项式先赋值给第三个类对象
if( p1.maxPow > p2.maxPow )
{//多项式p1更长,p3 = p1,p3 = p3 + p2;
for( int i = 0; i < finalMaxPower; i++ )
p3.coe[ i ] += p2.coe[ i ];
return p3;
}
else
{//多项式p2更长,p3 = p2,p3 = p3 + p1;
for( int i = 0; i < finalMaxPower; i++ )
p3.coe[ i ] += p1.coe[ i ];
return p3;
}
}
private:
int maxPow;
double coe[ SIZE ];
};
#endif
Polynomial.cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
Polynomial::Polynomial()
{
for( int i = 0; i < SIZE; i++ )
coe[ i ] = 0.0;
}
//Polynomial::Polynomial( Polynomial &p )
Polynomial::Polynomial( double a[], const int pow )
{
setCoe( a, pow );
}
Polynomial::~Polynomial()
{
}
void Polynomial::setCoe( double pcoe[], int max )
{
setMaxPow( max );
for( int i = 0; i < max; i++ )
coe[ i ] = pcoe[ i ];
}
void Polynomial::setMaxPow( int max )
{
maxPow = max;
}
void Polynomial::print() const
{
for( int i = 0; i < maxPow; i++ )
{
if( coe[i] )
{
if( i == 1 )
cout << showpos <<coe[ i ] << "x";
else if( i == 0 )
cout << coe[ i ];
else
cout << showpos << coe[ i ] << "x^" << noshowpos << i;
}
}
}
//mian.cpp
#include <iostream>
#include <string>
#include <vector>
#include"Polynomial.h"
using namespace std;
int main()
{
double i = 0.0;
cout << i << endl;
double a[ 5 ] = { -1.5, 23, 0, 43, -2 };
double b[ 7 ] = { 2, -8, 4, -10.9, 3, -5.5, 9 };
double c[ 5 ] = { -1.5, 23, 0, 43, -2 };
double d[ 7 ] = { 2, -8, 4, -10.9, 3, -5.5, 9 };
//double c[100];
Polynomial p1( a, sizeof( a ) / sizeof( double ) );
Polynomial p2( b, sizeof( b ) / sizeof( double ) );
Polynomial p4( c, sizeof( c ) / sizeof( double ) );
Polynomial p5( d, sizeof( d ) / sizeof( double ) );
Polynomial p3;
p1.print();
cout << endl;
p2.print();
cout << endl;
p3 = p1.operator+( p2 );
cout << "\n\np1.operator+( p2 ):" << endl;
p3.print();
cout << endl;
p3 = p4 + p5;
cout << "p3 = p4 + p5:" << endl;
p3.print();
cout << endl;
}