public class test20 { public static void main(String[] args) { float fm = 1f; float fz = 1f; float temp; float sum = 0f; for (int i=0;i20;i++){
目前成都創(chuàng)新互聯(lián)已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、大關(guān)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
temp = fm;
fm = fz;
fz = fz + temp;
sum += fz/fm;
//System.out.println(sum);
}
System.out.println(sum);
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOTest {
public static void main(String[] args) {
String str = "123\r\n456";
writeFile(str);//寫
String str1 = readFile();//讀
System.out.println(str1);
}
/**
* 傳遞寫的內(nèi)容
* @param str
*/
static void writeFile(String str) {
try {
File file = new File("d:\\file.txt");
if(file.exists()){//存在
file.delete();//刪除再建
file.createNewFile();
}else{
file.createNewFile();//不存在直接創(chuàng)建
}
FileWriter fw = new FileWriter(file);//文件寫IO
fw.write(str);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 返回讀取的內(nèi)容
* @return
*/
static String readFile() {
String str = "", temp = null;
try {
File file = new File("d:\\file.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);//文件讀IO
while((temp = br.readLine())!=null){//讀到結(jié)束為止
str += (temp+"\n");
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
剛寫的,夠朋友好好學(xué)習(xí)一下啦,呵呵
多多看API,多多練習(xí)
程序在VC7.1上編譯執(zhí)行通過,使用的都是標(biāo)準(zhǔn)的C/C++庫函數(shù),理論上可以在任何規(guī)范的編譯器上編譯。具有輸入和導(dǎo)入數(shù)據(jù)、存檔3個(gè)功能。
如果輸入和導(dǎo)入文件格式正確,程序運(yùn)行完全沒有問題。
沒有對(duì)非法輸入和非法文件進(jìn)行處理。因?yàn)榧由线@些東西,程序會(huì)太復(fù)雜,如有需要,可以加上。
源程序:
#include iostream
#include string.h
using namespace std;
struct GameDoc
{
char player[32];
int level;
int id;
int hp;
int exp;
};
void saveFile( char * fileName, GameDoc data )
{
FILE* fp = fopen(fileName, "wb");
if(fp)
{
fwrite(data, sizeof(data), 1, fp);
fclose(fp);
cout " 存檔成功 \n" endl;
}
else
{
cout "寫入文件失敗\n" endl;
}
}
void inputDoc()
{
GameDoc data;
char fileName[256];
cout "請(qǐng)輸入各項(xiàng)數(shù)據(jù),輸入后按回車將開始下一項(xiàng)輸入" endl;
cout "玩家姓名:" endl;
cin data.player;
cout "等級(jí):" endl;
cin data.level;
cout "ID號(hào):" endl;
cin data.id;
cout "生命值" endl;
cin data.hp;
cout "經(jīng)驗(yàn)值" endl;
cin data.exp;
cout "存檔文件名" endl;
cin fileName;
saveFile(fileName, data);
}
void importDoc()
{
GameDoc data;
char fileName[256];
cout "" endl;
// 示例導(dǎo)入文件(內(nèi)容):李四,2,107,50,102
cout "導(dǎo)入文件名" endl;
cout "文件格式:玩家姓名,等級(jí),ID號(hào),生命值,經(jīng)驗(yàn)值(以英文逗號(hào)分隔,不能有空格)" endl;
cin fileName;
FILE* imFp = fopen(fileName, "r");
if (!imFp)
{
cout "導(dǎo)入文件失敗\n" endl;
return;
}
char line[128];
fgets(line, 128, imFp);
char *tok;
tok = strtok(line, ",");
strcpy(data.player, tok);
tok = strtok(NULL, ",");
data.level = atoi(tok);
tok = strtok(NULL, ",");
data.id = atoi(tok);
tok = strtok(NULL, ",");
data.hp = atoi(tok);
tok = strtok(NULL, ",");
data.exp = atoi(tok);
cout "存檔文件名" endl;
cin fileName;
saveFile(fileName, data);
}
void readDoc()
{
GameDoc data;
char fileName[256];
cout "讀入文件名" endl;
cin fileName;
FILE* fp = fopen(fileName, "rb");
if (fp)
{
fread(data, sizeof(data), 1, fp);
fclose(fp);
cout " 讀檔成功 " endl;
cout "玩家姓名:" data.player endl;
cout "等級(jí):" data.level endl;
cout "ID號(hào):" data.id endl;
cout "生命值" data.hp endl;
cout "經(jīng)驗(yàn)值" data.exp endl;
cout endl;
}
else
{
cout "讀入文件失敗\n" endl;
}
}
int main(int argc, char** argv)
{
int selectNo = 0;
while(selectNo != 4)
{
cout "模擬存檔/讀擋過程,請(qǐng)選擇相應(yīng)操作(輸入1, 2, 3或4)" endl;
cout "1 輸入存檔數(shù)據(jù) 2 導(dǎo)入存檔數(shù)據(jù) 3 讀檔并顯示 4 退出" endl;
cin selectNo;
switch(selectNo)
{
case 1:
inputDoc();
break;
case 2:
importDoc();
break;
case 3:
readDoc();
break;
case 4:
break;
default:
cout "選擇錯(cuò)誤,請(qǐng)輸入1, 2, 3或4" endl;
}
}
return 0;
}