|登录 |注册

查看: 4525|回复: 25
打印 上一主题 下一主题

help~c programming assignment

[复制链接]
yangzz
2009-9-10 02:03 PM
Question 2:  Merging and listing selected records using files.

The employee salary details of the two branches (Branch-A and Brach-B) of XYZ company are kept in two separate files (Salary_A.txt  and Salary_B.txt)  with each line representing information about one employee. The Human Resource Department wants to have only one list showing salary information about all the Branch-A and Brach-B employees together. Therefore the two text files must be combined/merged into a single text file (Salary_AB.txt) which has the Name filed in alphabetical order.

Write a C program to merge the two files to produce a combined file. After creating the combined file the program must be capable of listing the salary details of all the clerks, engineers, or managers (depending on the user selection) with the Name field in alphabetical order and the total salary printed at the end. The program should display the following menu to the user for them to select one of the four options listed.



Sample Menu:
                -------------------------
                  SALARY MENU
                        ------------------------
1.   TO LIST CLERKS
2.        TO LIST ENGINEERS
3.        TO LIST MANAGERS
4.        TO EXIT
---------------------------
                Please enter your option <1/2/3/4>:


Sample data Salary_A.txt  file (sorted on Name field):

Name        ID No.        Designation        Branch        Salary(RM)
Abraham        1001        Manger        A        3000
Clinton        1003        Clerk        A        2000
Ibrahim        1005        Engineer        A        4000
King        1007        Engineer        A        4000

Sample data for Salary_B.txt file (sorted on Name field):

Name        ID No.        Designation        Branch        Salary(RM)
Jasmine         1002        Manger        B        3000
Kim        1004        Clerk        B        2000
Noor        1006        Engineer        B        4000

friday before 4pm need submit, help~

[ 本帖最后由 yangzz 于 2009-9-10 02:16 PM 编辑 ]
Super-Tomato
2009-9-10 04:32 PM
原帖由 yangzz 于 2009-9-10 02:03 PM 发表
Question 2:  Merging and listing selected records using files.

