Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).
Please do not use MFC, STL and other libraries in your implementation.
3. Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).
Please do not use MFC, STL and other libraries in your implementation.
bool operator>(const String& left, const String& right) { int a = strcmp(left.a,right.a); if(a > 0) return true; else return false;
}
istream& operator>>(istream& is, String& s){ delete[] s.a; s.a = new char[20]; int m = 20; char c; int i = 0; while (is.get(c) && isspace(c)); if (is) { do {s.a = c; i++; /*if(i >= 20){ cout << "Input too much characters!" << endl; exit(-1); }*/ if(i == m - 1 ){ s.a = '\0'; char* b = new char[m]; strcpy(b,s.a); m = m * 2; s.a = new char[m]; strcpy(s.a,b); delete[] b; } } while (is.get(c) && !isspace(c)); //如果读到空白,将其放回. if (is) is.unget(); } s.size = i; s.a = '\0'; return is; }
int main(){ String a = "abcd"; String b = "www"; //String c(6,b);这么写不对. String c(6,'l'); String d; String e = a;//abcd String f; cin >> f;//需要输入... String g; g = a + b;//abcdwww
if(a < b) cout << "a < b" << endl; else cout << "a >= b" << endl; if(e == a) cout << "e == a" << endl; else cout << "e != a" << endl;
b += a;
cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl; cout << e << endl; cout << f << endl; cout << g << endl; cout << g[0] << endl; return 0; }
4. Implement a single-direction linked list sorting algorithm. Please first define the data structure of linked list and then implement the sorting algorithm.
int atoi(const char *nptr); 函数说明 atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再 遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。 返回值 返回转换后的整型数。
#include <stdio.h> #include <ctype.h>
int myAtoi(const char* s){ int result = 0; int flag = 1; int i = 0; while(isspace(s)) i++; if(s == '-'){ flag = -1; i++; } if(s == '+') i++; while(s != '\0'){ if((s > '9') || (s < '0')) break; int j = s - '0'; result = 10 * result + j; i++; } result = result * flag; return result; }
int main(){ char* a = " -1234def"; char* b = "+1234"; int i = myAtoi(a); int j = myAtoi(b); printf("%d \n",i); printf("%d",j); return 0; }
2.int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 输出是多少? 3.解释局部变量、全局变量和静态变量的含义。 4.解释堆和栈的区别。 5.论述含参数的宏与函数的优缺点。 普天C++笔试题 1.实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数。 2.写一个函数,将其中的\t都转换成4个空格。 3.Windows程序的入口是哪里?写出Windows消息机制的流程。 4.如何定义和实现一个类的成员函数为回调函数? 5.C++里面是不是所有的动作都是main()引起的?如果不是,请举例。 6.C++里面如何声明const void f(void)函数为C程序中的库函数? 7.下列哪两个是等同的 int b; A const int* a = &b; B const* int a = &b; C const int* const a = &b; D int const* const a = &b; 8.内联函数在编译时是否做参数类型检查? void g(base & b){ b.play; } void main(){ son s; g(s); return; } 华为笔试题 1.请你分别画出OSI的七层网络结构图和TCP/IP的五层结构图。 2.请你详细地解释一下IP协议的定义,在哪个层上面?主要有什么作用?TCP与UDP呢? 3.请问交换机和路由器各自的实现原理是什么?分别在哪个层次上面实现的? 4.请问C++的类和C里面的struct有什么区别? 5.请讲一讲析构函数和虚函数的用法和作用。 6.全局变量和局部变量有什么区别?是怎么实现的?操作系统和编译器是怎么知道的? 7.8086是多少位的系统?在数据总线上是怎么实现的? Sony笔试题 1.完成下列程序 * *.*. *..*..*.. *...*...*...*... *....*....*....*....*.... *.....*.....*.....*.....*.....*..... *......*......*......*......*......*......*...... *.......*.......*.......*.......*.......*.......*.......*....... #include <stdio.h> #define N 8 int main() { int i; int j; int k; --------------------------------------------------------- | | | | | | --------------------------------------------------------- return 0; } 2.完成程序,实现对数组的降序排序 #include <stdio.h> void sort( ); int main() { int array[]={45,56,76,234,1,34,23,2,3}; //数字任//意给出 sort( ); return 0; } void sort( ) { ____________________________________ | | | | |-----------------------------------------------------| } 3.费波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他方法,但要说明你选择的理由。 #include <stdio.h> int Pheponatch(int); int main() { printf("The 10th is %d",Pheponatch(10)); return 0; } int Pheponatch(int N) { -------------------------------- | | | | -------------------------------- } 4.下列程序运行时会崩溃,请找出错误并改正,并且说明原因。 #include <stdio.h> #include <malloc.h> typedef struct{ TNode* left; TNode* right; int value; } TNode; TNode* root=NULL; void append(int N); int main() { append(63); append(45); append(32); append(77); append(96); append(21); append(17); // Again, 数字任意给出 } void append(int N) { TNode* NewNode=(TNode *)malloc(sizeof(TNode)); NewNode->value=N;