Question

씬어먹는 C 언어. x (1) E x Minnesota Judicial x G Womens World C 나매스발매정보,. 신사/압구정 거리파 10.1. Name_year Papago x + M X x x https://l

x x | 나매시발매정보. 신사/압구정 거리피 씬어먹는 C 언어. G Womens World C 10.1. Name year i (0) 머리스타일에 어 Minnesota Judiaal x Papago x 6 M X x x

나대시발대정보,e x| 신사/말구정 거리패 씬어먹는 C 언어. x | . (1) 메리스타일에 어 x Repl it- ElactricM 10.1. Name_year i C Given * Papago M x X https://l나대시발대정보,e x| 신사/말구정 거리패 씬어먹는 C 언어. x | . (1) 메리스타일에 어 x Repl it- ElactricM 10.1. Name year i C Given * Papago M x X https://l

Name_year_test.c


#include "Name_year.h"

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>

void print_name_year(const Name_year * ny)
{
   printf("%s,%d\n", ny->name, ny->year);
}

int main()
{
   char * ny_format = "%s,%d\n";

#if PHASE >= 1

   puts("Phase 1 tests");

   // Make sure we can create an object and get the data back.
   // We're creating the name as a local array so that we can
   // make sure that create_name_year() does not merely make
   // a copy of the pointer.
   char stroustrup[20];
   strcpy(stroustrup, "Stroustrup");
   Name_year * ny_stroustrup = create_name_year(stroustrup, 1950);
   stroustrup[0] = '\0';
   print_name_year(ny_stroustrup);

   // Make sure we can create a second object independent from the first one.
   char knuth[20];
   strcpy(knuth, "Knuth");
   Name_year * ny_knuth = create_name_year(knuth, 1938);
   knuth[0] = '\0';
   print_name_year(ny_stroustrup);
   print_name_year(ny_knuth);

   destroy_name_year(ny_stroustrup);
   destroy_name_year(ny_knuth);

#endif

#if PHASE >= 2

   std::cout << "Phase 2 tests\n";

   // Make sure we can change the year.
   Name_year ny_dijkstra{ "Dijkstra", 1980 };
   std::cout << ny_dijkstra.get_name() << "," << ny_dijkstra.get_year() << '\n';
   ny_dijkstra.set_year(1930);
   std::cout << ny_dijkstra.get_name() << "," << ny_dijkstra.get_year() << '\n';

   // Make sure the proper exception is thrown from the ctor.
   try
   {
       Name_year ny_lovelace{ "Lovelace", 1815 };
       std::cout << "Error: no exception thrown for invalid year in ctor\n";
   }
   catch (std::runtime_error &)
   {
       std::cout << "Correct exception thrown for invalid year in ctor\n";
   }
   catch (...)
   {
       std::cout << "Wrong type of exception thrown for invalid year in ctor\n";
   }

   // Make sure the proper exception is thrown from set_year().
   Name_year ny_lovelace{ "Lovelace", 1915 };
   try
   {
       ny_lovelace.set_year(1815);
       std::cout << "Error: no exception thrown for invalid year in set_year()\n";
   }
   catch (std::runtime_error &)
   {
       std::cout << "Correct exception thrown for invalid year in set_year()\n";
   }
   catch (...)
   {
       std::cout << "Wrong type of exception thrown for invalid year in set_year()\n";
   }

   // Make sure trying to set an invalid year did not change the object.
   std::cout << ny_lovelace.get_name() << "," << ny_lovelace.get_year() << '\n';

#endif

#if PHASE>=3

   std::cout << "Phase 3 tests\n";

   // Create a vector of Name_pairs.
   std::vector<Name_year> nys{
       { "Stroustrup", 1950 },
   { "Knuth", 1938 },
   { "Smith", 1961 },
   { "Smith", 1960 },
   { "Dijkstra", 1930 }
   };

   // Make sure we can print them out
   std::cout << "Before sorting\n";
   for (const auto & ny : nys)
       std::cout << ny << '\n';

   // Make sure the two Smiths are equal.
   if (nys[2] == nys[3])
       ;
   else
       std::cout << "Error: operator== returned false for equivalent objects\n";
   if (nys[2] != nys[3])
       std::cout << "Erorr: operator!= returned true for equivalent objects\n";

   // Make sure we can sort. We'll use stable_sort to make sure
   // the year is taken into account (the Smiths should swap relative
   // position).
   std::stable_sort(std::begin(nys), std::end(nys));
   std::cout << "After sorting\n";
   for (const auto & ny : nys)
       std::cout << ny << '\n';

#endif

   return 0;
}

