int Insert(ZGGZ tp[], int n) { if (n == 0) return -1; int count = n; int i; Disp(tp,count); while(1){ int k; stringinput(tp[count].num, 10, "编号"); k = Locate(tp, count, tp[count].num, 1); if (k < 0) continue; for (i = count; i > k; --i) { tp[i] = tp[i-1]; } stringinput(tp[k].num, 10, "新编号"); stringinput(tp[k].name, 15, "新姓名"); tp[k].jbgz = numberinput("新基本工资"); tp[k].jj = numberinput("新奖金"); tp[k].kk = numberinput("新扣款"); //完成相关计算 tp[k].yfgz = tp[k].jbgz + tp[k].jj - tp[k].kk; tp[k].sk = tp[k].yfgz * 0.4; tp[k].sfgz = tp[k].yfgz - tp[k].sk; count++; saveflag = 1; Disp(tp, count); return count; } }
void Modify(ZGGZ tp[], int n) { if (n == 0) return; int count = n; int k; char select; if (count == 0) { return ; } Disp(tp, count); printf("请输入修改编号\n"); stringinput(tp[count].num, 10, "编号"); if (strcmp(tp[count].num, "0") == 0) { return ; } k = Locate(tp, count, tp[count].num, 1); if (k < 0) return;//未找到直接返回主菜单 printf("修改编号? y or n\n"); scanf("%c", &select);getchar(); if ( select == 'y' || select == 'Y') stringinput(tp[k].num, 10, "新编号"); printf("修改姓名? y or n\n"); scanf("%c", &select);getchar(); if ( select == 'y' || select == 'Y') stringinput(tp[k].name, 15, "新姓名"); printf("修改基本工资? y or n\n"); scanf("%c", &select);getchar(); if ( select == 'y' || select == 'Y') tp[k].jbgz = numberinput("新基本工资"); printf("修改奖金? y or n\n"); scanf("%c", &select);getchar(); if ( select == 'y' || select == 'Y') tp[k].jj = numberinput("新奖金"); printf("修改扣款? y or n\n"); scanf("%c", &select);getchar(); if ( select == 'y' || select == 'Y') tp[k].kk = numberinput("新扣款"); //完成相关计算 tp[k].yfgz = tp[k].jbgz + tp[k].jj - tp[k].kk; tp[k].sk = tp[k].yfgz * 0.4; tp[k].sfgz = tp[k].yfgz - tp[k].sk; saveflag = 1; Disp(tp, count); }
int Add(ZGGZ tp[],int n) { int count = n; char choice;// = 'y'; while (1) { //输入相关信息 stringinput(tp[count].num, 10, "编号"); if (strcmp(tp[count].num, "0") == 0) { menu(); break; } while (1) { int k; k = Locate(tp, count, tp[count].num, 1); if (k < 0) break; printf("编号已存在,重新输入\n"); stringinput(tp[count].num, 10, "编号"); } stringinput(tp[count].name, 15, "姓名"); tp[count].jbgz = numberinput("基本工资"); tp[count].jj = numberinput("奖金"); tp[count].kk = numberinput("扣款"); //完成相关计算,count++ tp[count].yfgz = tp[count].jbgz + tp[count].jj - tp[count].kk; tp[count].sk = tp[count].yfgz * 0.4; tp[count].sfgz = tp[count].yfgz - tp[count].sk; count++; saveflag = 1; //如果还有要添加,继续;否则退出。 printf("如果还有要添加,输入(y or n):\n"); scanf("%c", &choice);getchar(); /*choice = getchar();*/ if(choice=='y'||choice=='Y') continue; printf("\n\n\n\n\n"); menu();//添加操作完成后显示菜单供选择 break; } return count; /*int add(ZGGZ TP[],int n )*/ /*函数用于在数组tp中增加工资记录元素,并返回数组中的当前记录数。*/ }
int main(void) { char *prompt1 = "Please enter a name for this bill: "; char *prompt2 = "Please enter total cost of bill (in $ and cents): $"; char *prompt3 = "Please enter number of days the bill is for (whole numbers only): "; char *prompt4 = "Please enter number of tenants this bill is for (up to 10): "; char *prompt5 = "Please enter tenant name: "; char *prompt6 = "Please enter number of days (during the bill) tenant has been here for (whole numbers only): "; /*temp variables*/ int i; int j; int at_least_one_tenant_wholebill = 0; char bill_name[BILL_NAME_LEN]; char temp_tenant_name[TENANT_NAME_LEN + EXTRA_SPACES]; char temp_int_inputs[INT_INPUT_LEN + EXTRA_SPACES]; char temp_double_inputs[DOUBLE_INPUT_LEN + EXTRA_SPACES]; int num_of_tenants; int bill_num_of_days; int tenant_num_of_days; double total_cost_of_bill; double cost_of_bill_per_day; double cost_per_person; /*create array of size 10 for max number of tenants, may change memory allocation later*/ Tenant peoplearray[NUMBER_OF_TENANTS]; /*initialise memory*/ initTenant(peoplearray); printf("\t Share House Bill Calculator\t\t\n"); printf("\t By James Snee, (c) 2014.\t\t\n"); printf("-------------------------------------------------------\n\n"); printf("INFORMATION: This program will work out the total cost of\n"); printf("a bill, for each person living in the house, according to\n"); printf("how many days they were living there at the time of the\n"); printf("billing cycle. If not one person has been at the residence\n"); printf("for the entire bill, the remainder will be split evenly\n"); printf("among all tenants.\n"); printf("If at any time you would like to quit, simply press enter,\n"); printf("although nothing will be saved.\n\n"); /*If nothing entered, quit program as per above*/ if(stringinput(prompt1, bill_name, BILL_NAME_LEN) == FALSE){ printf("Error: Nothing entered! Exiting....\n"); return EXIT_FAILURE; } total_cost_of_bill = doubleinput(prompt2, temp_double_inputs, DOUBLE_INPUT_LEN + EXTRA_SPACES); if (total_cost_of_bill == 0){ printf("Error: Nothing entered! Exiting....\n"); return EXIT_FAILURE; } bill_num_of_days = intinput(prompt3, temp_int_inputs, INT_INPUT_LEN + EXTRA_SPACES); if (bill_num_of_days == 0){ printf("Error: Nothing entered! Exiting....\n"); return EXIT_FAILURE; } /*calculate cost of bill per day*/ cost_of_bill_per_day = total_cost_of_bill / bill_num_of_days; printf("Cost of bill per day is $%g.\n", cost_of_bill_per_day); num_of_tenants = intinput(prompt4, temp_int_inputs, INT_INPUT_LEN + EXTRA_SPACES); if (num_of_tenants == 0){ printf("Error: Nothing entered! Exiting....\n"); return EXIT_FAILURE; } /*divide the cost of bill per day by how many people*/ cost_per_person = cost_of_bill_per_day / num_of_tenants; printf("Cost of bill per person, per day is $%g.\n", cost_per_person); double total_calculated_cost; /*count of all tenants who have been at the place for the whole bill*/ int divisor = 0; for (i = 1; i <= num_of_tenants; i++){ if(stringinput(prompt5, temp_tenant_name, TENANT_NAME_LEN + EXTRA_SPACES) == FALSE){ printf("Error: Nothing entered! Exiting....\n"); return EXIT_FAILURE; } /*copy temp string to array*/ strcpy(peoplearray[i].tenant_name, temp_tenant_name); printf("Tenant number %i is %s.\n", i, peoplearray[i].tenant_name); tenant_num_of_days = intinput(prompt6, temp_int_inputs, INT_INPUT_LEN + EXTRA_SPACES); if (tenant_num_of_days == 0){ printf("Error: Nothing entered! Exiting....\n"); return EXIT_FAILURE; } else if (tenant_num_of_days > bill_num_of_days){ printf("Error: Tenant's number of days cannot exceed bill's number of days! Exiting...\n"); return EXIT_FAILURE; } /*put number of days into array*/ peoplearray[i].num_of_days = tenant_num_of_days; /*we need to multiply tenants number of days with cost pp per day*/ peoplearray[i].final_tenant_cost = cost_per_person * peoplearray[i].num_of_days; /*if tenant has been here for whole bill, set boolean to true*/ if (peoplearray[i].num_of_days == bill_num_of_days){ peoplearray[i].wholebill = 1; at_least_one_tenant_wholebill = 1; printf("%s has been here for the whole bill.\n", peoplearray[i].tenant_name); divisor++; }else { peoplearray[i].wholebill = 0; } printf("%s's final cost is $%g.\n", peoplearray[i].tenant_name, peoplearray[i].final_tenant_cost); total_calculated_cost += peoplearray[i].final_tenant_cost; } /*if they have not been here for the whole bill, that means we need to*/ /*make sure that, if there is a discrepancy at the end of the calculation*/ /*we add the remainder to people who HAVE been there for the whole bill*/ if (total_calculated_cost != total_cost_of_bill){ double remainder = total_cost_of_bill - total_calculated_cost; printf("There was a discrepancy. Remainder is $%g.\n", remainder); /*if there is at least one tenant who has been there for the whole bill, execute this*/ if (at_least_one_tenant_wholebill == 1){ for(int count = 1; count <= num_of_tenants; count++){ if (peoplearray[count].wholebill == 1){ peoplearray[count].final_tenant_cost += (remainder / divisor); } } } else{ /*this means all the tenants haven't been there the entire time, distribute the remainder among everyone*/ for (int count2 = 1; count2 <= num_of_tenants; count2++){ peoplearray[count2].final_tenant_cost += (remainder / num_of_tenants); } } } printf("\n\n\t\tFinal Bill Calculation:\t%s\n",bill_name); printf("--------------------------------------------------------\n"); printf("\nTotal number of days for bill: %i\n", bill_num_of_days); printf("Cost per day for bill: $%g\n", cost_of_bill_per_day); printf("Cost per person, per day for bill: $%g\n", cost_per_person); printf("Number of Tenants: %i\n\n", num_of_tenants); printf("Tenant Name\t\tNumber of Days\t\tTotal Cost\t\t\n"); double final_total_to_print = 0; for (j = 1; j <= num_of_tenants; j++){ /*Loop to print all information goes here*/ printf("%s\t\t\t%i\t\t\t$%g\t\t\t\n", peoplearray[j].tenant_name,peoplearray[j].num_of_days, peoplearray[j].final_tenant_cost); final_total_to_print += peoplearray[j].final_tenant_cost; } printf("\nTotal Cost of Bill:\t\t\t\t$%g\n", final_total_to_print); return EXIT_SUCCESS; }
void Qur(ZGGZ tp[],int n) { /*函数用于在数组tp中按职工编号或者姓名查找满足条件的记录,并显示记录。1 search by number,----------2 search by name\n");*/ if (n == 0) return; int count = n; int i; int select; char choice; int k = -1; while (1) { //输入相关信息 printf("1)按编号查询\n2)按姓名查询\n3)退出\n"); scanf("%d", &select); if (select == 1) {//按编号查询 /*Disp(tp, count);*/ stringinput(tp[count].num, 10, "编号"); if (strcmp(tp[count].num, "0") == 0) { break; } k = Locate(tp, count, tp[count].num, 1); if (k >= 0) {//找到目标,进行查询 for (i = 0; i < count; ++i) { if ( i == k) { printf("%s\n", HEADER2); printf(FORMAT, DATA); printf("%s\n", HEADER3); printf(END); break; } } } else { printf("编号不存在,重新输入\n"); continue; } } else if (select == 2) {//按姓名查询 /*Disp(tp, count);*/ stringinput(tp[count].name, 10, "姓名"); if (strcmp(tp[count].name, "0") == 0) { break; } k = Locate(tp, count, tp[count].name, 2); printf("%d\n", k); if (k >= 0) {//找到目标,进行查询 for (i = 0; i < count; ++i) { if ( i == k) { printf("%s\n", HEADER2); printf(FORMAT, DATA); printf("%s\n", HEADER3); printf(END); break; } } } else { printf("编号不存在,重新输入\n"); continue; } } else if (select == 3) { break; } else {//输入错误 printf("请输入正确选项!\n"); continue; } //如果还要查询,继续;否则退出。 printf("如果还要查询,输入(y or n):"); scanf("%c", &choice);getchar(); if(choice == 'y' || choice == 'Y') continue; printf("\n\n\n\n\n"); menu();//query操作完成后显示菜单供选择 break; } }
int Del(ZGGZ tp[],int n) { /*函数用于先在数组tp中找到满足条件的记录,然后删除该记录。*/ /*-1 delete by number,----------2 delete by name\n"*/ if (n == 0) return -1; int count = n; int i; int select; char choice; int k = -1; while (1) { //输入相关信息 printf("1)按编号删除\n2)按姓名删除\n"); scanf("%d", &select); if (select == 1) {//按编号删除 Disp(tp, count); stringinput(tp[count].num, 10, "编号"); if (strcmp(tp[count].num, "0") == 0) { menu(); break; } while (1) { if (k == 0) break; k = Locate(tp, count, tp[count].num, 1); printf("%d\n", k); if (k >= 0) {//找到目标,进行删除 for (i = k; i < count; ++i) { tp[i] = tp[i+1]; /*printf("i = %d\n", i);*/ } k = 0;//此处k=0表示删除完成,while开始处检测是否有此标记 continue; } printf("编号不存在,重新输入\n"); stringinput(tp[count].num, 10, "编号"); } } else if (select == 2) {//按姓名删除 Disp(tp, count); stringinput(tp[count].name, 15, "姓名"); if (strcmp(tp[count].name, "0") == 0) { menu(); break; } while (1) { if (k == 0) break; k = Locate(tp, count, tp[count].name, 2); printf("%d\n", k); if (k >= 0) {//找到目标,进行删除 for (i = k; i < count; ++i) { tp[i] = tp[i+1]; printf("i = %d\n", i); } k = 0;//此处k=0表示删除完成,while开始处检测是否有此标记 continue; } printf("姓名不存在,重新输入\n"); stringinput(tp[count].name, 15, "姓名"); } } else {//输入错误 printf("请输入正确选项!\n"); break; } count--; saveflag = 1; //如果还要删除,继续;否则退出。 printf("如果还要删除,输入(y or n):"); scanf("%c", &choice);getchar(); if(choice == 'y' || choice == 'Y') continue; printf("\n\n\n\n\n"); menu();//添加操作完成后显示菜单供选择 break; } return count;//删除完成 }
/************************************************************************** * delete_ticket() - Request user input from the administrator specifying * the name, type and zone of the ticket. You will then search the stock * list to delete this ticket. All three fields entered must match for the * ticket to be deleted. Please ensure that you free any memory that was * dynamically allocated. This function implements requirement 7 from the * assignment 2 specifications. **************************************************************************/ void delete_ticket(tm_type * tm) { /*Pointers to nodes*/ struct stock_node *current = NULL, *previous = NULL, *next = NULL; /*Temp variables*/ char temp_ticket_name[TICKET_NAME_LEN], temp_ticket_type[1 + EXTRA_SPACES], temp_ticket_zone[TICKET_ZONE_LEN+1]; char *print; /*prompts*/ char * prompt1 = "Ticket name (1-40 characters): "; char * prompt2 = "Ticket type (1 character): "; char * prompt3 = "Ticket zone (1-3 characters): "; /*point current to head node*/ current = tm->stock->head_stock; printf("\nDelete Ticket\n"); printf("----------------\n\n"); if (stringinput(prompt1, temp_ticket_name, TICKET_NAME_LEN) == FALSE){ return; } if (charinput(prompt2, temp_ticket_type, 1 + EXTRA_SPACES) == FALSE){ return; } if (zoneinput(prompt3, temp_ticket_zone, TICKET_TYPE_LEN + EXTRA_SPACES) == FALSE){ return; } /*Remove from the list*/ while (current != NULL){ /*If ticket name is the same as the one entered in*/ if (strcmp(current->data->ticket_name, temp_ticket_name) == 0 && current->data->ticket_type == temp_ticket_type[0] && strcmp(current->data->ticket_zone, temp_ticket_zone) == 0){ /*We have found it*/ if (current == tm->stock->head_stock){ /*set the print variable to what type it is*/ if (current->data->ticket_type == 'F'){ print = "full fare"; } else { print = "concession"; } /*set the head stock to next node*/ tm->stock->head_stock = current->next_node; /*decrement stock number by 1*/ tm->stock->num_stock_items--; printf("\nTicket \'%s %s zone %s\' has been removed from ticketing machine.\n", temp_ticket_name, print, temp_ticket_zone); return; } else{ /*set the print variable to what type it is*/ if (current->data->ticket_type == 'F'){ print = "full fare"; } else { print = "concession"; } /*If the current is not head, set next to current->next and previous->next to next and free*/ next = current->next_node; previous->next_node = next; free(current); /*decrement stocknumber by 1*/ tm->stock->num_stock_items--; printf("\nTicket \'%s %s zone %s\' has been removed from ticketing machine.\n", temp_ticket_name, print, temp_ticket_zone); return; } } /*If conditions arent met, iterate through list*/ previous = current; current = current->next_node; } printf("Error: Ticket does not exist! Could not delete ticket!\n\n"); return; }
/************************************************************************** * purchase_ticket() - get input from the user about the ticket they wish * to purchase, retrieve that ticket from the list and adjust levels of the * stock and the coins upon payment of the ticket. This function implements * requirement 4 from the assignment 2 specification. **************************************************************************/ void purchase_ticket(tm_type * tm) { /*declare temp variables*/ stock_node *current = NULL; struct coin * coinlist = NULL; char temp_ticket_name[TICKET_NAME_LEN], temp_ticket_type[1 + EXTRA_SPACES], temp_ticket_zone[TICKET_ZONE_LEN+1]; char *prompt1 = "Enter a ticket name (1-40 characters): "; char *prompt2 = "Enter a ticket type (1 character): "; char *prompt3 = "Enter a zone (1, 2, or 1+2): "; char * print; int returnedprice; /*set node to head*/ current = tm->stock->head_stock; coinlist = tm->coins; printf("\nPurchase Ticket\n"); printf("---------------\n\n"); if (stringinput(prompt1, temp_ticket_name, TICKET_NAME_LEN) == FALSE){ return; } if (charinput(prompt2, temp_ticket_type, 1 + EXTRA_SPACES) == FALSE){ return; } if (zoneinput(prompt3, temp_ticket_zone, TICKET_TYPE_LEN + EXTRA_SPACES) == FALSE){ return; } while(current != NULL){ if (strcmp(current->data->ticket_name, temp_ticket_name) == 0 && current->data->ticket_type == temp_ticket_type[0] && strcmp(current->data->ticket_zone, temp_ticket_zone) == 0){ /*Sets which ticket type*/ if (current->data->ticket_type == 'F'){ print = "Full Fare"; } else { print = "Concession"; } /*send this to enter coins function*/ if((returnedprice = enter_coin(tm, current->data->ticket_price)) == -1){ return; } else{ /*break out of while loop and process coin return*/ break; } } current = current->next_node; if (current->next_node == NULL){ printf("Error: Could not find ticket!\n"); return; } } /*decrement the stock items and qty by 1*/ current->data->stock_level--; tm->stock->num_stock_items--; printf("Thank you for purchasing a %s %s zone %s ticket.\n", current->data->ticket_name, print, current->data->ticket_zone); printf("Your change is: $%.2f\n", (float)(returnedprice * -1) / 100); printf("Your change coins are: "); if (returnedprice == 0){ printf("None\n"); } else if (returnedprice >= -5){ coinlist[0].count--; printf("5c\n"); } else if (returnedprice >= -10){ coinlist[1].count--; printf("10c\n"); } else if (returnedprice >= -20){ coinlist[2].count--; printf("20c\n"); } else if (returnedprice >= -50){ coinlist[3].count--; printf("50c\n"); } else if (returnedprice >= -100){ coinlist[4].count--; printf("$1\n"); } else if (returnedprice >= -200){ coinlist[5].count--; printf("$2\n"); } }
/*************************************************************************** * add_ticket() - Request user input about creating a new ticket type in * the system. You need to validate this input and then create and insert * a new ticket into the system, sorted by ticket name. this option * implements requirement 6 in the assignment 2 specifications. **************************************************************************/ void add_ticket(tm_type * tm) { struct stock_data *data = NULL; char temp_ticket_name[TICKET_NAME_LEN], temp_ticket_type[1 + EXTRA_SPACES], temp_ticket_zone[TICKET_ZONE_LEN+1]; char char_ticket_price[10]; unsigned int temp_ticket_price = 0; BOOLEAN finished = FALSE; char * prompt1 = "Ticket name (1-40 characters): "; char * prompt2 = "Ticket type (1 character): "; char * prompt3 = "Ticket zone (1-3 characters): "; printf("\nAdd Ticket\n"); printf("-----------\n\n"); if (stringinput(prompt1, temp_ticket_name, TICKET_NAME_LEN) == FALSE){ return; } if (charinput(prompt2, temp_ticket_type, 1 + EXTRA_SPACES) == FALSE){ return; } if (zoneinput(prompt3, temp_ticket_zone, TICKET_TYPE_LEN + EXTRA_SPACES) == FALSE){ return; } /*Input cents*/ do{ printf("Price (in cents): "); fgets(char_ticket_price, 10, stdin); if(char_ticket_price[strlen(char_ticket_price) -1] !='\n'){ printf("Error: Input was too long! Try again.\n"); read_rest_of_line(); } else if (strcmp(char_ticket_price, "\n") == 0){ return; } else{ char_ticket_price[strlen(char_ticket_price) -1] = '\0'; temp_ticket_price = atoi(char_ticket_price); finished = TRUE; } } while (!finished); /*Malloc stock_data*/ data = (struct stock_data *)malloc(sizeof(struct stock_data)); /*If memory allocation fails*/ if (!data){ perror("Error: Memory allocation failed! Exiting...\n"); free(data); exit(EXIT_FAILURE); } /*Add temp variables to struct*/ strcpy(data -> ticket_name, temp_ticket_name); data ->ticket_type = *temp_ticket_type; strcpy(data -> ticket_zone, temp_ticket_zone); data -> ticket_price = temp_ticket_price; data -> stock_level = DEFAULT_STOCK_LEVEL; /*Send data to add stock function*/ add_stock_to_node(tm, data); printf("Ticket successfully added to system.\n"); }