#include<iostream> using namespace std; int main(){ double degreeC,degreeF; cout<<"Please enter the degree C!"<<endl; cin>>degreeC; degreeF=degreeC*9/5+32; cout<<"The degree C transform to degree F is "<<degreeF<<endl; cout<<"Please enter the degree F!"<<endl; cin>>degreeF; degreeC=(degreeF-32)*5/9; cout<<"The degree F transform to degree C is "<<degreeC<<endl; return 0; }0227練習
#include<iostream> #include<cstdlib> using namespace std; int main(){ int n[101][200]={0}; n[0][0]=1; int i,j; for(i=1;i<=100;i++){ for(j=0;j<200;j++) n[i][j]=n[i-1][j]*i; for(j=0;j<200;j++) if(n[i][j]>=10){ n[i][j+1]+=n[i][j]/10; n[i][j]%=10; } int t=199; while(n[i][t]==0) t--; cout<<i<<"!="; for(;t>=0;t--) cout<<n[i][t]; cout<<endl; } }0306練習
# include <iostream> #include <cstdlib> #include<cmath> using namespace std; class CTime { private: // members data int m_hours; int m_mins; int m_secs; public: // constructors CTime(void); CTime(int hours, int mins, int secs); // other members function bool setTime(int hours, int mins, int secs); int getHours(); int getMins(); int getSecs(); void diff(CTime *time); void print(); }; // Auxiliary Function bool isValidTime(int hours,int mins,int secs); // constructors CTime::CTime(){ m_hours=0; m_mins=0; m_secs=0; } CTime::CTime(int hours, int mins, int secs) { m_hours=hours; m_mins=mins; m_secs=secs; } // member methods bool CTime::setTime(int hours, int mins, int secs){ if (!isValidTime(hours, mins, secs)) return false; else { m_hours=hours; m_mins=mins; m_secs=secs; return true; } } void CTime::diff(CTime *time){ int sum1=m_hours*3600+m_mins*60+m_secs; int sum2=time->m_hours*3600+time->m_mins*60+time->m_secs; int sum=abs(sum2-sum1); int h=sum/3600; int m=sum%3600/60; int s=sum%60; if(h<10) cout<<0; cout << h << ":"; if(m<10) cout<<0; cout << m << ":"; if(s<10) cout<<0; cout << s << endl; } int CTime::getHours(){ return m_hours; } int CTime::getMins(){ return m_mins; } int CTime::getSecs(){ return m_secs; } void CTime::print() { if(m_hours<10) cout<<0; cout << m_hours << ":"; if(m_mins<10) cout<<0; cout << m_mins << ":"; if(m_secs<10) cout<<0; cout << m_secs << endl; // ??? 00:00:00 } bool isValidTime(int hours,int mins,int secs) { return ( hours >=0 && hours < 24 && mins >= 0 && mins < 60&& secs >= 0 && secs < 60 ); } int main(){ CTime *time1=new CTime(12, 59, 59); cout << " time1>> "; time1->print(); CTime *time2=new CTime(13, 0, 0); // generate an CTime object time2 and time is 13:00:00 cout << " time2>> "; time2->print(); // add a member function into the CTime class for processing time difference // cout << " diftime>> "; time1->diff(time2); }ex0320 MS visual studio C++
// php.cpp: 主要專案檔。 #include "stdafx.h" #include "stdlib.h" using namespace System; using namespace System::Text::RegularExpressions; using namespace System::Collections; int main(array<System::String ^> ^args) { String^ input="Information and Management Department"; String^ infix="67 + 55 * 5 - 32 / 4"; String^ postfix=""; Regex^ rx=gcnew Regex(" "); array<String^> ^a=rx->Split(infix); for each(String^ ss in a) Console::WriteLine(";"+ss); Stack^ stk1=gcnew Stack(); for each(String^ ss in a){ if(ss=="+" || ss=="-" ||ss=="*" ||ss=="/" ){ if(stk1->Count ==0){ stk1->Push(ss); }else{ String^ peek=stk1->Peek()->ToString(); if(ss=="*" ||ss=="/" ){ if(peek=="-"||peek=="+"){ stk1->Push(ss); }else{ while(stk1->Count!=0) postfix=postfix+" "+stk1->Pop(); stk1->Push(ss); } }else{ while(stk1->Count!=0) postfix=postfix+" "+stk1->Pop(); stk1->Push(ss); } } }else{ postfix=postfix+" "+ss; } } while(stk1->Count!=0) postfix=postfix+" "+stk1->Pop(); Console::WriteLine("postfix >>" + postfix); int op1,op2,op3; array<String^> ^b=rx->Split(postfix); for each(String^ ss in b){ if(ss=="") ; else{ if(ss=="+"){ op1=Convert::ToInt32(stk1->Pop()->ToString()); op2=Convert::ToInt32(stk1->Pop()->ToString()); op3=op2+op1; stk1->Push(Convert::ToString(op3)); }else if(ss=="-" ){ op1=Convert::ToInt32(stk1->Pop()->ToString()); op2=Convert::ToInt32(stk1->Pop()->ToString()); op3=op2-op1; stk1->Push(Convert::ToString(op3)); }else if(ss=="*"){ op1=Convert::ToInt32(stk1->Pop()->ToString()); op2=Convert::ToInt32(stk1->Pop()->ToString()); op3=op2*op1; stk1->Push(Convert::ToString(op3)); }else if(ss=="/"){ op1=Convert::ToInt32(stk1->Pop()->ToString()); op2=Convert::ToInt32(stk1->Pop()->ToString()); op3=op2/op1; stk1->Push(Convert::ToString(op3)); }else{ stk1->Push(ss); } } } Console::WriteLine("sum >>" + stk1->Pop()); system("pause"); return 0; }0327練習A
// 0327A.cpp: 主要專案檔。 #include "stdafx.h" #include "stdlib.h" using namespace System; int main(array<System::String ^> ^args) { array<int> ^day=gcnew array<int>{10,9,3,7}; try // 監視程式碼的區塊 { int b = Convert::ToInt32("ABC") ; Console::WriteLine("b is {0}", b); } catch (ArithmeticException ^e) // 取得”除以零”的錯誤 { Console::WriteLine("ArithmeticException: {0}",e->Message); }catch (ArgumentException ^e) { Console::WriteLine("ArgumentException: {0}",e->Message); }catch (FormatException ^e) { Console::WriteLine("FormatException: {0}",e->Message); }catch (IndexOutOfRangeException ^e) { Console::WriteLine("IndexOutOfRangeException: {0}",e->Message); }catch (Exception ^e) // 例外狀況 { Console::WriteLine("Exception: {0}",e->Message); } __finally { Console::WriteLine("End of Job"); } system("pause"); return 0; }0327練習B
#include <iostream> #include <cstdlib> #include <math.h> using namespace std; class interest{ private: char type; // 'S': Single rate; 'C':Compound interest double prin, rate=0.05; // prin: principal; rate: bank rate int year; public: interest(char tp); interest(char tp, double rt); int getResult(double year); void setPrin(double pp); void setRate(double rr); }; interest::interest(char tp){ type = tp; } interest::interest(char tp, double rt){ type = tp; rate = rt; } int interest::getResult(double yy){ // Formula of Single= p(1+nr) // Formula of Compound= P(1+r)^n year = yy; if(type=='S'){ return round(prin*(1+year*rate)); }else{ return round(prin*pow((1+rate),year)); } } void interest::setPrin(double pp){ prin = pp; } void interest::setRate(double rr){ rate = rr; } int main(){ double prin=2000, rate=0.05; interest *A = new interest('S'); interest *B = new interest('C',rate); A->setPrin(prin); B->setPrin(prin); interest *D = new interest('S'); interest *C = new interest('C',rate); C->setPrin(5000); C->setRate(0.0315); D->setPrin(5000); D->setRate(0.0315); cout<< " *** Principal = " << prin << " \t **** Rate = "<< rate<<endl; // 2000, 0.05 cout<< "\nYears\t Bank A(Single) \tBank B(Compound) " << endl; cout<< "----------------------------------------------------- " << endl; for(int i=1;i<=10;i++) { //............... cout << " "<<i<<"\t\t"<<A->getResult(i); cout << "\t\t\t"<< B->getResult(i)<< endl; } cout << "===================================================== "<<endl; cout<< " *** Principal = 5000 \t **** Rate = 0.0315"<<endl; cout<< "\nYears\t Bank D(Single) \tBank C(Compound) " << endl; cout<< "----------------------------------------------------- " << endl; for(int i=1;i<=10;i++) { //............... cout << " "<<i<<"\t\t"<<D->getResult(i); cout << "\t\t\t"<< C->getResult(i)<< endl; } }0410練習
A9803002 Alexis 73 81 54 66 avg:66
A9803006 Matthew 98 87 74 83 avg:84
A9803008 Aiden 83 77 82 72 avg:78
A9803011 Chrimetor 53 53 46 55 avg:51 *
A9803017 Addisory 83 88 74 57 avg:73
A9803019 Chritopher 77 83 71 88 avg:79
A9803020 Mason 63 69 88 90 avg:79
A9803023 Addison 95 97 94 82 avg:91
A9803028 Joshua 43 52 60 55 avg:53 *
A9803029 Andrew 71 79 64 72 avg:70
A9803030 Elizabeth 63 61 58 51 avg:57 *
// test.cpp: 主要專案檔。 #include "stdafx.h" #include "string.h" #include "stdio.h" using namespace System; using namespace System::IO; int main(array<System::String ^> ^args) { try { String^ fid2="d:\\stdrec1.txt"; if(!File::Exists(fid2)){ Console::WriteLine(L" File does not exists!!"); return 0; } String^ fid="d:\\report.txt"; if(File::Exists(fid)){ Console::WriteLine(L" File already exists!!"); File::Delete(fid); return 0; } StreamWriter^ sw=File::CreateText(fid); StreamReader^ sr=gcnew StreamReader(fid2); String ^line, ^sid, ^sname, ^sc1, ^sc2, ^sc3, ^sc4; while((line = sr->ReadLine()) != nullptr){ sid=line->Substring(0,8); sname=line->Substring(8,10); sc1=line->Substring(19,2); sc2=line->Substring(22,2); sc3=line->Substring(25,2); sc4=line->Substring(28); int avg=Convert::ToDouble(sc1)*0.2+Convert::ToDouble(sc2)*0.2+Convert::ToDouble(sc3)*0.3+Convert::ToDouble(sc4)*0.3; String ^fail=" "; if(avg < 60) fail="*"; Console::WriteLine("{0} {1} {2} {3} {4} {5} avg:{6} {7}",sid,sname,sc1,sc2,sc3,sc4,avg,fail); sw->WriteLine("{0} {1} {2} {3} {4} {5} avg:{6} {7}",sid,sname,sc1,sc2,sc3,sc4,avg,fail); } sr->Close(); sw->Close(); } catch(Exception^ e){ Console::WriteLine("The file can't be read: {0}",e->Message); } return 0; }0417檔案輸出輸入
ifstream (檔案輸入)、ofstream (檔案輸出) 以及 fstream (檔案輸出入)
※檔案的開啟與關閉
開啟檔案
檔案物件.open(“檔案名稱”,ios::開啟模式);
可供選擇的開啟模式
- ios::app 開啟可供附加資料的檔案
- ios::in 開啟可供讀取資料的檔案
- ios::out 開啟可供寫入資料的檔案
- ios::trunc 若檔案已存在,先刪除它,再開啟
- ios::binary 開啟二進位的輸入/輸出檔案
關閉檔案
檔案物件.close();
※文字檔的處理
將資料寫入文字檔
將資料附加到已存在的文字檔
從文字檔案讀取資料
get()、getline()、put()
利用 put() 將字串寫入檔案
文字檔的拷貝與讀取
//從檔案內讀取一個字元,並把它寫入 ch 字元變數
檔案物件.get(ch);
//從檔案內最多讀取 N-1 個字元,或是讀取到 \n
//並把它存放到字串 str 中
檔案物件.getline(str, N, ’\n’);
//將ch字元變數的值寫入檔案內
檔案物件.put(ch);
prog18_1
//prog18_1, 將資料寫入文字檔 #include <fstream> // 載入fstream標頭檔 #include <iostream> #include <cstdlib> using namespace std; int main(void) { ofstream ofile("c:\\donkey.txt",ios::out); // 建立ofile物件 if(ofile.is_open()) // 測試檔案是否被開啟 { ofile << "我有一隻小毛驢" << endl; // 將字串寫入檔案 ofile << "我從來也不騎" << endl; // 將字串寫入檔案 cout << "已將字串寫入檔案..." << endl; } else cout << "檔案開啟失敗..." << endl; ofile.close(); // 關閉檔案 system("pause"); return 0; }prog18_2
//prog18_2, 將資料附加到已存在的文字檔 #include <fstream> // 載入fstream標頭檔 #include <iostream> #include <cstdlib> using namespace std; int main(void) { ofstream afile("c:\\donkey.txt",ios::app); // 建立afile物件 從檔案結尾寫入(輸出)資料 if(afile.is_open()) // 測試檔案是否被開啟 { afile << "有一天我心血來潮騎著去趕集"; // 將字串寫入檔案 cout << "已將字串附加到檔案了..." <<endl; } else cout << "檔案開啟失敗..." << endl; afile.close(); // 關閉檔案 system("pause"); return 0; }prog18_3
//prog18_3, 從檔案讀入資料 #include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main(void) { char txt[40]; // 建立字元陣列,用來接收字串 ifstream ifile("c:\\donkey.txt",ios::in); while(!ifile.eof()) // 判別是否讀到檔案的尾端 { ifile >> txt; // 將檔案內容寫入字元陣列 cout << txt << endl; } ifile.close(); // 關閉檔案 system("pause"); return 0; }prog18_4
//prog18_4, 利用put()將字串寫入檔案 #include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main(void) { char txt[]="Welcome to the C++ world" ; // 建立字元陣列 int i=0; ofstream ofile("F:\\welcome.txt",ios::out); while(txt[i] != '\0') // 判別txt[i]字元是否為字串尾端 { ofile.put(txt[i]); // 將字元txt[i]寫入檔案 i++; } cout << "字串寫入完成..." << endl; ofile.close(); system("pause"); return 0; }prog18_5
//prog18_5, 文字檔的拷貝與讀取 #include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main(void) { char txt[80],ch; ifstream ifile1("c:\\welcome.txt",ios::in); ofstream ofile("c:\\welcome2.txt",ios::out); while(ifile1.get(ch)) // 判別是否讀到檔案的尾端 ofile.put(ch); cout << "拷貝完成..." << endl; ifile1.close(); ofile.close(); ifstream ifile2("c:\\welcome2.txt",ios::in); while(!ifile2.eof()) // 判別是否讀到檔案的尾端 { ifile2.getline(txt,80,'\n'); cout << txt << endl; } ifile2.close(); system("pause"); return 0; }prog18_6
//prog18_6, 二進位檔寫入的練習 #include <fstream> // 載入fstream標頭檔 #include <iostream> #include <cstdlib> #include <cmath> // 載入數學函數庫cmath using namespace std; int main(void) { double num; ofstream ofile("c:\\binary.dat",ios::binary); // 開啟可供寫入的二進位檔 for(int i=1;i<=5;i++) { num=sqrt((double)i); // 將i轉成double,再計算sqrt(i) ofile.write((char*)&num,sizeof(num)); // 將num寫入二進位檔 } cout << "已將資料寫入二進位檔了..." << endl; ofile.close(); // 關閉檔案 system("pause"); return 0; }prog18_7
//prog18_7, 讀取二進位檔 #include <fstream> // 載入fstream標頭檔 #include <iostream> #include <cstdlib> using namespace std; int main(void) { ifstream ifile("c:\\binary.dat",ios::binary); // 開啟二進位檔 double num; for(int i=1;i<=5;i++) { ifile.read((char*) &num,sizeof(num)); // 從二進位檔中讀取資料 cout << num << endl; // 印出讀取的內容 } cout << "二進位檔已被讀取了..." << endl; ifile.close(); // 關閉檔案 system("pause"); return 0; }prog18_8
//prog18_8, 將物件的內容寫入二進位檔 #include <fstream> // 載入fstream標頭檔 #include <iostream> #include <cstdlib> using namespace std; class CStudent { protected: char name[40]; int age; public: void get_data(void) // 成員函數,用來輸入物件的資料成員 { cout << "Enter name: "; cin >> name; cout << "Enter age: "; cin >> age; } void show_data(void) // 成員函數,用來顯示物件的資料成員 { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main(void) { CStudent st; st.get_data(); ofstream ofile("c:\\student.dat",ios::binary); ofile.write((char*) &st,sizeof(st)); // 將物件寫入二進位檔中 cout << "資料已寫入檔案中..." << endl; ofile.close(); // 關閉檔案 system("pause"); return 0; }prog18_9
//prog18_9, 從二進位檔裡讀取物件的資料 #include <fstream> // 載入fstream標頭檔 #include <iostream> #include <cstdlib> using namespace std; class CStudent { protected: char name[40]; int age; public: void get_data(void) // 成員函數,用來輸入物件的資料成員 { cout << "Enter name: "; cin >> name; cout << "Enter age: "; cin >> age; } void show_data(void) // 成員函數,用來顯示物件的資料成員 { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main(void) { CStudent st; ifstream ifile("c:\\student.dat",ios::binary); ifile.read((char*) &st,sizeof(st)); st.show_data(); ifile.close(); // 關閉檔案 system("pause"); return 0; }0417作業
Report for Ansi C++
A9803002 Alexis 73 81 54 66 avg: 66
A9803006 Matthew 98 87 74 83 avg: 84
A9803008 Aiden 83 77 82 72 avg: 78
A9803011 Chrimetor 53 53 46 55 avg: 51*
A9803017 Addisory 83 88 74 57 avg: 73
A9803019 Chritopher 77 83 71 88 avg: 79
A9803020 Mason 63 69 88 90 avg: 79
A9803023 Addison 95 97 94 82 avg: 91
A9803028 Joshua 43 52 60 55 avg: 53*
A9803029 Andrew 71 79 64 72 avg: 70
A9803030 Elizabeth 63 61 58 51 avg: 57*
使用C的atoi()與atof()。
先利用c_str()轉成C string,再用atoi()與atof()。
double n = atof(s.c_str());
int n = atoi(s.c_str());
//prog18_5, 文字檔的拷貝與讀取 #include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main(void) { char txt[80],ch; string rec; ifstream ifile("D:\\stdRec1.txt",ios::in); ofstream ofile("D:\\report2.txt",ios::out); if(ofile.is_open()){ ofile<< " Report for Ansi C++ "<< endl; while(ifile.getline(txt,sizeof(txt),'\n')){ rec = txt; string id = rec.substr(0,8); string name = rec.substr(8,10); double sc1 = atof(rec.substr(18,3).c_str()); double sc2 = atof(rec.substr(21,3).c_str()); double sc3 = atof(rec.substr(24,3).c_str()); double sc4 = atof(rec.substr(27,3).c_str()); //cout<<id<<" "<<name<<" "<<sc1<<" "<<sc2<<" "<<sc3<<" "<<sc4<<" "<<endl; int avg = sc1*0.2+sc2*0.2+sc3*0.3+sc4*0.3; string fail=" "; if(avg < 60) fail="*"; ofile<<id<<" "<<name<<" "<<sc1<<" "<<sc2<<" "<<sc3<<" "<<sc4<<" avg: "<<avg<<fail<<endl; } ifile.close(); ofile.close(); } system("pause"); return 0; }
沒有留言:
張貼留言