1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;
//#define SIZE 100
const int SIZE = 100;
//returns a malloc'd string which must be freed
char *foo(int x){
//string string_A[10];
//string foo(int x,string string_A){
char *string = malloc(10);
sprintf(string, "%d", x);
return string;
}
/* main() contains a lot of code. First, take this code and convert it to C++.
* This means replacing the C style strings with C++ style strings, swapping out the
* I/O from C style to C++ style, etc.
*/
int main(int argc, char **argv){
/******************************************************* //COMPLETE
char *string1 = "This is a string";
char *string2 = "This is another string";
char *string3 = "This is a string";
char string4[SIZE];
*******************************************************/
string string1 = "This is a string";
string string2 = "This is another string";
string string3 = "This is a string";
string string4[SIZE];
//stack is for automatic variables -- also known as local variables
//heap is for dynamic variables -- anything created with malloc
/********int array[SIZE];**************/ //COMPLETE
vector<int> array1(SIZE);
//FIX THIS ||||||||| FIX THIS ||||||||| FIX THIS |||||||| FIX THIS
int *pointer = (int *)malloc(SIZE * sizeof(int));
//array initializer statements
/************int init[] = {1,2,3,4,5,6};****************/ //COMPLETE
vector<int> init(6);
for(int i = 1; i < 7; i++){
init[i]=i;
}
/************* fprintf(stdout, "string1 = %s\nstring2 = %s\nstring3 = %s\n", string1, string2, string3); **************/ //COMPLETE
cout << "string 1 = " << string1 << endl << "string 2 = " << string2 << endl << "string 3 = " << string3 << endl;
/* prompt, and take in a new string, no more than SIZE characters */
/************************************************************************************* //COMPLETE
fprintf(stdout, "Please enter a string, and hit return: ");
fgets(string4, SIZE, stdin);
fprintf(stdout, "string4 = %s", string4);
**************************************************************************************/
cout << "Please enter a string, and hit return : ";
cin >> string4;
cout << "string4 = " << string4 << endl;
/************************************************************************************** //COMPLETE
if (strcmp(string1, string2) == 0){
//they are equal
fprintf(stdout, "string1 = string2\n");
}
else{
fprintf(stdout, "string1 %c string2\n",(strcmp(string1, string2) > 0) ? '>' : '<');
}
*************************************************************************************/
if(string1==string2){
cout << "String1 = String2" << endl;
}
else if(string1>string2){
cout << "String1 > String2" << endl;
}
else{
cout << "String1 < String2" << endl;
}
/****************************************** //COMPLETE
this is equivalent
x = cond ? a : b;
to this
if(cond)
x = a;
else
x = b;
******************************************/
/*********************************************** //COMPLETE
if (strcmp(string1, string3) == 0){
// they are equal
//fprintf(stdout, "string1 = string3\n");
}
else{
fprintf(stdout, "string1 %c string3\n", (strcmp(string1, string3) > 0) ? '>' : '<');
}
***********************************************/
if(string1==string3){
cout << "String1 = String3" << endl;
}
else if(string1>string3){
cout << "String1 > String3" << endl;
}
else{
cout << "String1 < String3" << endl;
}
/*********************************************** //COMPLETE
if (strcmp(string3, string2) == 0){
fprintf(stdout, "string3 = string2\n");
}
else{
fprintf(stdout, "string3 %c string2\n", (strcmp(string3, string2) > 0) ? '>' : '<');
}
***********************************************/
if(string2==string3){
cout << "String2 = String3" << endl;
}
else if(string2>string3){
cout << "String2 > String3" << endl;
}
else{
cout << "String2 < String3" << endl;
}
/********************************************** //COMPLETE
walk through the arrays, initialize, and then walk through again and print out
for (int i = 0; i < SIZE; ++i){
array[i] = i;
pointer[i] = i;
}
**********************************************/
for (int i = 0; i < SIZE; ++i){
array1[i] = i;
}
/************************************************************************** //COMPLETE
for (int i = 0; i < SIZE; ++i){
fprintf(stdout, "%d\t%d\n", array[i], pointer[i]);
}
***************************************************************************/
for(int i = 0; i < SIZE; ++i){
cout << array1[i] << " " << pointer[i] << endl;
}
/* clean up my mess */
//free(pointer);
delete[] pointer;
return 0;
}
|