please help me as soon as possible!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Name_year.h

#ifndef NAME_YEAR_H_

#define NAME_YEAR_H_

typedef struct

{

               char *name;

               int year;

}Name_year;

Name_year * create_name_year(const char * name, int year);

void destroy_name_year(Name_year * ny);

#endif /* NAME_YEAR_H_ */

//end of Name_year.h

// Name_year.c

#include "Name_year.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

Name_year * create_name_year(const char * name, int year)

{

               Name_year *ny = (Name_year*) malloc(sizeof(Name_year));

               if(ny == NULL)

               {

                              return NULL;

               }

               ny->name = (char*) malloc(sizeof(char)*(strlen(name)+1));

               if(ny->name == NULL)

                              return NULL;      

               strcpy(ny->name,name);

               ny->year = year;

               return ny;

}

void destroy_name_year(Name_year * ny)

{

               free(ny->name);

               free(ny);

}

//end of Name_year.c

// Name_year_test.c

#include "Name_year.h"

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <string.h>

void print_name_year(const Name_year * ny)

{

   printf("%s,%d\n", ny->name, ny->year);

}

int main()

{

   char * ny_format = "%s,%d\n";

#if PHASE >= 1

   puts("Phase 1 tests");

   // Make sure we can create an object and get the data back.

   // We're creating the name as a local array so that we can

   // make sure that create_name_year() does not merely make

   // a copy of the pointer.

   char stroustrup[20];

   strcpy(stroustrup, "Stroustrup");

   Name_year * ny_stroustrup = create_name_year(stroustrup, 1950);

   stroustrup[0] = '\0';

   print_name_year(ny_stroustrup);

   // Make sure we can create a second object independent from the first one.

   char knuth[20];

   strcpy(knuth, "Knuth");

   Name_year * ny_knuth = create_name_year(knuth, 1938);

   knuth[0] = '\0';

   print_name_year(ny_stroustrup);

   print_name_year(ny_knuth);

   destroy_name_year(ny_stroustrup);

   destroy_name_year(ny_knuth);

#endif

#if PHASE >= 2

   std::cout << "Phase 2 tests\n";

   // Make sure we can change the year.

   Name_year ny_dijkstra{ "Dijkstra", 1980 };

   std::cout << ny_dijkstra.get_name() << "," << ny_dijkstra.get_year() << '\n';

   ny_dijkstra.set_year(1930);

   std::cout << ny_dijkstra.get_name() << "," << ny_dijkstra.get_year() << '\n';

   // Make sure the proper exception is thrown from the ctor.

   try

   {

       Name_year ny_lovelace{ "Lovelace", 1815 };

       std::cout << "Error: no exception thrown for invalid year in ctor\n";

   }

   catch (std::runtime_error &)

   {

       std::cout << "Correct exception thrown for invalid year in ctor\n";

   }

   catch (...)

   {

       std::cout << "Wrong type of exception thrown for invalid year in ctor\n";

   }

   // Make sure the proper exception is thrown from set_year().

   Name_year ny_lovelace{ "Lovelace", 1915 };

   try

   {

       ny_lovelace.set_year(1815);

       std::cout << "Error: no exception thrown for invalid year in set_year()\n";

   }

   catch (std::runtime_error &)

   {

      std::cout << "Correct exception thrown for invalid year in set_year()\n";

   }

   catch (...)

   {

       std::cout << "Wrong type of exception thrown for invalid year in set_year()\n";

   }

   // Make sure trying to set an invalid year did not change the object.

   std::cout << ny_lovelace.get_name() << "," << ny_lovelace.get_year() << '\n';

#endif

#if PHASE>=3

   std::cout << "Phase 3 tests\n";

   // Create a vector of Name_pairs.

   std::vector<Name_year> nys{

       { "Stroustrup", 1950 },

   { "Knuth", 1938 },

   { "Smith", 1961 },

   { "Smith", 1960 },

   { "Dijkstra", 1930 }

   };

   // Make sure we can print them out

   std::cout << "Before sorting\n";

   for (const auto & ny : nys)

       std::cout << ny << '\n';

   // Make sure the two Smiths are equal.

   if (nys[2] == nys[3])

       ;

   else

       std::cout << "Error: operator== returned false for equivalent objects\n";

   if (nys[2] != nys[3])

       std::cout << "Erorr: operator!= returned true for equivalent objects\n";

   // Make sure we can sort. We'll use stable_sort to make sure

   // the year is taken into account (the Smiths should swap relative

   // position).

   std::stable_sort(std::begin(nys), std::end(nys));

   std::cout << "After sorting\n";

   for (const auto & ny : nys)

       std::cout << ny << '\n';

#endif

   return 0;

}

//end of Name_year_test.c

Output:

Phase 1 tests Stroustrup,1950 Stroustrup,1950 Knuth,1938

Add a comment
Know the answer?
Add Answer to:
Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 10.3 Name_year in C - phase 3 This lab is part 2 of a series of...

    10.3 Name_year in C - phase 3 This lab is part 2 of a series of 3 labs that will walk you through the process of creating a C struct and related functions that are the equivalent of the Name_year class you created in chapter 9. For this phase, we will Create an init_name_year() function that initializes a Name_year object. Move the object initialization logic from create_name_year() to init_name_year(). Create a function called compare_name_year() to compare two Name_year objects. The...

  • -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the...

    -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the passport photo that belongs in that particular passport. This Photo class holds its location or file name and the pixel data of the image. In this lab the pixel data is only used to show how memory maybe used when a program uses large objects. We will not...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • Can someone please complete the "allTheQueensAreSafe" function? #include <stdio.h> #include <stdlib.h> void printBoard(int *whichRow, int n)...

    Can someone please complete the "allTheQueensAreSafe" function? #include <stdio.h> #include <stdlib.h> void printBoard(int *whichRow, int n) {    int row, col;    for (row = 0; row < n; row++)    {        for (col = 0; col < n; col++)        {            printf("%c", (whichRow[col] == row) ? 'Q' : '.');        }        printf("\n");    }    printf("\n"); } int allTheQueensAreSafe(int *whichRow, int n, int currentCol) {    // TODO: Write a function that returns 1 if all the queens represented by    // this array are safe (i.e., none...

  • I wrote this code but there’s an issue with it : #include <iostream> #include <vector&...

    I wrote this code but there’s an issue with it : #include <iostream> #include <vector> #include <string> #include <fstream> using namespace std; class Borrower { private: string ID, name; public: Borrower() :ID("0"), name("no name yet") {} void setID(string nID); void setName(string nID); string getID(); string getName(); }; void Borrower::setID(string nID) { ID = nID; } void Borrower::setName(string nname) { name = nname; } string Borrower::getID() { return ID; } string Borrower::getName() { return name; } class Book { private: string...

  • #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part,...

    #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part, your program loads a vending machine serving cold drinks. You start with many foods, some are drinks. Your code loads a vending machine from foods, or, it uses water as a default drink. Create class Drink, make an array of drinks, load it and display it. Part 1 steps: [5 points] Create a class called Drink that contains information about a single drink. Provide...

  • Program 2 #include #include using namespace std; int main() { int total = 0; int num...

    Program 2 #include #include using namespace std; int main() { int total = 0; int num = 0; ifstream inputFile; inputFile.open("inFile.txt"); while(!inputFile.eof()) { // until we have reached the end of the file inputFile >> num; total += num; } inputFile.close(); cout << "The total value is " << total << "." << endl; Lab Questions: We should start by activating IO-exceptions. Do so using the same method in Step 3 of the Program 1 assignment above, except that instead...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT