Posted: Fri Aug 11, 2006 5:13 pm
Beware the spacing!
title/first name may be an empty string
title/first name may be an empty string
Code: Select all
//little black book
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20001
//#define M 65
struct lil{
char dept[63];
char title[28];
char fname[28];
char lname[28];
char hadd[53];
char hph[53];
char wph[33];
char cbox[33];
}book[MAX];
int cmp(const void *a,const void *b)
{
lil *p=(lil *)a;
lil *q=(lil *)b;
return strcmp( p->lname,q->lname );
}
int main(void)
{
int test, i, flag=1, flagi=0, flagn, m=0;
char ch='\0';
//freopen("450.txt","r", stdin);
//freopen("0.out","w", stdout);
scanf("%d", &test);
ch=getc(stdin);
while(1)
{
flagn=0;
if(flag)gets(book[m].dept);
//-----------------title
if(flagi)i=1;
else if(!flagi)i=0;
while(ch!=',')
{
ch=getc(stdin);
book[m].title[i++]= ch;
}
book[m].title[i-1]='\0';
ch='\0';
//--------------------first name
i=0;
while(ch!=',')
{
ch=getc(stdin);
book[m].fname[i++]= ch;
}
book[m].fname[i-1]='\0';
ch='\0';
//------------------last name
i=0;
while(ch!=',')
{
ch=getc(stdin);
book[m].lname[i++]= ch;
}
book[m].lname[i-1]='\0';
ch='\0';
//----------------home address
i=0;
while(ch!=',')
{
ch=getc(stdin);
book[m].hadd[i++]= ch;
}
book[m].hadd[i-1]='\0';
ch='\0';
//----------------home phone
i=0;
while(ch!=',')
{
ch=getc(stdin);
book[m].hph[i++]= ch;
}
book[m].hph[i-1]='\0';
ch='\0';
//----------------work phone
i=0;
while(ch!=',')
{
ch=getc(stdin);
book[m].wph[i++]= ch;
}
book[m].wph[i-1]='\0';
ch='\0';
//---------------campus box
i=0;
while(ch!='\n' || ch!=EOF)
{
ch=getc(stdin);
if(ch=='\n')
{
flagn=1;
break;
}
else if(ch==EOF)
break;
book[m].cbox[i++]= ch;
}
book[m].cbox[i]='\0';
m++;
if(ch==EOF)
break;
ch='\0';
ch=getc(stdin);
if(ch=='\n'&& flagn==1)
{
flag=1;
flagi=0;
}
else
{
i=0;
book[m].title[i]=ch;
flagi=1;
flag=0;
}
}
qsort(book,m,sizeof(book[0]),cmp);
for(i=0;i<m;i++)
{
printf("----------------------------------------\n");
printf("%s %s %s\n", book[i].title, book[i].fname, book[i].lname);
printf("%s\n", book[i].hadd);
printf("Department: %s\n",book[i].dept);
printf("Home Phone: %s\n", book[i].hph);
printf("Work Phone: %s\n",book[i].wph);
printf("Campus Box: %s\n", book[i].cbox);
}
return 0;
}
Code: Select all
#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main(){
string dept, MemberInfo, temp, fin;
list<string>facultyMember;
list<string>LastName;
int numDept, countDept;
//freopen("M:\\Documents and Settings\\prince\\Desktop\\450.txt","rt",stdin);
while (true){
getline(cin,temp,'\n');
if (temp == "")
break;
numDept = atoi(temp.c_str());
countDept = 0;
while (countDept < numDept){
getline(cin, dept, '\n');
while (( getline(cin, MemberInfo,'\n') && MemberInfo != "")){
temp = MemberInfo;
temp.erase(0,temp.find(",",0)+1);
temp.erase(0,temp.find(",",0)+1);
string temp1(temp.begin(),temp.find(",",0));
LastName.push_back(temp1);
LastName.sort();
list<string>::iterator it = LastName.begin();
list<string>::iterator itFM = facultyMember.begin();
for (int i = 0; i < LastName.size(); i++){
if (*it != temp1){
it++;
itFM++;
}
else
break;
}
facultyMember.insert(itFM,dept+","+MemberInfo);
}
countDept++;
}
list<string>::iterator itFM = facultyMember.begin();
int i = 0;
while(i++ < facultyMember.size()){
printf("----------------------------------------\n");
string temp = *itFM;
dept = string(temp.begin(),temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
for (int i = 0; i < 3; i++){
string str(temp.begin(),temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
cout <<str <<" ";
}
string str(temp.begin(),temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
cout <<"\n" << str<<"\n";
cout <<"Department: "<<dept<<"\n";
cout <<"Home Phone: ";
str = string(temp.begin(),temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
cout << str <<"\n";
cout <<"Work Phone: ";
str = string(temp.begin(),temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
cout << str <<"\n";
cout <<"Campus Box: ";
cout << temp <<"\n";
itFM++;
}
}
return 0;
}
because it doesn't know a constructor of std::string whose parameters are std::string's iterator, and a size_t.string temp1(temp.begin(),temp.find(",",0));
Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main(){
string dept, MemberInfo, temp, fin, str, temp1;
list<string>facultyMember;
list<string>LastName;
int i, numDept, countDept, start, pos;
string::iterator it;
char ch[1000];
while (1 == scanf("%d",&numDept)){
facultyMember.clear();
LastName.clear();
gets(ch);
countDept = 0;
while(countDept < numDept){
gets(ch);
dept = string(ch);
if (dept == "")
continue;
while (gets(ch) != NULL){
MemberInfo = string(ch);
if (MemberInfo == "")
break;
temp = MemberInfo;
temp.erase(0,temp.find(",",0)+1);
temp.erase(0,temp.find(",",0)+1);
temp1 = string(temp.begin(),temp.begin()+temp.find(',',0));
LastName.push_back(temp1);
LastName.sort();
list<string>::iterator it = LastName.begin();
list<string>::iterator itFM = facultyMember.begin();
for (i = 0; i < LastName.size(); i++){
if (*it != temp1){
it++; itFM++;
}
else break;
}
facultyMember.insert(itFM,dept+","+MemberInfo);
}
countDept++;
}
list<string>::iterator itFM = facultyMember.begin();
int i = 0;
while(i++ < facultyMember.size()){
printf("----------------------------------------\n");
string temp = *itFM;
dept = string(temp.begin(),temp.begin()+temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
for (int l = 0; l < 3; l++){
str = string(temp.begin(),temp.begin()+temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
printf("%s ",str.c_str());
}
str = string(temp.begin(),temp.begin()+temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
printf("\n%s\n",str.c_str());
printf("Department: %s\n",dept.c_str());
printf("Home Phone: ");
str = string(temp.begin(),temp.begin()+temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
printf("%s\n",str.c_str());
printf("Work Phone: ");
str = string(temp.begin(),temp.begin()+temp.find(",",0));
temp.erase(0,temp.find(",",0)+1);
printf("%s\n",str.c_str());
printf("Campus Box: ");
printf("%s\n",temp.c_str());
itFM++;
}
}
return 0;
}
Code: Select all
3
English Department
Dr.,Tom,Davis,Anystreet USA,+1-555-2832,+1-555-2423,823
Mrs.,Jessica,Lembeck,Center Street,+1-555-2543,+1-555-8584,928
Dr.,Bob,Davisa,Anywherestreet USA,545-2832,555-2222,911
Computer Science
Mr.,John,Euler,East Pleasure CA,+1-555-1432,+1-555-2343,126
Geology
Ms.,Aisha,Ang,West Pleasure TT,+868-525-1422,+868-166-2343,666
Code: Select all
----------------------------------------
Ms. Aisha Ang
West Pleasure TT
Department: Geology
Home Phone: +868-525-1422
Work Phone: +868-166-2343
Campus Box: 666
----------------------------------------
Dr. Tom Davis
Anystreet USA
Department: English Department
Home Phone: +1-555-2832
Work Phone: +1-555-2423
Campus Box: 823
----------------------------------------
Dr. Bob Davisa
Anywherestreet USA
Department: English Department
Home Phone: 545-2832
Work Phone: 555-2222
Campus Box: 911
----------------------------------------
Mr. John Euler
East Pleasure CA
Department: Computer Science
Home Phone: +1-555-1432
Work Phone: +1-555-2343
Campus Box: 126
----------------------------------------
Mrs. Jessica Lembeck
Center Street
Department: English Department
Home Phone: +1-555-2543
Work Phone: +1-555-8584
Campus Box: 928
Thank you for sharing your thoughts.brianfry713 wrote:Don't over-complicate it. Yes there are often many ways to solve a problem, but I'd recommend going with the most straightforward that is correct and fast enough. Don't take a simple problem and try to make it difficult.
Thank you. I tried this approach on a couple problems and it really does help. For example, 11136 (Hoax or What) looks like it's really hard (because it's so long-winded) but like you said, it's not so once you think about it for a bit. I remember that when I first started on UVA back in '10, I couldn't do some of the problems because they were so long-winded / confusing. It didn't occur to me that this was a strategy.brianfry713 wrote:After reading the problem statement think about what it's asking you to do at a high simple level. Some problem statements are long, verbose, and intentionally misleading but are actually easy to code once you figure it out. On this problem it's just asking you to read in some records and display them sorted.
Thanks for sharing.brianfry713 wrote:I usually just read the problem statement and then start coding. Sometimes it helps to draw a diagram on paper.