The employee salary details of the two branches (Branch-A and Brach-B) of XYZ company are kept in two separate files (Salary_A.tx ...


1. 請先自己動手做, 遇到任何不解問題才放上來
2. 不要等到要交的前一天才來等別人幫你擦屁股
3. 中文論壇請盡量把你的題目中文化

回复 #2 Super-Tomato 的帖子

yangzz
2009-9-10 10:05 PM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{int*a,*b,*ahead,*bhead,temp,*Salary;
FILE*fp1;
char name[10],post[25];int id,branch,salary,op=1;
fp1=fopen("salary_A.txt","w");
while(op==1)
{
printf("enter name, id, post, branch and salary:");
scanf("%s%d%s%d%d",&name,&id,&post,&branch,&salary);
fprintf(fp1,"%s\t%d\t%s\t\t%d\t%d\n",name,id,post,branch,salary);
printf("Enter 1 to continue:");
scanf("%d",&op);
}
fclose(fp1);

FILE*fp2;
fp2=fopen("salary_B.txt","w");
while(op==1)
{
printf("enter name, id, post, branch and salary:");
scanf("%s%d%s%d%d",&name,&id,&post,&branch,&salary);
fprintf(fp2,"%s\t%d\t%s\t\t%d\t%d\n",name,id,post,branch,salary);
printf("Enter 1 to continue:");
scanf("%d",&op);
}
fclose(fp2);

FILE*fp3;
int option;
fp2=fopen("salary_B.txt","r");
fp1=fopen("salary_A.txt","r");
fp3=fopen("salary_AB.txt","w+");
if(fp1==NULL||fp2==NULL){
        printf("error in opening file\n");
        return 1;
}
ahead=bhead=NULL;
while(!feof(fp1))
{
        temp=(Salary*)malloc(sizeof(Salary));
        fscanf(fp1,"%s%d%s%s%s\n",temp->name,temp->id,temp->post,temp->branch,temp->sal);
        temp->next=NULL;
        if(ahead==NULL)
        {
        *a=temp;
        *ahead=*a;
        }
        else
        {
        a->next=temp;
        *a=temp;
        }
        while(!feof(fp2))
        {
        temp=(Salary*)malloc(sizeof(Salary));
        fscanf(fp2,"%s%d%s%s%s\n",temp->name,temp->id,temp->post,temp->branch,temp->sal);
        temp->next=NUll;
        if(bhead==NULL)
        {
        *b->next=temp;
        *bhead=*b;
        }
        }
        *a=*ahead;
        *b=*bhead;
        while(a!=NULL||b!=NULL)
        {
        if(a!=NULL)
        {
        fprintf(fp3,"%s\t%d%s%s%s\n",a->name,a->id,a->post,a->branch,a->sal);
        a=a->next;
        }
        else if(b!=NULL)
        {
        fprintf(fp3,"%s\t%d%s%s%s\n",b->name,b->id,b->post,b->branch,b->sal);
        b=b->next;
        }
        }


int option=1; while (option!=5)
        {
        printf("-----------\n");
        printf("SALARY MENU\n");
        printf("-----------\n");
        printf("1. TO LIST CLERK\n");
        printf("2. TO LIST ENGINEERS\n");
        printf("3. TO LIST MANAGERS\n");
        printf("4. TO EXIT\n");
        printf("-----------\n");
        printf("PLEASE ENTER YOUR OPTION <1/2/3/4>");
        scanf("%d",&option);
        a=ahead;
        b=bhead;
        fseek(fp3,0,SEEK_SET);
        switch(option){
        case 1:
        &salary);
        while(!feof(fp3)){
        fscanF(fp3,"%s%s%s%s%s",a->name,a->id,a->post,a->branch,a->sal);
        if(strcmp(a->des,"Clerk")==0)
        printf("%10s%10d%10s%10s%10f\n",a->name,a->id,a->post,a->branch,a->sal);
        }
        break;
        case 2:
        while(!feof(fp3)){
        fscanF(fp3,"%s%s%s%s%s",a->name,a->id,a->post,a->branch,a->sal);
        if(strcmp(a->post,"Engineer")==0)
        printf("%10s%10d%10s%10s%10f\n",a->name,a->id,a->post,a->branch,a->sal);
        }
        break;
        case 3:
        while(!feof(fp3)){
        fscanF(fp3,"%s%s%s%s%s",a->name,a->id,a->post,a->branch,a->sal);
        if(strcmp(a->des,"manager")==0)
        printf("%10s%10d%10s%10s%10f\n",a->name,a->id,a->post,a->branch,a->sal);
        }
        break;
        case 4:
        printf("Thank you!!\n");
        return 0;
        break;
        default:
        printf("invalid input,please enter option again\n");
        break;
        }
        fclose(fp3);
        return 0;
        }


我就写到这里~但error弄不走~
Super-Tomato
2009-9-10 11:29 PM
哇.... 那麼難看 coding 哦
那你的 error 是甚麼??
AirWalker
2009-9-10 11:49 PM
哈哈。。有人中骂哦。。哈哈。。em0013

回复 #4 Super-Tomato 的帖子

yangzz
2009-9-10 11:53 PM
我读工程系~学coding已经很不错了啦~看到都傻掉~换了换了~现在用
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{char cmpa,cmpb;

FILE*fp1;
char name[10],designation[25];int id,branch,op=1;float salary,post;
fp1=fopen("salary_A.txt","w");
while(op==1)
{
printf("enter name, id, post, branch and salary:");
scanf("%s,%d,%s,%d,%d",&name,&id,&post,&branch,&salary);
fprintf(fp1,"%s\t%d\t%s\t\t%d\t%d\n",name,id,post,branch,salary);
printf("Enter 1 to continue:");
scanf("%d",&op);
}
fclose(fp1);


FILE*fp2;
fp2=fopen("salary_B.txt","w");
while(op==1)
{
printf("enter name, id, post, branch and salary:");
scanf("%s,%d,%s,%d,%d",&name,&id,&post,&branch,&salary);
fprintf(fp2,"%s\t%d\t%s\t\t%d\t%d\n",name,id,designation,branch,salary);
printf("Enter 1 to continue:");
scanf("%d",&op);
}
fclose(fp2);
}

等等下~看看error先

回复 #5 AirWalker 的帖子

yangzz
2009-9-10 11:55 PM
静静啦你!我做到很pek cek了的咯~人家我是算equilibelium的~不是programming~好过有些人读IT但什么都不懂~哈哈em0012 em0012
AirWalker
2009-9-11 12:04 AM
严格上是foundation in IT 我懂vb啊。。em0037
Super-Tomato
2009-9-11 12:07 AM
原帖由 yangzz 于 2009-9-10 11:53 PM 发表
我读工程系~学coding已经很不错了啦~看到都傻掉~换了换了~现在用
#include
#include
#include

int main()
{char cmpa,cmpb;

FILE*fp1;
char name[10],designation[25];int id,branch,op=1;float sal ...



我看了沒甚麼大問題, 直接貼出你的錯誤吧, 如果沒錯應該可以正常編譯

回复 #9 Super-Tomato 的帖子

yangzz
2009-9-11 12:46 AM
又弄多一个了~这个比较好~但print出来的东西会多一行~


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

int main()
{

FILE*fp1;
FILE*fp2;
FILE*fp3;
char name[10],post[25],name1[10],post1[25];
int id,branch,id1,branch1;
float salary,salary1;
fp1=fopen("salary_A.txt","r");
fp2=fopen("salary_B.txt","r");
fp3=fopen("MERGE.txt","w");
while(!feof(fp1))
{
fscanf(fp1,"%s%d%s%d%f",&name,&id,&post,&branch,&salary);

fprintf(fp3,"%s\t%d\t%s\t\t%d\t%.2f\n",name,id,post,branch,salary);
}

while(!feof(fp2))
{
fscanf(fp2,"%s%d%s%d%f",&name1,&id1,&post1,&branch1,&salary1);
fprintf(fp3,"%s\t%d\t%s\t\t%d\t%.2f\n",name1,id1,post1,branch1,salary1);
}


fclose(fp1);
fclose(fp2);





return 0;

}



这个的error就是当我compile是~
会出现这个~
DEREK        1        A                1        10.00
ABIAK        2        B                2        11.00
KANIN        3        C                3        12.00
KANIN        3        C                3        12.00
ABOY        999        Z                100        99.00
ADON        998        Y                99        111.00
ADON        998        Y                99        111.00
为什么kanin还有adon会print多一次??
怎样fix?
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2026-1-1 05:35 AM , Processed in 0.095856 second(s), 25 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部