Create a program, Similar to the following code. Change the them to planets. You
ID: 3690130 • Letter: C
Question
Create a program, Similar to the following code.
Change the them to planets. You will need a data structure with information about planets.
Your program must have all of the functionality of the states program, but for planets.
Source code is a .C file
// StatesAndCapitals.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct State
{
char name[100];
char capital[100];
float lon,lat;
struct State *next;
};
struct State head;
struct State *EnterNewState()
{
struct State *newstate;
newstate = (struct State *)malloc(sizeof(struct State));
printf("Please enter the name:");
gets(newstate->name);
strtok(newstate->name, " ");
printf("Please enter the capital:");
gets(newstate->capital);
strtok(newstate->capital, " ");
printf("Please enter the longtitude:");
scanf("%f", &newstate->lon);
printf("Please enter the lattitude:");
scanf("%f", &newstate->lat);
newstate->next = NULL;
return(newstate);
}
void AppendToList()
{
struct State *newstate = EnterNewState();
struct State *walker = head.next;
while (walker->next != NULL ) // != NULL)
{
walker = walker->next;
}
walker->next = newstate;
}
void InsertInSortedOrder()
{
struct State *newstate = EnterNewState();
struct State *walker = head.next;
while (walker != NULL)
{
int cmp = _stricmp(newstate->name, walker->name);
if (cmp < 0)
{
newstate->next = walker->next;
walker->next = newstate;
return;
}
}
}
void LoadData()
{
int howmanystates, i;
struct State *walker;
char temp[10];
FILE *fp = fopen("c:\work\data.txt", "r");
if (fp == NULL)
{
printf("The data file could not be opened. ");
return;
}
fscanf(fp, "%d", &howmanystates);
fgets(temp, sizeof(temp), fp);
walker = &head;
for (i = 0; i < howmanystates; i++)
{
struct State *newstate = (struct State *)malloc(sizeof(struct State));
fgets(newstate->name, sizeof(newstate->name), fp);
strtok(newstate->name, " ");
fgets(newstate->capital, sizeof(newstate->capital), fp);
strtok(newstate->capital, " " );
fscanf(fp, "%f", &newstate->lon);
fgets(temp, sizeof(temp), fp);
fscanf(fp, "%f", &newstate->lat);
fgets(temp, sizeof(temp), fp);
newstate->next = NULL;
walker->next = newstate;
walker = walker->next;
}
fclose(fp);
}
void DisplaySingleState(struct State *st)
{
printf("Name:%s, Capital:%s, long:%f, lat:%f ", st->name, st->capital, st->lon, st->lat);
}
void SearchForState()
{
char search[100];
struct State *walker;
printf("Please enter a state to search for:");
gets(search);
strtok(search, " ");
walker = head.next;
while (walker != NULL)
{
if (_stricmp(search, walker->name) == 0)
{
DisplaySingleState(walker);
return;
}
walker = walker->next;
}
printf("That state was now found! ");
}
void DisplayList()
{
struct State *walker;
walker = head.next;
while (walker != NULL)
{
DisplaySingleState(walker);
walker = walker->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
LoadData();
DisplayList();
SearchForState();
return 0;
}
// StatesAndCapitals.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct State
{
char name[100];
char capital[100];
float lon,lat;
struct State *next;
};
struct State head;
struct State *EnterNewState()
{
struct State *newstate;
newstate = (struct State *)malloc(sizeof(struct State));
printf("Please enter the name:");
gets(newstate->name);
strtok(newstate->name, " ");
printf("Please enter the capital:");
gets(newstate->capital);
strtok(newstate->capital, " ");
printf("Please enter the longtitude:");
scanf("%f", &newstate->lon);
printf("Please enter the lattitude:");
scanf("%f", &newstate->lat);
newstate->next = NULL;
return(newstate);
}
void AppendToList()
{
struct State *newstate = EnterNewState();
struct State *walker = head.next;
while (walker->next != NULL ) // != NULL)
{
walker = walker->next;
}
walker->next = newstate;
}
void InsertInSortedOrder()
{
struct State *newstate = EnterNewState();
struct State *walker = head.next;
while (walker != NULL)
{
int cmp = _stricmp(newstate->name, walker->name);
if (cmp < 0)
{
newstate->next = walker->next;
walker->next = newstate;
return;
}
}
}
void LoadData()
{
int howmanystates, i;
struct State *walker;
char temp[10];
FILE *fp = fopen("c:\work\data.txt", "r");
if (fp == NULL)
{
printf("The data file could not be opened. ");
return;
}
fscanf(fp, "%d", &howmanystates);
fgets(temp, sizeof(temp), fp);
walker = &head;
for (i = 0; i < howmanystates; i++)
{
struct State *newstate = (struct State *)malloc(sizeof(struct State));
fgets(newstate->name, sizeof(newstate->name), fp);
strtok(newstate->name, " ");
fgets(newstate->capital, sizeof(newstate->capital), fp);
strtok(newstate->capital, " " );
fscanf(fp, "%f", &newstate->lon);
fgets(temp, sizeof(temp), fp);
fscanf(fp, "%f", &newstate->lat);
fgets(temp, sizeof(temp), fp);
newstate->next = NULL;
walker->next = newstate;
walker = walker->next;
}
fclose(fp);
}
void DisplaySingleState(struct State *st)
{
printf("Name:%s, Capital:%s, long:%f, lat:%f ", st->name, st->capital, st->lon, st->lat);
}
void SearchForState()
{
char search[100];
struct State *walker;
printf("Please enter a state to search for:");
gets(search);
strtok(search, " ");
walker = head.next;
while (walker != NULL)
{
if (_stricmp(search, walker->name) == 0)
{
DisplaySingleState(walker);
return;
}
walker = walker->next;
}
printf("That state was now found! ");
}
void DisplayList()
{
struct State *walker;
walker = head.next;
while (walker != NULL)
{
DisplaySingleState(walker);
walker = walker->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
LoadData();
DisplayList();
SearchForState();
return 0;
}
Explanation / Answer
Answer:)
#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct Planet
{
char name[100];
char satellite[100];
float sun_dis;
struct Planet *next;
};
struct Planet head;
void DisplayList();
struct Planet *EnterNewPlanet()
{
struct Planet *newPlanet;
newPlanet = (struct Planet *)malloc(sizeof(struct Planet));
printf("Please enter the name:");
gets(newPlanet->name);
strtok(newPlanet->name, " ");
printf("Please enter the satellite:");
gets(newPlanet->satellite);
strtok(newPlanet->satellite, " ");
printf("Please enter the distance from sun:");
scanf("%f", &newPlanet->sun_dis);
newPlanet->next = NULL;
return(newPlanet);
}
void AppendToList()
{
struct Planet *newPlanet = EnterNewPlanet();
struct Planet *walker = head.next;
while (walker->next != NULL ) // != NULL)
{
walker = walker->next;
}
walker->next = newPlanet;
}
void InsertInSortedOrder()
{
struct Planet *newPlanet = EnterNewPlanet();
struct Planet *walker = head.next;
while (walker != NULL)
{
int cmp = strcmp(newPlanet->name, walker->name);
if (cmp < 0)
{
newPlanet->next = walker->next;
walker->next = newPlanet;
return;
}
}
}
void LoadData()
{
int howmanyPlanet=0, i;
struct Planet *walker;
printf("Enter the number of planets you want to add to the list::");
scanf("%d",howmanyPlanet);
walker = &head;
for (i = 0; i < howmanyPlanet; i++)
{
struct Planet *newPlanet = (struct Planet *)malloc(sizeof(struct Planet));
printf("Enter the name of the planet::");
gets(newPlanet->name);
printf("Enter the planet's satellite::");
gets(newPlanet->satellite);
printf("Enter the planet's distance from sun::");
scanf("%f", &newPlanet->sun_dis);
newPlanet->next = NULL;
walker->next = newPlanet;
walker = walker->next;
}
}
void DisplaySinglePlanet(struct Planet *st)
{
printf("Name:%s, Satellite:%s, Distance fom sun:%f ", st->name, st->satellite, st->sun_dis);
}
void SearchForPlanet()
{
char search[100];
struct Planet *walker;
struct Planet *found;
struct Planet *prev ;
printf("Please enter a planet that you want to delete:");
gets(search);
strtok(search, " ");
walker = head.next;
prev = head.next;
while (walker != NULL)
{
if (strcmp(search, walker->name) == 0)
{
found = walker;
prev->next = walker->next;
free(found);
printf("List after deletion::");
DisplayList();
return;
}
prev = walker;
walker = walker->next;
}
printf("That planet was now found! ");
}
void DisplayList()
{
struct Planet *walker;
walker = head.next;
while (walker != NULL)
{
DisplaySinglePlanet(walker);
walker = walker->next;
}
}
int main()
{
int choice;
while(1)
{
printf("* Please make a choice from the following: * 1. Add a planet 2. Delete a planet 3. Show all planets in list 4. Quit this program ");
scanf("%d", choice);
if(choice == 1)
LoadData();
if(choice == 2)
SearchForPlanet();
if(choice == 3)
DisplayList();
if(choice == 4)
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.