C programming: can i get help writting this program, Using your favorite text ed
ID: 3856455 • Letter: C
Question
C programming:
can i get help writting this program,
Using your favorite text editor, use the data on the next page to create a file which has a
state/city string and a temperature for the city on each line such as:
Arizona,Tucson:107
For any given state/city, there may be many lines in the file. The lines towards the
bottom of the file represent the most recent temperatures. Write a program which does
the following:
1. Prompts the user to enter the name of the input file; if the user does not enter a
name, use a default file name, such as temperatures.txt
2. Reads the data from file into an ordered list of stacks. The list is sorted in
ascending order by the state/city string, a unique key. The temperature values for
a given state/city are pushed onto its stack. (See example below).
• The stack nodes contain
o an integer (temperature) and
o a pointer to the next stack node.
• The list nodes contain
o a state/city string,
o a pointer to the next state/city node,
o a pointer to the previous state/city node,
o a pointer to the stack of temperature nodes for that
state/city,
o a count of the nodes in the stack, and
o a total of the temperature values in that stack.
Requirement: circularly doubly-linked list with one sentinel node
3. Displays the sorted list in ascending order (state/city and only one temperature
value – at the top of the stack. (A – Z)
4. Displays the sorted list in descending order (state/city and only one temperature
value – at the top of the stack. (Z – A).
5. Search loop. Prompts the user for a state/city string. If the state/city string is in
the list, display the most recent temperature and the average temperature for that
state/city. Give an error message if the state/city string is not found in the list.
Prompts the user repeatedly until s/he enters “quit”.
EXAMPLE
// input file
Arizona,Tucson:99
Oregon,Portland:85
Arizona,Tucson:90
Oregon,Portland:75
Arizona,Tucson:100
// 2 nodes in the linked list: each node has its own stack
{Arizona,Tucson, 289, 3} {Oregon,Portland, 160, 2}
//stack top //stack top
100 75
90 85
INPUT FILE: temperatures.txt
Pennsylvania,Philadelphia:91
California,San Francisco:75
Nevada,Reno:108
Arizona,Flagstaff:81
California,Yreka:101
Arizona,Tucson:107
California,Los Angeles:78
California,Los Angeles:81
Pennsylvania,Pittsburgh:89
Oregon,Salem:90
California,Los Angeles:82
Arizona,Flagstaff:84
California,San Francisco:64
Oregon,Salem:83
California,San Francisco:68
Arizona,Tucson:99
California,Yreka:100
Arizona,Phoenix:109
Oregon,Portland:82
Arizona,Tucson:103
Oregon,Portland:79
Arizona,Phoenix:107
California,Cupertino:88
California,San Francisco:82
Arizona,Tucson:109
Oregon,Salem:85
Pennsylvania,Philadelphia:86
California,Los Angeles:97
Nevada,Reno:108
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
//stack node
struct stack_struct{
int temperature; //temperature as an integer
struct stack_struct *next; //a pointer to the next stack node
};
typedef struct stack_struct stack;
//list node
struct list_struct{
char state_city[456]; // name of state/city
struct list_struct *next; // pointer to next state/city
struct list_struct *previous; // pointer to previous state/city
stack *temp; // pointer to the first temperature recorded;
int numberoftemperature; // number of nodes of temperature for that state/city
int totaltemperature; // total temperature for that state/city
};
typedef struct list_struct list;
//strcmp1 function
int strcmp1(char s1[], char s[]){
int i1;
int a;
int i;
i = 0;
i1 = 0;
printf(" %s %s ",s1,s);
while(i == 0){
i = s1[i1] - s[i1];
i1++;
}
return i;
}
//sort_the_linked_list function
int sort_the_linked_list(list *reference_list, int a){
char s[456];
char s1[456];
char sort_char[456];
int sort_int;
int i;
i = 0;
char state_city1[456][a];
int temperature[a];
while(reference_list){
strcpy(state_city1[i],reference_list->state_city);
temperature[i] = reference_list->temp->temperature;
reference_list=reference_list->next;
i++;
}
a = i;
i = 0;
int i1;
int i1_1;
for (i = 0; i <(a-1); i++){
for (i1 = 0; i1 < a-i-1; i1++){
strcpy(s,(state_city1[i1]));
strcpy(s1,(state_city1[i1 + 1]));
i1_1 = strcmp1(s,s1);
printf("%d",i1_1);
if ( i1_1>0 ){
printf(" deep");
strcpy(sort_char,state_city1[i1]);
strcpy(state_city1[i1],state_city1[i1+1]);
strcpy(state_city1[i1+1],sort_char);
sort_int = temperature[i1];
temperature[i1] = temperature[i1 + 1];
temperature[i1 + 1] = sort_int;
}
}
}
printf(" In Ascending order ");
i = 0;
for(i = 0; i<a; i++){
printf(" %s %d",state_city1[i],temperature[i]);
}
printf(" In Decending order ");
i = a;
for(i = a-1; i>=0; i--){
printf(" %s %d",state_city1[i],temperature[i]);
}
return 0;
}
//search_linked_list function
int search_linked_list(list *reference_list){
char s[456];
int a;
a = 0;
while(1){
printf(" Please enter the name of State,City : ");
scanf("%s",s);
if(!strcmp(s,"quit")){
break;
}
while(a==0){
if(!strcmp(s,reference_list->state_city)){
a = 1;
break;
}
if(reference_list->next == NULL){
break;
}
reference_list = reference_list->next;
}
if(a == 0){
printf(" No State/City found ");
}
if(a != 0){
printf(" Name : %s Total temperature : %d Average Temp : %d",reference_list->state_city, reference_list->totaltemperature,(reference_list->totaltemperature)/(reference_list->numberoftemperature));
}
while(reference_list->previous){
reference_list = reference_list->previous;
}
}
}
int main(){
FILE *filepointer;
char filename[456];
printf(" Please Enter the name(in file.txt format) of the file(write N if you refuse to enter) : ");// user will write N if he refuses to enter
scanf("%s",filename); // takes the filename
if(!(strcmp(filename,"N"))){
filepointer = fopen("temp.txt","r"); // If N is entered, it goes with the default string
printf("file tried to be opened");
}
else{
filepointer = fopen(filename,"r"); // opens the file specified
if(filepointer == NULL){
printf(" Invalid filename, going with the default : "); // opens the default file if invalid filename is entered
filepointer = fopen("temp.txt","r");
}
}
if(filepointer == NULL){
printf("NULL");
}
char state_city_temperature[456];
list *list1;
list *reference_list;
reference_list = NULL;
stack *stack1;
stack *reference_stack;
int a;
a = 0;
int x;
char s[2] = ":";
char s1[2] = ":";
int temperature1;
char *state_city1;
while(fgets(state_city_temperature,456,filepointer)){//This code contains one row of entry as a string
// eg : Arizona,Tucson:99
a++;
x = 0;
strcpy(state_city1,strtok(state_city_temperature,s));;
temperature1 = atoi(strtok(NULL,s1));
if(a > 1){
while(x==0){
if(!strcmp(state_city1,reference_list->state_city)){
x = 1;
break;
}
if(reference_list->next == NULL){
break;
}
reference_list = reference_list->next;
}
if(x == 0){
while((reference_list->previous)){
reference_list = reference_list->previous;
}
}
}
if(x == 0){
list1 = (list *)malloc(sizeof(list));
list1->next = reference_list;
list1->previous = NULL;
if(a > 1){
reference_list->previous = list1;
}
reference_list = list1;
stack1 = (stack *)malloc(sizeof(stack));
list1->temp = stack1;
stack1->temperature = temperature1;
stack1->next = NULL;
list1->numberoftemperature = 1;
list1->totaltemperature = temperature1;
strcpy(list1->state_city,state_city1);
}
if(x == 1){
stack1 = (stack *)malloc(sizeof(stack));
stack1->next = reference_list->temp;
reference_list->temp = stack1;
stack1->temperature = temperature1;
reference_list->numberoftemperature++;
reference_list->totaltemperature = reference_list->totaltemperature + temperature1;
while((reference_list->previous)){
reference_list = reference_list->previous;
}
}
}
sort_the_linked_list(reference_list, a);
search_linked_list(reference_list);
fclose(filepointer);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.