资源预览内容
第1页 / 共59页
第2页 / 共59页
第3页 / 共59页
第4页 / 共59页
第5页 / 共59页
第6页 / 共59页
第7页 / 共59页
第8页 / 共59页
第9页 / 共59页
第10页 / 共59页
亲,该文档总共59页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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, then 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(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 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 integer 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=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? 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 used 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 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; 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:46: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 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?
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号