?? encode.cpp
字號:
//************************************************************
//程序名稱:字典問題
//
//
//
//
//
//編寫者:謝日敏
//
//
//開發(fā)日期:2008年5月25日
//************************************************************
#include <stdio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#include<math.h>
double fac(int n); ///*************計算階乘****************/
double Composite_operators(int i,int j);/*************復(fù)合運算****************/
void main()
{
int checknum,stringLine=0,linenum=0,stringLinelength=0,n;
double codenum;
char ch[7];
char linestr[7];
char *temp;
//這里的InputStream用于讀取字符串,也就是第一行之后的字符串
ifstream InputStream("Input.txt");
if(InputStream.fail())
{
cout<<" 不能打開文件: "<<"Input.txt"<<" 或者不存在數(shù)據(jù)!!!!!";
cout<<" 請在檢查Input.txt文件的路徑是否正確!!!!! ";
}
//這里的GetStringLineNum用于讀取行數(shù),也就是K值
ifstream GetStringLineNum("Input.txt");
if(!GetStringLineNum){
cout<<" 不能打開文件: "<<"Input.txt"<<" 或者不存在數(shù)據(jù)!!!!!";
cout<<" 請在檢查Input.txt文件的路徑是否正確!!!!! ";
}
GetStringLineNum.getline(linestr,7);
/*********取得行數(shù)k值***********/
//atoi是字符串轉(zhuǎn)換成整型函數(shù)
linenum=atoi(linestr);
GetStringLineNum.close();
ofstream OutputStream("output.txt");
if(!OutputStream)
{
cout<<"無法輸出到"<<"output.txt"<<"文件";
}
//從讀取input.txt文件中的、
while (OutputStream && InputStream.getline(ch,20) && (linenum>=stringLine) )
{
// cout<<ch<<endl;
int total[7],strLength;
double sum1=0,sum2=0,codingNumber;
stringLine++;
temp=ch;
//從第二行開始掃描
if (stringLine>1)
{
strLength=strlen(ch);
//判斷不超過6個字符
if (strLength>6)
{
cout<<"輸入的"<<stringLine<<"行字符串,各自的長度要求不超過 6! ";
cout<<"請重新嘗試!"<<endl;
return;
}
//升序判斷
for(int h=0;h<strLength-1;h++)
{
if ((temp[h]>temp[h+1]) ||(temp[h]==temp[h+1]))
{
cout<<"輸入的"<<stringLine<<"行字符串不是按照升序排列! ";
cout<<"請重新輸入!"<<endl;
return;
}
}
//如果只有一個字符串直接輸入其值
if(strLength==1)
{
codingNumber=temp[0]-'a'+1;
}
else
{
for (int i=0;i<strLength;i++)
{
total[i]=temp[i]-'a'+1;
// cout<<"aaaa:::"<<total[i]<<endl;
}
//計算字符串所有組合個數(shù)
for(int j=1;j<=strLength;j++)
{
sum1=sum1+Composite_operators(26,j);
}
// cout<<"aaaa:::"<<sum1<<endl;
int k=strLength;
int m=0;
//逆向計算所有可能的個數(shù)
while((k>0)||(m<k))
{
sum2=sum2+Composite_operators((26-total[m]),k);
k--;
m++;
}
//計算編碼位置
codingNumber=(sum1-sum2);
if (total[strLength-1]==26)
{
codingNumber=codingNumber+1;
}
//類型識別處理
codenum=codingNumber;
checknum=(int)codingNumber;
if ((codenum-checknum)!=0)
{
codingNumber=checknum+1;
}
//邊界處理
if(strcmp(temp,"stwxyz")>=0)
{
codingNumber++;
OutputStream<<codingNumber;
}
else
OutputStream<<codingNumber;
}
OutputStream<<endl;
cout<<codingNumber<<endl;
}
}
InputStream.close();
OutputStream.close();
}
/*************用遞歸計算階乘****************/
double fac(int n)
{
//當(dāng)n為0或為1時返回1
if(n==0||n==1)
return 1;
else
return fac(n-1)*n;
}
double Composite_operators(int i,int j)
{
double result;
result=(fac(i)/fac(i-j))/fac(j);
return result;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -