《c习题参考模板》由会员分享,可在线阅读,更多相关《c习题参考模板(59页珍藏版)》请在金锄头文库上搜索。
1、C+ assignment(1) Following is the C+ assignment for this week. (1) Write a simple program to demonstrate the sizes of fundamental data types, including int,double,float,char,bool,short int, long int,etc. using the operator sizeof(); (2) Write a function int f(int n) to calculate thefactorial of n, t
2、hen call it in main() function. The sample source codes will be presented next weekHi, Every one! Following is the sample source codes for C+ assignment 1, for reference only. (1)#include using namespace std;int main() cout sizeof(bool) endl; cout sizeof(char)endl; cout sizeof(int) endl; cout sizeof
3、(short) endl; cout sizeof(long) endl; cout sizeof(float) endl; cout sizeof(double) endl; The output of this program is:1142448 (2)#include using namespace std;int factorial(int n) int fac=1; for(int i=2;i=n;i+) fac *=i; return fac;int main() cout n; cout Result of f( n ) is factorial(n) endl; return
4、 0; / C+ assignment(2)Hi, Every one! Following is the C+ assignment for this week. (1) Inspect this program:#include using namespace std;int main() char c; for(c=-128;c127;c+) cout ( (int)c, c ) endl; cout ( (int)c, c) endl; getchar(); What is the output?The data type char can be considered as integ
5、er with single byte. How to output a char variable as an integer? How to output an integer as a char? What is ASCII? (2)Inspect this program:#include using namespace std;int main() int num; cout numendl; num=2; cout (num 1)endl; int n=5; if(num=2 & n=5) cout goodendl; num =-2; num =1; if(num=-4 & n=
6、5) cout another goodendl; getchar(); Can you imagin the output? Is your idea correct? If a variable is not initialized, what is its value? Left shift operation and standard output operation are both represented as , so, how to distinguish them? Left shift by 1 bit means multiplying by 2, doesnt it?
7、How about right shift operation? (3)Write a function that outputs a char variable as a binary sequence。 (4)Output a minus value as a binary sequence. Make clear what thetwos-complement is, and check it actually. (5)Write a function that outputs a float variable as a binary sequence。(Pointers are use
8、d in this function. Ask the teaching assistant for detail.) (6)Write a program that outputs the greatest common divisor of two integers and the least common multiple of two integers. Source code: output a float as a binary sequence(2013-03-20 20:16:20)分类:CppHi, Everyone! The following is the sample
9、source code for one problem given the other day: how to output a float variable as a binary sequence. Please study it and compare with your works.#include using namespace std;/output a single byte as a binary sequencevoid BinaryOut(char c) unsigned char tmp=0x80; for(int i=0;i8;i+) if(c&tmp) cout 1;
10、 else cout =1; /output several contiguous bytes form a given address, with the highest byte on the leftmostvoid show(char *p,int len) for(int i=len-1;i=0;i-) BinaryOut(*(p+i); int main() float d=-28.0; show(char*)&d,sizeof(d);/using type-cast to fetch bytes getchar();C+ assignment(3)(2013-03-25 14:4
11、6:37)分类:CppHi, Every one! Following is the C+ assignment for this week. (1) Inspect this program:#include using namespace std;int main() int x=5,y=8; x = sizeof(y+); cout x endl; cout y endl; getchar(); What is the output?Is it the same as you have expected? Do you know the concepts Compile-time and
12、 Run-time? (2)Inspect this program:#include using namespace std;void BinaryOut(char c) unsigned char tmp=0x80; for(int i=0;i8;i+) if(c&tmp) cout 1; else cout =1; cout endl;int main() char ch=-1; BinaryOut(ch); ch=127; BinaryOut(ch); getchar(); The program can output a char variable as a binary sequence. If we change the data type of tmp into char,does this program still work?Why not?How to output a variable of arbitrary type as a binary sequence? (3)Inspect this program:#include#includeusing namespace std;int main() cout What is your name?