Question

Travis has a documented list of people and their symptoms (to prevent a FERPA violation Travis has replaced the students namOutput Specification For each query you will either output all the students that have a given symptom or all the symptoms a gSample Input Sample Output 2 5 u john sneezed u john coughed u tyler cried q student john q student tyler sneezed coughed 1 c

using structs, in simplest form

using c programing

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

Hi,Please find the solution and rate the answer.

//

// main.c

// Student Diseases & Symptoms

//

#include <stdio.h>

#include "string.h"

typedef struct studentsymptoms studsym;

struct studentsymptoms{

char name[100];

char symptom[100];

studsym *next;

};

studsym *head;

studsym *passingAround;

void printAll(studsym *head){

while(head!=NULL){

printf("Name is %s, and Symptom is %s",head->name,head->symptom);

head=head->next;

}

}

char* removeNewLine(char str[]){

int i=0;

while(str[i++]!='\0'){

if(str[i]=='\n')str[i]='\0';

}

return str;

}

studsym* add(studsym *head,studsym *item){

item->next=NULL;

if(head==NULL){

head=item;

return head;

}

studsym *temp = head;

while(temp->next!=NULL){

temp = temp->next;

}

temp->next = item;

return head;

}

int findLength(const char data[]){

int i=0;

while(data[i++]!='\0');

return i;

}

int areStringsSame(const char str1[],const char str2[]){

int len1=findLength(str1),len2=findLength(str2);

// int same = 0;

if(len1==len2){

for (int i=0; i<len2; i++) {

if(str2[i]!=str1[i]){

return 10;

}

}

return 0;//means same

}else{

return 10;//means not matched

}

}

studsym* findByName(studsym *head,char name[]){

if(head==NULL){

return NULL;

}

studsym *temp = head;

while(temp!=NULL){

if(areStringsSame(temp->name, name)==0){

studsym *tempy = (studsym*)malloc(sizeof(studsym));

strcpy(tempy->name,temp->name);

strcpy(tempy->symptom,temp->symptom);

passingAround = add(passingAround, tempy);

}

temp = temp->next;

}

return passingAround;

}

studsym* findBySymptom(studsym *head,char symptom[]){

if(head==NULL){

return NULL;

}

studsym *temp = head;

while(temp!=NULL){

if(areStringsSame(removeNewLine(temp->symptom), symptom)!=0){

studsym *tempy = (studsym*)malloc(sizeof(studsym));

strcpy(temp->name,tempy->name);

strcpy(temp->symptom,tempy->symptom);

add(passingAround, tempy);

}

temp = temp->next;

}

return passingAround;

}

void printAllSymptoms(studsym *head){

while (head!=NULL) {

printf("%s, ",head->symptom);

head=head->next;

}

}

void printAllNames(studsym *head){

while (head!=NULL) {

printf("%s, ",head->name);

head=head->next;

}

}

int main(int argc, const char * argv[]) {

printf("Enter the number of queries:");

int choice=0;

scanf("%d",&choice);

for (int i=0; i<choice; i++) {

char data[100];

// gets(data);

fgets(data, 100, stdin);

if(data[0]=='\n')

fgets(data, 100, stdin);

  

// printf("%s",data);

if(data[0]=='q'){

// int i=2;

strtok(data," ");

char *findd = strtok(NULL," ");

if(areStringsSame("student", findd)==0){

studsym *x= findByName(head,removeNewLine(strtok(NULL," ")));

printAllSymptoms(x);

}else{

studsym *poin = findBySymptom(head,removeNewLine(strtok(NULL," ")));

printAllNames(poin);

}

}else{

int k=0;

char name[100],symptom[100];

strtok(data," ");

strcpy(name,strtok(NULL," "));

strcpy(symptom,strtok(NULL," "));

studsym *another = (studsym*)malloc(sizeof(studsym));

printf("%s",name);

printf("%s\n",symptom);

strcpy(another->name, name);

strcpy(another->symptom, symptom);

// another->symptom = symptom;

head = add(head,another);

}

}

  

}

Some extra commas are coming, which may be deleted. This is the simplest possible program. Tried best.

Sample output:

Enter the number of queries:8 u eric laughed ericlaughed u tim cried timcried u tim laughed timlaughed u eric cried ericcried

Add a comment
Know the answer?
Add Answer to:
using structs, in simplest form using c programing Travis has a documented list of people and...
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
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