题目:
本题要求编写程序,按照规定格式输出表格。
输入格式:
本题目没有输入。
输出格式:
要求严格按照给出的格式输出下列表格:
------------------------------------ Province Area(km2) Pop.(10K) ------------------------------------ Anhui 139600.00 6461.00 Beijing 16410.54 1180.70 Chongqing 82400.00 3144.23 Shanghai 6340.50 1360.26 Zhejiang 101800.00 4894.00
------------------------------------
方法一:
#include <stdio.h> #include <string.h> #include <stdlib.h> void Print_Lines(); void Print_String(char string[]); int main() { int index=0; int i=0; char buffer[18][20]={"Province ","Area(km2) ","Pop.(10k) \n", "Anhui ","139600.00 ","6461.00 \n", "Beijing ","16410.54 ","1180.70 \n", "Chongqing ","82400.00 ","3144.23 \n", "Shanghai ","6340.50 ","1360.26 \n", "Zhejing ","101800.00 ","4894.00 \n"}; Print_Lines(); for(i=0;i<3;i++) { Print_String(buffer[index++]); } Print_Lines(); for(i=0;i<15;i++) { Print_String(buffer[index++]); } Print_Lines(); system("pause"); } void Print_Lines() { int i=0; for(i=0; i<36;i++) { printf("%s","-"); } printf("\n"); } void Print_String(char string[]) { printf("%s",string); }
方法二:
#include<stdio.h> #include<stdlib.h> #include<string.h> void PrintProvince(char name[]); void PrintArea(char area[]); void PrintPop(char pop[]); void PrintLines(int num); int main() { int index=0; int i=0; int length=0; char column1[6][20]={"Province","Anhui","Beijing","Chongqing","Shanghai","Zhejing",}; char column2[6][20]={"Area(km2)","139600.00","16410.54","82400.00","6340.50","101800.00",}; char column3[6][20]={"Pop.(10k)","6461.0","1180.70","3144.23","1360.26","4894.00"}; PrintLines(36); for (i = 0; i < 6; i++) { PrintProvince(column1[i]); PrintArea(column2[i]); printf(" "); PrintPop(column3[i]); printf("\n"); } PrintLines(36); system("pause"); } void PrintLines(int num) { int i=0; for(i=0; i<num;i++) { printf("%s","-"); } printf("\n"); } void PrintProvince(char name[]) { printf("%-14s",name); } void PrintArea(char area[]) { printf("%9s",area); } void PrintPop(char pop[]) { printf("%s",pop); }