****************************
創(chuàng)新互聯(lián)自成立以來,一直致力于為企業(yè)提供從網(wǎng)站策劃、網(wǎng)站設(shè)計、成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、電子商務、網(wǎng)站推廣、網(wǎng)站優(yōu)化到為企業(yè)提供個性化軟件開發(fā)等基于互聯(lián)網(wǎng)的全面整合營銷服務。公司擁有豐富的網(wǎng)站建設(shè)和互聯(lián)網(wǎng)應用系統(tǒng)開發(fā)管理經(jīng)驗、成熟的應用系統(tǒng)解決方案、優(yōu)秀的網(wǎng)站開發(fā)工程師團隊及專業(yè)的網(wǎng)站設(shè)計師團隊。
FileName: WordCount.cpp
Author: cox Version : 1.0 Date: 2007/1/12
Description: 文件中指定單詞個數(shù)的統(tǒng)計
Version: 1.0
Function List:
1. TransformFile() // 將文件轉(zhuǎn)換為有效的格式
2. CountWord() // 單詞計數(shù)
3. IsValidChar() // 檢查是否有效的英語字母
4. GetInput() // 從輸入流讀取數(shù)據(jù)
5. UpperCase() // 字符強制轉(zhuǎn)換為大寫
History:
author time version desc
cox 2007/1/12 1.0 build this moudle
***************************************************/
#include iostream
#include fstream
#include string
using namespace std;
#include "linklist\\linklist.h"
bool TransformFile( char* inFile, char* outFile );
template typename datatype
bool CountWord( char* filename, LinkListdatatype list );
bool IsValidChar( char ch );
template typename datatype
bool GetInput( LinkListdatatype list );
bool UpperCase( string str );
/*************************************************
Function: main()
Description: 主函數(shù)
Calls: 鏈表類, TransformFile(), GetInput(),
CountWord()
Called By: 無
Table Accessed: 無
Table Updated: 無
Input: 無
Output: 結(jié)果
Return: 0
Others: 無
*************************************************/
int main()
{
LinkListstring wordList;
TransformFile( "test1.txt", "test1.tmp" );
if ( !GetInput( wordList ) )
{
cout "Bad input!!" endl;
}
if ( !CountWord( "test1.tmp", wordList ) )
{
cout "An error accord dring count!!" endl;
}
return 0;
}
/*************************************************
Function: TransformFile()
Description: 將文件去掉標點符號, 并按照單詞以空格分組
Calls: IsValidChar()
Called By: main()
Table Accessed: 無
Table Updated: 無
Input: char* inFile // 輸入文件名
char* outFile // 輸出文件名
Output: 狀態(tài)
Return: 操作是否成功
Others: 無
*************************************************/
bool TransformFile( char* inFile, char* outFile )
{
ofstream outFileStream( outFile, ios::out );
ifstream inFileStream ( inFile , ios::in );
if ( !outFileStream || !inFileStream )
{
cout "Open files error!!" endl;
return false;
}
char readChar = '\0';
cout "Transfroming...";
// 轉(zhuǎn)換
while ( inFileStream.get(readChar) )
{
if ( IsValidChar(readChar) )
{
outFileStream readChar;
}
else
{
outFileStream " ";
}
}
cout "OK\n";
outFileStream.close();
inFileStream.close();
return true;
}
/*************************************************
Function: IsValidChar()
Description: 檢查是否有效的英語字符
Calls: 無
Called By: TransformFile()
Table Accessed: 無
Table Updated: 無
Input: char ch // 待檢查的字符
Output: 無
Return: true為是
Others: 無
*************************************************/
bool IsValidChar( char ch )
{
if ( (ch = 65 ch = 90) || (ch = 97 ch = 122) )
{
return true;
}
else
{
return false;
}
}
/*************************************************
Function: GetInput()
Description: 讀取輸入流并存入鏈表中
Calls: 鏈表類
Called By: main()
Table Accessed: 無
Table Updated: 無
Input: LinkListdatatype list // 儲存輸入數(shù)據(jù)的鏈表
Output: 無
Return: 操作是否成功, true為成功
Others: 無
*************************************************/
template typename datatype
bool GetInput( LinkListdatatype list )
{
string strInput = "";
cout "Input your words to count, split by \',\': ";
cin strInput;
string tmpString = "";
strInput += ",";
for ( int index = 0; index strInput.size(); index++ )
{
if ( strInput[index] == ',' )
{
list.Insert(tmpString);
tmpString = "";
}
else
{
tmpString += strInput[index];
}
}
return true;
}
/*************************************************
Function: CountWord()
Description: 單詞計數(shù)
Calls: 鏈表類, UpperCase()
Called By: main()
Table Accessed: 無
Table Updated: 無
Input: char* filename // 待計數(shù)的文件
LinkListdatatype list // 儲存輸入數(shù)據(jù)的鏈表
Output: 無
Return: 操作是否成功, true為成功
Others: 無
*************************************************/
template typename datatype
bool CountWord( char* filename, LinkListdatatype list )
{
ifstream inFileStream ( filename , ios::in );
int lenth = list.Lenth();
int *counts = new int [lenth];
for ( int clrIndex = 0; clrIndex list.Lenth(); clrIndex++ )
{
counts[clrIndex] = 0;
}
string tmpString;
string tmpRead;
while ( inFileStream tmpRead )
{
for ( int index = 0; index list.Lenth(); index++ )
{
list.Locate(index+1);
tmpString = list.Peek();
UpperCase( tmpString );
UpperCase( tmpRead );
if ( tmpString == tmpRead )
{
counts[index]++;
}
}
}
for ( int index = 0; index lenth; index++ )
{
list.Locate(index+1);
tmpString = list.Peek();
cout tmpString " = " counts[index] endl;
}
cout endl;
inFileStream.close();
return true;
}
/*************************************************
Function: UpperCase()
Description: 強制轉(zhuǎn)換成大寫
Calls: 無
Called By: CountWord()
Table Accessed: 無
Table Updated: 無
Input: string str // 待轉(zhuǎn)換的字符
Output: 無
Return: 操作是否成功, true為成功
Others: 無
*************************************************/
bool UpperCase( string str )
{
for ( int index = 0; index str.size(); index++ )
{
if ( str[index] 96 )
{
str[index] -= 32;
}
}
return true;
}
需要鏈表,自己寫一個吧,大一就該學了的~~
#include stdio.h
long f(int n)
{
long term=1;
for (int i=1; i=n; i++)
{
term = term * i;
}
return term;
}
main()
{
int i, j, n;
long term, sum = 0;
printf("Input n:");
scanf("%d", n);
for (i=1; i=n; i++)
{
sum = sum + f(i);
}
printf("1!+2!+…+%d! = %ld\n", n, sum);
}
c語言是沒法把字符串轉(zhuǎn)換成對應函數(shù)的,c語言是結(jié)構(gòu)化語言,程序怎么執(zhí)行在編譯時已經(jīng)確定,沒法像c#之類的托管代碼高級語言能夠動態(tài)綁定或者叫后期綁定。因為托管代碼有運行時去選擇執(zhí)行,而c語言編譯后的可執(zhí)行文件為操作系統(tǒng)直接調(diào)用了,所以沒法動態(tài)綁定。
能不能說清楚函數(shù)的功能
我寫一個函數(shù),功能是判斷一個字符是數(shù)字還是字母
函數(shù)名我用的is()你可以隨便改
int is(char c)
{
if(c='9'c='0') return 1; //如果是數(shù)字,就返回1
else if(c='Z'c='A') return 2; //如果是大寫字母,就返回2
else if(c='z'c='a') return 3; //如果是小寫字母,就返回3
esle return 0; //啥都不是就返回0
}
int main(void)
{
int i;
char str[80];
gets(str); //輸入一個字符串,相當于scanf("%s",str);
for(i=0;str[i]!=0;i++)
switch( is(str[i]) )
{
case 1:printf(" 數(shù)字"); break;
case 2: case 3:printf("字母");break;
default:printf("啥都不是");
}
returned 0;
}
如果是初學,你應該是要這種方式吧
不過建議使用上面那種‘
int fun(char str[])
{
int i;
//把你循環(huán)那部分放在這里
}
int main(void)
{
char str[80];
gets(str);
fun(str);
returned 0;
}