1.(未使用原書例題)練習多檔案組織。在乙個標頭檔案中定義一種學生的結構體,儲存姓名和年齡,宣告三個函式分別用於詢問有多少個學生,輸入學生的資訊和展示學生的資訊。在另乙個原始檔中給出所有函式的定義。在主程式中使用new初始化結構指標,呼叫三個函式。
//main.cpp
#include#include
"extra.h
"using
namespace
std;
intmain()
//extra.h
#ifndef _extra_h_
#define _extra_h_#include
const
int len = 30
;struct
student
;extern
intstudent_num;
void
set_student();
void
input_student(student sdu);
void
show_student(student sdu);
#endif
//!_extra_h_
//2.(未使用原書例題,對應第四題)修改上題的程式,使用過載函式編寫函式的其它版本,並使用命名空間包括和拓展。主程式的執行過程見注釋。defineextra.cpp
#include#include
"extra.h
"using
namespace
std;
int student_num = 1
;void
set_student()
; cin.
get(); //
消除換行符,以便cin.get輸入
}void
input_student(student sdu_i)
; cin.
get();
}}void
show_student(student sdu_o)
}
//main.cpp
#include#include
"extra.h
"using
namespace
std;
using
namespace
stu;
intmain()
//extra.h
#ifndef _extra_h_
#define _extra_h_#include
#include
namespace
stu;
extern
intstudent_num;
void
set_student();
void input_student(student stu_i_ar); //
輸入學生資訊,互動式版本
void input_student(student & stu_i, char str, int age); //
輸入學生資訊,非互動式版本
void show_student(const student stu_o_ar); //
展示結構體陣列資訊的版本
void show_student(const student & stu_o); //
展示單個結構體資訊的版本
}#endif
//!_extra_h_
//程式執行結果:defineextra.cpp
#include#include
"extra.h
"using
namespace
std;
int stu::student_num = 1
;void
stu::set_student()
; cin.
get(); //
消除換行符,以便cin.get輸入
}void
stu::input_student(student stu_i_ar)
; cin.
get();
}}void stu::input_student(student & stu_i, char str, int
age)
void stu::show_student(const
student stu_o_ar)
}void stu::show_student(const student &stu_o)
*編譯過程中報了好多個編譯器執行之前檢查不出來的錯,總結如下:
1.使用string要注意,它是包含在標準命名空間內的,所以要麼using namespace std,要麼std::string。
2.注意檢查不同檔案裡宣告的函式和其定義,使用的引數型別是不是一致的。
3.strcpy()和strcpy_s()函式的使用注意事項:
C primer plus第9章程式設計練習答案
程式設計練習答案是我學習過程中所敲,作為乙個學渣,答案是以完成習題和易理解為導向,很少有一些五彩繽紛的演算法。程式僅作為交流,如有錯誤與不足還請指出。9.11.1 include stdio.h double min double x,double y intmain void 片9.11.2 in...
第9章順序容器 vector string練習題
include includeusing namespace std vector iterator findint vector iterator beg,vector iterator end,int ival int tmain int argc,tchar argv vectorivec i...
C Primer Plus(第6版)第四章程式設計練習
1.編寫乙個程式,提示使用者輸入名和姓,然後以 名,姓 的格式列印出來。include include int main 4.編寫乙個程式,提示使用者輸入身高 單位 英吋 和姓名,然後以下面的格式顯示使用者剛輸入的資訊 dabney,you are 6.208 feet tall 使用float型別...